diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 3ca15ab871f..7d31626c141 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -233,16 +233,16 @@ void SetProxyInIO(net::URLRequestContextGetter* getter, Session::Session(AtomBrowserContext* browser_context) : browser_context_(browser_context) { AttachAsUserData(browser_context); + // Observe DownloadManger to get download notifications. - auto download_manager = - content::BrowserContext::GetDownloadManager(browser_context); - download_manager->AddObserver(this); + content::BrowserContext::GetDownloadManager(browser_context)-> + AddObserver(this); } Session::~Session() { - auto download_manager = - content::BrowserContext::GetDownloadManager(browser_context_); - download_manager->RemoveObserver(this); + content::BrowserContext::GetDownloadManager(browser_context())-> + RemoveObserver(this); + Destroy(); } void Session::OnDownloadCreated(content::DownloadManager* manager, @@ -257,8 +257,16 @@ void Session::OnDownloadCreated(content::DownloadManager* manager, } } +bool Session::IsDestroyed() const { + return !browser_context_; +} + +void Session::Destroy() { + browser_context_ = nullptr; +} + void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) { - new ResolveProxyHelper(browser_context_, url, callback); + new ResolveProxyHelper(browser_context(), url, callback); } void Session::ClearCache(const net::CompletionCallback& callback) { @@ -279,7 +287,7 @@ void Session::ClearStorageData(mate::Arguments* args) { } auto storage_partition = - content::BrowserContext::GetStoragePartition(browser_context_, nullptr); + content::BrowserContext::GetStoragePartition(browser_context(), nullptr); storage_partition->ClearData( options.storage_types, options.quota_types, options.origin, content::StoragePartition::OriginMatcherFunction(), @@ -300,7 +308,7 @@ void Session::SetDownloadPath(const base::FilePath& path) { v8::Local Session::Cookies(v8::Isolate* isolate) { if (cookies_.IsEmpty()) { - auto handle = atom::api::Cookies::Create(isolate, browser_context_); + auto handle = atom::api::Cookies::Create(isolate, browser_context()); cookies_.Reset(isolate, handle.ToV8()); } return v8::Local::New(isolate, cookies_); @@ -319,8 +327,7 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( // static mate::Handle Session::CreateFrom( - v8::Isolate* isolate, - AtomBrowserContext* browser_context) { + v8::Isolate* isolate, AtomBrowserContext* browser_context) { auto existing = TrackableObject::FromWrappedClass(isolate, browser_context); if (existing) return mate::CreateHandle(isolate, static_cast(existing)); @@ -330,6 +337,14 @@ mate::Handle Session::CreateFrom( return handle; } +// static +mate::Handle Session::FromPartition( + v8::Isolate* isolate, const std::string& partition, bool in_memory) { + auto browser_context = brightray::BrowserContext::From(partition, in_memory); + return CreateFrom(isolate, + static_cast(browser_context.get())); +} + void SetWrapSession(const WrapSessionCallback& callback) { g_wrap_session = callback; } @@ -348,6 +363,7 @@ void Initialize(v8::Local exports, v8::Local unused, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); mate::Dictionary dict(isolate, exports); + dict.SetMethod("fromPartition", &atom::api::Session::FromPartition); dict.SetMethod("_setWrapSession", &atom::api::SetWrapSession); dict.SetMethod("_clearWrapSession", &atom::api::ClearWrapSession); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index c06348974ff..14406e57af5 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -37,7 +37,11 @@ class Session: public mate::TrackableObject, static mate::Handle CreateFrom( v8::Isolate* isolate, AtomBrowserContext* browser_context); - AtomBrowserContext* browser_context() const { return browser_context_; } + // Gets the Session of |partition| and |in_memory|. + static mate::Handle FromPartition( + v8::Isolate* isolate, const std::string& partition, bool in_memory); + + AtomBrowserContext* browser_context() const { return browser_context_.get(); } protected: explicit Session(AtomBrowserContext* browser_context); @@ -47,11 +51,15 @@ class Session: public mate::TrackableObject, void OnDownloadCreated(content::DownloadManager* manager, content::DownloadItem* item) override; - // mate::Wrappable implementations: + // mate::Wrappable: mate::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate) override; + bool IsDestroyed() const override; private: + // mate::TrackableObject: + void Destroy() override; + void ResolveProxy(const GURL& url, ResolveProxyCallback callback); void ClearCache(const net::CompletionCallback& callback); void ClearStorageData(mate::Arguments* args); @@ -59,9 +67,10 @@ class Session: public mate::TrackableObject, void SetDownloadPath(const base::FilePath& path); v8::Local Cookies(v8::Isolate* isolate); + // Cached object for cookies API. v8::Global cookies_; - AtomBrowserContext* browser_context_; // weak ref + scoped_refptr browser_context_; DISALLOW_COPY_AND_ASSIGN(Session); }; diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 7cefe8e45e3..d22f7aab61f 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -11,6 +11,7 @@ #include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/native_window.h" +#include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_view_guest_delegate.h" #include "atom/common/api/api_messages.h" #include "atom/common/api/event_emitter_caller.h" @@ -153,31 +154,41 @@ WebContents::WebContents(content::WebContents* web_contents) web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent()); } -WebContents::WebContents(const mate::Dictionary& options) { +WebContents::WebContents(v8::Isolate* isolate, + const mate::Dictionary& options) { + // Whether it is a guest WebContents. bool is_guest = false; options.Get("isGuest", &is_guest); - type_ = is_guest ? WEB_VIEW : BROWSER_WINDOW; - content::BrowserContext* browser_context = - AtomBrowserMainParts::Get()->browser_context(); + // Obtain the session. + std::string partition; + mate::Handle session; + if (options.Get("session", &session)) { + } else if (options.Get("partition", &partition) && !partition.empty()) { + bool in_memory = true; + if (base::StartsWith(partition, "persist:", base::CompareCase::SENSITIVE)) { + in_memory = false; + partition = partition.substr(8); + } + session = Session::FromPartition(isolate, partition, in_memory); + } else { + // Use the default session if not specified. + session = Session::FromPartition(isolate, "", false); + } + session_.Reset(isolate, session.ToV8()); + content::WebContents* web_contents; if (is_guest) { - GURL guest_site; - options.Get("partition", &guest_site); - // use hosts' browser_context when no partition is specified. - if (!guest_site.query().empty()) { - browser_context = AtomBrowserMainParts::Get() - ->GetBrowserContextForPartition(guest_site); - } - auto site_instance = - content::SiteInstance::CreateForURL(browser_context, guest_site); - content::WebContents::CreateParams params(browser_context, site_instance); + content::SiteInstance* site_instance = content::SiteInstance::CreateForURL( + session->browser_context(), GURL("chrome-guest://fake-host")); + content::WebContents::CreateParams params( + session->browser_context(), site_instance); guest_delegate_.reset(new WebViewGuestDelegate); params.guest_delegate = guest_delegate_.get(); web_contents = content::WebContents::Create(params); } else { - content::WebContents::CreateParams params(browser_context); + content::WebContents::CreateParams params(session->browser_context()); web_contents = content::WebContents::Create(params); } @@ -185,6 +196,11 @@ WebContents::WebContents(const mate::Dictionary& options) { AttachAsUserData(web_contents); InitWithWebContents(web_contents); + // Save the preferences. + base::DictionaryValue web_preferences; + mate::ConvertFromV8(isolate, options.GetHandle(), &web_preferences); + new WebContentsPreferences(web_contents, &web_preferences); + web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent()); if (is_guest) { @@ -489,6 +505,7 @@ void WebContents::NavigationEntryCommitted( } void WebContents::Destroy() { + session_.Reset(); if (type_ == WEB_VIEW && managed_web_contents()) { // When force destroying the "destroyed" event is not emitted. WebContentsDestroyed(); @@ -651,10 +668,6 @@ void WebContents::InspectServiceWorker() { } v8::Local WebContents::Session(v8::Isolate* isolate) { - if (session_.IsEmpty()) { - auto handle = Session::CreateFrom(isolate, GetBrowserContext()); - session_.Reset(isolate, handle.ToV8()); - } return v8::Local::New(isolate, session_); } @@ -890,7 +903,7 @@ mate::Handle WebContents::CreateFrom( // static mate::Handle WebContents::Create( v8::Isolate* isolate, const mate::Dictionary& options) { - auto handle = mate::CreateHandle(isolate, new WebContents(options)); + auto handle = mate::CreateHandle(isolate, new WebContents(isolate, options)); g_wrap_web_contents.Run(handle.ToV8()); return handle; } diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index e75e88fd703..e2e405b7385 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -48,7 +48,9 @@ class WebContents : public mate::TrackableObject, static mate::Handle Create( v8::Isolate* isolate, const mate::Dictionary& options); - void Destroy(); + // mate::TrackableObject: + void Destroy() override; + bool IsAlive() const; int GetID() const; bool Equal(const WebContents* web_contents) const; @@ -116,7 +118,7 @@ class WebContents : public mate::TrackableObject, protected: explicit WebContents(content::WebContents* web_contents); - explicit WebContents(const mate::Dictionary& options); + WebContents(v8::Isolate* isolate, const mate::Dictionary& options); ~WebContents(); // mate::Wrappable: diff --git a/atom/browser/api/atom_api_web_view_manager.cc b/atom/browser/api/atom_api_web_view_manager.cc index 221d0d7118b..83287ac8b2f 100644 --- a/atom/browser/api/atom_api_web_view_manager.cc +++ b/atom/browser/api/atom_api_web_view_manager.cc @@ -3,14 +3,12 @@ // found in the LICENSE file. #include "atom/browser/api/atom_api_web_contents.h" -#include "atom/browser/web_view_constants.h" +#include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_view_manager.h" -#include "atom/common/native_mate_converters/gurl_converter.h" +#include "atom/common/native_mate_converters/value_converter.h" +#include "atom/common/node_includes.h" #include "content/public/browser/browser_context.h" #include "native_mate/dictionary.h" -#include "net/base/filename_util.h" - -#include "atom/common/node_includes.h" namespace mate { @@ -26,31 +24,6 @@ struct Converter { } }; -template<> -struct Converter { - static bool FromV8(v8::Isolate* isolate, v8::Local val, - atom::WebViewManager::WebViewInfo* out) { - Dictionary options; - if (!ConvertFromV8(isolate, val, &options)) - return false; - - GURL preload_url; - if (!options.Get(atom::web_view::kPreloadUrl, &preload_url)) - return false; - - if (!preload_url.is_empty() && - !net::FileURLToFilePath(preload_url, &(out->preload_script))) - return false; - - return options.Get(atom::web_view::kNodeIntegration, - &(out->node_integration)) && - options.Get(atom::web_view::kPlugins, &(out->plugins)) && - options.Get(atom::web_view::kPartitionId, &(out->partition_id)) && - options.Get(atom::web_view::kDisableWebSecurity, - &(out->disable_web_security)); - } -}; - } // namespace mate namespace { @@ -69,17 +42,13 @@ void AddGuest(int guest_instance_id, int element_instance_id, content::WebContents* embedder, content::WebContents* guest_web_contents, - atom::WebViewManager::WebViewInfo info) { + const base::DictionaryValue& options) { auto manager = GetWebViewManager(embedder); if (manager) manager->AddGuest(guest_instance_id, element_instance_id, embedder, guest_web_contents); - info.guest_instance_id = guest_instance_id; - info.embedder = embedder; - auto data = new atom::WebViewManager::WebViewInfoUserData(info); - guest_web_contents->SetUserData( - atom::web_view::kWebViewInfoKeyName, data); + atom::WebContentsPreferences::From(guest_web_contents)->Merge(options); } void RemoveGuest(content::WebContents* embedder, int guest_instance_id) { diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index e6d16fe6fa5..e171fa7698f 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -13,6 +13,7 @@ #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/options_switches.h" #include "content/public/browser/render_process_host.h" #include "native_mate/constructor.h" #include "native_mate/dictionary.h" @@ -64,9 +65,22 @@ void OnCapturePageDone( Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) { + // Use options['web-preferences'] to create WebContents. + mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate); + options.Get(switches::kWebPreferences, &web_preferences); + + // Be compatible with old options which are now in web_preferences. + std::string str; + double d; + if (options.Get(switches::kNodeIntegration, &str)) + web_preferences.Set(switches::kNodeIntegration, str); + if (options.Get(switches::kPreloadScript, &str)) + web_preferences.Set(switches::kPreloadScript, str); + if (options.Get(switches::kZoomFactor, &d)) + web_preferences.Set(switches::kZoomFactor, d); + // Creates the WebContents used by BrowserWindow. - mate::Dictionary web_contents_options(isolate, v8::Object::New(isolate)); - auto web_contents = WebContents::Create(isolate, web_contents_options); + auto web_contents = WebContents::Create(isolate, web_preferences); web_contents_.Reset(isolate, web_contents.ToV8()); api_web_contents_ = web_contents.get(); @@ -207,7 +221,10 @@ bool Window::IsDestroyed() const { } void Window::Destroy() { - window_->CloseContents(nullptr); + if (window_) { + window_->CloseContents(nullptr); + window_.reset(); + } } void Window::Close() { diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index de2f093ea3e..4384e03a249 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -78,8 +78,10 @@ class Window : public mate::TrackableObject, bool IsDestroyed() const override; private: + // mate::TrackableObject: + void Destroy() override; + // APIs for NativeWindow. - void Destroy(); void Close(); bool IsClosed(); void Focus(); diff --git a/atom/browser/api/trackable_object.cc b/atom/browser/api/trackable_object.cc index ae9fa44230a..50bfa59e6a7 100644 --- a/atom/browser/api/trackable_object.cc +++ b/atom/browser/api/trackable_object.cc @@ -29,7 +29,9 @@ class IDUserData : public base::SupportsUserData::Data { } // namespace TrackableObjectBase::TrackableObjectBase() - : weak_map_id_(0), wrapped_(nullptr) { + : weak_map_id_(0), wrapped_(nullptr), weak_factory_(this) { + RegisterDestructionCallback( + base::Bind(&TrackableObjectBase::Destroy, weak_factory_.GetWeakPtr())); } TrackableObjectBase::~TrackableObjectBase() { @@ -61,8 +63,9 @@ int32_t TrackableObjectBase::GetIDFromWrappedClass(base::SupportsUserData* w) { } // static -void TrackableObjectBase::RegisterDestructionCallback(void (*c)()) { - atom::AtomBrowserMainParts::Get()->RegisterDestructionCallback(base::Bind(c)); +void TrackableObjectBase::RegisterDestructionCallback( + const base::Closure& closure) { + atom::AtomBrowserMainParts::Get()->RegisterDestructionCallback(closure); } } // namespace mate diff --git a/atom/browser/api/trackable_object.h b/atom/browser/api/trackable_object.h index 656904cc45d..8ff7d1c0404 100644 --- a/atom/browser/api/trackable_object.h +++ b/atom/browser/api/trackable_object.h @@ -9,7 +9,9 @@ #include "atom/browser/api/event_emitter.h" #include "atom/common/id_weak_map.h" +#include "base/bind.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" namespace base { class SupportsUserData; @@ -28,6 +30,9 @@ class TrackableObjectBase : public mate::EventEmitter { // Wrap TrackableObject into a class that SupportsUserData. void AttachAsUserData(base::SupportsUserData* wrapped); + // Subclasses should implement this to destroy their native types. + virtual void Destroy() = 0; + protected: ~TrackableObjectBase() override; @@ -39,12 +44,14 @@ class TrackableObjectBase : public mate::EventEmitter { // Register a callback that should be destroyed before JavaScript environment // gets destroyed. - static void RegisterDestructionCallback(void (*callback)()); + static void RegisterDestructionCallback(const base::Closure& closure); int32_t weak_map_id_; base::SupportsUserData* wrapped_; private: + base::WeakPtrFactory weak_factory_; + DISALLOW_COPY_AND_ASSIGN(TrackableObjectBase); }; @@ -85,7 +92,8 @@ class TrackableObject : public TrackableObjectBase { } TrackableObject() { - RegisterDestructionCallback(&TrackableObject::ReleaseAllWeakReferences); + RegisterDestructionCallback( + base::Bind(&TrackableObject::ReleaseAllWeakReferences)); } // Removes this instance from the weak map. diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index fad952d5336..e45caceab01 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -15,8 +15,7 @@ #include "atom/browser/atom_speech_recognition_manager_delegate.h" #include "atom/browser/browser.h" #include "atom/browser/native_window.h" -#include "atom/browser/web_view_manager.h" -#include "atom/browser/web_view_constants.h" +#include "atom/browser/web_contents_preferences.h" #include "atom/browser/window_list.h" #include "atom/common/options_switches.h" #include "base/command_line.h" @@ -38,6 +37,7 @@ #include "net/ssl/ssl_cert_request_info.h" #include "ppapi/host/ppapi_host.h" #include "ui/base/l10n/l10n_util.h" +#include "v8/include/v8.h" namespace atom { @@ -54,37 +54,6 @@ bool g_suppress_renderer_process_restart = false; // Custom schemes to be registered to standard. std::string g_custom_schemes = ""; -// Find out the owner of the child process according to |process_id|. -enum ProcessOwner { - OWNER_NATIVE_WINDOW, - OWNER_GUEST_WEB_CONTENTS, - OWNER_NONE, // it might be devtools though. -}; - -ProcessOwner GetProcessOwner(int process_id, - NativeWindow** window, - WebViewManager::WebViewInfo* info) { - content::WebContents* web_contents = content::WebContents::FromRenderViewHost( - content::RenderViewHost::FromID(process_id, kDefaultRoutingID)); - if (!web_contents) - return OWNER_NONE; - - // First search for NativeWindow. - *window = NativeWindow::FromWebContents(web_contents); - if (*window) - return OWNER_NATIVE_WINDOW; - - // Then search for guest WebContents. - auto data = static_cast( - web_contents->GetUserData(web_view::kWebViewInfoKeyName)); - if (data) { - *info = data->web_view_info(); - return OWNER_GUEST_WEB_CONTENTS; - } - - return OWNER_NONE; -} - scoped_refptr ImportCertFromFile( const base::FilePath& path) { if (path.empty()) @@ -160,16 +129,7 @@ void AtomBrowserClient::OverrideWebkitPrefs( // Custom preferences of guest page. auto web_contents = content::WebContents::FromRenderViewHost(host); - auto info = static_cast( - web_contents->GetUserData(web_view::kWebViewInfoKeyName)); - if (info) { - prefs->web_security_enabled = !info->web_view_info().disable_web_security; - return; - } - - NativeWindow* window = NativeWindow::FromWebContents(web_contents); - if (window) - window->OverrideWebkitPrefs(prefs); + WebContentsPreferences::OverrideWebkitPrefs(web_contents, prefs); } std::string AtomBrowserClient::GetApplicationLocale() { @@ -225,23 +185,14 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches( if (ContainsKey(pending_processes_, process_id)) process_id = pending_processes_[process_id]; - NativeWindow* window; - WebViewManager::WebViewInfo info; - ProcessOwner owner = GetProcessOwner(process_id, &window, &info); + // Get the WebContents of the render process. + content::WebContents* web_contents = content::WebContents::FromRenderViewHost( + content::RenderViewHost::FromID(process_id, kDefaultRoutingID)); + if (!web_contents) + return; - if (owner == OWNER_NATIVE_WINDOW) { - window->AppendExtraCommandLineSwitches(command_line); - } else if (owner == OWNER_GUEST_WEB_CONTENTS) { - command_line->AppendSwitchASCII( - switches::kGuestInstanceID, base::IntToString(info.guest_instance_id)); - command_line->AppendSwitchASCII( - switches::kNodeIntegration, 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); - } + WebContentsPreferences::AppendExtraCommandLineSwitches( + web_contents, command_line); } void AtomBrowserClient::DidCreatePpapiPlugin( diff --git a/atom/browser/atom_browser_context.cc b/atom/browser/atom_browser_context.cc index 583a6324a8f..d1ef09e70ff 100644 --- a/atom/browser/atom_browser_context.cc +++ b/atom/browser/atom_browser_context.cc @@ -56,8 +56,10 @@ std::string RemoveWhitespace(const std::string& str) { } // namespace -AtomBrowserContext::AtomBrowserContext() - : job_factory_(new AtomURLRequestJobFactory) { +AtomBrowserContext::AtomBrowserContext(const std::string& partition, + bool in_memory) + : brightray::BrowserContext(partition, in_memory), + job_factory_(new AtomURLRequestJobFactory) { } AtomBrowserContext::~AtomBrowserContext() { @@ -150,7 +152,7 @@ AtomBrowserContext::GetDownloadManagerDelegate() { content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() { if (!guest_manager_) - guest_manager_.reset(new WebViewManager(this)); + guest_manager_.reset(new WebViewManager); return guest_manager_.get(); } @@ -162,3 +164,13 @@ void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) { } } // namespace atom + +namespace brightray { + +// static +scoped_refptr BrowserContext::Create( + const std::string& partition, bool in_memory) { + return make_scoped_refptr(new atom::AtomBrowserContext(partition, in_memory)); +} + +} // namespace brightray diff --git a/atom/browser/atom_browser_context.h b/atom/browser/atom_browser_context.h index c1ff613b8c0..c99461ad9a8 100644 --- a/atom/browser/atom_browser_context.h +++ b/atom/browser/atom_browser_context.h @@ -17,8 +17,8 @@ class WebViewManager; class AtomBrowserContext : public brightray::BrowserContext { public: - AtomBrowserContext(); - virtual ~AtomBrowserContext(); + AtomBrowserContext(const std::string& partition, bool in_memory); + ~AtomBrowserContext() override; // brightray::URLRequestContextGetter::Delegate: std::string GetUserAgent() override; @@ -41,7 +41,8 @@ class AtomBrowserContext : public brightray::BrowserContext { scoped_ptr download_manager_delegate_; scoped_ptr guest_manager_; - AtomURLRequestJobFactory* job_factory_; // Weak reference. + // Managed by brightray::BrowserContext. + AtomURLRequestJobFactory* job_factory_; DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext); }; diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index 806c12b7eac..31a49046414 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -26,24 +26,6 @@ namespace atom { -namespace { - -const base::FilePath::CharType kStoragePartitionDirname[] = - FILE_PATH_LITERAL("Partitions"); - -void GetStoragePartitionConfig(const GURL& partition, - base::FilePath* partition_path, - bool* in_memory, - std::string* id) { - *in_memory = (partition.path() != "/persist"); - net::UnescapeRule::Type flags = - net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS; - *id = net::UnescapeURLComponent(partition.query(), flags); - *partition_path = base::FilePath(kStoragePartitionDirname).AppendASCII(*id); -} - -} // namespace - // static AtomBrowserMainParts* AtomBrowserMainParts::self_ = NULL; @@ -68,30 +50,11 @@ AtomBrowserMainParts* AtomBrowserMainParts::Get() { return self_; } -content::BrowserContext* AtomBrowserMainParts::GetBrowserContextForPartition( - const GURL& partition) { - std::string id; - bool in_memory; - base::FilePath partition_path; - GetStoragePartitionConfig(partition, &partition_path, &in_memory, &id); - if (browser_context_map_.contains(id)) - return browser_context_map_.get(id); - - scoped_ptr browser_context(CreateBrowserContext()); - browser_context->Initialize(partition_path.AsUTF8Unsafe(), in_memory); - browser_context_map_.set(id, browser_context.Pass()); - return browser_context_map_.get(id); -} - void AtomBrowserMainParts::RegisterDestructionCallback( const base::Closure& callback) { destruction_callbacks_.push_back(callback); } -brightray::BrowserContext* AtomBrowserMainParts::CreateBrowserContext() { - return new AtomBrowserContext(); -} - void AtomBrowserMainParts::PostEarlyInitialization() { brightray::BrowserMainParts::PostEarlyInitialization(); diff --git a/atom/browser/atom_browser_main_parts.h b/atom/browser/atom_browser_main_parts.h index d952f432882..59ff7c977c8 100644 --- a/atom/browser/atom_browser_main_parts.h +++ b/atom/browser/atom_browser_main_parts.h @@ -6,11 +6,9 @@ #define ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_ #include -#include #include #include "base/callback.h" -#include "base/containers/scoped_ptr_hash_map.h" #include "base/timer/timer.h" #include "brightray/browser/browser_main_parts.h" #include "content/public/browser/browser_context.h" @@ -33,10 +31,6 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { static AtomBrowserMainParts* Get(); - // Returns the BrowserContext associated with the partition. - content::BrowserContext* GetBrowserContextForPartition( - const GURL& partition); - // Register a callback that should be destroyed before JavaScript environment // gets destroyed. void RegisterDestructionCallback(const base::Closure& callback); @@ -44,10 +38,7 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { Browser* browser() { return browser_.get(); } protected: - // Implementations of brightray::BrowserMainParts. - brightray::BrowserContext* CreateBrowserContext() override; - - // Implementations of content::BrowserMainParts. + // content::BrowserMainParts: void PostEarlyInitialization() override; void PreMainMessageLoopRun() override; #if defined(OS_MACOSX) @@ -78,10 +69,6 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { // List of callbacks should be executed before destroying JS env. std::list destruction_callbacks_; - // partition_id => browser_context - base::ScopedPtrHashMap> - browser_context_map_; - static AtomBrowserMainParts* self_; DISALLOW_COPY_AND_ASSIGN(AtomBrowserMainParts); diff --git a/atom/browser/lib/guest-view-manager.coffee b/atom/browser/lib/guest-view-manager.coffee index 58127469908..1c05b65b2dc 100644 --- a/atom/browser/lib/guest-view-manager.coffee +++ b/atom/browser/lib/guest-view-manager.coffee @@ -38,29 +38,12 @@ moveLastToFirst = (list) -> getNextInstanceId = (webContents) -> ++nextInstanceId -# Generate URL encoded partition id. -getPartitionId = (partition) -> - # Guest site url will be chrome-guest://fake-host/{persist}?{partitionId} - partitionId = "chrome-guest://fake-host/" - if partition - persist = partition.startsWith('persist:') - if persist - partition = partition.substring('persist:'.length) - partitionId += 'persist?' - else - # Just to differentiate from same persistant ID - partition += "_temp" - partitionId += '?' - partitionId += encodeURIComponent(partition) - return partitionId - # Create a new guest instance. createGuest = (embedder, params) -> webViewManager ?= process.atomBinding 'web_view_manager' id = getNextInstanceId embedder - partitionId = getPartitionId params.partition - guest = webContents.create {isGuest: true, partition: partitionId, embedder} + guest = webContents.create {isGuest: true, partition: params.partition, embedder} guestInstances[id] = {guest, embedder} # Destroy guest when the embedder is gone or navigated. @@ -132,12 +115,13 @@ attachGuest = (embedder, elementInstanceId, guestInstanceId, params) -> return unless guestInstances[oldGuestInstanceId]? destroyGuest embedder, oldGuestInstanceId - webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest, - nodeIntegration: params.nodeintegration - plugins: params.plugins - disableWebSecurity: params.disablewebsecurity - preloadUrl: params.preload ? '' - partitionId: getPartitionId(params.partition) + webPreferences = + 'guest-instance-id': guestInstanceId + 'node-integration': params.nodeintegration ? false + 'plugins': params.plugins + 'web-security': !params.disablewebsecurity + webPreferences['preload-url'] = params.preload if params.preload + webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest, webPreferences guest.attachParams = params embedderElementsMap[key] = guestInstanceId diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 96085846bca..997fe72c4ae 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -15,12 +15,10 @@ #include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/options_switches.h" -#include "base/command_line.h" #include "base/files/file_util.h" #include "base/json/json_writer.h" #include "base/prefs/pref_service.h" #include "base/message_loop/message_loop.h" -#include "base/strings/string_number_conversions.h" #include "base/strings/utf_string_conversions.h" #include "brightray/browser/inspectable_web_contents.h" #include "brightray/browser/inspectable_web_contents_view.h" @@ -31,8 +29,6 @@ #include "content/public/browser/render_view_host.h" #include "content/public/browser/render_widget_host_view.h" #include "content/public/common/content_switches.h" -#include "content/public/common/renderer_preferences.h" -#include "content/public/common/web_preferences.h" #include "ipc/ipc_message_macros.h" #include "native_mate/dictionary.h" #include "ui/gfx/codec/png_codec.h" @@ -43,10 +39,6 @@ #include "ui/gfx/screen.h" #include "ui/gl/gpu_switching_manager.h" -#if defined(OS_WIN) -#include "ui/gfx/switches.h" -#endif - using content::NavigationEntry; using content::RenderWidgetHostView; using content::RenderWidgetHost; @@ -57,17 +49,6 @@ namespace atom { namespace { -// Array of available web runtime features. -const char* kWebRuntimeFeatures[] = { - switches::kExperimentalFeatures, - switches::kExperimentalCanvasFeatures, - switches::kSubpixelFontScaling, - switches::kOverlayScrollbars, - switches::kOverlayFullscreenVideo, - switches::kSharedWorker, - switches::kPageVisibility, -}; - // Convert draggable regions in raw format to SkRegion format. Caller is // responsible for deleting the returned SkRegion instance. scoped_ptr DraggableRegionsToSkRegion( @@ -94,9 +75,7 @@ NativeWindow::NativeWindow( transparent_(false), enable_larger_than_screen_(false), is_closed_(false), - node_integration_(true), has_dialog_attached_(false), - zoom_factor_(1.0), aspect_ratio_(0.0), inspectable_web_contents_(inspectable_web_contents), weak_factory_(this) { @@ -105,7 +84,6 @@ NativeWindow::NativeWindow( options.Get(switches::kFrame, &has_frame_); options.Get(switches::kTransparent, &transparent_); options.Get(switches::kEnableLargerThanScreen, &enable_larger_than_screen_); - options.Get(switches::kNodeIntegration, &node_integration_); // Tell the content module to initialize renderer widget with transparent // mode. @@ -114,25 +92,6 @@ NativeWindow::NativeWindow( // Read icon before window is created. options.Get(switches::kIcon, &icon_); - // The "preload" option must be absolute path. - if (options.Get(switches::kPreloadScript, &preload_script_) && - !preload_script_.IsAbsolute()) { - LOG(ERROR) << "Path of \"preload\" script must be absolute."; - preload_script_.clear(); - } - - // Be compatible with old API of "node-integration" option. - std::string old_string_token; - if (options.Get(switches::kNodeIntegration, &old_string_token) && - old_string_token != "disable") - node_integration_ = true; - - // Read the web preferences. - options.Get(switches::kWebPreferences, &web_preferences_); - - // Read the zoom factor before any navigation. - options.Get(switches::kZoomFactor, &zoom_factor_); - WindowList::AddWindow(this); } @@ -381,81 +340,6 @@ void NativeWindow::RendererResponsive(content::WebContents* source) { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive()); } -void NativeWindow::AppendExtraCommandLineSwitches( - base::CommandLine* command_line) { - // Append --node-integration to renderer process. - command_line->AppendSwitchASCII(switches::kNodeIntegration, - node_integration_ ? "true" : "false"); - - // Append --preload. - if (!preload_script_.empty()) - command_line->AppendSwitchPath(switches::kPreloadScript, preload_script_); - - // Append --zoom-factor. - if (zoom_factor_ != 1.0) - command_line->AppendSwitchASCII(switches::kZoomFactor, - base::DoubleToString(zoom_factor_)); - - if (web_preferences_.IsEmpty()) - return; - - bool b; -#if defined(OS_WIN) - // Check if DirectWrite is disabled. - if (web_preferences_.Get(switches::kDirectWrite, &b) && !b) - command_line->AppendSwitch(::switches::kDisableDirectWrite); -#endif - - // Check if plugins are enabled. - if (web_preferences_.Get("plugins", &b) && b) - command_line->AppendSwitch(switches::kEnablePlugins); - - // This set of options are not availabe in WebPreferences, so we have to pass - // them via command line and enable them in renderer procss. - for (size_t i = 0; i < arraysize(kWebRuntimeFeatures); ++i) { - const char* feature = kWebRuntimeFeatures[i]; - if (web_preferences_.Get(feature, &b)) - command_line->AppendSwitchASCII(feature, b ? "true" : "false"); - } -} - -void NativeWindow::OverrideWebkitPrefs(content::WebPreferences* prefs) { - if (web_preferences_.IsEmpty()) - return; - - bool b; - std::vector list; - if (web_preferences_.Get("javascript", &b)) - prefs->javascript_enabled = b; - if (web_preferences_.Get("images", &b)) - prefs->images_enabled = b; - if (web_preferences_.Get("java", &b)) - prefs->java_enabled = b; - if (web_preferences_.Get("text-areas-are-resizable", &b)) - prefs->text_areas_are_resizable = b; - if (web_preferences_.Get("webgl", &b)) - prefs->experimental_webgl_enabled = b; - if (web_preferences_.Get("webaudio", &b)) - prefs->webaudio_enabled = b; - if (web_preferences_.Get("web-security", &b)) { - prefs->web_security_enabled = b; - prefs->allow_displaying_insecure_content = !b; - prefs->allow_running_insecure_content = !b; - } - if (web_preferences_.Get("allow-displaying-insecure-content", &b)) - prefs->allow_displaying_insecure_content = b; - if (web_preferences_.Get("allow-running-insecure-content", &b)) - prefs->allow_running_insecure_content = b; - if (web_preferences_.Get("extra-plugin-dirs", &list)) { - if (content::PluginService::GetInstance()->NPAPIPluginsSupported()) { - for (size_t i = 0; i < list.size(); ++i) - content::PluginService::GetInstance()->AddExtraPluginDir(list[i]); - } else { - LOG(WARNING) << "NPAPI plugins not supported on this platform"; - } - } -} - void NativeWindow::NotifyWindowClosed() { if (is_closed_) return; diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 10889a5ccaf..b89e2b58e6e 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -19,23 +19,17 @@ #include "content/public/browser/readback_types.h" #include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_user_data.h" -#include "native_mate/persistent_dictionary.h" #include "ui/gfx/image/image.h" #include "ui/gfx/image/image_skia.h" class SkRegion; -namespace base { -class CommandLine; -} - namespace brightray { class InspectableWebContents; } namespace content { struct NativeWebKeyboardEvent; -struct WebPreferences; } namespace gfx { @@ -189,10 +183,6 @@ class NativeWindow : public content::WebContentsObserver, content::WebContents*, const content::NativeWebKeyboardEvent& event) {} - // Called when renderer process is going to be started. - void AppendExtraCommandLineSwitches(base::CommandLine* command_line); - void OverrideWebkitPrefs(content::WebPreferences* prefs); - // Public API used by platform-dependent delegates and observers to send UI // related notifications. void NotifyWindowClosed(); @@ -282,9 +272,6 @@ class NativeWindow : public content::WebContentsObserver, // The windows has been closed. bool is_closed_; - // Whether node integration is enabled. - bool node_integration_; - // There is a dialog that has been attached to window. bool has_dialog_attached_; @@ -292,15 +279,6 @@ class NativeWindow : public content::WebContentsObserver, // it should be cancelled when we can prove that the window is responsive. base::CancelableClosure window_unresposive_closure_; - // Web preferences. - mate::PersistentDictionary web_preferences_; - - // The script to load before page's JavaScript starts to run. - base::FilePath preload_script_; - - // Page's default zoom factor. - double zoom_factor_; - // Used to maintain the aspect ratio of a view which is inside of the // content view. double aspect_ratio_; diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc new file mode 100644 index 00000000000..19ea826a5ff --- /dev/null +++ b/atom/browser/web_contents_preferences.cc @@ -0,0 +1,162 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/web_contents_preferences.h" + +#include + +#include "atom/common/options_switches.h" +#include "base/command_line.h" +#include "base/strings/string_number_conversions.h" +#include "content/public/common/web_preferences.h" +#include "net/base/filename_util.h" + +#if defined(OS_WIN) +#include "ui/gfx/switches.h" +#endif + +namespace atom { + +namespace { + +// Pointer as WebContents's user data key. +const char* kWebPreferencesKey = "WebContentsPreferences"; + +// Array of available web runtime features. +const char* kWebRuntimeFeatures[] = { + switches::kExperimentalFeatures, + switches::kExperimentalCanvasFeatures, + switches::kSubpixelFontScaling, + switches::kOverlayScrollbars, + switches::kOverlayFullscreenVideo, + switches::kSharedWorker, + switches::kPageVisibility, +}; + +} // namespace + +WebContentsPreferences::WebContentsPreferences( + content::WebContents* web_contents, + base::DictionaryValue* web_preferences) { + web_preferences_.Swap(web_preferences); + web_contents->SetUserData(kWebPreferencesKey, this); +} + +WebContentsPreferences::~WebContentsPreferences() { +} + +void WebContentsPreferences::Merge(const base::DictionaryValue& extend) { + web_preferences_.MergeDictionary(&extend); +} + +// static +WebContentsPreferences* WebContentsPreferences::From( + content::WebContents* web_contents) { + return static_cast( + web_contents->GetUserData(kWebPreferencesKey)); +} + +// static +void WebContentsPreferences::AppendExtraCommandLineSwitches( + content::WebContents* web_contents, base::CommandLine* command_line) { + WebContentsPreferences* self = From(web_contents); + if (!self) + return; + + base::DictionaryValue& web_preferences = self->web_preferences_; + + bool b; +#if defined(OS_WIN) + // Check if DirectWrite is disabled. + if (web_preferences.GetBoolean(switches::kDirectWrite, &b) && !b) + command_line->AppendSwitch(::switches::kDisableDirectWrite); +#endif + + // Check if plugins are enabled. + if (web_preferences.GetBoolean("plugins", &b) && b) + command_line->AppendSwitch(switches::kEnablePlugins); + + // This set of options are not availabe in WebPreferences, so we have to pass + // them via command line and enable them in renderer procss. + for (size_t i = 0; i < arraysize(kWebRuntimeFeatures); ++i) { + const char* feature = kWebRuntimeFeatures[i]; + if (web_preferences.GetBoolean(feature, &b)) + command_line->AppendSwitchASCII(feature, b ? "true" : "false"); + } + + // Check if we have node integration specified. + bool node_integration = true; + web_preferences.GetBoolean(switches::kNodeIntegration, &node_integration); + // Be compatible with old API of "node-integration" option. + std::string old_token; + if (web_preferences.GetString(switches::kNodeIntegration, &old_token) && + old_token != "disable") + node_integration = true; + command_line->AppendSwitchASCII(switches::kNodeIntegration, + node_integration ? "true" : "false"); + + // The preload script. + base::FilePath::StringType preload; + if (web_preferences.GetString(switches::kPreloadScript, &preload)) { + if (base::FilePath(preload).IsAbsolute()) + command_line->AppendSwitchNative(switches::kPreloadScript, preload); + else + LOG(ERROR) << "preload script must have abosulute path."; + } else if (web_preferences.GetString(switches::kPreloadUrl, &preload)) { + // Translate to file path if there is "preload-url" option. + base::FilePath preload_path; + if (net::FileURLToFilePath(GURL(preload), &preload_path)) + command_line->AppendSwitchPath(switches::kPreloadScript, preload_path); + else + LOG(ERROR) << "preload url must be file:// protocol."; + } + + // The zoom factor. + double zoom_factor = 1.0; + if (web_preferences.GetDouble(switches::kZoomFactor, &zoom_factor) && + zoom_factor != 1.0) + command_line->AppendSwitchASCII(switches::kZoomFactor, + base::DoubleToString(zoom_factor)); + + // --guest-instance-id, which is used to identify guest WebContents. + int guest_instance_id; + if (web_preferences.GetInteger(switches::kGuestInstanceID, + &guest_instance_id)) + command_line->AppendSwitchASCII(switches::kGuestInstanceID, + base::IntToString(guest_instance_id)); +} + +// static +void WebContentsPreferences::OverrideWebkitPrefs( + content::WebContents* web_contents, content::WebPreferences* prefs) { + WebContentsPreferences* self = From(web_contents); + if (!self) + return; + + bool b; + if (self->web_preferences_.GetBoolean("javascript", &b)) + prefs->javascript_enabled = b; + if (self->web_preferences_.GetBoolean("images", &b)) + prefs->images_enabled = b; + if (self->web_preferences_.GetBoolean("java", &b)) + prefs->java_enabled = b; + if (self->web_preferences_.GetBoolean("text-areas-are-resizable", &b)) + prefs->text_areas_are_resizable = b; + if (self->web_preferences_.GetBoolean("webgl", &b)) + prefs->experimental_webgl_enabled = b; + if (self->web_preferences_.GetBoolean("webaudio", &b)) + prefs->webaudio_enabled = b; + if (self->web_preferences_.GetBoolean("web-security", &b)) { + prefs->web_security_enabled = b; + prefs->allow_displaying_insecure_content = !b; + prefs->allow_running_insecure_content = !b; + } + if (self->web_preferences_.GetBoolean("allow-displaying-insecure-content", + &b)) + prefs->allow_displaying_insecure_content = b; + if (self->web_preferences_.GetBoolean("allow-running-insecure-content", &b)) + prefs->allow_running_insecure_content = b; +} + +} // namespace atom diff --git a/atom/browser/web_contents_preferences.h b/atom/browser/web_contents_preferences.h new file mode 100644 index 00000000000..310456725aa --- /dev/null +++ b/atom/browser/web_contents_preferences.h @@ -0,0 +1,53 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ +#define ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ + +#include "base/values.h" +#include "content/public/browser/web_contents_user_data.h" + +namespace base { +class CommandLine; +} + +namespace content { +struct WebPreferences; +} + +namespace atom { + +// Stores and applies the preferences of WebContents. +class WebContentsPreferences + : public content::WebContentsUserData { + public: + // Get the preferences of |web_contents|. + static WebContentsPreferences* From(content::WebContents* web_contents); + + // Append command paramters according to |web_contents|'s preferences. + static void AppendExtraCommandLineSwitches( + content::WebContents* web_contents, base::CommandLine* command_line); + + // Modify the WebPreferences according to |web_contents|'s preferences. + static void OverrideWebkitPrefs( + content::WebContents* web_contents, content::WebPreferences* prefs); + + WebContentsPreferences(content::WebContents* web_contents, + base::DictionaryValue* web_preferences); + ~WebContentsPreferences() override; + + // $.extend(|web_preferences_|, |new_web_preferences|). + void Merge(const base::DictionaryValue& new_web_preferences); + + private: + friend class content::WebContentsUserData; + + base::DictionaryValue web_preferences_; + + DISALLOW_COPY_AND_ASSIGN(WebContentsPreferences); +}; + +} // namespace atom + +#endif // ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ diff --git a/atom/browser/web_view_manager.cc b/atom/browser/web_view_manager.cc index 9d0b9337599..d404c1a4368 100644 --- a/atom/browser/web_view_manager.cc +++ b/atom/browser/web_view_manager.cc @@ -5,13 +5,12 @@ #include "atom/browser/web_view_manager.h" #include "atom/browser/atom_browser_context.h" -#include "atom/browser/atom_browser_main_parts.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/web_contents.h" namespace atom { -WebViewManager::WebViewManager(content::BrowserContext* context) { +WebViewManager::WebViewManager() { } WebViewManager::~WebViewManager() { @@ -21,7 +20,6 @@ void WebViewManager::AddGuest(int guest_instance_id, int element_instance_id, content::WebContents* embedder, content::WebContents* web_contents) { - base::AutoLock auto_lock(lock_); web_contents_embedder_map_[guest_instance_id] = { web_contents, embedder }; // Map the element in embedder to guest. @@ -31,7 +29,6 @@ void WebViewManager::AddGuest(int guest_instance_id, } void WebViewManager::RemoveGuest(int guest_instance_id) { - base::AutoLock auto_lock(lock_); if (!ContainsKey(web_contents_embedder_map_, guest_instance_id)) return; diff --git a/atom/browser/web_view_manager.h b/atom/browser/web_view_manager.h index 20c04484972..ff9a8ecba2a 100644 --- a/atom/browser/web_view_manager.h +++ b/atom/browser/web_view_manager.h @@ -7,45 +7,14 @@ #include -#include "base/files/file_path.h" -#include "base/supports_user_data.h" -#include "base/synchronization/lock.h" #include "content/public/browser/browser_plugin_guest_manager.h" -#include "content/public/browser/site_instance.h" - -namespace content { -class BrowserContext; -class RenderProcessHost; -} namespace atom { class WebViewManager : public content::BrowserPluginGuestManager { public: - struct WebViewInfo { - int guest_instance_id; - content::WebContents* embedder; - bool node_integration; - bool plugins; - bool disable_web_security; - base::FilePath preload_script; - GURL partition_id; - }; - - class WebViewInfoUserData : public base::SupportsUserData::Data { - public: - explicit WebViewInfoUserData(WebViewInfo info) - : web_view_info_(info) {} - ~WebViewInfoUserData() override {} - - WebViewInfo& web_view_info() { return web_view_info_; } - - private: - WebViewInfo web_view_info_; - }; - - explicit WebViewManager(content::BrowserContext* context); - virtual ~WebViewManager(); + WebViewManager(); + ~WebViewManager() override; void AddGuest(int guest_instance_id, int element_instance_id, @@ -90,8 +59,6 @@ class WebViewManager : public content::BrowserPluginGuestManager { // (embedder_process_id, element_instance_id) => guest_instance_id std::map element_instance_id_to_guest_map_; - base::Lock lock_; - DISALLOW_COPY_AND_ASSIGN(WebViewManager); }; diff --git a/atom/common/options_switches.cc b/atom/common/options_switches.cc index 8f457116ec3..ce5398e447f 100644 --- a/atom/common/options_switches.cc +++ b/atom/common/options_switches.cc @@ -75,6 +75,9 @@ const char kGuestInstanceID[] = "guest-instance-id"; // Script that will be loaded by guest WebContents before other scripts. const char kPreloadScript[] = "preload"; +// Like --preload, but the passed argument is an URL. +const char kPreloadUrl[] = "preload-url"; + // Whether the window should be transparent. const char kTransparent[] = "transparent"; diff --git a/atom/common/options_switches.h b/atom/common/options_switches.h index 2dd48ee32a1..c9298618bdf 100644 --- a/atom/common/options_switches.h +++ b/atom/common/options_switches.h @@ -41,6 +41,7 @@ extern const char kPpapiFlashPath[]; extern const char kPpapiFlashVersion[]; extern const char kGuestInstanceID[]; extern const char kPreloadScript[]; +extern const char kPreloadUrl[]; extern const char kTransparent[]; extern const char kType[]; extern const char kDisableAutoHideCursor[]; diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index e9d2c7f6feb..d74e999379b 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -48,8 +48,6 @@ Properties `width` and `height` are required. * `fullscreen` Boolean - Whether the window should show in fullscreen. When set to `false` the fullscreen button will also be hidden on OS X. * `skip-taskbar` Boolean - Whether to show the window in taskbar. -* `zoom-factor` Number - The default zoom factor of the page, `3.0` represents -`300%`. * `kiosk` Boolean - The kiosk mode. * `title` String - Default window title. * `icon` [NativeImage](native-image.md) - The window icon, when omitted on @@ -57,8 +55,6 @@ Properties `width` and `height` are required. * `show` Boolean - Whether window should be shown when created. * `frame` Boolean - Specify `false` to create a [Frameless Window](frameless-window.md). -* `node-integration` Boolean - Whether node integration is enabled. Default - is `true`. * `accept-first-mouse` Boolean - Whether the web view accepts a single mouse-down event that simultaneously activates the window. * `disable-auto-hide-cursor` Boolean - Whether to hide cursor when typing. @@ -68,10 +64,6 @@ Properties `width` and `height` are required. than screen. * `dark-theme` Boolean - Forces using dark theme for the window, only works on some GTK+3 desktop environments. -* `preload` String - Specifies a script that will be loaded before other - scripts run in the window. This script will always have access to node APIs - no matter whether node integration is turned on for the window, and the path - of `preload` script has to be absolute path. * `transparent` Boolean - Makes the window [transparent](frameless-window.md). * `type` String - Specifies the type of the window, possible types are `desktop`, `dock`, `toolbar`, `splash`, `notification`. This only works on @@ -79,11 +71,25 @@ Properties `width` and `height` are required. * `standard-window` Boolean - Uses the OS X's standard window instead of the textured window. Defaults to `true`. * `web-preferences` Object - Settings of web page's features, properties: + * `node-integration` Boolean - Whether node integration is enabled. Default + is `true`. + * `preload` String - Specifies a script that will be loaded before other + scripts run in the page. This script will always have access to node APIs + no matter whether node integration is turned on for the page, and the path + of `preload` script has to be absolute path. + * `partition` String - Sets the session used by the page. If `partition` + starts with `persist:`, the page will use a persistent session available to + all pages in the app with the same `partition`. if there is no `persist:` + prefix, the page will use an in-memory session. By assigning the same + `partition`, multiple pages can share the same session. If the `partition` + is unset then default session of the app will be used. + * `zoom-factor` Number - The default zoom factor of the page, `3.0` represents + `300%`. * `javascript` Boolean * `web-security` Boolean - When setting `false`, it will disable the - same-origin policy (Usually using testing websites by people), and set `allow_displaying_insecure_content` - and `allow_running_insecure_content` to `true` if these two options are not - set by user. + same-origin policy (Usually using testing websites by people), and set + `allow_displaying_insecure_content` and `allow_running_insecure_content` to + `true` if these two options are not set by user. * `allow-displaying-insecure-content` Boolean - Allow an https page to display content like images from http URLs. * `allow-running-insecure-content` Boolean - Allow a https page to run @@ -93,13 +99,7 @@ Properties `width` and `height` are required. * `text-areas-are-resizable` Boolean * `webgl` Boolean * `webaudio` Boolean - * `plugins` Boolean - Whether plugins should be enabled, currently only - `NPAPI` plugins are supported. - * `extra-plugin-dirs` Array - Array of paths that would be searched for - plugins. Note that if you want to add a directory under your app, you - should use `__dirname` or `process.resourcesPath` to join the paths to - make them absolute, using relative paths would make Electron search - under current working directory. + * `plugins` Boolean - Whether plugins should be enabled. * `experimental-features` Boolean * `experimental-canvas-features` Boolean * `subpixel-font-scaling` Boolean diff --git a/docs/api/web-view-tag.md b/docs/api/web-view-tag.md index c1b1f705e95..85039070216 100644 --- a/docs/api/web-view-tag.md +++ b/docs/api/web-view-tag.md @@ -138,16 +138,16 @@ If "on", the guest page will have web security disabled. ``` -Sets the storage partition used by the `webview`. If the storage partition ID starts with `persist:`, -the `webview` will use a persistent storage partition available to all `webview` in the app with -the same storage partition ID. if there is no `persist:` prefix, the `webview` will -use an in-memory storage partition. By assigning the same partition ID, multiple `webview` -can share the same storage partition. If the storage partition ID is unset then default storage -of the app will be used. +Sets the session used by the page. If `partition` starts with `persist:`, the +page will use a persistent session available to all pages in the app with the +same `partition`. if there is no `persist:` prefix, the page will use an +in-memory session. By assigning the same `partition`, multiple pages can share +the same session. If the `partition` is unset then default session of the app +will be used. -This value can only be modified before the first navigation, since the storage partition of an active -renderer process cannot change. Subsequent attempts to modify the value will fail with a -DOM exception. +This value can only be modified before the first navigation, since the session +of an active renderer process cannot change. Subsequent attempts to modify the +value will fail with a DOM exception. ## Methods diff --git a/filenames.gypi b/filenames.gypi index 2f97d01507e..b4d3bcb5e18 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -226,14 +226,16 @@ 'atom/browser/ui/x/window_state_watcher.h', 'atom/browser/ui/x/x_window_utils.cc', 'atom/browser/ui/x/x_window_utils.h', + 'atom/browser/web_contents_preferences.cc', + 'atom/browser/web_contents_preferences.h', + 'atom/browser/web_dialog_helper.cc', + 'atom/browser/web_dialog_helper.h', 'atom/browser/web_view_constants.cc', 'atom/browser/web_view_constants.h', 'atom/browser/web_view_guest_delegate.cc', 'atom/browser/web_view_guest_delegate.h', 'atom/browser/web_view_manager.cc', 'atom/browser/web_view_manager.h', - 'atom/browser/web_dialog_helper.cc', - 'atom/browser/web_dialog_helper.h', 'atom/browser/window_list.cc', 'atom/browser/window_list.h', 'atom/browser/window_list_observer.h', diff --git a/spec/webview-spec.coffee b/spec/webview-spec.coffee index fe281510b40..3eeacb0ad0e 100644 --- a/spec/webview-spec.coffee +++ b/spec/webview-spec.coffee @@ -160,7 +160,7 @@ describe ' tag', -> assert.equal e.message, 'undefined undefined undefined undefined' done() webview.src = "file://#{fixtures}/pages/c.html" - webview.partition = "test" + webview.partition = 'test1' document.body.appendChild webview it 'inserts node symbols when set', (done) -> @@ -169,7 +169,7 @@ describe ' tag', -> done() webview.setAttribute 'nodeintegration', 'on' webview.src = "file://#{fixtures}/pages/d.html" - webview.partition = "test" + webview.partition = 'test2' document.body.appendChild webview it 'isolates storage for different id', (done) -> @@ -180,7 +180,7 @@ describe ' tag', -> window.localStorage.setItem 'test', 'one' webview.addEventListener 'console-message', listener webview.src = "file://#{fixtures}/pages/partition/one.html" - webview.partition = "test" + webview.partition = 'test3' document.body.appendChild webview it 'uses current session storage when no id is provided', (done) -> diff --git a/vendor/brightray b/vendor/brightray index 4d8f5d879d4..8d64120b51b 160000 --- a/vendor/brightray +++ b/vendor/brightray @@ -1 +1 @@ -Subproject commit 4d8f5d879d484db54895c3456ded3a3d3246415d +Subproject commit 8d64120b51b48be46eaa419957b965c0ccfc6c8f