Move global preload implementation to be session based

This commit is contained in:
Samuel Attard 2017-09-17 01:05:49 +10:00 committed by Cheng Zhao
parent 448ccc261d
commit 3b80ee0655
9 changed files with 58 additions and 61 deletions

View file

@ -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);

View file

@ -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);
};

View file

@ -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_;

View file

@ -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);

View file

@ -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);

View file

@ -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);