Merge pull request #12168 from electron/easier-web-contents-preferences

Cleanup the static methods of WebContentsPreferences
This commit is contained in:
Cheng Zhao 2018-03-22 15:58:04 +09:00 committed by GitHub
commit 67f052a6e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 160 additions and 198 deletions

View file

@ -115,14 +115,13 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
// These preferences will be used when the webContent launches new
// render processes.
auto* existing_preferences =
WebContentsPreferences::FromWebContents(web_contents->web_contents());
WebContentsPreferences::From(web_contents->web_contents());
base::DictionaryValue web_preferences_dict;
if (mate::ConvertFromV8(isolate, web_preferences.GetHandle(),
&web_preferences_dict)) {
existing_preferences->web_preferences()->Clear();
existing_preferences->dict()->Clear();
existing_preferences->Merge(web_preferences_dict);
}
} else {
// Creates the WebContents used by BrowserWindow.
web_contents = WebContents::Create(isolate, web_preferences);

View file

@ -1122,10 +1122,9 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
// created after loading a page.
const auto view = web_contents()->GetRenderWidgetHostView();
if (view) {
WebContentsPreferences* web_preferences =
WebContentsPreferences::FromWebContents(web_contents());
auto* web_preferences = WebContentsPreferences::From(web_contents());
std::string color_name;
if (web_preferences->web_preferences()->GetString(options::kBackgroundColor,
if (web_preferences->dict()->GetString(options::kBackgroundColor,
&color_name)) {
view->SetBackgroundColor(ParseHexColor(color_name));
} else {
@ -1840,11 +1839,10 @@ void WebContents::OnGetZoomLevel(content::RenderFrameHost* rfh,
}
v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
WebContentsPreferences* web_preferences =
WebContentsPreferences::FromWebContents(web_contents());
auto* web_preferences = WebContentsPreferences::From(web_contents());
if (!web_preferences)
return v8::Null(isolate);
return mate::ConvertToV8(isolate, *web_preferences->web_preferences());
return mate::ConvertToV8(isolate, *web_preferences->dict());
}
v8::Local<v8::Value> WebContents::GetLastWebPreferences(v8::Isolate* isolate) {
@ -1852,7 +1850,7 @@ v8::Local<v8::Value> WebContents::GetLastWebPreferences(v8::Isolate* isolate) {
WebContentsPreferences::FromWebContents(web_contents());
if (!web_preferences)
return v8::Null(isolate);
return mate::ConvertToV8(isolate, *web_preferences->last_web_preferences());
return mate::ConvertToV8(isolate, *web_preferences->last_dict());
}
v8::Local<v8::Value> WebContents::GetOwnerBrowserWindow() {

View file

@ -34,7 +34,7 @@ void AddGuest(int guest_instance_id,
->SetDefaultZoomFactor(zoom_factor);
}
WebContentsPreferences::FromWebContents(guest_web_contents)->Merge(options);
WebContentsPreferences::From(guest_web_contents)->Merge(options);
}
void RemoveGuest(content::WebContents* embedder, int guest_instance_id) {

View file

@ -173,17 +173,15 @@ 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(
GetWebContentsFromProcessID(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);
}
@ -211,8 +209,10 @@ void AtomBrowserClient::OverrideWebkitPrefs(
prefs->allow_running_insecure_content = false;
// Custom preferences of guest page.
auto web_contents = content::WebContents::FromRenderViewHost(host);
WebContentsPreferences::OverrideWebkitPrefs(web_contents, prefs);
auto* web_contents = content::WebContents::FromRenderViewHost(host);
auto* web_preferences = WebContentsPreferences::From(web_contents);
if (web_preferences)
web_preferences->OverrideWebkitPrefs(prefs);
}
void AtomBrowserClient::OverrideSiteInstanceForNavigation(
@ -235,10 +235,9 @@ void AtomBrowserClient::OverrideSiteInstanceForNavigation(
// Do we have an affinity site to manage ?
std::string affinity;
auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
auto* web_preferences = web_contents ?
WebContentsPreferences::FromWebContents(web_contents) : nullptr;
auto* web_preferences = WebContentsPreferences::From(web_contents);
if (web_preferences &&
web_preferences->web_preferences()->GetString("affinity", &affinity) &&
web_preferences->dict()->GetString("affinity", &affinity) &&
!affinity.empty()) {
affinity = base::ToLowerASCII(affinity);
auto iter = site_per_affinities.find(affinity);
@ -323,8 +322,9 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
content::WebContents* web_contents = GetWebContentsFromProcessID(process_id);
if (web_contents) {
WebContentsPreferences::AppendExtraCommandLineSwitches(
web_contents, command_line);
auto* web_preferences = WebContentsPreferences::From(web_contents);
if (web_preferences)
web_preferences->AppendCommandLineSwitches(command_line);
SessionPreferences::AppendExtraCommandLineSwitches(
web_contents->GetBrowserContext(), command_line);

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

@ -6,6 +6,7 @@
#include <vector>
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/web_contents_preferences.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/permission_type.h"
@ -19,11 +20,12 @@ namespace atom {
namespace {
bool WebContentsDestroyed(int process_id) {
auto contents =
WebContentsPreferences::GetWebContentsFromProcessID(process_id);
if (!contents)
content::WebContents* web_contents =
static_cast<AtomBrowserClient*>(AtomBrowserClient::Get())->
GetWebContentsFromProcessID(process_id);
if (!web_contents)
return true;
return contents->IsBeingDestroyed();
return web_contents->IsBeingDestroyed();
}
void PermissionRequestResponseCallbackWrapper(

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

@ -1370,22 +1370,21 @@ void NativeWindowViews::ShowAutofillPopup(
bool is_offsceen = false;
bool is_embedder_offscreen = false;
auto* web_contents_preferences =
WebContentsPreferences::FromWebContents(web_contents);
if (web_contents_preferences) {
const auto* web_preferences = web_contents_preferences->web_preferences();
web_preferences->GetBoolean("offscreen", &is_offsceen);
auto* web_preferences = WebContentsPreferences::From(web_contents);
if (web_preferences) {
web_preferences->dict()->GetBoolean("offscreen", &is_offsceen);
int guest_instance_id = 0;
web_preferences->GetInteger(options::kGuestInstanceID, &guest_instance_id);
web_preferences->dict()->GetInteger(options::kGuestInstanceID,
&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

@ -45,7 +45,7 @@ WebContentsPreferences::WebContentsPreferences(
copied.Delete("isGuest");
copied.Delete("session");
mate::ConvertFromV8(isolate, copied.GetHandle(), &web_preferences_);
mate::ConvertFromV8(isolate, copied.GetHandle(), &dict_);
web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
instances_.push_back(this);
@ -70,7 +70,8 @@ WebContentsPreferences::WebContentsPreferences(
SetDefaultBoolIfUndefined(options::kScrollBounce, false);
#endif
SetDefaultBoolIfUndefined("offscreen", false);
last_web_preferences_.MergeDictionary(&web_preferences_);
last_dict_ = std::move(*dict_.CreateDeepCopy());
}
WebContentsPreferences::~WebContentsPreferences() {
@ -79,18 +80,25 @@ 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) {
web_preferences_.MergeDictionary(&extend);
dict_.MergeDictionary(&extend);
}
// static
@ -105,69 +113,62 @@ content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID(
}
// static
void WebContentsPreferences::AppendExtraCommandLineSwitches(
content::WebContents* web_contents, base::CommandLine* command_line) {
WebContentsPreferences* self = FromWebContents(web_contents);
if (!self)
return;
base::DictionaryValue& web_preferences = self->web_preferences_;
// We are appending args to a webContents so let's save the current state
// of our preferences object so that during the lifetime of the WebContents
// we can fetch the options used to initally configure the WebContents
self->last_web_preferences_.Clear();
self->last_web_preferences_.MergeDictionary(&web_preferences);
WebContentsPreferences* WebContentsPreferences::From(
content::WebContents* web_contents) {
if (!web_contents)
return nullptr;
return FromWebContents(web_contents);
}
void WebContentsPreferences::AppendCommandLineSwitches(
base::CommandLine* command_line) {
bool b;
// Check if plugins are enabled.
if (web_preferences.GetBoolean("plugins", &b) && b)
if (dict_.GetBoolean("plugins", &b) && b)
command_line->AppendSwitch(switches::kEnablePlugins);
// Experimental flags.
if (web_preferences.GetBoolean(options::kExperimentalFeatures, &b) && b)
if (dict_.GetBoolean(options::kExperimentalFeatures, &b) && b)
command_line->AppendSwitch(
::switches::kEnableExperimentalWebPlatformFeatures);
if (web_preferences.GetBoolean(options::kExperimentalCanvasFeatures, &b) && b)
if (dict_.GetBoolean(options::kExperimentalCanvasFeatures, &b) && b)
command_line->AppendSwitch(::switches::kEnableExperimentalCanvasFeatures);
// Check if we have node integration specified.
bool node_integration = true;
web_preferences.GetBoolean(options::kNodeIntegration, &node_integration);
dict_.GetBoolean(options::kNodeIntegration, &node_integration);
command_line->AppendSwitchASCII(switches::kNodeIntegration,
node_integration ? "true" : "false");
// Whether to enable node integration in Worker.
if (web_preferences.GetBoolean(options::kNodeIntegrationInWorker, &b) && b)
if (dict_.GetBoolean(options::kNodeIntegrationInWorker, &b) && b)
command_line->AppendSwitch(switches::kNodeIntegrationInWorker);
// Check if webview tag creation is enabled, default to nodeIntegration value.
// TODO(kevinsawicki): Default to false in 2.0
bool webview_tag = node_integration;
web_preferences.GetBoolean(options::kWebviewTag, &webview_tag);
dict_.GetBoolean(options::kWebviewTag, &webview_tag);
command_line->AppendSwitchASCII(switches::kWebviewTag,
webview_tag ? "true" : "false");
// If the `sandbox` option was passed to the BrowserWindow's webPreferences,
// pass `--enable-sandbox` to the renderer so it won't have any node.js
// integration.
bool sandbox = false;
if (web_preferences.GetBoolean("sandbox", &sandbox) && sandbox) {
if (dict_.GetBoolean("sandbox", &b) && b)
command_line->AppendSwitch(switches::kEnableSandbox);
} else if (!command_line->HasSwitch(switches::kEnableSandbox)) {
else if (!command_line->HasSwitch(switches::kEnableSandbox))
command_line->AppendSwitch(::switches::kNoSandbox);
}
if (web_preferences.GetBoolean("nativeWindowOpen", &b) && b)
if (dict_.GetBoolean("nativeWindowOpen", &b) && b)
command_line->AppendSwitch(switches::kNativeWindowOpen);
// The preload script.
base::FilePath::StringType preload;
if (web_preferences.GetString(options::kPreloadScript, &preload)) {
if (dict_.GetString(options::kPreloadScript, &preload)) {
if (base::FilePath(preload).IsAbsolute())
command_line->AppendSwitchNative(switches::kPreloadScript, preload);
else
LOG(ERROR) << "preload script must have absolute path.";
} else if (web_preferences.GetString(options::kPreloadURL, &preload)) {
} else if (dict_.GetString(options::kPreloadURL, &preload)) {
// Translate to file path if there is "preload-url" option.
base::FilePath preload_path;
if (net::FileURLToFilePath(GURL(preload), &preload_path))
@ -178,49 +179,44 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
// Custom args for renderer process
base::Value* customArgs;
if ((web_preferences.Get(options::kCustomArgs, &customArgs))
&& (customArgs->is_list())) {
if (dict_.Get(options::kCustomArgs, &customArgs) &&
customArgs->is_list()) {
for (const base::Value& customArg : customArgs->GetList()) {
if (customArg.is_string()) {
if (customArg.is_string())
command_line->AppendArg(customArg.GetString());
}
}
}
// Run Electron APIs and preload script in isolated world
bool isolated;
if (web_preferences.GetBoolean(options::kContextIsolation, &isolated) &&
isolated)
if (dict_.GetBoolean(options::kContextIsolation, &b) && b)
command_line->AppendSwitch(switches::kContextIsolation);
// --background-color.
std::string color;
if (web_preferences.GetString(options::kBackgroundColor, &color))
command_line->AppendSwitchASCII(switches::kBackgroundColor, color);
std::string s;
if (dict_.GetString(options::kBackgroundColor, &s))
command_line->AppendSwitchASCII(switches::kBackgroundColor, s);
// --guest-instance-id, which is used to identify guest WebContents.
int guest_instance_id = 0;
if (web_preferences.GetInteger(options::kGuestInstanceID, &guest_instance_id))
if (dict_.GetInteger(options::kGuestInstanceID, &guest_instance_id))
command_line->AppendSwitchASCII(switches::kGuestInstanceID,
base::IntToString(guest_instance_id));
// Pass the opener's window id.
int opener_id;
if (web_preferences.GetInteger(options::kOpenerID, &opener_id))
if (dict_.GetInteger(options::kOpenerID, &opener_id))
command_line->AppendSwitchASCII(switches::kOpenerID,
base::IntToString(opener_id));
#if defined(OS_MACOSX)
// Enable scroll bounce.
bool scroll_bounce;
if (web_preferences.GetBoolean(options::kScrollBounce, &scroll_bounce) &&
scroll_bounce)
if (dict_.GetBoolean(options::kScrollBounce, &b) && b)
command_line->AppendSwitch(switches::kScrollBounce);
#endif
// Custom command line switches.
const base::ListValue* args;
if (web_preferences.GetList("commandLineSwitches", &args)) {
if (dict_.GetList("commandLineSwitches", &args)) {
for (size_t i = 0; i < args->GetSize(); ++i) {
std::string arg;
if (args->GetString(i, &arg) && !arg.empty())
@ -229,26 +225,21 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
}
// Enable blink features.
std::string blink_features;
if (web_preferences.GetString(options::kBlinkFeatures, &blink_features))
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures,
blink_features);
if (dict_.GetString(options::kBlinkFeatures, &s))
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures, s);
// Disable blink features.
std::string disable_blink_features;
if (web_preferences.GetString(options::kDisableBlinkFeatures,
&disable_blink_features))
command_line->AppendSwitchASCII(::switches::kDisableBlinkFeatures,
disable_blink_features);
if (dict_.GetString(options::kDisableBlinkFeatures, &s))
command_line->AppendSwitchASCII(::switches::kDisableBlinkFeatures, s);
if (guest_instance_id) {
// Webview `document.visibilityState` tracks window visibility so we need
// to let it know if the window happens to be hidden right now.
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) {
auto* relay = NativeWindowRelay::FromWebContents(web_contents);
auto* relay = NativeWindowRelay::FromWebContents(embedder);
if (relay) {
auto* window = relay->window.get();
if (window) {
@ -261,51 +252,34 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
}
}
}
// We are appending args to a webContents so let's save the current state
// of our preferences object so that during the lifetime of the WebContents
// we can fetch the options used to initally configure the WebContents
last_dict_ = std::move(*dict_.CreateDeepCopy());
}
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->web_preferences_;
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) {
WebContentsPreferences* self = FromWebContents(web_contents);
if (!self)
return;
content::WebPreferences* prefs) {
bool b;
if (self->web_preferences_.GetBoolean("javascript", &b))
if (dict_.GetBoolean("javascript", &b))
prefs->javascript_enabled = b;
if (self->web_preferences_.GetBoolean("images", &b))
if (dict_.GetBoolean("images", &b))
prefs->images_enabled = b;
if (self->web_preferences_.GetBoolean("textAreasAreResizable", &b))
if (dict_.GetBoolean("textAreasAreResizable", &b))
prefs->text_areas_are_resizable = b;
if (self->web_preferences_.GetBoolean("webgl", &b)) {
if (dict_.GetBoolean("webgl", &b)) {
prefs->webgl1_enabled = b;
prefs->webgl2_enabled = b;
}
if (self->web_preferences_.GetBoolean("webSecurity", &b)) {
if (dict_.GetBoolean("webSecurity", &b)) {
prefs->web_security_enabled = b;
prefs->allow_running_insecure_content = !b;
}
if (self->web_preferences_.GetBoolean("allowRunningInsecureContent", &b))
if (dict_.GetBoolean("allowRunningInsecureContent", &b))
prefs->allow_running_insecure_content = b;
const base::DictionaryValue* fonts = nullptr;
if (self->web_preferences_.GetDictionary("defaultFontFamily", &fonts)) {
if (dict_.GetDictionary("defaultFontFamily", &fonts)) {
base::string16 font;
if (fonts->GetString("standard", &font))
prefs->standard_font_family_map[content::kCommonScript] = font;
@ -321,37 +295,28 @@ void WebContentsPreferences::OverrideWebkitPrefs(
prefs->fantasy_font_family_map[content::kCommonScript] = font;
}
int size;
if (self->GetInteger("defaultFontSize", &size))
if (GetInteger("defaultFontSize", &size))
prefs->default_font_size = size;
if (self->GetInteger("defaultMonospaceFontSize", &size))
if (GetInteger("defaultMonospaceFontSize", &size))
prefs->default_fixed_font_size = size;
if (self->GetInteger("minimumFontSize", &size))
if (GetInteger("minimumFontSize", &size))
prefs->minimum_font_size = size;
std::string encoding;
if (self->web_preferences_.GetString("defaultEncoding", &encoding))
if (dict_.GetString("defaultEncoding", &encoding))
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 (web_preferences_.GetInteger(attributeName, intValue))
if (dict_.GetInteger(attribute_name, val))
return true;
base::string16 stringValue;
if (web_preferences_.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->web_preferences()->GetString(attribute_name, string_value);
}
} // namespace atom

View file

@ -29,52 +29,49 @@ namespace atom {
class WebContentsPreferences
: public content::WebContentsUserData<WebContentsPreferences> {
public:
// Get WebContents according to process ID.
// FIXME(zcbenz): This method does not belong here.
static content::WebContents* GetWebContentsFromProcessID(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);
// Get self from WebContents.
static WebContentsPreferences* From(content::WebContents* web_contents);
WebContentsPreferences(content::WebContents* web_contents,
const mate::Dictionary& web_preferences);
~WebContentsPreferences() override;
// $.extend(|web_preferences_|, |new_web_preferences|).
// 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);
// Append command paramters according to preferences.
void AppendCommandLineSwitches(base::CommandLine* command_line);
// Modify the WebPreferences according to preferences.
void OverrideWebkitPrefs(content::WebPreferences* prefs);
// Returns the web preferences.
base::DictionaryValue* web_preferences() { return &web_preferences_; }
base::DictionaryValue* last_web_preferences() {
return &last_web_preferences_;
}
base::DictionaryValue* dict() { return &dict_; }
const base::DictionaryValue* dict() const { return &dict_; }
base::DictionaryValue* last_dict() { return &last_dict_; }
private:
friend class content::WebContentsUserData<WebContentsPreferences>;
friend class AtomBrowserClient;
// Get WebContents according to process ID.
static content::WebContents* GetWebContentsFromProcessID(int process_id);
// 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_;
content::WebContents* web_contents_;
base::DictionaryValue web_preferences_;
base::DictionaryValue last_web_preferences_;
base::DictionaryValue dict_;
base::DictionaryValue last_dict_;
DISALLOW_COPY_AND_ASSIGN(WebContentsPreferences);
};