From 936d8c11177a78a58d7c10e41aeafa6d722ccb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Lach=C3=A8ze?= Date: Wed, 30 Aug 2017 23:31:21 +0200 Subject: [PATCH] Add webframe. executeJavaScriptInIsolatedWorld Attempt runInIsolatedWorldContext Replace RunInIsolatedWorldContext by GetIsolatedWorldGlobalObject Fix linting Remove useless getIsolatedWorldGlobalObject Add support for scriptExecutionType --- atom/renderer/api/atom_api_web_frame.cc | 50 +++++++++++++++++++++++++ atom/renderer/api/atom_api_web_frame.h | 4 ++ 2 files changed, 54 insertions(+) diff --git a/atom/renderer/api/atom_api_web_frame.cc b/atom/renderer/api/atom_api_web_frame.cc index fbda47b29760..3e88dd72ea15 100644 --- a/atom/renderer/api/atom_api_web_frame.cc +++ b/atom/renderer/api/atom_api_web_frame.cc @@ -28,6 +28,31 @@ #include "atom/common/node_includes.h" +namespace mate { + +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Handle 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; + } +}; +} // namespace mate + namespace atom { namespace api { @@ -226,6 +251,29 @@ void WebFrame::ExecuteJavaScript(const base::string16& code, callback.release()); } +void WebFrame::ExecuteJavaScriptInIsolatedWorld(int world_id, + const base::string16& code, + mate::Arguments* args) { + std::vector sources; + sources.push_back(blink::WebScriptSource(blink::WebString::FromUTF16(code))); + + 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::unique_ptr callback( + new ScriptExecutionCallback(completion_callback)); + + web_frame_->RequestExecuteScriptInIsolatedWorld( + world_id, &sources.front(), sources.size(), has_user_gesture, + scriptExecutionType, callback.release()); +} + // static mate::Handle WebFrame::Create(v8::Isolate* isolate) { return mate::CreateHandle(isolate, new WebFrame(isolate)); @@ -275,6 +323,8 @@ void WebFrame::BuildPrototype( .SetMethod("insertText", &WebFrame::InsertText) .SetMethod("insertCSS", &WebFrame::InsertCSS) .SetMethod("executeJavaScript", &WebFrame::ExecuteJavaScript) + .SetMethod("executeJavaScriptInIsolatedWorld", + &WebFrame::ExecuteJavaScriptInIsolatedWorld) .SetMethod("getResourceUsage", &WebFrame::GetResourceUsage) .SetMethod("clearCache", &WebFrame::ClearCache) // TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings diff --git a/atom/renderer/api/atom_api_web_frame.h b/atom/renderer/api/atom_api_web_frame.h index 6c6d0b19f985..6578feebd282 100644 --- a/atom/renderer/api/atom_api_web_frame.h +++ b/atom/renderer/api/atom_api_web_frame.h @@ -7,6 +7,7 @@ #include #include +#include #include "atom/renderer/guest_view_container.h" #include "native_mate/handle.h" @@ -73,6 +74,9 @@ class WebFrame : public mate::Wrappable { // Excecuting scripts. void ExecuteJavaScript(const base::string16& code, mate::Arguments* args); + void ExecuteJavaScriptInIsolatedWorld(int world_id, + const base::string16& code, + mate::Arguments* args); // Resource related methods blink::WebCache::ResourceTypeStats GetResourceUsage(v8::Isolate* isolate);