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

View file

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

View file

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

View file

@ -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(

View file

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

View file

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

View file

@ -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<WebContentsPreferences>;
// 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<WebContentsPreferences*> instances_;