From 3b80ee06552d24998cfceeb2a836e6e1308c00b8 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Sun, 17 Sep 2017 01:05:49 +1000 Subject: [PATCH] Move global preload implementation to be session based --- atom/browser/api/atom_api_session.cc | 15 ++++++++++++ atom/browser/api/atom_api_session.h | 4 ++++ atom/browser/api/atom_api_web_contents.h | 2 +- atom/browser/api/atom_api_window.cc | 22 ----------------- atom/browser/api/atom_api_window.h | 4 ---- atom/browser/web_contents_preferences.cc | 6 ++++- docs/api/browser-window.md | 19 --------------- docs/api/session.md | 17 ++++++++++++++ spec/api-browser-window-spec.js | 30 +++++++++++++----------- 9 files changed, 58 insertions(+), 61 deletions(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 0a124c446623..ce60fb0aed33 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/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 Session::GetPreloads() { + return g_preloads; +} + v8::Local 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); diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 72f186e4fee4..00ad00002f75 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -81,6 +81,9 @@ class Session: public mate::TrackableObject, 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 GetPreloads(); v8::Local Cookies(v8::Isolate* isolate); v8::Local Protocol(v8::Isolate* isolate); v8::Local WebRequest(v8::Isolate* isolate); @@ -103,6 +106,7 @@ class Session: public mate::TrackableObject, std::string devtools_network_emulation_client_id_; scoped_refptr browser_context_; + std::vector g_preloads; DISALLOW_COPY_AND_ASSIGN(Session); }; diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 16f23210557c..71436b3ad600 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -230,6 +230,7 @@ class WebContents : public mate::TrackableObject, v8::Local 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, const std::vector& labels); private: - AtomBrowserContext* GetBrowserContext() const; uint32_t GetNextRequestId() { return ++request_id_; diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 041601057fae..ea7e6c473c1f 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -4,8 +4,6 @@ #include "atom/browser/api/atom_api_window.h" -#include - #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 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 Window::GetGlobalPreloads() { - return g_preloads; -} - void Window::WillCloseWindow(bool* prevent_default) { *prevent_default = Emit("close"); } @@ -1167,12 +1151,6 @@ void Initialize(v8::Local exports, v8::Local unused, &mate::TrackableObject::FromWeakMapID); browser_window.SetMethod("getAllWindows", &mate::TrackableObject::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); diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 304339218887..657688f128b0 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -54,10 +54,6 @@ class Window : public mate::TrackableObject, int32_t ID() const; - static void AddGlobalPreload(const base::FilePath::StringType& preloadPath); - static void RemoveGlobalPreload(const base::FilePath::StringType& preloadPath); - static std::vector GetGlobalPreloads(); - protected: Window(v8::Isolate* isolate, v8::Local wrapper, const mate::Dictionary& options); diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc index 262f164c29b5..1f19eb0e778c 100644 --- a/atom/browser/web_contents_preferences.cc +++ b/atom/browser/web_contents_preferences.cc @@ -8,6 +8,8 @@ #include #include +#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 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); diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index bb97c50b61df..ef90dc013307 100644 --- a/docs/api/browser-window.md +++ b/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: diff --git a/docs/api/session.md b/docs/api/session.md index f75b6399f10b..54ca97fe662e 100644 --- a/docs/api/session.md +++ b/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`: diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 1ffbbf01a679..2a37de50f8d2 100644 --- a/spec/api-browser-window-spec.js +++ b/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: {