diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index f96f3ce6d31f..5a6aa19a56e7 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -173,17 +173,14 @@ void AtomBrowserClient::RenderProcessWillLaunch( host->AddFilter( new WidevineCdmMessageFilter(process_id, host->GetBrowserContext())); - content::WebContents* web_contents = GetWebContentsFromProcessID(process_id); - ProcessPreferences process_prefs; - process_prefs.sandbox = - WebContentsPreferences::IsPreferenceEnabled("sandbox", web_contents); - process_prefs.native_window_open = - WebContentsPreferences::IsPreferenceEnabled("nativeWindowOpen", - web_contents); - process_prefs.disable_popups = - WebContentsPreferences::IsPreferenceEnabled("disablePopups", - web_contents); - AddProcessPreferences(host->GetID(), process_prefs); + ProcessPreferences prefs; + auto* web_preferences = WebContentsPreferences::From(process_id); + if (web_preferences) { + prefs.sandbox = web_preferences->IsEnabled("sandbox"); + prefs.native_window_open = web_preferences->IsEnabled("nativeWindowOpen"); + prefs.disable_popups = web_preferences->IsEnabled("disablePopups"); + } + AddProcessPreferences(host->GetID(), prefs); // ensure the ProcessPreferences is removed later host->AddObserver(this); } diff --git a/atom/browser/atom_browser_client.h b/atom/browser/atom_browser_client.h index ceacd354ab3d..38b412dc6de1 100644 --- a/atom/browser/atom_browser_client.h +++ b/atom/browser/atom_browser_client.h @@ -118,15 +118,16 @@ class AtomBrowserClient : public brightray::BrowserClient, int exit_code) override; private: - bool ShouldCreateNewSiteInstance(content::RenderFrameHost* render_frame_host, - content::BrowserContext* browser_context, - content::SiteInstance* current_instance, - const GURL& dest_url); struct ProcessPreferences { bool sandbox = false; bool native_window_open = false; bool disable_popups = false; }; + + bool ShouldCreateNewSiteInstance(content::RenderFrameHost* render_frame_host, + content::BrowserContext* browser_context, + content::SiteInstance* current_instance, + const GURL& dest_url); void AddProcessPreferences(int process_id, ProcessPreferences prefs); void RemoveProcessPreferences(int process_id); bool IsProcessObserved(int process_id); diff --git a/atom/browser/atom_javascript_dialog_manager.cc b/atom/browser/atom_javascript_dialog_manager.cc index 8e80e58a94c6..16eab2989dbf 100644 --- a/atom/browser/atom_javascript_dialog_manager.cc +++ b/atom/browser/atom_javascript_dialog_manager.cc @@ -55,13 +55,13 @@ void AtomJavaScriptDialogManager::RunJavaScriptDialog( origin_counts_[origin]++; - std::string checkbox_string; - if (origin_counts_[origin] > 1 && - WebContentsPreferences::IsPreferenceEnabled("safeDialogs", - web_contents)) { - if (!WebContentsPreferences::GetString("safeDialogsMessage", - &checkbox_string, web_contents)) { - checkbox_string = "Prevent this app from creating additional dialogs"; + std::string checkbox; + if (origin_counts_[origin] > 1) { + auto* web_preferences = WebContentsPreferences::From(web_contents); + if (web_preferences && + web_preferences->IsEnabled("safeDialogs") && + !web_preferences->dict()->GetString("safeDialogsMessage", &checkbox)) { + checkbox = "Prevent this app from creating additional dialogs"; } } @@ -70,7 +70,7 @@ void AtomJavaScriptDialogManager::RunJavaScriptDialog( relay ? relay->window.get() : nullptr, atom::MessageBoxType::MESSAGE_BOX_TYPE_NONE, buttons, -1, 0, atom::MessageBoxOptions::MESSAGE_BOX_NONE, "", - base::UTF16ToUTF8(message_text), "", checkbox_string, + base::UTF16ToUTF8(message_text), "", checkbox, false, gfx::ImageSkia(), base::Bind(&AtomJavaScriptDialogManager::OnMessageBoxCallback, base::Unretained(this), diff --git a/atom/browser/atom_resource_dispatcher_host_delegate.cc b/atom/browser/atom_resource_dispatcher_host_delegate.cc index d53adc9446ee..046f6e45f583 100644 --- a/atom/browser/atom_resource_dispatcher_host_delegate.cc +++ b/atom/browser/atom_resource_dispatcher_host_delegate.cc @@ -80,9 +80,10 @@ void OnPdfResourceIntercepted( if (!web_contents) return; - if (!WebContentsPreferences::IsPreferenceEnabled("plugins", web_contents)) { - auto browser_context = web_contents->GetBrowserContext(); - auto download_manager = + auto* web_preferences = WebContentsPreferences::From(web_contents); + if (!web_preferences || !web_preferences->IsEnabled("plugins")) { + auto* browser_context = web_contents->GetBrowserContext(); + auto* download_manager = content::BrowserContext::GetDownloadManager(browser_context); download_manager->DownloadUrl( diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index c824d0a76d1c..602bb01b665a 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -1378,12 +1378,13 @@ void NativeWindowViews::ShowAutofillPopup( &guest_instance_id); if (guest_instance_id) { - auto manager = WebViewManager::GetWebViewManager(web_contents); + auto* manager = WebViewManager::GetWebViewManager(web_contents); if (manager) { - auto embedder = manager->GetEmbedder(guest_instance_id); + auto* embedder = manager->GetEmbedder(guest_instance_id); if (embedder) { - is_embedder_offscreen = WebContentsPreferences::IsPreferenceEnabled( - "offscreen", embedder); + auto* embedder_prefs = WebContentsPreferences::From(embedder); + is_embedder_offscreen = embedder_prefs && + embedder_prefs->IsEnabled("offscreen"); } } } diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc index e45a6167c99b..02bea454fb91 100644 --- a/atom/browser/web_contents_preferences.cc +++ b/atom/browser/web_contents_preferences.cc @@ -80,16 +80,23 @@ WebContentsPreferences::~WebContentsPreferences() { instances_.end()); } -bool WebContentsPreferences::SetDefaultBoolIfUndefined(const std::string key, - bool val) { +bool WebContentsPreferences::SetDefaultBoolIfUndefined( + const base::StringPiece& key, bool val) { bool existing; - if (!web_preferences_.GetBoolean(key, &existing)) { - web_preferences_.SetBoolean(key, val); + if (!dict_.GetBoolean(key, &existing)) { + dict_.SetBoolean(key, val); return val; } return existing; } +bool WebContentsPreferences::IsEnabled(const base::StringPiece& name, + bool default_value) { + bool bool_value = default_value; + dict_.GetBoolean(name, &bool_value); + return bool_value; +} + void WebContentsPreferences::Merge(const base::DictionaryValue& extend) { dict_.MergeDictionary(&extend); } @@ -113,6 +120,11 @@ WebContentsPreferences* WebContentsPreferences::From( return FromWebContents(web_contents); } +// static +WebContentsPreferences* WebContentsPreferences::From(int process_id) { + return From(GetWebContentsFromProcessID(process_id)); +} + // static void WebContentsPreferences::AppendExtraCommandLineSwitches( content::WebContents* web_contents, base::CommandLine* command_line) { @@ -272,23 +284,6 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches( } } -bool WebContentsPreferences::IsPreferenceEnabled( - const std::string& attribute_name, - content::WebContents* web_contents) { - WebContentsPreferences* self; - if (!web_contents) - return false; - - self = FromWebContents(web_contents); - if (!self) - return false; - - base::DictionaryValue& web_preferences = self->dict_; - bool bool_value = false; - web_preferences.GetBoolean(attribute_name, &bool_value); - return bool_value; -} - // static void WebContentsPreferences::OverrideWebkitPrefs( content::WebContents* web_contents, content::WebPreferences* prefs) { @@ -303,17 +298,11 @@ void WebContentsPreferences::OverrideWebkitPrefs( prefs->images_enabled = b; if (self->dict_.GetBoolean("textAreasAreResizable", &b)) prefs->text_areas_are_resizable = b; -<<<<<<< HEAD - if (self->web_preferences_.GetBoolean("webgl", &b)) { + if (self->dict_.GetBoolean("webgl", &b)) { prefs->webgl1_enabled = b; prefs->webgl2_enabled = b; } - if (self->web_preferences_.GetBoolean("webSecurity", &b)) { -======= - if (self->dict_.GetBoolean("webgl", &b)) - prefs->experimental_webgl_enabled = b; if (self->dict_.GetBoolean("webSecurity", &b)) { ->>>>>>> web_prefrences() => dict() prefs->web_security_enabled = b; prefs->allow_running_insecure_content = !b; } @@ -347,26 +336,17 @@ void WebContentsPreferences::OverrideWebkitPrefs( prefs->default_encoding = encoding; } -bool WebContentsPreferences::GetInteger(const std::string& attributeName, - int* intValue) { +bool WebContentsPreferences::GetInteger(const base::StringPiece& attribute_name, + int* val) { // if it is already an integer, no conversion needed - if (dict_.GetInteger(attributeName, intValue)) + if (dict_.GetInteger(attribute_name, val)) return true; - base::string16 stringValue; - if (dict_.GetString(attributeName, &stringValue)) - return base::StringToInt(stringValue, intValue); + std::string str; + if (dict_.GetString(attribute_name, &str)) + return base::StringToInt(str, val); return false; } -bool WebContentsPreferences::GetString(const std::string& attribute_name, - std::string* string_value, - content::WebContents* web_contents) { - WebContentsPreferences* self = FromWebContents(web_contents); - if (!self) - return false; - return self->dict()->GetString(attribute_name, string_value); -} - } // namespace atom diff --git a/atom/browser/web_contents_preferences.h b/atom/browser/web_contents_preferences.h index 50165bbc43e3..ab2837d983d4 100644 --- a/atom/browser/web_contents_preferences.h +++ b/atom/browser/web_contents_preferences.h @@ -35,18 +35,13 @@ class WebContentsPreferences // Get self from WebContents. static WebContentsPreferences* From(content::WebContents* web_contents); + // Get self from procese ID. + static WebContentsPreferences* From(int process_id); // Append command paramters according to |web_contents|'s preferences. static void AppendExtraCommandLineSwitches( content::WebContents* web_contents, base::CommandLine* command_line); - static bool IsPreferenceEnabled(const std::string& attribute_name, - content::WebContents* web_contents); - - static bool GetString(const std::string& attribute_name, - std::string* string_value, - content::WebContents* web_contents); - // Modify the WebPreferences according to |web_contents|'s preferences. static void OverrideWebkitPrefs( content::WebContents* web_contents, content::WebPreferences* prefs); @@ -55,6 +50,9 @@ class WebContentsPreferences const mate::Dictionary& web_preferences); ~WebContentsPreferences() override; + // A simple way to know whether a Boolean property is enabled. + bool IsEnabled(const base::StringPiece& name, bool default_value = false); + // $.extend(|web_preferences|, |new_web_preferences|). void Merge(const base::DictionaryValue& new_web_preferences); @@ -67,10 +65,10 @@ class WebContentsPreferences friend class content::WebContentsUserData; // Set preference value to given bool if user did not provide value - bool SetDefaultBoolIfUndefined(const std::string key, bool val); + bool SetDefaultBoolIfUndefined(const base::StringPiece& key, bool val); // Get preferences value as integer possibly coercing it from a string - bool GetInteger(const std::string& attributeName, int* intValue); + bool GetInteger(const base::StringPiece& attribute_name, int* val); static std::vector instances_;