2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-10-24 10:24:12 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-03-13 23:13:05 +00:00
|
|
|
#include <limits>
|
2019-02-08 21:38:31 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2019-01-24 16:18:54 +00:00
|
|
|
#include <utility>
|
2019-02-08 21:38:31 +00:00
|
|
|
#include <vector>
|
2019-01-24 16:18:54 +00:00
|
|
|
|
2020-07-24 07:08:04 +00:00
|
|
|
#include "base/command_line.h"
|
2016-05-12 21:36:49 +00:00
|
|
|
#include "base/memory/memory_pressure_listener.h"
|
2020-10-19 11:48:16 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "components/spellcheck/renderer/spellcheck.h"
|
2014-12-08 16:05:34 +00:00
|
|
|
#include "content/public/renderer/render_frame.h"
|
2018-08-16 22:57:40 +00:00
|
|
|
#include "content/public/renderer/render_frame_observer.h"
|
2017-11-24 06:34:20 +00:00
|
|
|
#include "content/public/renderer/render_frame_visitor.h"
|
2015-10-06 15:33:14 +00:00
|
|
|
#include "content/public/renderer/render_view.h"
|
2019-06-04 01:18:23 +00:00
|
|
|
#include "services/service_manager/public/cpp/interface_provider.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/api/api.mojom.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_converters/blink_converter.h"
|
2020-03-03 00:11:40 +00:00
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
2020-11-10 17:06:03 +00:00
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2020-04-03 02:59:19 +00:00
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2019-11-01 06:10:32 +00:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2020-07-23 21:32:20 +00:00
|
|
|
#include "shell/common/options_switches.h"
|
2021-01-26 22:23:35 +00:00
|
|
|
#include "shell/renderer/api/context_bridge/object_cache.h"
|
|
|
|
#include "shell/renderer/api/electron_api_context_bridge.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/renderer/api/electron_api_spell_check_client.h"
|
2020-10-19 11:48:16 +00:00
|
|
|
#include "shell/renderer/electron_renderer_client.h"
|
2020-12-14 18:57:36 +00:00
|
|
|
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
|
2019-10-18 19:57:34 +00:00
|
|
|
#include "third_party/blink/public/common/page/page_zoom.h"
|
|
|
|
#include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h"
|
2020-11-10 17:06:03 +00:00
|
|
|
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
|
2018-07-20 16:08:18 +00:00
|
|
|
#include "third_party/blink/public/platform/web_cache.h"
|
2019-01-21 20:45:15 +00:00
|
|
|
#include "third_party/blink/public/platform/web_isolated_world_info.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "third_party/blink/public/web/web_custom_element.h"
|
2018-07-20 16:08:18 +00:00
|
|
|
#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"
|
2020-06-22 17:35:10 +00:00
|
|
|
#include "ui/base/ime/ime_text_span.h"
|
2018-10-27 00:31:56 +00:00
|
|
|
#include "url/url_util.h"
|
2014-10-24 10:24:12 +00:00
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
namespace gin {
|
2017-08-30 21:31:21 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Converter<blink::WebLocalFrame::ScriptExecutionType> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
2017-11-15 10:14:41 +00:00
|
|
|
v8::Local<v8::Value> val,
|
2017-08-30 21:31:21 +00:00
|
|
|
blink::WebLocalFrame::ScriptExecutionType* out) {
|
|
|
|
std::string execution_type;
|
|
|
|
if (!ConvertFromV8(isolate, val, &execution_type))
|
|
|
|
return false;
|
|
|
|
if (execution_type == "asynchronous") {
|
|
|
|
*out = blink::WebLocalFrame::kAsynchronous;
|
2018-04-18 01:55:30 +00:00
|
|
|
} else if (execution_type == "asynchronousBlockingOnload") {
|
2017-08-30 21:31:21 +00:00
|
|
|
*out = blink::WebLocalFrame::kAsynchronousBlockingOnload;
|
|
|
|
} else if (execution_type == "synchronous") {
|
|
|
|
*out = blink::WebLocalFrame::kSynchronous;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2017-11-15 10:14:41 +00:00
|
|
|
|
2019-07-17 21:34:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
} // namespace gin
|
2017-08-30 21:31:21 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2014-10-24 10:24:12 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2016-02-17 17:03:27 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:18:16 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
|
2020-10-19 11:48:16 +00:00
|
|
|
bool SpellCheckWord(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
const std::string& word,
|
2021-03-16 16:18:45 +00:00
|
|
|
std::vector<std::u16string>* optional_suggestions) {
|
2020-10-19 11:48:16 +00:00
|
|
|
size_t start;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
ElectronRendererClient* client = ElectronRendererClient::Get();
|
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame)
|
|
|
|
return true;
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string w = base::UTF8ToUTF16(word);
|
2020-10-19 11:48:16 +00:00
|
|
|
int id = render_frame->GetRoutingID();
|
|
|
|
return client->GetSpellCheck()->SpellCheckWord(
|
|
|
|
w.c_str(), 0, word.size(), id, &start, &length, optional_suggestions);
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:18:16 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-13 14:26:59 +00:00
|
|
|
class RenderFrameStatus final : public content::RenderFrameObserver {
|
2018-08-16 22:57:40 +00:00
|
|
|
public:
|
|
|
|
explicit RenderFrameStatus(content::RenderFrame* render_frame)
|
|
|
|
: content::RenderFrameObserver(render_frame) {}
|
2019-09-16 22:12:00 +00:00
|
|
|
~RenderFrameStatus() final = default;
|
2018-08-16 22:57:40 +00:00
|
|
|
|
|
|
|
bool is_ok() { return render_frame() != nullptr; }
|
|
|
|
|
|
|
|
// RenderFrameObserver implementation.
|
|
|
|
void OnDestruct() final {}
|
|
|
|
};
|
|
|
|
|
2016-02-17 17:03:27 +00:00
|
|
|
class ScriptExecutionCallback : public blink::WebScriptExecutionCallback {
|
|
|
|
public:
|
2020-03-03 00:11:40 +00:00
|
|
|
// 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)>;
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
explicit ScriptExecutionCallback(
|
2020-03-03 00:11:40 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise,
|
2020-07-23 21:32:20 +00:00
|
|
|
bool world_safe_result,
|
2020-03-03 00:11:40 +00:00
|
|
|
CompletionCallback callback)
|
2020-07-23 21:32:20 +00:00
|
|
|
: promise_(std::move(promise)),
|
|
|
|
world_safe_result_(world_safe_result),
|
|
|
|
callback_(std::move(callback)) {}
|
2020-03-03 00:11:40 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
~ScriptExecutionCallback() override = default;
|
2016-02-17 17:03:27 +00:00
|
|
|
|
2020-07-23 21:32:20 +00:00
|
|
|
void CopyResultToCallingContextAndFinalize(
|
|
|
|
v8::Isolate* isolate,
|
2021-01-26 22:23:35 +00:00
|
|
|
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";
|
2020-07-23 21:32:20 +00:00
|
|
|
{
|
|
|
|
v8::TryCatch try_catch(isolate);
|
2021-01-26 22:23:35 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-07-23 21:32:20 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
auto message = try_catch.Message();
|
|
|
|
|
2021-01-26 22:23:35 +00:00
|
|
|
if (!message.IsEmpty()) {
|
|
|
|
gin::ConvertFromV8(isolate, message->Get(), &error_message);
|
2020-07-23 21:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2021-01-26 22:23:35 +00:00
|
|
|
v8::Local<v8::Value> cloned_value = maybe_result.ToLocalChecked();
|
2020-07-23 21:32:20 +00:00
|
|
|
if (callback_)
|
|
|
|
std::move(callback_).Run(cloned_value, v8::Undefined(isolate));
|
|
|
|
promise_.Resolve(cloned_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:42:33 +00:00
|
|
|
void Completed(
|
2016-02-17 17:03:27 +00:00
|
|
|
const blink::WebVector<v8::Local<v8::Value>>& result) override {
|
2021-03-29 10:45:08 +00:00
|
|
|
v8::Isolate* isolate = promise_.isolate();
|
2019-06-05 22:43:02 +00:00
|
|
|
if (!result.empty()) {
|
|
|
|
if (!result[0].IsEmpty()) {
|
2020-07-23 21:32:20 +00:00
|
|
|
v8::Local<v8::Value> value = result[0];
|
|
|
|
// Either world safe results are disabled or 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 =
|
|
|
|
world_safe_result_ &&
|
|
|
|
!(value->IsObject() &&
|
|
|
|
promise_.GetContext() ==
|
|
|
|
value.As<v8::Object>()->CreationContext()) &&
|
|
|
|
value->IsObject();
|
|
|
|
if (should_clone_value) {
|
2021-01-26 22:23:35 +00:00
|
|
|
CopyResultToCallingContextAndFinalize(isolate,
|
|
|
|
value.As<v8::Object>());
|
2020-07-23 21:32:20 +00:00
|
|
|
} else {
|
|
|
|
// Right now only single results per frame is supported.
|
|
|
|
if (callback_)
|
|
|
|
std::move(callback_).Run(value, v8::Undefined(isolate));
|
|
|
|
promise_.Resolve(value);
|
|
|
|
}
|
2019-06-05 22:43:02 +00:00
|
|
|
} else {
|
2020-03-03 00:11:40 +00:00
|
|
|
const char* error_message =
|
2019-06-05 22:43:02 +00:00
|
|
|
"Script failed to execute, this normally means an error "
|
2020-03-03 00:11:40 +00:00
|
|
|
"was thrown. Check the renderer console for the error.";
|
|
|
|
if (!callback_.is_null()) {
|
2020-07-23 21:32:20 +00:00
|
|
|
v8::Local<v8::Context> context = promise_.GetContext();
|
|
|
|
v8::Context::Scope context_scope(context);
|
2020-03-03 00:11:40 +00:00
|
|
|
std::move(callback_).Run(
|
|
|
|
v8::Undefined(isolate),
|
|
|
|
v8::Exception::Error(
|
|
|
|
v8::String::NewFromUtf8(isolate, error_message)
|
|
|
|
.ToLocalChecked()));
|
|
|
|
}
|
|
|
|
promise_.RejectWithErrorMessage(error_message);
|
2019-06-05 22:43:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-03 00:11:40 +00:00
|
|
|
const char* error_message =
|
2019-06-05 22:43:02 +00:00
|
|
|
"WebFrame was removed before script could run. This normally means "
|
2020-03-03 00:11:40 +00:00
|
|
|
"the underlying frame was destroyed";
|
|
|
|
if (!callback_.is_null()) {
|
2020-07-23 21:32:20 +00:00
|
|
|
v8::Local<v8::Context> context = promise_.GetContext();
|
|
|
|
v8::Context::Scope context_scope(context);
|
2020-03-03 00:11:40 +00:00
|
|
|
std::move(callback_).Run(
|
|
|
|
v8::Undefined(isolate),
|
|
|
|
v8::Exception::Error(v8::String::NewFromUtf8(isolate, error_message)
|
|
|
|
.ToLocalChecked()));
|
|
|
|
}
|
|
|
|
promise_.RejectWithErrorMessage(error_message);
|
2019-06-05 22:43:02 +00:00
|
|
|
}
|
2016-02-17 17:03:27 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise_;
|
2020-07-23 21:32:20 +00:00
|
|
|
bool world_safe_result_;
|
2020-03-03 00:11:40 +00:00
|
|
|
CompletionCallback callback_;
|
2016-02-17 17:03:27 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ScriptExecutionCallback);
|
|
|
|
};
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
class FrameSetSpellChecker : public content::RenderFrameVisitor {
|
2017-11-24 06:34:20 +00:00
|
|
|
public:
|
2019-02-08 21:38:31 +00:00
|
|
|
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);
|
2017-11-24 06:34:20 +00:00
|
|
|
}
|
2019-02-08 21:38:31 +00:00
|
|
|
|
2017-11-24 06:34:20 +00:00
|
|
|
bool Visit(content::RenderFrame* render_frame) override {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* view = render_frame->GetRenderView();
|
2017-11-24 06:34:20 +00:00
|
|
|
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_;
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(FrameSetSpellChecker);
|
|
|
|
};
|
2016-02-17 17:03:27 +00:00
|
|
|
|
2019-09-13 14:26:59 +00:00
|
|
|
class SpellCheckerHolder final : public content::RenderFrameObserver {
|
2019-01-24 16:18:54 +00:00
|
|
|
public:
|
2019-02-08 21:38:31 +00:00
|
|
|
// 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)
|
2019-01-24 16:18:54 +00:00
|
|
|
: content::RenderFrameObserver(render_frame),
|
2019-02-08 21:38:31 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-01-24 16:18:54 +00:00
|
|
|
|
|
|
|
// RenderFrameObserver implementation.
|
|
|
|
void OnDestruct() final {
|
2019-02-08 21:38:31 +00:00
|
|
|
// 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.
|
2019-01-24 16:18:54 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:18:54 +00:00
|
|
|
private:
|
2019-02-08 21:38:31 +00:00
|
|
|
static std::set<SpellCheckerHolder*> instances_;
|
|
|
|
|
2019-01-24 16:18:54 +00:00
|
|
|
std::unique_ptr<SpellCheckClient> spell_check_client_;
|
|
|
|
};
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
} // namespace
|
2014-10-24 10:24:12 +00:00
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
// static
|
|
|
|
std::set<SpellCheckerHolder*> SpellCheckerHolder::instances_;
|
2014-10-24 10:24:12 +00:00
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
void SetName(v8::Local<v8::Value> window, const std::string& name) {
|
|
|
|
GetRenderFrame(window)->GetWebFrame()->SetName(
|
|
|
|
blink::WebString::FromUTF8(name));
|
2014-10-24 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
void SetZoomLevel(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
double level) {
|
2019-02-08 21:38:31 +00:00
|
|
|
content::RenderFrame* render_frame = GetRenderFrame(window);
|
2020-04-03 02:59:19 +00:00
|
|
|
if (!render_frame) {
|
|
|
|
thrower.ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.setZoomLevel could be "
|
|
|
|
"executed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
mojo::Remote<mojom::ElectronBrowser> browser_remote;
|
2020-12-14 18:57:36 +00:00
|
|
|
render_frame->GetBrowserInterfaceBroker()->GetInterface(
|
2020-06-22 17:35:10 +00:00
|
|
|
browser_remote.BindNewPipeAndPassReceiver());
|
|
|
|
browser_remote->SetTemporaryZoomLevel(level);
|
2014-10-24 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
double GetZoomLevel(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> window) {
|
2017-01-31 09:25:48 +00:00
|
|
|
double result = 0.0;
|
2019-02-08 21:38:31 +00:00
|
|
|
content::RenderFrame* render_frame = GetRenderFrame(window);
|
2020-04-03 02:59:19 +00:00
|
|
|
if (!render_frame) {
|
|
|
|
thrower.ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.getZoomLevel could be "
|
|
|
|
"executed");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
mojo::Remote<mojom::ElectronBrowser> browser_remote;
|
2020-12-14 18:57:36 +00:00
|
|
|
render_frame->GetBrowserInterfaceBroker()->GetInterface(
|
2020-06-22 17:35:10 +00:00
|
|
|
browser_remote.BindNewPipeAndPassReceiver());
|
|
|
|
browser_remote->DoGetZoomLevel(&result);
|
2017-01-30 17:06:50 +00:00
|
|
|
return result;
|
2014-10-24 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 23:13:05 +00:00
|
|
|
void SetZoomFactor(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
double factor) {
|
|
|
|
if (factor < std::numeric_limits<double>::epsilon()) {
|
|
|
|
thrower.ThrowError("'zoomFactor' must be a double greater than 0.0");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
SetZoomLevel(thrower, window, blink::PageZoomFactorToZoomLevel(factor));
|
2014-10-24 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
double GetZoomFactor(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> window) {
|
|
|
|
double zoom_level = GetZoomLevel(thrower, window);
|
|
|
|
return blink::PageZoomLevelToZoomFactor(zoom_level);
|
2014-10-24 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
v8::Local<v8::Value> GetWebPreference(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
std::string pref_name) {
|
|
|
|
content::RenderFrame* render_frame = GetRenderFrame(window);
|
|
|
|
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::kWorldSafeExecuteJavaScript) {
|
|
|
|
return gin::ConvertToV8(isolate, prefs.world_safe_execute_javascript);
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
void SetVisualZoomLevelLimits(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> window,
|
2019-02-08 21:38:31 +00:00
|
|
|
double min_level,
|
|
|
|
double max_level) {
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
thrower.ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.setVisualZoomLevelLimits "
|
|
|
|
"could be executed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
blink::WebFrame* web_frame = render_frame->GetWebFrame();
|
2019-02-08 21:38:31 +00:00
|
|
|
web_frame->View()->SetDefaultPageScaleLimits(min_level, max_level);
|
2015-08-27 14:08:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
void AllowGuestViewElementDefinition(gin_helper::ErrorThrower thrower,
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
v8::Local<v8::Object> context,
|
|
|
|
v8::Local<v8::Function> register_cb) {
|
2020-04-03 02:59:19 +00:00
|
|
|
v8::HandleScope handle_scope(thrower.isolate());
|
2018-12-14 06:38:35 +00:00
|
|
|
v8::Context::Scope context_scope(context->CreationContext());
|
2019-01-12 01:00:43 +00:00
|
|
|
blink::WebCustomElement::EmbedderNamesAllowedScope embedder_names_scope;
|
2020-04-03 02:59:19 +00:00
|
|
|
|
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
thrower.ThrowError(
|
|
|
|
"Render frame was torn down before "
|
|
|
|
"webFrame.allowGuestViewElementDefinition could be executed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_frame->GetWebFrame()->RequestExecuteV8Function(
|
|
|
|
context->CreationContext(), register_cb, v8::Null(thrower.isolate()), 0,
|
|
|
|
nullptr, nullptr);
|
2014-10-24 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
int GetWebFrameId(v8::Local<v8::Value> window,
|
|
|
|
v8::Local<v8::Value> content_window) {
|
2018-08-16 22:57:40 +00:00
|
|
|
// 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);
|
2015-05-29 05:47:09 +00:00
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
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());
|
2014-12-08 16:05:34 +00:00
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
return render_frame->GetRoutingID();
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
void SetSpellCheckProvider(gin_helper::Arguments* args,
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
const std::string& language,
|
|
|
|
v8::Local<v8::Object> provider) {
|
2019-01-09 19:17:05 +00:00
|
|
|
auto context = args->isolate()->GetCurrentContext();
|
2019-10-31 07:56:00 +00:00
|
|
|
if (!provider->Has(context, gin::StringToV8(args->isolate(), "spellCheck"))
|
2019-01-09 19:17:05 +00:00
|
|
|
.ToChecked()) {
|
2014-12-20 04:42:19 +00:00
|
|
|
args->ThrowError("\"spellCheck\" has to be defined");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
// Remove the old client.
|
|
|
|
content::RenderFrame* render_frame = GetRenderFrame(window);
|
2020-04-03 02:59:19 +00:00
|
|
|
if (!render_frame) {
|
|
|
|
args->ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.setSpellCheckProvider "
|
|
|
|
"could be executed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
auto* existing = SpellCheckerHolder::FromRenderFrame(render_frame);
|
|
|
|
if (existing)
|
|
|
|
existing->UnsetAndDestroy();
|
|
|
|
|
2017-11-24 06:34:20 +00:00
|
|
|
// Set spellchecker for all live frames in the same process or
|
|
|
|
// in the sandbox mode for all live sub frames to this WebFrame.
|
2019-02-08 21:38:31 +00:00
|
|
|
auto spell_check_client =
|
|
|
|
std::make_unique<SpellCheckClient>(language, args->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));
|
2014-12-18 21:54:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
void InsertText(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
const std::string& text) {
|
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
thrower.ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.insertText could be "
|
|
|
|
"executed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
blink::WebFrame* web_frame = render_frame->GetWebFrame();
|
2019-02-08 21:38:31 +00:00
|
|
|
if (web_frame->IsWebLocalFrame()) {
|
|
|
|
web_frame->ToWebLocalFrame()
|
|
|
|
->FrameWidget()
|
|
|
|
->GetActiveWebInputMethodController()
|
|
|
|
->CommitText(blink::WebString::FromUTF8(text),
|
2020-06-22 17:35:10 +00:00
|
|
|
blink::WebVector<ui::ImeTextSpan>(), blink::WebRange(), 0);
|
2019-02-08 21:38:31 +00:00
|
|
|
}
|
2016-01-13 03:21:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string InsertCSS(v8::Local<v8::Value> window,
|
2019-07-17 21:34:05 +00:00
|
|
|
const std::string& css,
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Arguments* args) {
|
2019-07-17 21:34:05 +00:00
|
|
|
blink::WebDocument::CSSOrigin css_origin =
|
|
|
|
blink::WebDocument::CSSOrigin::kAuthorOrigin;
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Dictionary options;
|
2019-07-17 21:34:05 +00:00
|
|
|
if (args->GetNext(&options))
|
|
|
|
options.Get("cssOrigin", &css_origin);
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
args->ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.insertCSS could be "
|
|
|
|
"executed");
|
2021-03-16 16:18:45 +00:00
|
|
|
return std::u16string();
|
2020-04-03 02:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blink::WebFrame* web_frame = render_frame->GetWebFrame();
|
2019-02-08 21:38:31 +00:00
|
|
|
if (web_frame->IsWebLocalFrame()) {
|
2019-06-17 15:39:36 +00:00
|
|
|
return web_frame->ToWebLocalFrame()
|
|
|
|
->GetDocument()
|
2019-07-17 21:34:05 +00:00
|
|
|
.InsertStyleSheet(blink::WebString::FromUTF8(css), nullptr, css_origin)
|
2019-06-17 15:39:36 +00:00
|
|
|
.Utf16();
|
|
|
|
}
|
2021-03-16 16:18:45 +00:00
|
|
|
return std::u16string();
|
2019-06-17 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
void RemoveInsertedCSS(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> window,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& key) {
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
thrower.ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.removeInsertedCSS could be "
|
|
|
|
"executed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
blink::WebFrame* web_frame = render_frame->GetWebFrame();
|
2019-06-17 15:39:36 +00:00
|
|
|
if (web_frame->IsWebLocalFrame()) {
|
|
|
|
web_frame->ToWebLocalFrame()->GetDocument().RemoveInsertedStyleSheet(
|
|
|
|
blink::WebString::FromUTF16(key));
|
2019-02-08 21:38:31 +00:00
|
|
|
}
|
2016-12-19 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
v8::Local<v8::Promise> ExecuteJavaScript(gin_helper::Arguments* args,
|
2019-03-14 19:08:54 +00:00
|
|
|
v8::Local<v8::Value> window,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& code) {
|
2019-03-14 19:08:54 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
2019-03-14 19:08:54 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
promise.RejectWithErrorMessage(
|
|
|
|
"Render frame was torn down before webFrame.executeJavaScript could be "
|
|
|
|
"executed");
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-01-13 04:17:56 +00:00
|
|
|
bool has_user_gesture = false;
|
|
|
|
args->GetNext(&has_user_gesture);
|
2019-03-14 19:08:54 +00:00
|
|
|
|
2020-03-03 00:11:40 +00:00
|
|
|
ScriptExecutionCallback::CompletionCallback completion_callback;
|
|
|
|
args->GetNext(&completion_callback);
|
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
auto& prefs = render_frame->GetBlinkPreferences();
|
2020-07-23 21:32:20 +00:00
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
render_frame->GetWebFrame()->RequestExecuteScriptAndReturnValue(
|
2017-06-16 20:42:33 +00:00
|
|
|
blink::WebScriptSource(blink::WebString::FromUTF16(code)),
|
2020-03-03 00:11:40 +00:00
|
|
|
has_user_gesture,
|
2020-11-10 17:06:03 +00:00
|
|
|
new ScriptExecutionCallback(std::move(promise),
|
|
|
|
prefs.world_safe_execute_javascript,
|
2020-03-03 00:11:40 +00:00
|
|
|
std::move(completion_callback)));
|
2019-03-14 19:08:54 +00:00
|
|
|
|
|
|
|
return handle;
|
2016-01-13 04:11:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:08:54 +00:00
|
|
|
v8::Local<v8::Promise> ExecuteJavaScriptInIsolatedWorld(
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Arguments* args,
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> window,
|
2017-12-19 01:42:36 +00:00
|
|
|
int world_id,
|
2019-10-31 07:56:00 +00:00
|
|
|
const std::vector<gin_helper::Dictionary>& scripts) {
|
2019-03-14 19:08:54 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
2019-03-14 19:08:54 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
promise.RejectWithErrorMessage(
|
|
|
|
"Render frame was torn down before "
|
|
|
|
"webFrame.executeJavaScriptInIsolatedWorld could be executed");
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-03-03 00:11:40 +00:00
|
|
|
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);
|
|
|
|
|
2017-08-30 21:31:21 +00:00
|
|
|
std::vector<blink::WebScriptSource> sources;
|
2017-11-15 10:14:41 +00:00
|
|
|
|
|
|
|
for (const auto& script : scripts) {
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string code;
|
|
|
|
std::u16string url;
|
2017-11-15 10:14:41 +00:00
|
|
|
int start_line = 1;
|
|
|
|
script.Get("url", &url);
|
|
|
|
script.Get("startLine", &start_line);
|
|
|
|
|
|
|
|
if (!script.Get("code", &code)) {
|
2020-03-03 00:11:40 +00:00
|
|
|
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);
|
2019-03-14 19:08:54 +00:00
|
|
|
return handle;
|
2017-11-15 10:14:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
sources.emplace_back(
|
|
|
|
blink::WebScriptSource(blink::WebString::FromUTF16(code),
|
|
|
|
blink::WebURL(GURL(url)), start_line));
|
2017-11-15 10:14:41 +00:00
|
|
|
}
|
2017-08-30 21:31:21 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
auto& prefs = render_frame->GetBlinkPreferences();
|
2020-07-23 21:32:20 +00:00
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
render_frame->GetWebFrame()->RequestExecuteScriptInIsolatedWorld(
|
2017-08-30 21:31:21 +00:00
|
|
|
world_id, &sources.front(), sources.size(), has_user_gesture,
|
2020-03-03 00:11:40 +00:00
|
|
|
scriptExecutionType,
|
2020-11-10 17:06:03 +00:00
|
|
|
new ScriptExecutionCallback(std::move(promise),
|
|
|
|
prefs.world_safe_execute_javascript,
|
2020-03-03 00:11:40 +00:00
|
|
|
std::move(completion_callback)));
|
2019-03-14 19:08:54 +00:00
|
|
|
|
|
|
|
return handle;
|
2017-08-30 21:31:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 18:05:28 +00:00
|
|
|
void SetIsolatedWorldInfo(v8::Local<v8::Value> window,
|
|
|
|
int world_id,
|
2019-10-31 07:56:00 +00:00
|
|
|
const gin_helper::Dictionary& options,
|
|
|
|
gin_helper::Arguments* args) {
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
args->ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.setIsolatedWorldInfo could "
|
|
|
|
"be executed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:45:15 +00:00
|
|
|
std::string origin_url, security_policy, name;
|
|
|
|
options.Get("securityOrigin", &origin_url);
|
|
|
|
options.Get("csp", &security_policy);
|
2019-02-13 18:05:28 +00:00
|
|
|
options.Get("name", &name);
|
|
|
|
|
2019-01-21 20:45:15 +00:00
|
|
|
if (!security_policy.empty() && origin_url.empty()) {
|
2019-02-13 18:05:28 +00:00
|
|
|
args->ThrowError(
|
|
|
|
"If csp is specified, securityOrigin should also be specified");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:45:15 +00:00
|
|
|
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);
|
2020-07-22 05:34:34 +00:00
|
|
|
blink::SetIsolatedWorldInfo(world_id, info);
|
2019-02-13 18:05:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 19:57:34 +00:00
|
|
|
blink::WebCacheResourceTypeStats GetResourceUsage(v8::Isolate* isolate) {
|
|
|
|
blink::WebCacheResourceTypeStats stats;
|
2017-06-16 20:42:33 +00:00
|
|
|
blink::WebCache::GetResourceTypeStats(&stats);
|
2016-05-13 17:43:08 +00:00
|
|
|
return stats;
|
2016-05-12 20:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:18:16 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
|
2020-10-19 11:48:16 +00:00
|
|
|
bool IsWordMisspelled(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
const std::string& word) {
|
|
|
|
return !SpellCheckWord(isolate, window, word, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::vector<std::u16string> GetWordSuggestions(v8::Isolate* isolate,
|
2020-10-19 11:48:16 +00:00
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
const std::string& word) {
|
2021-03-16 16:18:45 +00:00
|
|
|
std::vector<std::u16string> suggestions;
|
2020-10-19 11:48:16 +00:00
|
|
|
SpellCheckWord(isolate, window, word, &suggestions);
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:18:16 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
void ClearCache(v8::Isolate* isolate) {
|
2016-05-12 21:36:49 +00:00
|
|
|
isolate->IdleNotificationDeadline(0.5);
|
2017-06-16 20:42:33 +00:00
|
|
|
blink::WebCache::Clear();
|
2016-05-12 21:36:49 +00:00
|
|
|
base::MemoryPressureListener::NotifyMemoryPressure(
|
2018-04-18 01:55:30 +00:00
|
|
|
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
|
2016-05-12 21:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> FindFrameByRoutingId(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
int routing_id) {
|
|
|
|
content::RenderFrame* render_frame =
|
|
|
|
content::RenderFrame::FromRoutingID(routing_id);
|
|
|
|
if (render_frame)
|
|
|
|
return render_frame->GetWebFrame()->MainWorldScriptContext()->Global();
|
2018-01-09 19:41:40 +00:00
|
|
|
else
|
2019-02-08 21:38:31 +00:00
|
|
|
return v8::Null(isolate);
|
2018-01-09 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> GetOpener(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window) {
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame)
|
|
|
|
return v8::Null(isolate);
|
|
|
|
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->Opener();
|
2018-01-09 19:41:40 +00:00
|
|
|
if (frame && frame->IsWebLocalFrame())
|
2019-02-08 21:38:31 +00:00
|
|
|
return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global();
|
2018-01-09 19:41:40 +00:00
|
|
|
else
|
2019-02-08 21:38:31 +00:00
|
|
|
return v8::Null(isolate);
|
2018-01-09 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
// Don't name it as GetParent, Windows has API with same name.
|
|
|
|
v8::Local<v8::Value> GetFrameParent(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window) {
|
|
|
|
blink::WebFrame* frame = GetRenderFrame(window)->GetWebFrame()->Parent();
|
2018-01-09 19:41:40 +00:00
|
|
|
if (frame && frame->IsWebLocalFrame())
|
2019-02-08 21:38:31 +00:00
|
|
|
return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global();
|
2018-01-09 19:41:40 +00:00
|
|
|
else
|
2019-02-08 21:38:31 +00:00
|
|
|
return v8::Null(isolate);
|
2018-01-09 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> GetTop(v8::Isolate* isolate, v8::Local<v8::Value> window) {
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame)
|
|
|
|
return v8::Null(isolate);
|
|
|
|
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->Top();
|
2018-01-09 19:41:40 +00:00
|
|
|
if (frame && frame->IsWebLocalFrame())
|
2019-02-08 21:38:31 +00:00
|
|
|
return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global();
|
2018-01-09 19:41:40 +00:00
|
|
|
else
|
2019-02-08 21:38:31 +00:00
|
|
|
return v8::Null(isolate);
|
2018-01-09 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> GetFirstChild(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window) {
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame)
|
|
|
|
return v8::Null(isolate);
|
|
|
|
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->FirstChild();
|
2018-01-09 19:41:40 +00:00
|
|
|
if (frame && frame->IsWebLocalFrame())
|
2019-02-08 21:38:31 +00:00
|
|
|
return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global();
|
2018-01-09 19:41:40 +00:00
|
|
|
else
|
2019-02-08 21:38:31 +00:00
|
|
|
return v8::Null(isolate);
|
2018-01-09 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> GetNextSibling(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window) {
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame)
|
|
|
|
return v8::Null(isolate);
|
|
|
|
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->NextSibling();
|
2019-02-08 21:38:31 +00:00
|
|
|
if (frame && frame->IsWebLocalFrame())
|
|
|
|
return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global();
|
2018-01-09 19:41:40 +00:00
|
|
|
else
|
2019-02-08 21:38:31 +00:00
|
|
|
return v8::Null(isolate);
|
2018-01-09 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> GetFrameForSelector(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
const std::string& selector) {
|
|
|
|
blink::WebElement element =
|
|
|
|
GetRenderFrame(window)->GetWebFrame()->GetDocument().QuerySelector(
|
|
|
|
blink::WebString::FromUTF8(selector));
|
|
|
|
if (element.IsNull()) // not found
|
|
|
|
return v8::Null(isolate);
|
2018-01-09 19:41:40 +00:00
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
blink::WebFrame* frame = blink::WebFrame::FromFrameOwnerElement(element);
|
|
|
|
if (frame && frame->IsWebLocalFrame())
|
|
|
|
return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global();
|
2018-04-26 10:17:55 +00:00
|
|
|
else
|
2019-02-08 21:38:31 +00:00
|
|
|
return v8::Null(isolate);
|
2018-04-26 10:17:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
v8::Local<v8::Value> FindFrameByName(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> window,
|
|
|
|
const std::string& name) {
|
2020-04-03 02:59:19 +00:00
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame)
|
|
|
|
return v8::Null(isolate);
|
|
|
|
|
|
|
|
blink::WebFrame* frame = render_frame->GetWebFrame()->FindFrameByName(
|
|
|
|
blink::WebString::FromUTF8(name));
|
2019-02-08 21:38:31 +00:00
|
|
|
if (frame && frame->IsWebLocalFrame())
|
|
|
|
return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global();
|
|
|
|
else
|
|
|
|
return v8::Null(isolate);
|
2018-04-26 10:17:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 02:59:19 +00:00
|
|
|
int GetRoutingId(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> window) {
|
|
|
|
auto* render_frame = GetRenderFrame(window);
|
|
|
|
if (!render_frame) {
|
|
|
|
thrower.ThrowError(
|
|
|
|
"Render frame was torn down before webFrame.getRoutingId could be "
|
|
|
|
"executed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return render_frame->GetRoutingID();
|
2014-10-24 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2014-10-24 10:24:12 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2019-06-19 21:23:04 +00:00
|
|
|
using namespace electron::api; // NOLINT(build/namespaces)
|
2019-02-08 21:38:31 +00:00
|
|
|
|
2014-10-24 10:24:12 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2019-02-08 21:38:31 +00:00
|
|
|
dict.SetMethod("setName", &SetName);
|
|
|
|
dict.SetMethod("setZoomLevel", &SetZoomLevel);
|
|
|
|
dict.SetMethod("getZoomLevel", &GetZoomLevel);
|
|
|
|
dict.SetMethod("setZoomFactor", &SetZoomFactor);
|
|
|
|
dict.SetMethod("getZoomFactor", &GetZoomFactor);
|
|
|
|
dict.SetMethod("setVisualZoomLevelLimits", &SetVisualZoomLevelLimits);
|
|
|
|
dict.SetMethod("allowGuestViewElementDefinition",
|
|
|
|
&AllowGuestViewElementDefinition);
|
|
|
|
dict.SetMethod("getWebFrameId", &GetWebFrameId);
|
2020-11-10 17:06:03 +00:00
|
|
|
dict.SetMethod("getWebPreference", &GetWebPreference);
|
2019-02-08 21:38:31 +00:00
|
|
|
dict.SetMethod("setSpellCheckProvider", &SetSpellCheckProvider);
|
|
|
|
dict.SetMethod("insertText", &InsertText);
|
|
|
|
dict.SetMethod("insertCSS", &InsertCSS);
|
2019-06-17 15:39:36 +00:00
|
|
|
dict.SetMethod("removeInsertedCSS", &RemoveInsertedCSS);
|
2019-02-08 21:38:31 +00:00
|
|
|
dict.SetMethod("executeJavaScript", &ExecuteJavaScript);
|
|
|
|
dict.SetMethod("executeJavaScriptInIsolatedWorld",
|
|
|
|
&ExecuteJavaScriptInIsolatedWorld);
|
2019-02-13 18:05:28 +00:00
|
|
|
dict.SetMethod("setIsolatedWorldInfo", &SetIsolatedWorldInfo);
|
2019-02-08 21:38:31 +00:00
|
|
|
dict.SetMethod("getResourceUsage", &GetResourceUsage);
|
2020-10-19 11:48:16 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
dict.SetMethod("isWordMisspelled", &IsWordMisspelled);
|
|
|
|
dict.SetMethod("getWordSuggestions", &GetWordSuggestions);
|
|
|
|
#endif
|
2019-02-08 21:38:31 +00:00
|
|
|
dict.SetMethod("clearCache", &ClearCache);
|
|
|
|
dict.SetMethod("_findFrameByRoutingId", &FindFrameByRoutingId);
|
|
|
|
dict.SetMethod("_getFrameForSelector", &GetFrameForSelector);
|
|
|
|
dict.SetMethod("_findFrameByName", &FindFrameByName);
|
|
|
|
dict.SetMethod("_getOpener", &GetOpener);
|
|
|
|
dict.SetMethod("_getParent", &GetFrameParent);
|
|
|
|
dict.SetMethod("_getTop", &GetTop);
|
|
|
|
dict.SetMethod("_getFirstChild", &GetFirstChild);
|
|
|
|
dict.SetMethod("_getNextSibling", &GetNextSibling);
|
|
|
|
dict.SetMethod("_getRoutingId", &GetRoutingId);
|
2014-10-24 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_renderer_web_frame, Initialize)
|