22a70eb803
* chore: bump chromium in DEPS to 91.0.4464.0 * chore: rebuild chromium/dcheck.patch with import-patches -3 Mechanical only; no code changes * chore: remove content_browser_main_loop.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2725153 The function being patched (BrowserMainLoop::MainMessageLoopRun()) no longer exists. NB: if removing this introduces regressions the likely fix will be to add a similar patch for ShellBrowserMainParts::WillRunMainMessageLoop() which has similar code and was added at the same time this was removed. * chore: rebuild chromium/put_back_deleted_colors_for_autofill.patch with import-patches -3 Mechanical only; no code changes * chore: rebuild chromium/disable_color_correct_rendering.patch with import-patches -3 Mechanical only; no code changes * chore: rebuild chromium/eat_allow_disabling_blink_scheduler_throttling_per_renderview.patch with patch Mechanical only; no code changes * chore: rebuild chromium/gpu_notify_when_dxdiag_request_fails.patch with import-patches -3 Mechanical only; no code changes * chore: rebuild chromium/ui_gtk_public_header.patch manually no code changes * chore: rebuild chromium/web_contents.patch with import-patches -3 Mechanical only; no code changes * chore: remove v8/skip_global_registration_of_shared_arraybuffer_backing_stores.patch Refs: https://chromium-review.googlesource.com/c/v8/v8/+/2763874 This patch has been merged upstream * chore: export patches * chore: update add_trustedauthclient_to_urlloaderfactory.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2757969 Sync with removal of render_frame_id_ * chore: sync chromium/put_back_deleted_colors_for_autofill.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2785841 SkColorFromColorId() no longer takes theme, scheme args * chore: sync chromium/put_back_deleted_colors_for_autofill.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2772143 Change new calls to GetDarkSchemeColor to fit our patched call signature * chore: update add_trustedauthclient_to_urlloaderfactory.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2757969 Sync with removal of render_frame_id_ in our mojom * chore: update chromium/frame_host_manager.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2740008 UrlInfo ctor now takes UrlInfo::OriginIsolationRequest instead of a bool * chore: update chromium/revert_remove_contentrendererclient_shouldfork.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2755314 Upstream has removed `history_list_length_` which we were comparing to 0 to calculate our `is_initial_navigation` bool when calling ShouldFork(). ShouldFork() is ours and none of the code paths actually use that param, so this commit removes it altogether. * chore: update permissions_to_register Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2789074 Replace all uses of APIPermission::ID enum with Mojo type * refactor: update return type of PreMainMessageLoopRun() Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2725153 Used to return void; now returns an int errorcode. Note: 2725153 also has some nice doc updates about Browser's "stages" * refactor: sync ElectronBrowserMainParts to MainParts changes Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2725153 RunMainMessageLoopParts has been replaced with WillRunMainMessageLoop so `BrowserMainLoop::result_code_` is no longer available to us for our exit_code_ pointer. This variable held a dual role: (1) of course, hold the exit code, but also (2) was a nullptr before the message loop was ready, indicating to anyone calling SetExitCode() that we were still in startup and could just exit() without any extra steps. exit_code_ still fulfills these two roles but is now a base::Optional. * chore: update ElectronBrowserMainParts::PreDefaultMainMessageLoopRun Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2725153 BrowserMainParts::BrowsePreDefaultMainMesssageLoopRun() has been removed; move that work to the new WillRunMainMessageLoop(). * refactor: stop using CallbackList; it has been removed. Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2785973 * refactor: update use of threadpools. Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2773408 The upstream code is still in flux (e.g. reverts and re-lands) but the tl;dr for this commit is (1) include thread_pool.h if you're using it and (2) don't instantiate pools directly. * refactor: remove routing_id from CreateLoaderAndStart Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2762858 NB: One logic branch in ProxyingURLLoaderFactory::CreateLoaderAndStart calls std::make_unique<InProgressRequest>, which needs a routing_id. This PR uses the member field `routing_id_` since there's no longer one being passed into CreateLoaderAndStart. * refactor: sync to upstream ParittionOptions churn Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2771318 PartitionOptions' enums have changed. * refactor: update Manifest::Location usage Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2771320 tldr: s/Manifest::FOO/ManifestLocation::kFoo/ * chore: bump chromium in DEPS to 91.0.4465.0 * update patches * refactor: update extensions::Manifest to upstream Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2771320 - extensions::Manifest::COMPONENT + extensions::mojom::ManifestLocation::kExternalComponent * refactor: sync with upstream UrlInfo ctor changes Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2740008 UrlInfo ctor now takes UrlInfo::OriginIsolationRequest instead of a bool * chore: update invocation of convert_protocol_to_json.py Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2792623 python3 is being used in parts of the upstream build, but the copy of convert_protocol_to_json.py invoked in v8/third_party/inspector_protocol is not python3-friendly. Node has a py2+3-friendly version of it in its tools directory, so call it instead. * chore: use extensions::mojom::APIPermissionID Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2791122 tldr: - extensions::APIPermission::kFoo + extensions::mojom::APIPermissionID::kFoo * chore: Remove support for TLS1.0/1.1 in SSLVersionMin policy Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2765737 Remove TLS v1.0 & 1.1 from our SSLProtocolVersionFromString() function. This is the same change made upstream at https://chromium-review.googlesource.com/c/chromium/src/+/2765737/8/chrome/browser/ssl/ssl_config_service_manager_pref.cc * fixup! chore: update ElectronBrowserMainParts::PreDefaultMainMessageLoopRun * chore: Use IDType for permission change subscriptions. Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2791431 tldr: {Subscribe,Unsubscribe}PermissionStatusChange's tag type used to be an int; now it's the new SubscriptionId type (which is an IdType64). * chore: sync PowerMonitor code to upstream refactor Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2752635 tldr: PowerMonitor has been split into PowerStateObserver, PowerSuspendObserver, and PowerThermalObserver to reduce number of tasks posted to consumers who only need notifications for one of those things instead of all of them. * chore: use PartitionOptions's new Cookies field Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2771318 * Revert "refactor: remove routing_id from CreateLoaderAndStart" This reverts commit 8c9773b87a3c84f9073a47089eb2b6889d745245. 8c9773b was only a partial fix; reverting to start & try again. * update patches * chore: bump chromium in DEPS to 91.0.4466.0 * chore: update chromium/accelerator.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2795472 tldr: sync patch with upstream renamed variable & macro names. * chore: update chromium/gtk_visibility.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2796200 tldr: no code changes; just updating the diff to apply cleanly. note: ooh upstream Wayland hacking! * chore: update chromium/picture-in-picture.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2710023 tldr: no code changes; just updating the diff to apply cleanly. * chore: update chromium/worker_feat_add_hook_to_notify_script_ready.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2775573 tldr: no code changes; just updating the diff to apply cleanly. * chore: export_all_patches * chore: update chromium/feat_add_set_theme_source_to_allow_apps_to.patch Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2796511 tldr: NotifyObservers has been renamed to NotifyOnNativeThemeUpdated, so update the invocation in our patch. * chore: update ElectronBrowserClient w/upstream API Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2797454 tldr: GetDevToolsManagerDelegate() was returning an owned raw pointer. Replaced it with CreateDevToolsManagerDelegate() which uses unique_ptr<>. * chore: handle new content::PermissionType::FILE_HANDLING in toV8() Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2762201 `file-handling` string confirmed in https://chromium-review.googlesource.com/c/chromium/src/+/2762201/18/chrome/browser/ui/webui/settings/site_settings_helper.cc * refactor: remove routing_id from CreateLoaderAndStart pt 1 Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2762858 Part 1: the easiest ones * 2796724: Support Python3 https://chromium-review.googlesource.com/c/infra/luci/python-adb/+/2796724 * chore: bump chromium in DEPS to 91.0.4468.0 * 2668974: WebShare: Implement SharingServicePicker https://chromium-review.googlesource.com/c/chromium/src/+/2668974 * 2802766: Apply modernize-make-unique to media/ https://chromium-review.googlesource.com/c/chromium/src/+/2802766 * 2802823: Apply modernize-make-unique to gpu/ https://chromium-review.googlesource.com/c/chromium/src/+/2802823 * 2803041: Apply modernize-make-unique to remaining files https://chromium-review.googlesource.com/c/chromium/src/+/2803041 * 2798873: Convert GtkKeyBindingsHandler build checks to runtime checks https://chromium-review.googlesource.com/c/chromium/src/+/2798873 * 2733595: [ch-r] Parse ACCEPT_CH H2/3 frame and restart with new headers if needed https://chromium-review.googlesource.com/c/chromium/src/+/2733595 * chore: update patch indices * 2795107: Remove unused PermissionRequest IDs. https://chromium-review.googlesource.com/c/chromium/src/+/2795107 * chore: bump chromium in DEPS to 91.0.4469.0 * chore: fixup patch indices * chore: bump chromium in DEPS to 91.0.4469.5 * PiP 1.5: Add microphone, camera, and hang up buttons to the PiP window https://chromium-review.googlesource.com/c/chromium/src/+/2710023 * fixup! refactor: remove routing_id from CreateLoaderAndStart * refactor: use URLLoaderNetworkServiceObserver for auth requests from SimpleURLLoader * fixup! chore: fixup patch indices * 2724817: Expand scope of wasm-eval to all URLs https://chromium-review.googlesource.com/c/chromium/src/+/2724817 * Fixup patch after rebase * chore: bump chromium in DEPS to 91.0.4472.0 * 2797341: [ozone/x11] Enabled the global shortcut listener. https://chromium-review.googlesource.com/c/chromium/src/+/2797341 * 2805553: Reland Add GTK ColorMixers to ColorPipeline P1 https://chromium-review.googlesource.com/c/chromium/src/+/2805553 *2804366
: PiP 1.5: Label back to tab button with origin and center it https://chromium-review.googlesource.com/c/chromium/src/+/2804366 * 2784730: Fix crash on AX mode change in NativeViewHost without a Widget https://chromium-review.googlesource.com/c/chromium/src/+/2784730 * chore: update patch indices * 2810174: Add PdfAnnotationsEnabled policy. https://chromium-review.googlesource.com/c/chromium/src/+/2810174 * 2807829: Allow capturers to indicate if they want a WakeLock or not. https://chromium-review.googlesource.com/c/chromium/src/+/2807829 * chore: bump chromium in DEPS to 92.0.4473.0 * chore: bump chromium in DEPS to 92.0.4474.0 * chore: bump chromium in DEPS to 92.0.4475.0 * chore: update patches * chore: updates patches * chore: update is_media_key patch to handle new ozone impl Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2797341 * fix: ExecuteJavascript requests now need to be flagged as non-bf-aware Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2787195 * chore: icon_util_x11 is now icon_util_linux Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2791362 * build: update sysroots Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2628496 * build: fix missing symbols on linux build * use_ozone and use_x11 are not exclusive * new button view to build for pip Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2797341 Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2804366 * chore: fix broken gtk_util color patch * chore: remove patch conflict * build: update linux manifests * chore: build bttlb on all platforms for pip * chore: add thread_pool include for views delegate win * chore: fix lint * chore: add node patches for V8 changes * build: add missing base include on windows * fix: update frame host manager patch for new state transitions Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2714464 * chore: update windows zip manifests * chore: update mac zip manifests * chore: fix patch linting * refactor: implement missing URLLoaderNetworkServiceObserver methods It is against The Mojo Rules to leave hanging callbacks. These always have to be called. Refs:186528aab9
* spec: fix locale test on local linux * fix: pass the exit code correctly in new PreMainMessageLoopRun Refs:2622e91c44
* fix: ensure we early-exit when request_handler_ is not provided Refs:93077afbfb
* fix: strongly set result_code in the BrowserMainLoop * fix: invalid usage of non-targetted PostTask You must always either use a host threadpool or specify a target thread. In this case we did neither after this refactor. Refs:4e33ee0ad3
* chore: fix gn check * chore: remove stray .rej files in patch * chore: add mojo error code to url loader failure * build: ensure CI is truthy in arm test env * fix: handle windowCaptureMacV2 being enabled when fetching media source id Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2709931 Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> Co-authored-by: Samuel Attard <sattard@slack-corp.com>
984 lines
36 KiB
C++
984 lines
36 KiB
C++
// Copyright (c) 2014 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "base/command_line.h"
|
|
#include "base/memory/memory_pressure_listener.h"
|
|
#include "base/strings/utf_string_conversions.h"
|
|
#include "components/spellcheck/renderer/spellcheck.h"
|
|
#include "content/public/renderer/render_frame.h"
|
|
#include "content/public/renderer/render_frame_observer.h"
|
|
#include "content/public/renderer/render_frame_visitor.h"
|
|
#include "content/public/renderer/render_view.h"
|
|
#include "gin/handle.h"
|
|
#include "gin/object_template_builder.h"
|
|
#include "gin/wrappable.h"
|
|
#include "services/service_manager/public/cpp/interface_provider.h"
|
|
#include "shell/common/api/api.mojom.h"
|
|
#include "shell/common/gin_converters/blink_converter.h"
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
|
#include "shell/common/gin_helper/dictionary.h"
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
|
#include "shell/common/gin_helper/function_template_extensions.h"
|
|
#include "shell/common/gin_helper/promise.h"
|
|
#include "shell/common/node_includes.h"
|
|
#include "shell/common/options_switches.h"
|
|
#include "shell/renderer/api/context_bridge/object_cache.h"
|
|
#include "shell/renderer/api/electron_api_context_bridge.h"
|
|
#include "shell/renderer/api/electron_api_spell_check_client.h"
|
|
#include "shell/renderer/electron_renderer_client.h"
|
|
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
|
|
#include "third_party/blink/public/common/page/page_zoom.h"
|
|
#include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h"
|
|
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
|
|
#include "third_party/blink/public/platform/web_cache.h"
|
|
#include "third_party/blink/public/platform/web_isolated_world_info.h"
|
|
#include "third_party/blink/public/web/web_custom_element.h"
|
|
#include "third_party/blink/public/web/web_document.h"
|
|
#include "third_party/blink/public/web/web_element.h"
|
|
#include "third_party/blink/public/web/web_frame_widget.h"
|
|
#include "third_party/blink/public/web/web_input_method_controller.h"
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
|
#include "third_party/blink/public/web/web_script_execution_callback.h"
|
|
#include "third_party/blink/public/web/web_script_source.h"
|
|
#include "third_party/blink/public/web/web_view.h"
|
|
#include "ui/base/ime/ime_text_span.h"
|
|
#include "url/url_util.h"
|
|
|
|
namespace gin {
|
|
|
|
template <>
|
|
struct Converter<blink::WebLocalFrame::ScriptExecutionType> {
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
v8::Local<v8::Value> val,
|
|
blink::WebLocalFrame::ScriptExecutionType* out) {
|
|
std::string execution_type;
|
|
if (!ConvertFromV8(isolate, val, &execution_type))
|
|
return false;
|
|
if (execution_type == "asynchronous") {
|
|
*out = blink::WebLocalFrame::kAsynchronous;
|
|
} else if (execution_type == "asynchronousBlockingOnload") {
|
|
*out = blink::WebLocalFrame::kAsynchronousBlockingOnload;
|
|
} else if (execution_type == "synchronous") {
|
|
*out = blink::WebLocalFrame::kSynchronous;
|
|
} else {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
template <>
|
|
struct Converter<blink::WebDocument::CSSOrigin> {
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
v8::Local<v8::Value> val,
|
|
blink::WebDocument::CSSOrigin* out) {
|
|
std::string css_origin;
|
|
if (!ConvertFromV8(isolate, val, &css_origin))
|
|
return false;
|
|
if (css_origin == "user") {
|
|
*out = blink::WebDocument::CSSOrigin::kUserOrigin;
|
|
} else if (css_origin == "author") {
|
|
*out = blink::WebDocument::CSSOrigin::kAuthorOrigin;
|
|
} else {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
} // namespace gin
|
|
|
|
namespace electron {
|
|
|
|
namespace api {
|
|
|
|
namespace {
|
|
|
|
content::RenderFrame* GetRenderFrame(v8::Local<v8::Value> value) {
|
|
v8::Local<v8::Context> context =
|
|
v8::Local<v8::Object>::Cast(value)->CreationContext();
|
|
if (context.IsEmpty())
|
|
return nullptr;
|
|
blink::WebLocalFrame* frame = blink::WebLocalFrame::FrameForContext(context);
|
|
if (!frame)
|
|
return nullptr;
|
|
return content::RenderFrame::FromWebFrame(frame);
|
|
}
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
bool SpellCheckWord(content::RenderFrame* render_frame,
|
|
const std::string& word,
|
|
std::vector<std::u16string>* optional_suggestions) {
|
|
size_t start;
|
|
size_t length;
|
|
|
|
ElectronRendererClient* client = ElectronRendererClient::Get();
|
|
|
|
std::u16string w = base::UTF8ToUTF16(word);
|
|
int id = render_frame->GetRoutingID();
|
|
return client->GetSpellCheck()->SpellCheckWord(
|
|
w.c_str(), 0, word.size(), id, &start, &length, optional_suggestions);
|
|
}
|
|
|
|
#endif
|
|
|
|
class RenderFrameStatus final : public content::RenderFrameObserver {
|
|
public:
|
|
explicit RenderFrameStatus(content::RenderFrame* render_frame)
|
|
: content::RenderFrameObserver(render_frame) {}
|
|
~RenderFrameStatus() final = default;
|
|
|
|
bool is_ok() { return render_frame() != nullptr; }
|
|
|
|
// RenderFrameObserver implementation.
|
|
void OnDestruct() final {}
|
|
};
|
|
|
|
class ScriptExecutionCallback : public blink::WebScriptExecutionCallback {
|
|
public:
|
|
// for compatibility with the older version of this, error is after result
|
|
using CompletionCallback =
|
|
base::OnceCallback<void(const v8::Local<v8::Value>& result,
|
|
const v8::Local<v8::Value>& error)>;
|
|
|
|
explicit ScriptExecutionCallback(
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise,
|
|
CompletionCallback callback)
|
|
: promise_(std::move(promise)), callback_(std::move(callback)) {}
|
|
|
|
~ScriptExecutionCallback() override = default;
|
|
|
|
void CopyResultToCallingContextAndFinalize(
|
|
v8::Isolate* isolate,
|
|
const v8::Local<v8::Object>& result) {
|
|
v8::MaybeLocal<v8::Value> maybe_result;
|
|
bool success = true;
|
|
std::string error_message =
|
|
"An unknown exception occurred while getting the result of the script";
|
|
{
|
|
v8::TryCatch try_catch(isolate);
|
|
context_bridge::ObjectCache object_cache;
|
|
maybe_result = PassValueToOtherContext(result->CreationContext(),
|
|
promise_.GetContext(), result,
|
|
&object_cache, false, 0);
|
|
if (maybe_result.IsEmpty() || try_catch.HasCaught()) {
|
|
success = false;
|
|
}
|
|
if (try_catch.HasCaught()) {
|
|
auto message = try_catch.Message();
|
|
|
|
if (!message.IsEmpty()) {
|
|
gin::ConvertFromV8(isolate, message->Get(), &error_message);
|
|
}
|
|
}
|
|
}
|
|
if (!success) {
|
|
// Failed convert so we send undefined everywhere
|
|
if (callback_)
|
|
std::move(callback_).Run(
|
|
v8::Undefined(isolate),
|
|
v8::Exception::Error(
|
|
v8::String::NewFromUtf8(isolate, error_message.c_str())
|
|
.ToLocalChecked()));
|
|
promise_.RejectWithErrorMessage(error_message);
|
|
} else {
|
|
v8::Local<v8::Context> context = promise_.GetContext();
|
|
v8::Context::Scope context_scope(context);
|
|
v8::Local<v8::Value> cloned_value = maybe_result.ToLocalChecked();
|
|
if (callback_)
|
|
std::move(callback_).Run(cloned_value, v8::Undefined(isolate));
|
|
promise_.Resolve(cloned_value);
|
|
}
|
|
}
|
|
|
|
void Completed(
|
|
const blink::WebVector<v8::Local<v8::Value>>& result) override {
|
|
v8::Isolate* isolate = promise_.isolate();
|
|
if (!result.empty()) {
|
|
if (!result[0].IsEmpty()) {
|
|
v8::Local<v8::Value> value = result[0];
|
|
// Either the result was created in the same world as the caller
|
|
// or the result is not an object and therefore does not have a
|
|
// prototype chain to protect
|
|
bool should_clone_value =
|
|
!(value->IsObject() &&
|
|
promise_.GetContext() ==
|
|
value.As<v8::Object>()->CreationContext()) &&
|
|
value->IsObject();
|
|
if (should_clone_value) {
|
|
CopyResultToCallingContextAndFinalize(isolate,
|
|
value.As<v8::Object>());
|
|
} else {
|
|
// Right now only single results per frame is supported.
|
|
if (callback_)
|
|
std::move(callback_).Run(value, v8::Undefined(isolate));
|
|
promise_.Resolve(value);
|
|
}
|
|
} else {
|
|
const char* error_message =
|
|
"Script failed to execute, this normally means an error "
|
|
"was thrown. Check the renderer console for the error.";
|
|
if (!callback_.is_null()) {
|
|
v8::Local<v8::Context> context = promise_.GetContext();
|
|
v8::Context::Scope context_scope(context);
|
|
std::move(callback_).Run(
|
|
v8::Undefined(isolate),
|
|
v8::Exception::Error(
|
|
v8::String::NewFromUtf8(isolate, error_message)
|
|
.ToLocalChecked()));
|
|
}
|
|
promise_.RejectWithErrorMessage(error_message);
|
|
}
|
|
} else {
|
|
const char* error_message =
|
|
"WebFrame was removed before script could run. This normally means "
|
|
"the underlying frame was destroyed";
|
|
if (!callback_.is_null()) {
|
|
v8::Local<v8::Context> context = promise_.GetContext();
|
|
v8::Context::Scope context_scope(context);
|
|
std::move(callback_).Run(
|
|
v8::Undefined(isolate),
|
|
v8::Exception::Error(v8::String::NewFromUtf8(isolate, error_message)
|
|
.ToLocalChecked()));
|
|
}
|
|
promise_.RejectWithErrorMessage(error_message);
|
|
}
|
|
delete this;
|
|
}
|
|
|
|
private:
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise_;
|
|
CompletionCallback callback_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ScriptExecutionCallback);
|
|
};
|
|
|
|
class FrameSetSpellChecker : public content::RenderFrameVisitor {
|
|
public:
|
|
FrameSetSpellChecker(SpellCheckClient* spell_check_client,
|
|
content::RenderFrame* main_frame)
|
|
: spell_check_client_(spell_check_client), main_frame_(main_frame) {
|
|
content::RenderFrame::ForEach(this);
|
|
main_frame->GetWebFrame()->SetSpellCheckPanelHostClient(spell_check_client);
|
|
}
|
|
|
|
bool Visit(content::RenderFrame* render_frame) override {
|
|
auto* view = render_frame->GetRenderView();
|
|
if (view->GetMainRenderFrame() == main_frame_ ||
|
|
(render_frame->IsMainFrame() && render_frame == main_frame_)) {
|
|
render_frame->GetWebFrame()->SetTextCheckClient(spell_check_client_);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
SpellCheckClient* spell_check_client_;
|
|
content::RenderFrame* main_frame_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(FrameSetSpellChecker);
|
|
};
|
|
|
|
class SpellCheckerHolder final : public content::RenderFrameObserver {
|
|
public:
|
|
// Find existing holder for the |render_frame|.
|
|
static SpellCheckerHolder* FromRenderFrame(
|
|
content::RenderFrame* render_frame) {
|
|
for (auto* holder : instances_) {
|
|
if (holder->render_frame() == render_frame)
|
|
return holder;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
SpellCheckerHolder(content::RenderFrame* render_frame,
|
|
std::unique_ptr<SpellCheckClient> spell_check_client)
|
|
: content::RenderFrameObserver(render_frame),
|
|
spell_check_client_(std::move(spell_check_client)) {
|
|
DCHECK(!FromRenderFrame(render_frame));
|
|
instances_.insert(this);
|
|
}
|
|
|
|
~SpellCheckerHolder() final { instances_.erase(this); }
|
|
|
|
void UnsetAndDestroy() {
|
|
FrameSetSpellChecker set_spell_checker(nullptr, render_frame());
|
|
delete this;
|
|
}
|
|
|
|
// RenderFrameObserver implementation.
|
|
void OnDestruct() final {
|
|
// Since we delete this in WillReleaseScriptContext, this method is unlikely
|
|
// to be called, but override anyway since I'm not sure if there are some
|
|
// corner cases.
|
|
//
|
|
// Note that while there are two "delete this", it is totally fine as the
|
|
// observer unsubscribes automatically in destructor and the other one won't
|
|
// be called.
|
|
//
|
|
// Also note that we should not call UnsetAndDestroy here, as the render
|
|
// frame is going to be destroyed.
|
|
delete this;
|
|
}
|
|
|
|
void WillReleaseScriptContext(v8::Local<v8::Context> context,
|
|
int world_id) final {
|
|
// Unset spell checker when the script context is going to be released, as
|
|
// the spell check implementation lives there.
|
|
UnsetAndDestroy();
|
|
}
|
|
|
|
private:
|
|
static std::set<SpellCheckerHolder*> instances_;
|
|
|
|
std::unique_ptr<SpellCheckClient> spell_check_client_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
|
|
public content::RenderFrameObserver {
|
|
public:
|
|
static gin::WrapperInfo kWrapperInfo;
|
|
|
|
static gin::Handle<WebFrameRenderer> Create(
|
|
v8::Isolate* isolate,
|
|
content::RenderFrame* render_frame) {
|
|
return gin::CreateHandle(isolate, new WebFrameRenderer(render_frame));
|
|
}
|
|
|
|
explicit WebFrameRenderer(content::RenderFrame* render_frame)
|
|
: content::RenderFrameObserver(render_frame) {
|
|
DCHECK(render_frame);
|
|
}
|
|
|
|
// gin::Wrappable:
|
|
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
v8::Isolate* isolate) override {
|
|
return gin::Wrappable<WebFrameRenderer>::GetObjectTemplateBuilder(isolate)
|
|
.SetMethod("getWebFrameId", &WebFrameRenderer::GetWebFrameId)
|
|
.SetMethod("setName", &WebFrameRenderer::SetName)
|
|
.SetMethod("setZoomLevel", &WebFrameRenderer::SetZoomLevel)
|
|
.SetMethod("getZoomLevel", &WebFrameRenderer::GetZoomLevel)
|
|
.SetMethod("setZoomFactor", &WebFrameRenderer::SetZoomFactor)
|
|
.SetMethod("getZoomFactor", &WebFrameRenderer::GetZoomFactor)
|
|
.SetMethod("getWebPreference", &WebFrameRenderer::GetWebPreference)
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
.SetMethod("isWordMisspelled", &WebFrameRenderer::IsWordMisspelled)
|
|
.SetMethod("getWordSuggestions", &WebFrameRenderer::GetWordSuggestions)
|
|
#endif
|
|
.SetMethod("setVisualZoomLevelLimits",
|
|
&WebFrameRenderer::SetVisualZoomLevelLimits)
|
|
.SetMethod("allowGuestViewElementDefinition",
|
|
&WebFrameRenderer::AllowGuestViewElementDefinition)
|
|
.SetMethod("insertText", &WebFrameRenderer::InsertText)
|
|
.SetMethod("insertCSS", &WebFrameRenderer::InsertCSS)
|
|
.SetMethod("removeInsertedCSS", &WebFrameRenderer::RemoveInsertedCSS)
|
|
.SetMethod("executeJavaScript", &WebFrameRenderer::ExecuteJavaScript)
|
|
.SetMethod("executeJavaScriptInIsolatedWorld",
|
|
&WebFrameRenderer::ExecuteJavaScriptInIsolatedWorld)
|
|
.SetMethod("setIsolatedWorldInfo",
|
|
&WebFrameRenderer::SetIsolatedWorldInfo)
|
|
.SetMethod("getResourceUsage", &WebFrameRenderer::GetResourceUsage)
|
|
.SetMethod("clearCache", &WebFrameRenderer::ClearCache)
|
|
.SetMethod("setSpellCheckProvider",
|
|
&WebFrameRenderer::SetSpellCheckProvider)
|
|
// Frame navigators
|
|
.SetMethod("findFrameByRoutingId",
|
|
&WebFrameRenderer::FindFrameByRoutingId)
|
|
.SetMethod("getFrameForSelector",
|
|
&WebFrameRenderer::GetFrameForSelector)
|
|
.SetMethod("findFrameByName", &WebFrameRenderer::FindFrameByName)
|
|
.SetProperty("opener", &WebFrameRenderer::GetOpener)
|
|
.SetProperty("parent", &WebFrameRenderer::GetFrameParent)
|
|
.SetProperty("top", &WebFrameRenderer::GetTop)
|
|
.SetProperty("firstChild", &WebFrameRenderer::GetFirstChild)
|
|
.SetProperty("nextSibling", &WebFrameRenderer::GetNextSibling)
|
|
.SetProperty("routingId", &WebFrameRenderer::GetRoutingId);
|
|
}
|
|
|
|
const char* GetTypeName() override { return "WebFrameRenderer"; }
|
|
|
|
void OnDestruct() override {}
|
|
|
|
private:
|
|
bool MaybeGetRenderFrame(v8::Isolate* isolate,
|
|
const std::string& method_name,
|
|
content::RenderFrame** render_frame_ptr) {
|
|
std::string error_msg;
|
|
if (!MaybeGetRenderFrame(&error_msg, method_name, render_frame_ptr)) {
|
|
gin_helper::ErrorThrower(isolate).ThrowError(error_msg);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool MaybeGetRenderFrame(std::string* error_msg,
|
|
const std::string& method_name,
|
|
content::RenderFrame** render_frame_ptr) {
|
|
auto* frame = render_frame();
|
|
if (!frame) {
|
|
*error_msg = "Render frame was torn down before webFrame." + method_name +
|
|
" could be "
|
|
"executed";
|
|
return false;
|
|
}
|
|
*render_frame_ptr = frame;
|
|
return true;
|
|
}
|
|
|
|
void SetName(v8::Isolate* isolate, const std::string& name) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "setName", &render_frame))
|
|
return;
|
|
|
|
render_frame->GetWebFrame()->SetName(blink::WebString::FromUTF8(name));
|
|
}
|
|
|
|
void SetZoomLevel(v8::Isolate* isolate, double level) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "setZoomLevel", &render_frame))
|
|
return;
|
|
|
|
mojo::Remote<mojom::ElectronBrowser> browser_remote;
|
|
render_frame->GetBrowserInterfaceBroker()->GetInterface(
|
|
browser_remote.BindNewPipeAndPassReceiver());
|
|
browser_remote->SetTemporaryZoomLevel(level);
|
|
}
|
|
|
|
double GetZoomLevel(v8::Isolate* isolate) {
|
|
double result = 0.0;
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "getZoomLevel", &render_frame))
|
|
return result;
|
|
|
|
mojo::Remote<mojom::ElectronBrowser> browser_remote;
|
|
render_frame->GetBrowserInterfaceBroker()->GetInterface(
|
|
browser_remote.BindNewPipeAndPassReceiver());
|
|
browser_remote->DoGetZoomLevel(&result);
|
|
return result;
|
|
}
|
|
|
|
void SetZoomFactor(gin_helper::ErrorThrower thrower, double factor) {
|
|
if (factor < std::numeric_limits<double>::epsilon()) {
|
|
thrower.ThrowError("'zoomFactor' must be a double greater than 0.0");
|
|
return;
|
|
}
|
|
|
|
SetZoomLevel(thrower.isolate(), blink::PageZoomFactorToZoomLevel(factor));
|
|
}
|
|
|
|
double GetZoomFactor(v8::Isolate* isolate) {
|
|
double zoom_level = GetZoomLevel(isolate);
|
|
return blink::PageZoomLevelToZoomFactor(zoom_level);
|
|
}
|
|
|
|
v8::Local<v8::Value> GetWebPreference(v8::Isolate* isolate,
|
|
std::string pref_name) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "getWebPreference", &render_frame))
|
|
return v8::Undefined(isolate);
|
|
|
|
const auto& prefs = render_frame->GetBlinkPreferences();
|
|
|
|
if (pref_name == options::kPreloadScripts) {
|
|
return gin::ConvertToV8(isolate, prefs.preloads);
|
|
} else if (pref_name == options::kDisableElectronSiteInstanceOverrides) {
|
|
return gin::ConvertToV8(isolate,
|
|
prefs.disable_electron_site_instance_overrides);
|
|
} else if (pref_name == options::kBackgroundColor) {
|
|
return gin::ConvertToV8(isolate, prefs.background_color);
|
|
} else if (pref_name == options::kOpenerID) {
|
|
// NOTE: openerId is internal-only.
|
|
return gin::ConvertToV8(isolate, prefs.opener_id);
|
|
} else if (pref_name == options::kContextIsolation) {
|
|
return gin::ConvertToV8(isolate, prefs.context_isolation);
|
|
} else if (pref_name == options::kGuestInstanceID) {
|
|
// NOTE: guestInstanceId is internal-only.
|
|
return gin::ConvertToV8(isolate, prefs.guest_instance_id);
|
|
} else if (pref_name == options::kHiddenPage) {
|
|
// NOTE: hiddenPage is internal-only.
|
|
return gin::ConvertToV8(isolate, prefs.hidden_page);
|
|
} else if (pref_name == options::kOffscreen) {
|
|
return gin::ConvertToV8(isolate, prefs.offscreen);
|
|
} else if (pref_name == options::kPreloadScript) {
|
|
return gin::ConvertToV8(isolate, prefs.preload.value());
|
|
} else if (pref_name == options::kNativeWindowOpen) {
|
|
return gin::ConvertToV8(isolate, prefs.native_window_open);
|
|
} else if (pref_name == options::kNodeIntegration) {
|
|
return gin::ConvertToV8(isolate, prefs.node_integration);
|
|
} else if (pref_name == options::kNodeIntegrationInWorker) {
|
|
return gin::ConvertToV8(isolate, prefs.node_integration_in_worker);
|
|
} else if (pref_name == options::kEnableNodeLeakageInRenderers) {
|
|
// NOTE: enableNodeLeakageInRenderers is internal-only.
|
|
return gin::ConvertToV8(isolate, prefs.node_leakage_in_renderers);
|
|
} else if (pref_name == options::kNodeIntegrationInSubFrames) {
|
|
return gin::ConvertToV8(isolate, true);
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
} else if (pref_name == options::kSpellcheck) {
|
|
return gin::ConvertToV8(isolate, prefs.enable_spellcheck);
|
|
#endif
|
|
} else if (pref_name == options::kPlugins) {
|
|
return gin::ConvertToV8(isolate, prefs.enable_plugins);
|
|
} else if (pref_name == options::kEnableWebSQL) {
|
|
return gin::ConvertToV8(isolate, prefs.enable_websql);
|
|
} else if (pref_name == options::kWebviewTag) {
|
|
return gin::ConvertToV8(isolate, prefs.webview_tag);
|
|
}
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
void SetVisualZoomLevelLimits(v8::Isolate* isolate,
|
|
double min_level,
|
|
double max_level) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "setVisualZoomLevelLimits",
|
|
&render_frame))
|
|
return;
|
|
|
|
blink::WebFrame* web_frame = render_frame->GetWebFrame();
|
|
web_frame->View()->SetDefaultPageScaleLimits(min_level, max_level);
|
|
}
|
|
|
|
void AllowGuestViewElementDefinition(v8::Isolate* isolate,
|
|
v8::Local<v8::Object> context,
|
|
v8::Local<v8::Function> register_cb) {
|
|
v8::HandleScope handle_scope(isolate);
|
|
v8::Context::Scope context_scope(context->CreationContext());
|
|
blink::WebCustomElement::EmbedderNamesAllowedScope embedder_names_scope;
|
|
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "allowGuestViewElementDefinition",
|
|
&render_frame))
|
|
return;
|
|
|
|
render_frame->GetWebFrame()->RequestExecuteV8Function(
|
|
context->CreationContext(), register_cb, v8::Null(isolate), 0, nullptr,
|
|
nullptr);
|
|
}
|
|
|
|
static int GetWebFrameId(v8::Local<v8::Value> content_window) {
|
|
// Get the WebLocalFrame before (possibly) executing any user-space JS while
|
|
// getting the |params|. We track the status of the RenderFrame via an
|
|
// observer in case it is deleted during user code execution.
|
|
content::RenderFrame* render_frame = GetRenderFrame(content_window);
|
|
RenderFrameStatus render_frame_status(render_frame);
|
|
|
|
if (!render_frame_status.is_ok())
|
|
return -1;
|
|
|
|
blink::WebLocalFrame* frame = render_frame->GetWebFrame();
|
|
// Parent must exist.
|
|
blink::WebFrame* parent_frame = frame->Parent();
|
|
DCHECK(parent_frame);
|
|
DCHECK(parent_frame->IsWebLocalFrame());
|
|
|
|
return render_frame->GetRoutingID();
|
|
}
|
|
|
|
void SetSpellCheckProvider(gin_helper::ErrorThrower thrower,
|
|
v8::Isolate* isolate,
|
|
const std::string& language,
|
|
v8::Local<v8::Object> provider) {
|
|
auto context = isolate->GetCurrentContext();
|
|
if (!provider->Has(context, gin::StringToV8(isolate, "spellCheck"))
|
|
.ToChecked()) {
|
|
thrower.ThrowError("\"spellCheck\" has to be defined");
|
|
return;
|
|
}
|
|
|
|
// Remove the old client.
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "setSpellCheckProvider", &render_frame))
|
|
return;
|
|
|
|
auto* existing = SpellCheckerHolder::FromRenderFrame(render_frame);
|
|
if (existing)
|
|
existing->UnsetAndDestroy();
|
|
|
|
// Set spellchecker for all live frames in the same process or
|
|
// in the sandbox mode for all live sub frames to this WebFrame.
|
|
auto spell_check_client =
|
|
std::make_unique<SpellCheckClient>(language, isolate, provider);
|
|
FrameSetSpellChecker spell_checker(spell_check_client.get(), render_frame);
|
|
|
|
// Attach the spell checker to RenderFrame.
|
|
new SpellCheckerHolder(render_frame, std::move(spell_check_client));
|
|
}
|
|
|
|
void InsertText(v8::Isolate* isolate, const std::string& text) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "insertText", &render_frame))
|
|
return;
|
|
|
|
blink::WebFrame* web_frame = render_frame->GetWebFrame();
|
|
if (web_frame->IsWebLocalFrame()) {
|
|
web_frame->ToWebLocalFrame()
|
|
->FrameWidget()
|
|
->GetActiveWebInputMethodController()
|
|
->CommitText(blink::WebString::FromUTF8(text),
|
|
blink::WebVector<ui::ImeTextSpan>(), blink::WebRange(),
|
|
0);
|
|
}
|
|
}
|
|
|
|
std::u16string InsertCSS(v8::Isolate* isolate,
|
|
const std::string& css,
|
|
gin::Arguments* args) {
|
|
blink::WebDocument::CSSOrigin css_origin =
|
|
blink::WebDocument::CSSOrigin::kAuthorOrigin;
|
|
|
|
gin_helper::Dictionary options;
|
|
if (args->GetNext(&options))
|
|
options.Get("cssOrigin", &css_origin);
|
|
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "insertCSS", &render_frame))
|
|
return std::u16string();
|
|
|
|
blink::WebFrame* web_frame = render_frame->GetWebFrame();
|
|
if (web_frame->IsWebLocalFrame()) {
|
|
return web_frame->ToWebLocalFrame()
|
|
->GetDocument()
|
|
.InsertStyleSheet(blink::WebString::FromUTF8(css), nullptr,
|
|
css_origin)
|
|
.Utf16();
|
|
}
|
|
return std::u16string();
|
|
}
|
|
|
|
void RemoveInsertedCSS(v8::Isolate* isolate, const std::u16string& key) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "removeInsertedCSS", &render_frame))
|
|
return;
|
|
|
|
blink::WebFrame* web_frame = render_frame->GetWebFrame();
|
|
if (web_frame->IsWebLocalFrame()) {
|
|
web_frame->ToWebLocalFrame()->GetDocument().RemoveInsertedStyleSheet(
|
|
blink::WebString::FromUTF16(key));
|
|
}
|
|
}
|
|
|
|
v8::Local<v8::Promise> ExecuteJavaScript(gin::Arguments* gin_args,
|
|
const std::u16string& code) {
|
|
gin_helper::Arguments* args = static_cast<gin_helper::Arguments*>(gin_args);
|
|
|
|
v8::Isolate* isolate = args->isolate();
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
content::RenderFrame* render_frame;
|
|
std::string error_msg;
|
|
if (!MaybeGetRenderFrame(&error_msg, "executeJavaScript", &render_frame)) {
|
|
promise.RejectWithErrorMessage(error_msg);
|
|
return handle;
|
|
}
|
|
|
|
bool has_user_gesture = false;
|
|
args->GetNext(&has_user_gesture);
|
|
|
|
ScriptExecutionCallback::CompletionCallback completion_callback;
|
|
args->GetNext(&completion_callback);
|
|
|
|
render_frame->GetWebFrame()->RequestExecuteScriptAndReturnValue(
|
|
blink::WebScriptSource(blink::WebString::FromUTF16(code)),
|
|
has_user_gesture,
|
|
new ScriptExecutionCallback(std::move(promise),
|
|
std::move(completion_callback)));
|
|
|
|
return handle;
|
|
}
|
|
|
|
v8::Local<v8::Promise> ExecuteJavaScriptInIsolatedWorld(
|
|
gin::Arguments* gin_args,
|
|
int world_id,
|
|
const std::vector<gin_helper::Dictionary>& scripts) {
|
|
gin_helper::Arguments* args = static_cast<gin_helper::Arguments*>(gin_args);
|
|
|
|
v8::Isolate* isolate = args->isolate();
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
content::RenderFrame* render_frame;
|
|
std::string error_msg;
|
|
if (!MaybeGetRenderFrame(&error_msg, "executeJavaScriptInIsolatedWorld",
|
|
&render_frame)) {
|
|
promise.RejectWithErrorMessage(error_msg);
|
|
return handle;
|
|
}
|
|
|
|
bool has_user_gesture = false;
|
|
args->GetNext(&has_user_gesture);
|
|
|
|
blink::WebLocalFrame::ScriptExecutionType scriptExecutionType =
|
|
blink::WebLocalFrame::kSynchronous;
|
|
args->GetNext(&scriptExecutionType);
|
|
|
|
ScriptExecutionCallback::CompletionCallback completion_callback;
|
|
args->GetNext(&completion_callback);
|
|
|
|
std::vector<blink::WebScriptSource> sources;
|
|
|
|
for (const auto& script : scripts) {
|
|
std::u16string code;
|
|
std::u16string url;
|
|
int start_line = 1;
|
|
script.Get("url", &url);
|
|
script.Get("startLine", &start_line);
|
|
|
|
if (!script.Get("code", &code)) {
|
|
const char* error_message = "Invalid 'code'";
|
|
if (!completion_callback.is_null()) {
|
|
std::move(completion_callback)
|
|
.Run(v8::Undefined(isolate),
|
|
v8::Exception::Error(
|
|
v8::String::NewFromUtf8(isolate, error_message)
|
|
.ToLocalChecked()));
|
|
}
|
|
promise.RejectWithErrorMessage(error_message);
|
|
return handle;
|
|
}
|
|
|
|
sources.emplace_back(
|
|
blink::WebScriptSource(blink::WebString::FromUTF16(code),
|
|
blink::WebURL(GURL(url)), start_line));
|
|
}
|
|
|
|
render_frame->GetWebFrame()->RequestExecuteScriptInIsolatedWorld(
|
|
world_id, &sources.front(), sources.size(), has_user_gesture,
|
|
scriptExecutionType,
|
|
new ScriptExecutionCallback(std::move(promise),
|
|
std::move(completion_callback)),
|
|
blink::BackForwardCacheAware::kPossiblyDisallow);
|
|
|
|
return handle;
|
|
}
|
|
|
|
void SetIsolatedWorldInfo(v8::Isolate* isolate,
|
|
int world_id,
|
|
const gin_helper::Dictionary& options) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "setIsolatedWorldInfo", &render_frame))
|
|
return;
|
|
|
|
std::string origin_url, security_policy, name;
|
|
options.Get("securityOrigin", &origin_url);
|
|
options.Get("csp", &security_policy);
|
|
options.Get("name", &name);
|
|
|
|
if (!security_policy.empty() && origin_url.empty()) {
|
|
gin_helper::ErrorThrower(isolate).ThrowError(
|
|
"If csp is specified, securityOrigin should also be specified");
|
|
return;
|
|
}
|
|
|
|
blink::WebIsolatedWorldInfo info;
|
|
info.security_origin = blink::WebSecurityOrigin::CreateFromString(
|
|
blink::WebString::FromUTF8(origin_url));
|
|
info.content_security_policy = blink::WebString::FromUTF8(security_policy);
|
|
info.human_readable_name = blink::WebString::FromUTF8(name);
|
|
blink::SetIsolatedWorldInfo(world_id, info);
|
|
}
|
|
|
|
blink::WebCacheResourceTypeStats GetResourceUsage(v8::Isolate* isolate) {
|
|
blink::WebCacheResourceTypeStats stats;
|
|
blink::WebCache::GetResourceTypeStats(&stats);
|
|
return stats;
|
|
}
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
bool IsWordMisspelled(v8::Isolate* isolate, const std::string& word) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "isWordMisspelled", &render_frame))
|
|
return false;
|
|
|
|
return !SpellCheckWord(render_frame, word, nullptr);
|
|
}
|
|
|
|
std::vector<std::u16string> GetWordSuggestions(v8::Isolate* isolate,
|
|
const std::string& word) {
|
|
content::RenderFrame* render_frame;
|
|
std::vector<std::u16string> suggestions;
|
|
if (!MaybeGetRenderFrame(isolate, "getWordSuggestions", &render_frame))
|
|
return suggestions;
|
|
|
|
SpellCheckWord(render_frame, word, &suggestions);
|
|
return suggestions;
|
|
}
|
|
#endif
|
|
|
|
void ClearCache(v8::Isolate* isolate) {
|
|
isolate->IdleNotificationDeadline(0.5);
|
|
blink::WebCache::Clear();
|
|
base::MemoryPressureListener::NotifyMemoryPressure(
|
|
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
|
|
}
|
|
|
|
v8::Local<v8::Value> FindFrameByRoutingId(v8::Isolate* isolate,
|
|
int routing_id) {
|
|
content::RenderFrame* render_frame =
|
|
content::RenderFrame::FromRoutingID(routing_id);
|
|
if (render_frame)
|
|
return WebFrameRenderer::Create(isolate, render_frame).ToV8();
|
|
else
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
v8::Local<v8::Value> GetOpener(v8::Isolate* isolate) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "opener", &render_frame))
|
|
return v8::Null(isolate);
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->Opener();
|
|
if (frame && frame->IsWebLocalFrame())
|
|
return WebFrameRenderer::Create(
|
|
isolate,
|
|
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame()))
|
|
.ToV8();
|
|
else
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
// Don't name it as GetParent, Windows has API with same name.
|
|
v8::Local<v8::Value> GetFrameParent(v8::Isolate* isolate) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "parent", &render_frame))
|
|
return v8::Null(isolate);
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->Parent();
|
|
if (frame && frame->IsWebLocalFrame())
|
|
return WebFrameRenderer::Create(
|
|
isolate,
|
|
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame()))
|
|
.ToV8();
|
|
else
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
v8::Local<v8::Value> GetTop(v8::Isolate* isolate) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "top", &render_frame))
|
|
return v8::Null(isolate);
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->Top();
|
|
if (frame && frame->IsWebLocalFrame())
|
|
return WebFrameRenderer::Create(
|
|
isolate,
|
|
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame()))
|
|
.ToV8();
|
|
else
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
v8::Local<v8::Value> GetFirstChild(v8::Isolate* isolate) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "firstChild", &render_frame))
|
|
return v8::Null(isolate);
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->FirstChild();
|
|
if (frame && frame->IsWebLocalFrame())
|
|
return WebFrameRenderer::Create(
|
|
isolate,
|
|
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame()))
|
|
.ToV8();
|
|
else
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
v8::Local<v8::Value> GetNextSibling(v8::Isolate* isolate) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "nextSibling", &render_frame))
|
|
return v8::Null(isolate);
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->NextSibling();
|
|
if (frame && frame->IsWebLocalFrame())
|
|
return WebFrameRenderer::Create(
|
|
isolate,
|
|
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame()))
|
|
.ToV8();
|
|
else
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
v8::Local<v8::Value> GetFrameForSelector(v8::Isolate* isolate,
|
|
const std::string& selector) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "getFrameForSelector", &render_frame))
|
|
return v8::Null(isolate);
|
|
|
|
blink::WebElement element =
|
|
render_frame->GetWebFrame()->GetDocument().QuerySelector(
|
|
blink::WebString::FromUTF8(selector));
|
|
if (element.IsNull()) // not found
|
|
return v8::Null(isolate);
|
|
|
|
blink::WebFrame* frame = blink::WebFrame::FromFrameOwnerElement(element);
|
|
if (frame && frame->IsWebLocalFrame())
|
|
return WebFrameRenderer::Create(
|
|
isolate,
|
|
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame()))
|
|
.ToV8();
|
|
else
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
v8::Local<v8::Value> FindFrameByName(v8::Isolate* isolate,
|
|
const std::string& name) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "getFrameForSelector", &render_frame))
|
|
return v8::Null(isolate);
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->FindFrameByName(
|
|
blink::WebString::FromUTF8(name));
|
|
if (frame && frame->IsWebLocalFrame())
|
|
return WebFrameRenderer::Create(
|
|
isolate,
|
|
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame()))
|
|
.ToV8();
|
|
else
|
|
return v8::Null(isolate);
|
|
}
|
|
|
|
int GetRoutingId(v8::Isolate* isolate) {
|
|
content::RenderFrame* render_frame;
|
|
if (!MaybeGetRenderFrame(isolate, "routingId", &render_frame))
|
|
return 0;
|
|
|
|
return render_frame->GetRoutingID();
|
|
}
|
|
};
|
|
|
|
gin::WrapperInfo WebFrameRenderer::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
// static
|
|
std::set<SpellCheckerHolder*> SpellCheckerHolder::instances_;
|
|
|
|
} // namespace api
|
|
|
|
} // namespace electron
|
|
|
|
namespace {
|
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
v8::Local<v8::Value> unused,
|
|
v8::Local<v8::Context> context,
|
|
void* priv) {
|
|
using namespace electron::api; // NOLINT(build/namespaces)
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
gin_helper::Dictionary dict(isolate, exports);
|
|
dict.Set("mainFrame",
|
|
WebFrameRenderer::Create(isolate, GetRenderFrame(exports)));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_renderer_web_frame, Initialize)
|