Browse Source

Move global preload implementation to be session based

Samuel Attard 7 years ago
parent
commit
3b80ee0655

+ 15 - 0
atom/browser/api/atom_api_session.cc

@@ -680,6 +680,18 @@ void Session::CreateInterruptedDownload(const mate::Dictionary& options) {
       length, last_modified, etag, base::Time::FromDoubleT(start_time)));
 }
 
+void Session::AddPreload(const base::FilePath::StringType& preloadPath) {
+  g_preloads.push_back(preloadPath);
+}
+void Session::RemovePreload(const base::FilePath::StringType& preloadPath) {
+  g_preloads.erase(
+    std::remove(g_preloads.begin(), g_preloads.end(), preloadPath),
+    g_preloads.end());
+}
+std::vector<base::FilePath::StringType> Session::GetPreloads() {
+  return g_preloads;
+}
+
 v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
   if (cookies_.IsEmpty()) {
     auto handle = Cookies::Create(isolate, browser_context());
@@ -766,6 +778,9 @@ void Session::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("getBlobData", &Session::GetBlobData)
       .SetMethod("createInterruptedDownload",
                  &Session::CreateInterruptedDownload)
+      .SetMethod("addPreload", &Session::AddPreload)
+      .SetMethod("removePreload", &Session::RemovePreload)
+      .SetMethod("getPreloads", &Session::GetPreloads)
       .SetProperty("cookies", &Session::Cookies)
       .SetProperty("protocol", &Session::Protocol)
       .SetProperty("webRequest", &Session::WebRequest);

+ 4 - 0
atom/browser/api/atom_api_session.h

@@ -81,6 +81,9 @@ class Session: public mate::TrackableObject<Session>,
   void GetBlobData(const std::string& uuid,
                    const AtomBlobReader::CompletionCallback& callback);
   void CreateInterruptedDownload(const mate::Dictionary& options);
+  void AddPreload(const base::FilePath::StringType& preloadPath);
+  void RemovePreload(const base::FilePath::StringType& preloadPath);
+  std::vector<base::FilePath::StringType> GetPreloads();
   v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
   v8::Local<v8::Value> Protocol(v8::Isolate* isolate);
   v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
@@ -103,6 +106,7 @@ class Session: public mate::TrackableObject<Session>,
   std::string devtools_network_emulation_client_id_;
 
   scoped_refptr<AtomBrowserContext> browser_context_;
+  std::vector<base::FilePath::StringType> g_preloads;
 
   DISALLOW_COPY_AND_ASSIGN(Session);
 };

+ 1 - 1
atom/browser/api/atom_api_web_contents.h

@@ -230,6 +230,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
   v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
 
   WebContentsZoomController* GetZoomController() { return zoom_controller_; }
+  AtomBrowserContext* GetBrowserContext() const;
 
  protected:
   WebContents(v8::Isolate* isolate,
@@ -367,7 +368,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
                          const std::vector<base::string16>& labels);
 
  private:
-  AtomBrowserContext* GetBrowserContext() const;
 
   uint32_t GetNextRequestId() {
     return ++request_id_;

+ 0 - 22
atom/browser/api/atom_api_window.cc

@@ -4,8 +4,6 @@
 
 #include "atom/browser/api/atom_api_window.h"
 
-#include <algorithm>
-
 #include "atom/browser/api/atom_api_browser_view.h"
 #include "atom/browser/api/atom_api_menu.h"
 #include "atom/browser/api/atom_api_web_contents.h"
@@ -18,7 +16,6 @@
 #include "atom/common/native_mate_converters/gurl_converter.h"
 #include "atom/common/native_mate_converters/image_converter.h"
 #include "atom/common/native_mate_converters/string16_converter.h"
-#include "atom/common/native_mate_converters/value_converter.h"
 #include "atom/common/options_switches.h"
 #include "base/command_line.h"
 #include "base/threading/thread_task_runner_handle.h"
@@ -176,19 +173,6 @@ Window::~Window() {
   base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, window_.release());
 }
 
-std::vector<base::FilePath::StringType> g_preloads;
-void Window::AddGlobalPreload(const base::FilePath::StringType& preloadPath) {
-  g_preloads.push_back(preloadPath);
-}
-void Window::RemoveGlobalPreload(const base::FilePath::StringType& preloadPath) {
-  g_preloads.erase(
-    std::remove(g_preloads.begin(), g_preloads.end(), preloadPath),
-    g_preloads.end());
-}
-std::vector<base::FilePath::StringType> Window::GetGlobalPreloads() {
-  return g_preloads;
-}
-
 void Window::WillCloseWindow(bool* prevent_default) {
   *prevent_default = Emit("close");
 }
@@ -1167,12 +1151,6 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
                            &mate::TrackableObject<Window>::FromWeakMapID);
   browser_window.SetMethod("getAllWindows",
                            &mate::TrackableObject<Window>::GetAll);
-  browser_window.SetMethod("addGlobalPreload",
-                           &Window::AddGlobalPreload);
-  browser_window.SetMethod("removeGlobalPreload",
-                           &Window::RemoveGlobalPreload);
-  browser_window.SetMethod("getGlobalPreloads",
-                           &Window::GetGlobalPreloads);
 
   mate::Dictionary dict(isolate, exports);
   dict.Set("BrowserWindow", browser_window);

+ 0 - 4
atom/browser/api/atom_api_window.h

@@ -54,10 +54,6 @@ class Window : public mate::TrackableObject<Window>,
 
   int32_t ID() const;
 
-  static void AddGlobalPreload(const base::FilePath::StringType& preloadPath);
-  static void RemoveGlobalPreload(const base::FilePath::StringType& preloadPath);
-  static std::vector<base::FilePath::StringType> GetGlobalPreloads();
-
  protected:
   Window(v8::Isolate* isolate, v8::Local<v8::Object> wrapper,
          const mate::Dictionary& options);

+ 5 - 1
atom/browser/web_contents_preferences.cc

@@ -8,6 +8,8 @@
 #include <string>
 #include <vector>
 
+#include "atom/browser/api/atom_api_session.h"
+#include "atom/browser/api/atom_api_web_contents.h"
 #include "atom/browser/api/atom_api_window.h"
 #include "atom/browser/native_window.h"
 #include "atom/browser/web_view_manager.h"
@@ -137,7 +139,9 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
       LOG(ERROR) << "preload url must be file:// protocol.";
   }
 
-  for (auto preloadPath : atom::api::Window::GetGlobalPreloads()) {
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  mate::Handle<atom::api::WebContents> api_web_contents = atom::api::WebContents::CreateFrom(isolate, web_contents);
+  for (auto preloadPath : atom::api::Session::CreateFrom(isolate, api_web_contents.get()->GetBrowserContext())->GetPreloads()) {
     if (base::FilePath(preloadPath).IsAbsolute())
       command_line->AppendSwitchNative(switches::kGlobalPreloadScript,
                                        preloadPath);

+ 0 - 19
docs/api/browser-window.md

@@ -662,25 +662,6 @@ console.log(installed)
 **Note:** This API cannot be called before the `ready` event of the `app` module
 is emitted.
 
-#### `BrowserWindow.addGlobalPreload(preloadPath)`
-
-* `preloadPath` String - An absolute path to the preload script
-
-Adds a script that will be executed on ALL new BrowserWindows just before normal `preload` scripts run.
-
-#### `BrowserWindow.removeGlobalPreload(preloadPath)`
-
-* `preloadPath` String - An absolute path to the preload script
-
-Removes the given script from the list of global preload scripts
-
-#### `BrowserWindow.getGlobalPreloads()`
-
-Returns `String[]` an array of paths to preload scripts that have been registered
-
-Adds a script that will be executed on ALL new BrowserWindows just before normal `preload` scripts run.
-
-
 ### Instance Properties
 
 Objects created with `new BrowserWindow` have the following properties:

+ 17 - 0
docs/api/session.md

@@ -384,6 +384,23 @@ the initial state will be `interrupted`. The download will start only when the
 
 Clears the session’s HTTP authentication cache.
 
+#### `ses.addPreload(preloadPath)`
+
+* `preloadPath` String - An absolute path to the preload script
+
+Adds a script that will be executed on ALL web contents that are associated with
+this session just before normal `preload` scripts run.
+
+#### `ses.removePreload(preloadPath)`
+
+* `preloadPath` String - An absolute path to the preload script
+
+Removes the given script from the list of preload scripts
+
+#### `ses.getPreloads()`
+
+Returns `String[]` an array of paths to preload scripts that have been registered
+
 ### Instance Properties
 
 The following properties are available on instances of `Session`:

+ 16 - 14
spec/api-browser-window-spec.js

@@ -9,7 +9,7 @@ const http = require('http')
 const {closeWindow} = require('./window-helpers')
 
 const {ipcRenderer, remote, screen} = require('electron')
-const {app, ipcMain, BrowserWindow, BrowserView, protocol, webContents} = remote
+const {app, ipcMain, BrowserWindow, BrowserView, protocol, session, webContents} = remote
 
 const isCI = remote.getGlobal('isCi')
 const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
@@ -1021,31 +1021,33 @@ describe('BrowserWindow module', () => {
       })
     })
 
-    describe('global preload scripts', function () {
-      it('can add and remove multiple global preload script', function () {
+    describe.only('session preload scripts', function () {
+      it('can add and remove multiple session preload script', function () {
         var preload = path.join(fixtures, 'module', 'set-global.js')
         var preload2 = path.join(fixtures, 'module', 'set-global-2.js')
-        assert.deepEqual(BrowserWindow.getGlobalPreloads(), [])
-        BrowserWindow.addGlobalPreload(preload)
-        assert.deepEqual(BrowserWindow.getGlobalPreloads(), [preload])
-        BrowserWindow.addGlobalPreload(preload2)
-        assert.deepEqual(BrowserWindow.getGlobalPreloads(), [preload, preload2])
-        BrowserWindow.removeGlobalPreload(preload)
-        assert.deepEqual(BrowserWindow.getGlobalPreloads(), [preload2])
-        BrowserWindow.removeGlobalPreload(preload2)
-        assert.deepEqual(BrowserWindow.getGlobalPreloads(), [])
+        const mSession = session.defaultSession;
+        assert.deepEqual(mSession.getPreloads(), [])
+        mSession.addPreload(preload)
+        assert.deepEqual(mSession.getPreloads(), [preload])
+        mSession.addPreload(preload2)
+        assert.deepEqual(mSession.getPreloads(), [preload, preload2])
+        mSession.removePreload(preload)
+        assert.deepEqual(mSession.getPreloads(), [preload2])
+        mSession.removePreload(preload2)
+        assert.deepEqual(mSession.getPreloads(), [])
       })
 
       it('loads the script before other scripts in window including normal preloads', function (done) {
         var preload = path.join(fixtures, 'module', 'set-global.js')
         var preload2 = path.join(fixtures, 'module', 'set-global-2.js')
+        const mSession = session.defaultSession;
         ipcMain.once('answer', function (event, test) {
-          BrowserWindow.removeGlobalPreload(preload2)
+          mSession.removePreload(preload2)
           assert.equal(test, 'preload2')
           done()
         })
         w.destroy()
-        BrowserWindow.addGlobalPreload(preload2)
+        mSession.addPreload(preload2)
         w = new BrowserWindow({
           show: false,
           webPreferences: {