Remove the static getter methods from WebContentsPreferences

This commit is contained in:
Cheng Zhao 2018-03-08 16:36:21 +09:00
parent 887bc12350
commit 3d47a8a2fd
7 changed files with 60 additions and 82 deletions

View file

@ -173,17 +173,14 @@ void AtomBrowserClient::RenderProcessWillLaunch(
host->AddFilter( host->AddFilter(
new WidevineCdmMessageFilter(process_id, host->GetBrowserContext())); new WidevineCdmMessageFilter(process_id, host->GetBrowserContext()));
content::WebContents* web_contents = GetWebContentsFromProcessID(process_id); ProcessPreferences prefs;
ProcessPreferences process_prefs; auto* web_preferences = WebContentsPreferences::From(process_id);
process_prefs.sandbox = if (web_preferences) {
WebContentsPreferences::IsPreferenceEnabled("sandbox", web_contents); prefs.sandbox = web_preferences->IsEnabled("sandbox");
process_prefs.native_window_open = prefs.native_window_open = web_preferences->IsEnabled("nativeWindowOpen");
WebContentsPreferences::IsPreferenceEnabled("nativeWindowOpen", prefs.disable_popups = web_preferences->IsEnabled("disablePopups");
web_contents); }
process_prefs.disable_popups = AddProcessPreferences(host->GetID(), prefs);
WebContentsPreferences::IsPreferenceEnabled("disablePopups",
web_contents);
AddProcessPreferences(host->GetID(), process_prefs);
// ensure the ProcessPreferences is removed later // ensure the ProcessPreferences is removed later
host->AddObserver(this); host->AddObserver(this);
} }

View file

@ -118,15 +118,16 @@ class AtomBrowserClient : public brightray::BrowserClient,
int exit_code) override; int exit_code) override;
private: private:
bool ShouldCreateNewSiteInstance(content::RenderFrameHost* render_frame_host,
content::BrowserContext* browser_context,
content::SiteInstance* current_instance,
const GURL& dest_url);
struct ProcessPreferences { struct ProcessPreferences {
bool sandbox = false; bool sandbox = false;
bool native_window_open = false; bool native_window_open = false;
bool disable_popups = 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 AddProcessPreferences(int process_id, ProcessPreferences prefs);
void RemoveProcessPreferences(int process_id); void RemoveProcessPreferences(int process_id);
bool IsProcessObserved(int process_id); bool IsProcessObserved(int process_id);

View file

@ -55,13 +55,13 @@ void AtomJavaScriptDialogManager::RunJavaScriptDialog(
origin_counts_[origin]++; origin_counts_[origin]++;
std::string checkbox_string; std::string checkbox;
if (origin_counts_[origin] > 1 && if (origin_counts_[origin] > 1) {
WebContentsPreferences::IsPreferenceEnabled("safeDialogs", auto* web_preferences = WebContentsPreferences::From(web_contents);
web_contents)) { if (web_preferences &&
if (!WebContentsPreferences::GetString("safeDialogsMessage", web_preferences->IsEnabled("safeDialogs") &&
&checkbox_string, web_contents)) { !web_preferences->dict()->GetString("safeDialogsMessage", &checkbox)) {
checkbox_string = "Prevent this app from creating additional dialogs"; checkbox = "Prevent this app from creating additional dialogs";
} }
} }
@ -70,7 +70,7 @@ void AtomJavaScriptDialogManager::RunJavaScriptDialog(
relay ? relay->window.get() : nullptr, relay ? relay->window.get() : nullptr,
atom::MessageBoxType::MESSAGE_BOX_TYPE_NONE, buttons, -1, 0, atom::MessageBoxType::MESSAGE_BOX_TYPE_NONE, buttons, -1, 0,
atom::MessageBoxOptions::MESSAGE_BOX_NONE, "", atom::MessageBoxOptions::MESSAGE_BOX_NONE, "",
base::UTF16ToUTF8(message_text), "", checkbox_string, base::UTF16ToUTF8(message_text), "", checkbox,
false, gfx::ImageSkia(), false, gfx::ImageSkia(),
base::Bind(&AtomJavaScriptDialogManager::OnMessageBoxCallback, base::Bind(&AtomJavaScriptDialogManager::OnMessageBoxCallback,
base::Unretained(this), base::Unretained(this),

View file

@ -80,9 +80,10 @@ void OnPdfResourceIntercepted(
if (!web_contents) if (!web_contents)
return; return;
if (!WebContentsPreferences::IsPreferenceEnabled("plugins", web_contents)) { auto* web_preferences = WebContentsPreferences::From(web_contents);
auto browser_context = web_contents->GetBrowserContext(); if (!web_preferences || !web_preferences->IsEnabled("plugins")) {
auto download_manager = auto* browser_context = web_contents->GetBrowserContext();
auto* download_manager =
content::BrowserContext::GetDownloadManager(browser_context); content::BrowserContext::GetDownloadManager(browser_context);
download_manager->DownloadUrl( download_manager->DownloadUrl(

View file

@ -1378,12 +1378,13 @@ void NativeWindowViews::ShowAutofillPopup(
&guest_instance_id); &guest_instance_id);
if (guest_instance_id) { if (guest_instance_id) {
auto manager = WebViewManager::GetWebViewManager(web_contents); auto* manager = WebViewManager::GetWebViewManager(web_contents);
if (manager) { if (manager) {
auto embedder = manager->GetEmbedder(guest_instance_id); auto* embedder = manager->GetEmbedder(guest_instance_id);
if (embedder) { if (embedder) {
is_embedder_offscreen = WebContentsPreferences::IsPreferenceEnabled( auto* embedder_prefs = WebContentsPreferences::From(embedder);
"offscreen", embedder); is_embedder_offscreen = embedder_prefs &&
embedder_prefs->IsEnabled("offscreen");
} }
} }
} }

View file

@ -80,16 +80,23 @@ WebContentsPreferences::~WebContentsPreferences() {
instances_.end()); instances_.end());
} }
bool WebContentsPreferences::SetDefaultBoolIfUndefined(const std::string key, bool WebContentsPreferences::SetDefaultBoolIfUndefined(
bool val) { const base::StringPiece& key, bool val) {
bool existing; bool existing;
if (!web_preferences_.GetBoolean(key, &existing)) { if (!dict_.GetBoolean(key, &existing)) {
web_preferences_.SetBoolean(key, val); dict_.SetBoolean(key, val);
return val; return val;
} }
return existing; 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) { void WebContentsPreferences::Merge(const base::DictionaryValue& extend) {
dict_.MergeDictionary(&extend); dict_.MergeDictionary(&extend);
} }
@ -113,6 +120,11 @@ WebContentsPreferences* WebContentsPreferences::From(
return FromWebContents(web_contents); return FromWebContents(web_contents);
} }
// static
WebContentsPreferences* WebContentsPreferences::From(int process_id) {
return From(GetWebContentsFromProcessID(process_id));
}
// static // static
void WebContentsPreferences::AppendExtraCommandLineSwitches( void WebContentsPreferences::AppendExtraCommandLineSwitches(
content::WebContents* web_contents, base::CommandLine* command_line) { 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 // static
void WebContentsPreferences::OverrideWebkitPrefs( void WebContentsPreferences::OverrideWebkitPrefs(
content::WebContents* web_contents, content::WebPreferences* prefs) { content::WebContents* web_contents, content::WebPreferences* prefs) {
@ -303,17 +298,11 @@ void WebContentsPreferences::OverrideWebkitPrefs(
prefs->images_enabled = b; prefs->images_enabled = b;
if (self->dict_.GetBoolean("textAreasAreResizable", &b)) if (self->dict_.GetBoolean("textAreasAreResizable", &b))
prefs->text_areas_are_resizable = b; prefs->text_areas_are_resizable = b;
<<<<<<< HEAD if (self->dict_.GetBoolean("webgl", &b)) {
if (self->web_preferences_.GetBoolean("webgl", &b)) {
prefs->webgl1_enabled = b; prefs->webgl1_enabled = b;
prefs->webgl2_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)) { if (self->dict_.GetBoolean("webSecurity", &b)) {
>>>>>>> web_prefrences() => dict()
prefs->web_security_enabled = b; prefs->web_security_enabled = b;
prefs->allow_running_insecure_content = !b; prefs->allow_running_insecure_content = !b;
} }
@ -347,26 +336,17 @@ void WebContentsPreferences::OverrideWebkitPrefs(
prefs->default_encoding = encoding; prefs->default_encoding = encoding;
} }
bool WebContentsPreferences::GetInteger(const std::string& attributeName, bool WebContentsPreferences::GetInteger(const base::StringPiece& attribute_name,
int* intValue) { int* val) {
// if it is already an integer, no conversion needed // if it is already an integer, no conversion needed
if (dict_.GetInteger(attributeName, intValue)) if (dict_.GetInteger(attribute_name, val))
return true; return true;
base::string16 stringValue; std::string str;
if (dict_.GetString(attributeName, &stringValue)) if (dict_.GetString(attribute_name, &str))
return base::StringToInt(stringValue, intValue); return base::StringToInt(str, val);
return false; 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 } // namespace atom

View file

@ -35,18 +35,13 @@ class WebContentsPreferences
// Get self from WebContents. // Get self from WebContents.
static WebContentsPreferences* From(content::WebContents* web_contents); 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. // Append command paramters according to |web_contents|'s preferences.
static void AppendExtraCommandLineSwitches( static void AppendExtraCommandLineSwitches(
content::WebContents* web_contents, base::CommandLine* command_line); 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. // Modify the WebPreferences according to |web_contents|'s preferences.
static void OverrideWebkitPrefs( static void OverrideWebkitPrefs(
content::WebContents* web_contents, content::WebPreferences* prefs); content::WebContents* web_contents, content::WebPreferences* prefs);
@ -55,6 +50,9 @@ class WebContentsPreferences
const mate::Dictionary& web_preferences); const mate::Dictionary& web_preferences);
~WebContentsPreferences() override; ~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|). // $.extend(|web_preferences|, |new_web_preferences|).
void Merge(const base::DictionaryValue& new_web_preferences); void Merge(const base::DictionaryValue& new_web_preferences);
@ -67,10 +65,10 @@ class WebContentsPreferences
friend class content::WebContentsUserData<WebContentsPreferences>; friend class content::WebContentsUserData<WebContentsPreferences>;
// Set preference value to given bool if user did not provide value // 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 // 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<WebContentsPreferences*> instances_; static std::vector<WebContentsPreferences*> instances_;