From 55dfddba774963fde915cbf427d4ee577b1adf61 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 13 Jan 2016 12:11:46 +0800 Subject: [PATCH] Handle executeJavaScript in JavaScript --- atom/browser/api/atom_api_web_contents.cc | 6 ----- atom/browser/api/atom_api_web_contents.h | 2 -- atom/browser/api/lib/web-contents.js | 30 +++++++++++----------- atom/common/api/api_messages.h | 4 --- atom/renderer/api/atom_api_web_frame.cc | 11 +++++++- atom/renderer/api/atom_api_web_frame.h | 3 +++ atom/renderer/atom_render_view_observer.cc | 22 ---------------- atom/renderer/atom_render_view_observer.h | 2 -- 8 files changed, 28 insertions(+), 52 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 4d449ed9c39b..614148f5067e 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -756,11 +756,6 @@ bool WebContents::SavePage(const base::FilePath& full_file_path, return handler->Handle(full_file_path, save_type); } -void WebContents::ExecuteJavaScript(const base::string16& code, - bool has_user_gesture) { - Send(new AtomViewMsg_ExecuteJavaScript(routing_id(), code, has_user_gesture)); -} - void WebContents::OpenDevTools(mate::Arguments* args) { if (type_ == REMOTE) return; @@ -1093,7 +1088,6 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("getUserAgent", &WebContents::GetUserAgent) .SetMethod("insertCSS", &WebContents::InsertCSS) .SetMethod("savePage", &WebContents::SavePage) - .SetMethod("_executeJavaScript", &WebContents::ExecuteJavaScript) .SetMethod("openDevTools", &WebContents::OpenDevTools) .SetMethod("closeDevTools", &WebContents::CloseDevTools) .SetMethod("isDevToolsOpened", &WebContents::IsDevToolsOpened) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index bd7149e38a93..59e16dcab85f 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -74,8 +74,6 @@ class WebContents : public mate::TrackableObject, bool SavePage(const base::FilePath& full_file_path, const content::SavePageType& save_type, const SavePageHandler::SavePageCallback& callback); - void ExecuteJavaScript(const base::string16& code, - bool has_user_gesture); void OpenDevTools(mate::Arguments* args); void CloseDevTools(); bool IsDevToolsOpened(); diff --git a/atom/browser/api/lib/web-contents.js b/atom/browser/api/lib/web-contents.js index d0abae7e65c4..4055901a3f18 100644 --- a/atom/browser/api/lib/web-contents.js +++ b/atom/browser/api/lib/web-contents.js @@ -57,6 +57,7 @@ PDFPageSize = { // Following methods are mapped to webFrame. const webFrameMethods = [ + 'executeJavaScript', 'insertText', ]; @@ -73,21 +74,6 @@ wrapWebContents = function(webContents) { return this._send(channel, slice.call(args)); }; - /* - Make sure webContents.executeJavaScript would run the code only when the - web contents has been loaded. - */ - webContents.executeJavaScript = function(code, hasUserGesture) { - if (hasUserGesture == null) { - hasUserGesture = false; - } - if (this.getURL() && !this.isLoading()) { - return this._executeJavaScript(code, hasUserGesture); - } else { - return webContents.once('did-finish-load', this._executeJavaScript.bind(this, code, hasUserGesture)); - } - }; - /* The navigation controller. */ controller = new NavigationController(webContents); ref1 = NavigationController.prototype; @@ -110,6 +96,20 @@ wrapWebContents = function(webContents) { }; } + // Make sure webContents.executeJavaScript would run the code only when the + // webContents has been loaded. + const executeJavaScript = webContents.executeJavaScript; + webContents.executeJavaScript = function(code, hasUserGesture) { + // TODO(zcbenz): Use default parameter after Chrome 49. + if (hasUserGesture === undefined) + hasUserGesture = false; + + if (this.getURL() && !this.isLoading()) + return executeJavaScript.call(this, code, hasUserGesture); + else + return this.once('did-finish-load', executeJavaScript.bind(this, code, hasUserGesture)); + }; + /* Dispatch IPC messages to the ipc module. */ webContents.on('ipc-message', function(event, packed) { var args, channel; diff --git a/atom/common/api/api_messages.h b/atom/common/api/api_messages.h index 274e1f533eb3..2c9bb3b2f3e4 100644 --- a/atom/common/api/api_messages.h +++ b/atom/common/api/api_messages.h @@ -40,10 +40,6 @@ IPC_MESSAGE_ROUTED2(AtomViewMsg_Message, base::string16 /* channel */, base::ListValue /* arguments */) -IPC_MESSAGE_ROUTED2(AtomViewMsg_ExecuteJavaScript, - base::string16 /* code */, - bool /* has user gesture */) - // Sent by the renderer when the draggable regions are updated. IPC_MESSAGE_ROUTED1(AtomViewHostMsg_UpdateDraggableRegions, std::vector /* regions */) diff --git a/atom/renderer/api/atom_api_web_frame.cc b/atom/renderer/api/atom_api_web_frame.cc index 276f833c5a0c..fe11253bb56b 100644 --- a/atom/renderer/api/atom_api_web_frame.cc +++ b/atom/renderer/api/atom_api_web_frame.cc @@ -15,6 +15,8 @@ #include "native_mate/object_template_builder.h" #include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebLocalFrame.h" +#include "third_party/WebKit/public/web/WebScopedUserGesture.h" +#include "third_party/WebKit/public/web/WebScriptSource.h" #include "third_party/WebKit/public/web/WebSecurityPolicy.h" #include "third_party/WebKit/public/web/WebView.h" @@ -120,6 +122,12 @@ void WebFrame::InsertText(const std::string& text) { web_frame_->insertText(blink::WebString::fromUTF8(text)); } +void WebFrame::ExecuteJavaScript(const base::string16& code, bool by_user) { + scoped_ptr gesture( + by_user ? new blink::WebScopedUserGesture : nullptr); + web_frame_->executeScriptAndReturnValue(blink::WebScriptSource(code)); +} + mate::ObjectTemplateBuilder WebFrame::GetObjectTemplateBuilder( v8::Isolate* isolate) { return mate::ObjectTemplateBuilder(isolate) @@ -141,7 +149,8 @@ mate::ObjectTemplateBuilder WebFrame::GetObjectTemplateBuilder( &WebFrame::RegisterURLSchemeAsBypassingCSP) .SetMethod("registerURLSchemeAsPrivileged", &WebFrame::RegisterURLSchemeAsPrivileged) - .SetMethod("insertText", &WebFrame::InsertText); + .SetMethod("insertText", &WebFrame::InsertText) + .SetMethod("executeJavaScript", &WebFrame::ExecuteJavaScript); } // static diff --git a/atom/renderer/api/atom_api_web_frame.h b/atom/renderer/api/atom_api_web_frame.h index 42175e12db35..632f6a517392 100644 --- a/atom/renderer/api/atom_api_web_frame.h +++ b/atom/renderer/api/atom_api_web_frame.h @@ -63,6 +63,9 @@ class WebFrame : public mate::Wrappable { // Editing. void InsertText(const std::string& text); + // Excecuting scripts. + void ExecuteJavaScript(const base::string16& code, bool by_user); + // mate::Wrappable: virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate); diff --git a/atom/renderer/atom_render_view_observer.cc b/atom/renderer/atom_render_view_observer.cc index bf2e1a70b788..cdbdb3d7c3ce 100644 --- a/atom/renderer/atom_render_view_observer.cc +++ b/atom/renderer/atom_render_view_observer.cc @@ -27,8 +27,6 @@ #include "third_party/WebKit/public/web/WebFrame.h" #include "third_party/WebKit/public/web/WebLocalFrame.h" #include "third_party/WebKit/public/web/WebKit.h" -#include "third_party/WebKit/public/web/WebScopedUserGesture.h" -#include "third_party/WebKit/public/web/WebScriptSource.h" #include "third_party/WebKit/public/web/WebView.h" #include "ui/base/resource/resource_bundle.h" #include "native_mate/dictionary.h" @@ -115,8 +113,6 @@ bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) { bool handled = true; IPC_BEGIN_MESSAGE_MAP(AtomRenderViewObserver, message) IPC_MESSAGE_HANDLER(AtomViewMsg_Message, OnBrowserMessage) - IPC_MESSAGE_HANDLER(AtomViewMsg_ExecuteJavaScript, - OnJavaScriptExecuteRequest) IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP() @@ -152,22 +148,4 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel, } } -void AtomRenderViewObserver::OnJavaScriptExecuteRequest( - const base::string16& code, bool has_user_gesture) { - if (!document_created_) - return; - - if (!render_view()->GetWebView()) - return; - - scoped_ptr gesture( - has_user_gesture ? new blink::WebScopedUserGesture : nullptr); - - v8::Isolate* isolate = blink::mainThreadIsolate(); - v8::HandleScope handle_scope(isolate); - - blink::WebFrame* frame = render_view()->GetWebView()->mainFrame(); - frame->executeScriptAndReturnValue(blink::WebScriptSource(code)); -} - } // namespace atom diff --git a/atom/renderer/atom_render_view_observer.h b/atom/renderer/atom_render_view_observer.h index 85a8c159d97e..4b9d59f3fa08 100644 --- a/atom/renderer/atom_render_view_observer.h +++ b/atom/renderer/atom_render_view_observer.h @@ -32,8 +32,6 @@ class AtomRenderViewObserver : public content::RenderViewObserver { void OnBrowserMessage(const base::string16& channel, const base::ListValue& args); - void OnJavaScriptExecuteRequest(const base::string16& code, - bool has_user_gesture); // Weak reference to renderer client. AtomRendererClient* renderer_client_;