Handle executeJavaScript in JavaScript
authorCheng Zhao <zcbenz@gmail.com>
Wed, 13 Jan 2016 04:11:46 +0000 (12:11 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 13 Jan 2016 04:11:46 +0000 (12:11 +0800)
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/api/lib/web-contents.js
atom/common/api/api_messages.h
atom/renderer/api/atom_api_web_frame.cc
atom/renderer/api/atom_api_web_frame.h
atom/renderer/atom_render_view_observer.cc
atom/renderer/atom_render_view_observer.h

index 4d449ed..614148f 100644 (file)
@@ -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)
index bd7149e..59e16dc 100644 (file)
@@ -74,8 +74,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
   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();
index d0abae7..4055901 100644 (file)
@@ -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;
index 274e1f5..2c9bb3b 100644 (file)
@@ -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<atom::DraggableRegion> /* regions */)
index 276f833..fe11253 100644 (file)
@@ -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<blink::WebScopedUserGesture> 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
index 42175e1..632f6a5 100644 (file)
@@ -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);
index bf2e1a7..cdbdb3d 100644 (file)
@@ -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<blink::WebScopedUserGesture> 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
index 85a8c15..4b9d59f 100644 (file)
@@ -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_;