]> khamidou — GitHub mirrors - electron.git/commitdiff
Load the "preload" script in <webview>
authorCheng Zhao <zcbenz@gmail.com>
Thu, 6 Nov 2014 07:13:37 +0000 (15:13 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 6 Nov 2014 07:13:37 +0000 (15:13 +0800)
atom/browser/atom_browser_client.cc
atom/browser/lib/guest-view-manager.coffee
atom/browser/web_view/web_view_manager.cc
atom/browser/web_view/web_view_manager.h
atom/browser/web_view/web_view_renderer_state.h
atom/common/options_switches.cc
atom/common/options_switches.h
atom/renderer/lib/init.coffee

index 0ae8bfed6742869a820609cf91a6181a69e4e247..c39acc06ca2e1455fc1036c3905e78caa4288d0b 100644 (file)
@@ -156,6 +156,10 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
           info.node_integration ? "true" : "false");
       if (info.plugins)
         command_line->AppendSwitch(switches::kEnablePlugins);
+      if (!info.preload_script.empty())
+        command_line->AppendSwitchPath(
+            switches::kPreloadScript,
+            info.preload_script);
     }
   }
 
index bec53ef30f2c3fb10ac2eef077fe052fd9305a1f..a5c5d021f401d26f4cf1545315bbf76cab0383c9 100644 (file)
@@ -33,7 +33,8 @@ createGuest = (embedder, params) ->
     guestInstanceId: id
     storagePartitionId: params.storagePartitionId
   guestInstances[id] = {guest, embedder}
-  webViewManager.addGuest id, embedder, guest, params.nodeIntegration, params.plugins
+  preload = params.preload ? ''
+  webViewManager.addGuest id, embedder, guest, params.nodeIntegration, params.plugins, preload
 
   # Destroy guest when the embedder is gone.
   embedder.once 'render-view-deleted', ->
index 88a1bdb2f7b2aaaaea68ea3a7f803b903eb7d74c..5dd427c6a796d5273a38e90f561d7bcaa9a49e0f 100644 (file)
@@ -7,12 +7,14 @@
 #include "atom/browser/api/atom_api_web_contents.h"
 #include "atom/browser/atom_browser_context.h"
 #include "atom/browser/web_view/web_view_renderer_state.h"
+#include "atom/common/native_mate_converters/gurl_converter.h"
 #include "base/bind.h"
 #include "base/stl_util.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/render_process_host.h"
 #include "native_mate/dictionary.h"
 #include "native_mate/object_template_builder.h"
+#include "net/base/filename_util.h"
 
 #include "atom/common/node_includes.h"
 
@@ -44,12 +46,14 @@ void WebViewManager::AddGuest(int guest_instance_id,
                               content::WebContents* embedder,
                               content::WebContents* web_contents,
                               bool node_integration,
-                              bool plugins) {
+                              bool plugins,
+                              const GURL& preload_url) {
   web_contents_map_[guest_instance_id] = { web_contents, embedder };
 
   WebViewRendererState::WebViewInfo web_view_info = {
     guest_instance_id, node_integration, plugins
   };
+  net::FileURLToFilePath(preload_url, &web_view_info.preload_script);
   content::BrowserThread::PostTask(
       content::BrowserThread::IO,
       FROM_HERE,
index f00e5a6607f0ec3dbfd3571b9f29b3c81b3c8688..e6874408956332708279fbff2ef9a4fff2dafcbb 100644 (file)
@@ -24,7 +24,8 @@ class WebViewManager : public content::BrowserPluginGuestManager {
                 content::WebContents* embedder,
                 content::WebContents* web_contents,
                 bool node_integration,
-                bool plugins);
+                bool plugins,
+                const GURL& preload_url);
   void RemoveGuest(int guest_instance_id);
 
  protected:
index 2de8308a9d4e5fd316f278b663bc4e70f4ee2ccc..bddf803704aa757cddddc2c7ab95ba8073fe513e 100644 (file)
@@ -9,6 +9,7 @@
 #include <string>
 #include <utility>
 
+#include "base/files/file_path.h"
 #include "base/memory/singleton.h"
 
 namespace atom {
@@ -23,6 +24,7 @@ class WebViewRendererState {
     int guest_instance_id;
     bool node_integration;
     bool plugins;
+    base::FilePath preload_script;
   };
 
   static WebViewRendererState* GetInstance();
index 3a61028c6b53fbd47067fa9e99d78bf1b3ff9cca..e6e65f204bbdbdc0d1dae213c9e582f65af16d54 100644 (file)
@@ -66,6 +66,9 @@ const char kEnablePlugins[] = "enable-plugins";
 // Instancd ID of guest WebContents.
 const char kGuestInstanceID[] = "guest-instance-id";
 
+// Script that will be loaded by guest WebContents before other scripts.
+const char kPreloadScript[] = "preload-script";
+
 // Web runtime features.
 const char kExperimentalFeatures[]       = "experimental-features";
 const char kExperimentalCanvasFeatures[] = "experimental-canvas-features";
index a0aeacc7703915d2df1fa89382445d15854e870a..a08983647ca1a1d340e14606fd4396d73394385e 100644 (file)
@@ -38,6 +38,7 @@ extern const char kDarkTheme[];
 extern const char kDirectWrite[];
 extern const char kEnablePlugins[];
 extern const char kGuestInstanceID[];
+extern const char kPreloadScript[];
 
 extern const char kExperimentalFeatures[];
 extern const char kExperimentalCanvasFeatures[];
index 011720e68b3b8b96de97723e58a02528e1f0b610..b5059f33f38b664d477d562865dbd73ac063faf9 100644 (file)
@@ -31,6 +31,8 @@ for arg in process.argv
     require('web-frame').setName 'ATOM_SHELL_GUEST_WEB_VIEW'
   else if arg.indexOf('--node-integration=') == 0
     nodeIntegration = arg.substr arg.indexOf('=') + 1
+  else if arg.indexOf('--preload-script=') == 0
+    preloadScript = arg.substr arg.indexOf('=') + 1
 
 if location.protocol is 'chrome-devtools:'
   # Override some inspector APIs.
@@ -86,3 +88,6 @@ else
     delete global.process
     delete global.setImmediate
     delete global.clearImmediate
+
+# Load the script specfied by the "preload" attribute.
+require preloadScript if preloadScript