refactor: remove base::Value from WebContentsPreferences (#30193)

This commit is contained in:
Jeremy Rose 2021-07-26 09:04:09 -07:00 committed by GitHub
parent 64ba8feb93
commit 385d0f590f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 433 additions and 415 deletions

View file

@ -369,8 +369,7 @@ void BrowserWindow::SetBackgroundColor(const std::string& color_name) {
auto* web_preferences = auto* web_preferences =
WebContentsPreferences::From(api_web_contents_->web_contents()); WebContentsPreferences::From(api_web_contents_->web_contents());
if (web_preferences) { if (web_preferences) {
web_preferences->preference()->SetStringKey(options::kBackgroundColor, web_preferences->SetBackgroundColor(ParseHexColor(color_name));
color_name);
} }
} }
} }

View file

@ -908,8 +908,7 @@ void WebContents::InitWithWebContents(content::WebContents* web_contents,
// Determine whether the WebContents is offscreen. // Determine whether the WebContents is offscreen.
auto* web_preferences = WebContentsPreferences::From(web_contents); auto* web_preferences = WebContentsPreferences::From(web_contents);
offscreen_ = offscreen_ = web_preferences && web_preferences->IsOffscreen();
web_preferences && web_preferences->IsEnabled(options::kOffscreen);
// Create InspectableWebContents. // Create InspectableWebContents.
inspectable_web_contents_ = std::make_unique<InspectableWebContents>( inspectable_web_contents_ = std::make_unique<InspectableWebContents>(
@ -1193,8 +1192,7 @@ bool WebContents::PlatformHandleKeyboardEvent(
// Check if the webContents has preferences and to ignore shortcuts // Check if the webContents has preferences and to ignore shortcuts
auto* web_preferences = WebContentsPreferences::From(source); auto* web_preferences = WebContentsPreferences::From(source);
if (web_preferences && if (web_preferences && web_preferences->ShouldIgnoreMenuShortcuts())
web_preferences->IsEnabled("ignoreMenuShortcuts", false))
return false; return false;
// Let the NativeWindow handle other parts. // Let the NativeWindow handle other parts.
@ -1378,12 +1376,7 @@ void WebContents::HandleNewRenderFrame(
auto* web_preferences = WebContentsPreferences::From(web_contents()); auto* web_preferences = WebContentsPreferences::From(web_contents());
if (web_preferences) { if (web_preferences) {
std::string color_name; std::string color_name;
if (web_preferences->GetPreference(options::kBackgroundColor, rwhv->SetBackgroundColor(web_preferences->GetBackgroundColor());
&color_name)) {
rwhv->SetBackgroundColor(ParseHexColor(color_name));
} else {
rwhv->SetBackgroundColor(SK_ColorTRANSPARENT);
}
} }
if (!background_throttling_) if (!background_throttling_)
@ -1514,8 +1507,7 @@ void WebContents::DidStartLoading() {
void WebContents::DidStopLoading() { void WebContents::DidStopLoading() {
auto* web_preferences = WebContentsPreferences::From(web_contents()); auto* web_preferences = WebContentsPreferences::From(web_contents());
if (web_preferences && if (web_preferences && web_preferences->ShouldUsePreferredSizeMode())
web_preferences->IsEnabled(options::kEnablePreferredSizeMode))
web_contents()->GetRenderViewHost()->EnablePreferredSizeMode(); web_contents()->GetRenderViewHost()->EnablePreferredSizeMode();
Emit("did-stop-loading"); Emit("did-stop-loading");
@ -2344,8 +2336,7 @@ void WebContents::InspectServiceWorker() {
void WebContents::SetIgnoreMenuShortcuts(bool ignore) { void WebContents::SetIgnoreMenuShortcuts(bool ignore) {
auto* web_preferences = WebContentsPreferences::From(web_contents()); auto* web_preferences = WebContentsPreferences::From(web_contents());
DCHECK(web_preferences); DCHECK(web_preferences);
web_preferences->preference()->SetKey("ignoreMenuShortcuts", web_preferences->SetIgnoreMenuShortcuts(ignore);
base::Value(ignore));
} }
void WebContents::SetAudioMuted(bool muted) { void WebContents::SetAudioMuted(bool muted) {
@ -3141,8 +3132,7 @@ void WebContents::NotifyUserActivation() {
void WebContents::SetImageAnimationPolicy(const std::string& new_policy) { void WebContents::SetImageAnimationPolicy(const std::string& new_policy) {
auto* web_preferences = WebContentsPreferences::From(web_contents()); auto* web_preferences = WebContentsPreferences::From(web_contents());
web_preferences->preference()->SetKey(options::kImageAnimationPolicy, web_preferences->SetImageAnimationPolicy(new_policy);
base::Value(new_policy));
web_contents()->OnWebPreferencesChanged(); web_contents()->OnWebPreferencesChanged();
} }
@ -3561,9 +3551,9 @@ void WebContents::SetHtmlApiFullscreen(bool enter_fullscreen) {
// Set fullscreen on window if allowed. // Set fullscreen on window if allowed.
auto* web_preferences = WebContentsPreferences::From(GetWebContents()); auto* web_preferences = WebContentsPreferences::From(GetWebContents());
bool html_fullscreenable = bool html_fullscreenable =
web_preferences ? !web_preferences->IsEnabled( web_preferences
options::kDisableHtmlFullscreenWindowResize) ? !web_preferences->ShouldDisableHtmlFullscreenWindowResize()
: true; : true;
if (html_fullscreenable) { if (html_fullscreenable) {
owner_window_->SetFullScreen(enter_fullscreen); owner_window_->SetFullScreen(enter_fullscreen);
@ -3818,11 +3808,10 @@ gin::Handle<WebContents> WebContents::CreateFromWebPreferences(
// render processes. // render processes.
auto* existing_preferences = auto* existing_preferences =
WebContentsPreferences::From(web_contents->web_contents()); WebContentsPreferences::From(web_contents->web_contents());
base::DictionaryValue web_preferences_dict; gin_helper::Dictionary web_preferences_dict;
if (gin::ConvertFromV8(isolate, web_preferences.GetHandle(), if (gin::ConvertFromV8(isolate, web_preferences.GetHandle(),
&web_preferences_dict)) { &web_preferences_dict)) {
existing_preferences->Clear(); existing_preferences->SetFromDictionary(web_preferences_dict);
existing_preferences->Merge(web_preferences_dict);
} }
} else { } else {
// Create one if not. // Create one if not.

View file

@ -50,8 +50,7 @@ bool WebContents::PlatformHandleKeyboardEvent(
// Check if the webContents has preferences and to ignore shortcuts // Check if the webContents has preferences and to ignore shortcuts
auto* web_preferences = WebContentsPreferences::From(source); auto* web_preferences = WebContentsPreferences::From(source);
if (web_preferences && if (web_preferences && web_preferences->ShouldIgnoreMenuShortcuts())
web_preferences->IsEnabled("ignoreMenuShortcuts", false))
return false; return false;
// Send the event to the menu before sending it to the window // Send the event to the menu before sending it to the window

View file

@ -7,7 +7,6 @@
#include "shell/browser/web_contents_zoom_controller.h" #include "shell/browser/web_contents_zoom_controller.h"
#include "shell/browser/web_view_manager.h" #include "shell/browser/web_view_manager.h"
#include "shell/common/gin_converters/content_converter.h" #include "shell/common/gin_converters/content_converter.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h" #include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h" #include "shell/common/node_includes.h"
#include "shell/common/options_switches.h" #include "shell/common/options_switches.h"
@ -19,13 +18,13 @@ namespace {
void AddGuest(int guest_instance_id, void AddGuest(int guest_instance_id,
content::WebContents* embedder, content::WebContents* embedder,
content::WebContents* guest_web_contents, content::WebContents* guest_web_contents,
const base::DictionaryValue& options) { const gin_helper::Dictionary& options) {
auto* manager = electron::WebViewManager::GetWebViewManager(embedder); auto* manager = electron::WebViewManager::GetWebViewManager(embedder);
if (manager) if (manager)
manager->AddGuest(guest_instance_id, embedder, guest_web_contents); manager->AddGuest(guest_instance_id, embedder, guest_web_contents);
double zoom_factor; double zoom_factor;
if (options.GetDouble(electron::options::kZoomFactor, &zoom_factor)) { if (options.Get(electron::options::kZoomFactor, &zoom_factor)) {
electron::WebContentsZoomController::FromWebContents(guest_web_contents) electron::WebContentsZoomController::FromWebContents(guest_web_contents)
->SetDefaultZoomFactor(zoom_factor); ->SetDefaultZoomFactor(zoom_factor);
} }

View file

@ -707,8 +707,8 @@ bool ElectronBrowserClient::CanCreateWindow(
content::WebContents* web_contents = content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(opener); content::WebContents::FromRenderFrameHost(opener);
WebContentsPreferences* prefs = WebContentsPreferences::From(web_contents); WebContentsPreferences* prefs = WebContentsPreferences::From(web_contents);
if (prefs && prefs->IsEnabled(options::kNativeWindowOpen)) { if (prefs && prefs->ShouldUseNativeWindowOpen()) {
if (prefs->IsEnabled("disablePopups")) { if (prefs->ShouldDisablePopups()) {
// <webview> without allowpopups attribute should return // <webview> without allowpopups attribute should return
// null from window.open calls // null from window.open calls
return false; return false;
@ -1411,7 +1411,7 @@ void ElectronBrowserClient::OverrideURLLoaderFactoryParams(
blink::LocalFrameToken(factory_params->top_frame_id.value())); blink::LocalFrameToken(factory_params->top_frame_id.value()));
auto* web_contents = content::WebContents::FromRenderFrameHost(rfh); auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
auto* prefs = WebContentsPreferences::From(web_contents); auto* prefs = WebContentsPreferences::From(web_contents);
if (prefs && !prefs->IsEnabled(options::kWebSecurity, true)) { if (prefs && !prefs->IsWebSecurityEnabled()) {
factory_params->is_corb_enabled = false; factory_params->is_corb_enabled = false;
factory_params->disable_web_security = true; factory_params->disable_web_security = true;
} }

View file

@ -126,8 +126,7 @@ void ElectronDownloadManagerDelegate::OnDownloadPathGenerated(
settings.default_path = default_path; settings.default_path = default_path;
auto* web_preferences = WebContentsPreferences::From(web_contents); auto* web_preferences = WebContentsPreferences::From(web_contents);
const bool offscreen = const bool offscreen = !web_preferences || web_preferences->IsOffscreen();
!web_preferences || web_preferences->IsEnabled(options::kOffscreen);
settings.force_detached = offscreen; settings.force_detached = offscreen;
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();

View file

@ -61,7 +61,7 @@ void ElectronJavaScriptDialogManager::RunJavaScriptDialog(
auto* web_preferences = WebContentsPreferences::From(web_contents); auto* web_preferences = WebContentsPreferences::From(web_contents);
if (web_preferences && web_preferences->IsEnabled("disableDialogs")) { if (web_preferences && web_preferences->ShouldDisableDialogs()) {
return std::move(callback).Run(false, std::u16string()); return std::move(callback).Run(false, std::u16string());
} }
@ -81,14 +81,14 @@ void ElectronJavaScriptDialogManager::RunJavaScriptDialog(
std::string checkbox; std::string checkbox;
if (origin_counts_[origin] > 1 && web_preferences && if (origin_counts_[origin] > 1 && web_preferences &&
web_preferences->IsEnabled("safeDialogs") && web_preferences->ShouldUseSafeDialogs() &&
!web_preferences->GetPreference("safeDialogsMessage", &checkbox)) { !web_preferences->GetSafeDialogsMessage(&checkbox)) {
checkbox = "Prevent this app from creating additional dialogs"; checkbox = "Prevent this app from creating additional dialogs";
} }
// Don't set parent for offscreen window. // Don't set parent for offscreen window.
NativeWindow* window = nullptr; NativeWindow* window = nullptr;
if (web_preferences && !web_preferences->IsEnabled(options::kOffscreen)) { if (web_preferences && !web_preferences->IsOffscreen()) {
auto* relay = NativeWindowRelay::FromWebContents(web_contents); auto* relay = NativeWindowRelay::FromWebContents(web_contents);
if (relay) if (relay)
window = relay->GetNativeWindow(); window = relay->GetNativeWindow();

View file

@ -17,7 +17,6 @@
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "electron/buildflags/buildflags.h"
#include "net/base/filename_util.h" #include "net/base/filename_util.h"
#include "sandbox/policy/switches.h" #include "sandbox/policy/switches.h"
#include "shell/browser/native_window.h" #include "shell/browser/native_window.h"
@ -28,73 +27,22 @@
#include "shell/common/process_util.h" #include "shell/common/process_util.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h" #include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/mojom/v8_cache_options.mojom.h" #include "third_party/blink/public/mojom/v8_cache_options.mojom.h"
#include "third_party/blink/public/mojom/webpreferences/web_preferences.mojom-blink.h" // nogncheck #include "third_party/blink/public/mojom/webpreferences/web_preferences.mojom.h"
#if defined(OS_WIN) #if defined(OS_WIN)
#include "ui/gfx/switches.h" #include "ui/gfx/switches.h"
#endif #endif
namespace { namespace gin {
bool GetAsString(const base::Value* val, template <>
base::StringPiece path, struct Converter<blink::mojom::AutoplayPolicy> {
std::string* out) { static bool FromV8(v8::Isolate* isolate,
if (val) { v8::Local<v8::Value> val,
auto* found = val->FindKeyOfType(path, base::Value::Type::STRING); blink::mojom::AutoplayPolicy* out) {
if (found) { std::string policy_str;
*out = found->GetString(); if (!ConvertFromV8(isolate, val, &policy_str))
return true; return false;
}
}
return false;
}
bool GetAsString(const base::Value* val,
base::StringPiece path,
std::u16string* out) {
if (val) {
auto* found = val->FindKeyOfType(path, base::Value::Type::STRING);
if (found) {
*out = base::UTF8ToUTF16(found->GetString());
return true;
}
}
return false;
}
#if defined(OS_WIN)
bool GetAsString(const base::Value* val,
base::StringPiece path,
std::wstring* out) {
if (val) {
auto* found = val->FindKeyOfType(path, base::Value::Type::STRING);
if (found) {
*out = base::UTF8ToWide(found->GetString());
return true;
}
}
return false;
}
#endif
bool GetAsInteger(const base::Value* val, base::StringPiece path, int* out) {
if (val) {
auto* found = val->FindKey(path);
if (found && found->is_int()) {
*out = found->GetInt();
return true;
} else if (found && found->is_string()) {
return base::StringToInt(found->GetString(), out);
}
}
return false;
}
bool GetAsAutoplayPolicy(const base::Value* val,
base::StringPiece path,
blink::mojom::AutoplayPolicy* out) {
std::string policy_str;
if (GetAsString(val, path, &policy_str)) {
if (policy_str == "no-user-gesture-required") { if (policy_str == "no-user-gesture-required") {
*out = blink::mojom::AutoplayPolicy::kNoUserGestureRequired; *out = blink::mojom::AutoplayPolicy::kNoUserGestureRequired;
return true; return true;
@ -107,192 +55,257 @@ bool GetAsAutoplayPolicy(const base::Value* val,
} }
return false; return false;
} }
return false; };
}
bool GetImageAnimationPolicy(const base::Value* val, template <>
blink::mojom::ImageAnimationPolicy* out) { struct Converter<blink::mojom::V8CacheOptions> {
std::string policy; static bool FromV8(v8::Isolate* isolate,
if (GetAsString(val, electron::options::kImageAnimationPolicy, &policy)) { v8::Local<v8::Value> val,
if (policy == "animate") { blink::mojom::V8CacheOptions* out) {
*out = blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed; std::string v8_cache_options;
if (!ConvertFromV8(isolate, val, &v8_cache_options))
return false;
if (v8_cache_options == "none") {
*out = blink::mojom::V8CacheOptions::kNone;
return true; return true;
} else if (policy == "animateOnce") { } else if (v8_cache_options == "code") {
*out = *out = blink::mojom::V8CacheOptions::kCode;
blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAnimateOnce;
return true; return true;
} else if (policy == "noAnimation") { } else if (v8_cache_options == "bypassHeatCheck") {
*out = *out = blink::mojom::V8CacheOptions::kCodeWithoutHeatCheck;
blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyNoAnimation; return true;
} else if (v8_cache_options == "bypassHeatCheckAndEagerCompile") {
*out = blink::mojom::V8CacheOptions::kFullCodeWithoutHeatCheck;
return true; return true;
} }
return false;
} }
return false; };
}
} // namespace } // namespace gin
namespace electron { namespace electron {
// static namespace {
std::vector<WebContentsPreferences*> WebContentsPreferences::instances_; std::vector<WebContentsPreferences*>& Instances() {
static base::NoDestructor<std::vector<WebContentsPreferences*>> g_instances;
return *g_instances;
}
} // namespace
WebContentsPreferences::WebContentsPreferences( WebContentsPreferences::WebContentsPreferences(
content::WebContents* web_contents, content::WebContents* web_contents,
const gin_helper::Dictionary& web_preferences) const gin_helper::Dictionary& web_preferences)
: web_contents_(web_contents) { : web_contents_(web_contents) {
v8::Isolate* isolate = web_preferences.isolate();
gin_helper::Dictionary copied(isolate, web_preferences.GetHandle()->Clone());
// Following fields should not be stored.
copied.Delete("embedder");
copied.Delete("session");
copied.Delete("type");
gin::ConvertFromV8(isolate, copied.GetHandle(), &preference_);
web_contents->SetUserData(UserDataKey(), base::WrapUnique(this)); web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
Instances().push_back(this);
instances_.push_back(this); SetFromDictionary(web_preferences);
// Set WebPreferences defaults onto the JS object
SetDefaultBoolIfUndefined(options::kPlugins, false);
SetDefaultBoolIfUndefined(options::kExperimentalFeatures, false);
SetDefaultBoolIfUndefined(options::kNodeIntegration, false);
SetDefaultBoolIfUndefined(options::kNodeIntegrationInSubFrames, false);
SetDefaultBoolIfUndefined(options::kNodeIntegrationInWorker, false);
SetDefaultBoolIfUndefined(options::kDisableHtmlFullscreenWindowResize, false);
SetDefaultBoolIfUndefined(options::kWebviewTag, false);
SetDefaultBoolIfUndefined(options::kSandbox, false);
SetDefaultBoolIfUndefined(options::kNativeWindowOpen, true);
SetDefaultBoolIfUndefined(options::kContextIsolation, true);
SetDefaultBoolIfUndefined(options::kJavaScript, true);
SetDefaultBoolIfUndefined(options::kImages, true);
SetDefaultBoolIfUndefined(options::kTextAreasAreResizable, true);
SetDefaultBoolIfUndefined(options::kWebGL, true);
SetDefaultBoolIfUndefined(options::kEnableWebSQL, true);
SetDefaultBoolIfUndefined(options::kEnablePreferredSizeMode, false);
bool webSecurity = true;
SetDefaultBoolIfUndefined(options::kWebSecurity, webSecurity);
// If webSecurity was explicitly set to false, let's inherit that into
// insecureContent
if (web_preferences.Get(options::kWebSecurity, &webSecurity) &&
!webSecurity) {
SetDefaultBoolIfUndefined(options::kAllowRunningInsecureContent, true);
} else {
SetDefaultBoolIfUndefined(options::kAllowRunningInsecureContent, false);
}
#if defined(OS_MAC)
SetDefaultBoolIfUndefined(options::kScrollBounce, false);
#endif
SetDefaultBoolIfUndefined(options::kOffscreen, false);
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
SetDefaultBoolIfUndefined(options::kSpellcheck, true);
#endif
// If this is a <webview> tag, and the embedder is offscreen-rendered, then // If this is a <webview> tag, and the embedder is offscreen-rendered, then
// this WebContents is also offscreen-rendered. // this WebContents is also offscreen-rendered.
int guest_instance_id = 0; if (guest_instance_id_) {
if (web_preferences.Get(options::kGuestInstanceID, &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) {
auto* embedder_preferences = WebContentsPreferences::From(embedder); auto* embedder_preferences = WebContentsPreferences::From(embedder);
if (embedder_preferences && if (embedder_preferences && embedder_preferences->IsOffscreen()) {
embedder_preferences->IsEnabled(options::kOffscreen)) { offscreen_ = true;
preference_.SetKey(options::kOffscreen, base::Value(true));
} }
} }
} }
} }
SetDefaults();
} }
WebContentsPreferences::~WebContentsPreferences() { WebContentsPreferences::~WebContentsPreferences() {
instances_.erase(std::remove(instances_.begin(), instances_.end(), this), Instances().erase(std::remove(Instances().begin(), Instances().end(), this),
instances_.end()); Instances().end());
}
void WebContentsPreferences::SetDefaults() {
if (IsEnabled(options::kSandbox)) {
SetBool(options::kNativeWindowOpen, true);
}
last_preference_ = preference_.Clone();
}
bool WebContentsPreferences::IsUndefined(base::StringPiece key) {
return !preference_.FindKeyOfType(key, base::Value::Type::BOOLEAN);
}
bool WebContentsPreferences::SetDefaultBoolIfUndefined(base::StringPiece key,
bool val) {
auto* current_value =
preference_.FindKeyOfType(key, base::Value::Type::BOOLEAN);
if (current_value) {
return current_value->GetBool();
} else {
preference_.SetKey(key, base::Value(val));
return val;
}
}
void WebContentsPreferences::SetBool(base::StringPiece key, bool value) {
preference_.SetKey(key, base::Value(value));
}
bool WebContentsPreferences::IsEnabled(base::StringPiece name,
bool default_value) const {
auto* current_value =
preference_.FindKeyOfType(name, base::Value::Type::BOOLEAN);
if (current_value)
return current_value->GetBool();
return default_value;
}
void WebContentsPreferences::Merge(const base::DictionaryValue& extend) {
if (preference_.is_dict())
static_cast<base::DictionaryValue*>(&preference_)->MergeDictionary(&extend);
SetDefaults();
} }
void WebContentsPreferences::Clear() { void WebContentsPreferences::Clear() {
if (preference_.is_dict()) plugins_ = false;
static_cast<base::DictionaryValue*>(&preference_)->Clear(); experimental_features_ = false;
node_integration_ = false;
node_integration_in_sub_frames_ = false;
node_integration_in_worker_ = false;
disable_html_fullscreen_window_resize_ = false;
webview_tag_ = false;
sandbox_ = false;
native_window_open_ = true;
context_isolation_ = true;
javascript_ = true;
images_ = true;
text_areas_are_resizable_ = true;
webgl_ = true;
enable_websql_ = true;
enable_preferred_size_mode_ = false;
web_security_ = true;
allow_running_insecure_content_ = false;
offscreen_ = false;
navigate_on_drag_drop_ = false;
autoplay_policy_ = blink::mojom::AutoplayPolicy::kNoUserGestureRequired;
default_font_family_.clear();
default_font_size_ = absl::nullopt;
default_monospace_font_size_ = absl::nullopt;
minimum_font_size_ = absl::nullopt;
default_encoding_ = absl::nullopt;
opener_id_ = 0;
guest_instance_id_ = 0;
custom_args_.clear();
custom_switches_.clear();
enable_blink_features_ = absl::nullopt;
disable_blink_features_ = absl::nullopt;
disable_popups_ = false;
disable_dialogs_ = false;
safe_dialogs_ = false;
safe_dialogs_message_ = absl::nullopt;
ignore_menu_shortcuts_ = false;
background_color_ = SK_ColorTRANSPARENT;
image_animation_policy_ =
blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed;
preload_path_ = absl::nullopt;
v8_cache_options_ = blink::mojom::V8CacheOptions::kDefault;
#if defined(OS_MAC)
scroll_bounce_ = false;
#endif
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
spellcheck_ = true;
#endif
} }
bool WebContentsPreferences::GetPreference(base::StringPiece name, void WebContentsPreferences::SetFromDictionary(
std::string* value) const { const gin_helper::Dictionary& web_preferences) {
return GetAsString(&preference_, name, value); Clear();
Merge(web_preferences);
}
void WebContentsPreferences::Merge(
const gin_helper::Dictionary& web_preferences) {
web_preferences.Get(options::kPlugins, &plugins_);
web_preferences.Get(options::kExperimentalFeatures, &experimental_features_);
web_preferences.Get(options::kNodeIntegration, &node_integration_);
web_preferences.Get(options::kNodeIntegrationInSubFrames,
&node_integration_in_sub_frames_);
web_preferences.Get(options::kNodeIntegrationInWorker,
&node_integration_in_worker_);
web_preferences.Get(options::kDisableHtmlFullscreenWindowResize,
&disable_html_fullscreen_window_resize_);
web_preferences.Get(options::kWebviewTag, &webview_tag_);
web_preferences.Get(options::kSandbox, &sandbox_);
web_preferences.Get(options::kNativeWindowOpen, &native_window_open_);
web_preferences.Get(options::kContextIsolation, &context_isolation_);
web_preferences.Get(options::kJavaScript, &javascript_);
web_preferences.Get(options::kImages, &images_);
web_preferences.Get(options::kTextAreasAreResizable,
&text_areas_are_resizable_);
web_preferences.Get(options::kWebGL, &webgl_);
web_preferences.Get(options::kEnableWebSQL, &enable_websql_);
web_preferences.Get(options::kEnablePreferredSizeMode,
&enable_preferred_size_mode_);
web_preferences.Get(options::kWebSecurity, &web_security_);
if (!web_preferences.Get(options::kAllowRunningInsecureContent,
&allow_running_insecure_content_) &&
!web_security_)
allow_running_insecure_content_ = true;
web_preferences.Get(options::kOffscreen, &offscreen_);
web_preferences.Get(options::kNavigateOnDragDrop, &navigate_on_drag_drop_);
web_preferences.Get("autoplayPolicy", &autoplay_policy_);
web_preferences.Get("defaultFontFamily", &default_font_family_);
int size;
if (web_preferences.Get("defaultFontSize", &size))
default_font_size_ = size;
if (web_preferences.Get("defaultMonospaceFontSize", &size))
default_monospace_font_size_ = size;
if (web_preferences.Get("minimumFontSize", &size))
minimum_font_size_ = size;
std::string encoding;
if (web_preferences.Get("defaultEncoding", &encoding))
default_encoding_ = encoding;
web_preferences.Get(options::kOpenerID, &opener_id_);
web_preferences.Get(options::kGuestInstanceID, &guest_instance_id_);
web_preferences.Get(options::kCustomArgs, &custom_args_);
web_preferences.Get("commandLineSwitches", &custom_switches_);
web_preferences.Get("disablePopups", &disable_popups_);
web_preferences.Get("disableDialogs", &disable_dialogs_);
web_preferences.Get("safeDialogs", &safe_dialogs_);
web_preferences.Get(options::kBackgroundColor, &background_color_);
std::string safe_dialogs_message;
if (web_preferences.Get("safeDialogsMessage", &safe_dialogs_message))
safe_dialogs_message_ = safe_dialogs_message;
web_preferences.Get("ignoreMenuShortcuts", &ignore_menu_shortcuts_);
std::string enable_blink_features;
if (web_preferences.Get(options::kEnableBlinkFeatures,
&enable_blink_features))
enable_blink_features_ = enable_blink_features;
std::string disable_blink_features;
if (web_preferences.Get(options::kDisableBlinkFeatures,
&disable_blink_features))
disable_blink_features_ = disable_blink_features;
base::FilePath::StringType preload_path;
std::string preload_url_str;
if (web_preferences.Get(options::kPreloadScript, &preload_path)) {
base::FilePath preload(preload_path);
if (preload.IsAbsolute()) {
preload_path_ = preload;
} else {
LOG(ERROR) << "preload script must have absolute path.";
}
} else if (web_preferences.Get(options::kPreloadURL, &preload_url_str)) {
// Translate to file path if there is "preload-url" option.
base::FilePath preload;
GURL preload_url(preload_url_str);
if (net::FileURLToFilePath(preload_url, &preload)) {
preload_path_ = preload;
} else {
LOG(ERROR) << "preload url must be file:// protocol.";
}
}
web_preferences.Get("v8CacheOptions", &v8_cache_options_);
#if defined(OS_MAC)
web_preferences.Get(options::kScrollBounce, &scroll_bounce_);
#endif
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
web_preferences.Get(options::kSpellcheck, &spellcheck_);
#endif
SaveLastPreferences();
}
bool WebContentsPreferences::GetSafeDialogsMessage(std::string* message) const {
if (safe_dialogs_message_) {
*message = *safe_dialogs_message_;
return true;
}
return false;
}
bool WebContentsPreferences::SetImageAnimationPolicy(std::string policy) {
if (policy == "animate") {
image_animation_policy_ =
blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed;
return true;
} else if (policy == "animateOnce") {
image_animation_policy_ =
blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAnimateOnce;
return true;
} else if (policy == "noAnimation") {
image_animation_policy_ =
blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyNoAnimation;
return true;
}
return false;
} }
bool WebContentsPreferences::GetPreloadPath(base::FilePath* path) const { bool WebContentsPreferences::GetPreloadPath(base::FilePath* path) const {
DCHECK(path); DCHECK(path);
base::FilePath::StringType preload_path; if (preload_path_) {
if (GetAsString(&preference_, options::kPreloadScript, &preload_path)) { *path = *preload_path_;
base::FilePath preload(preload_path); return true;
if (preload.IsAbsolute()) {
*path = std::move(preload);
return true;
} else {
LOG(ERROR) << "preload script must have absolute path.";
}
} else if (GetAsString(&preference_, options::kPreloadURL, &preload_path)) {
// Translate to file path if there is "preload-url" option.
base::FilePath preload;
GURL preload_url;
#if defined(OS_WIN)
preload_url = GURL(base::WideToUTF8(preload_path));
#else
preload_url = GURL(preload_path);
#endif
if (net::FileURLToFilePath(preload_url, &preload)) {
*path = std::move(preload);
return true;
} else {
LOG(ERROR) << "preload url must be file:// protocol.";
}
} }
return false; return false;
} }
@ -300,7 +313,7 @@ bool WebContentsPreferences::GetPreloadPath(base::FilePath* path) const {
// static // static
content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID( content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID(
int process_id) { int process_id) {
for (WebContentsPreferences* preferences : instances_) { for (WebContentsPreferences* preferences : Instances()) {
content::WebContents* web_contents = preferences->web_contents_; content::WebContents* web_contents = preferences->web_contents_;
if (web_contents->GetMainFrame()->GetProcess()->GetID() == process_id) if (web_contents->GetMainFrame()->GetProcess()->GetID() == process_id)
return web_contents; return web_contents;
@ -320,146 +333,141 @@ void WebContentsPreferences::AppendCommandLineSwitches(
base::CommandLine* command_line, base::CommandLine* command_line,
bool is_subframe) { bool is_subframe) {
// Experimental flags. // Experimental flags.
if (IsEnabled(options::kExperimentalFeatures)) if (experimental_features_)
command_line->AppendSwitch( command_line->AppendSwitch(
::switches::kEnableExperimentalWebPlatformFeatures); ::switches::kEnableExperimentalWebPlatformFeatures);
// Sandbox can be enabled for renderer processes hosting cross-origin frames // Sandbox can be enabled for renderer processes hosting cross-origin frames
// unless nodeIntegrationInSubFrames is enabled // unless nodeIntegrationInSubFrames is enabled
bool can_sandbox_frame = bool can_sandbox_frame = is_subframe && !node_integration_in_sub_frames_;
is_subframe && !IsEnabled(options::kNodeIntegrationInSubFrames);
if (IsEnabled(options::kSandbox) || can_sandbox_frame) { if (sandbox_ || can_sandbox_frame) {
command_line->AppendSwitch(switches::kEnableSandbox); command_line->AppendSwitch(switches::kEnableSandbox);
} else if (!command_line->HasSwitch(switches::kEnableSandbox)) { } else if (!command_line->HasSwitch(switches::kEnableSandbox)) {
command_line->AppendSwitch(sandbox::policy::switches::kNoSandbox); command_line->AppendSwitch(sandbox::policy::switches::kNoSandbox);
command_line->AppendSwitch(::switches::kNoZygote); command_line->AppendSwitch(::switches::kNoZygote);
} }
// Custom args for renderer process
auto* customArgs =
preference_.FindKeyOfType(options::kCustomArgs, base::Value::Type::LIST);
if (customArgs) {
for (const auto& customArg : customArgs->GetList()) {
if (customArg.is_string())
command_line->AppendArg(customArg.GetString());
}
}
#if defined(OS_MAC) #if defined(OS_MAC)
// Enable scroll bounce. // Enable scroll bounce.
if (IsEnabled(options::kScrollBounce)) if (scroll_bounce_)
command_line->AppendSwitch(switches::kScrollBounce); command_line->AppendSwitch(switches::kScrollBounce);
#endif #endif
// Custom args for renderer process
for (const auto& arg : custom_args_)
if (!arg.empty())
command_line->AppendArg(arg);
// Custom command line switches. // Custom command line switches.
auto* args = for (const auto& arg : custom_switches_)
preference_.FindKeyOfType("commandLineSwitches", base::Value::Type::LIST); if (!arg.empty())
if (args) { command_line->AppendSwitch(arg);
for (const auto& arg : args->GetList()) {
if (arg.is_string()) {
const auto& arg_val = arg.GetString();
if (!arg_val.empty())
command_line->AppendSwitch(arg_val);
}
}
}
std::string s; if (enable_blink_features_)
// Enable blink features. command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures,
if (GetAsString(&preference_, options::kEnableBlinkFeatures, &s)) *enable_blink_features_);
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures, s); if (disable_blink_features_)
command_line->AppendSwitchASCII(::switches::kDisableBlinkFeatures,
*disable_blink_features_);
// Disable blink features. if (node_integration_in_worker_)
if (GetAsString(&preference_, options::kDisableBlinkFeatures, &s))
command_line->AppendSwitchASCII(::switches::kDisableBlinkFeatures, s);
if (IsEnabled(options::kNodeIntegrationInWorker))
command_line->AppendSwitch(switches::kNodeIntegrationInWorker); command_line->AppendSwitch(switches::kNodeIntegrationInWorker);
// We are appending args to a webContents so let's save the current state // 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 // of our preferences object so that during the lifetime of the WebContents
// we can fetch the options used to initally configure the WebContents // we can fetch the options used to initally configure the WebContents
last_preference_ = preference_.Clone(); // last_preference_ = preference_.Clone();
SaveLastPreferences();
}
void WebContentsPreferences::SaveLastPreferences() {
last_web_preferences_ = base::Value(base::Value::Type::DICTIONARY);
last_web_preferences_.SetKey(options::kOpenerID, base::Value(opener_id_));
last_web_preferences_.SetKey(options::kNodeIntegration,
base::Value(node_integration_));
last_web_preferences_.SetKey(options::kNodeIntegrationInSubFrames,
base::Value(node_integration_in_sub_frames_));
last_web_preferences_.SetKey(options::kNativeWindowOpen,
base::Value(native_window_open_));
last_web_preferences_.SetKey(options::kSandbox, base::Value(sandbox_));
last_web_preferences_.SetKey(options::kContextIsolation,
base::Value(context_isolation_));
last_web_preferences_.SetKey(options::kJavaScript, base::Value(javascript_));
last_web_preferences_.SetKey(options::kEnableWebSQL,
base::Value(enable_websql_));
last_web_preferences_.SetKey(options::kWebviewTag, base::Value(webview_tag_));
last_web_preferences_.SetKey("disablePopups", base::Value(disable_popups_));
last_web_preferences_.SetKey(options::kWebSecurity,
base::Value(web_security_));
last_web_preferences_.SetKey(options::kAllowRunningInsecureContent,
base::Value(allow_running_insecure_content_));
last_web_preferences_.SetKey(options::kExperimentalFeatures,
base::Value(experimental_features_));
last_web_preferences_.SetKey(
options::kEnableBlinkFeatures,
base::Value(enable_blink_features_.value_or("")));
} }
void WebContentsPreferences::OverrideWebkitPrefs( void WebContentsPreferences::OverrideWebkitPrefs(
blink::web_pref::WebPreferences* prefs) { blink::web_pref::WebPreferences* prefs) {
prefs->javascript_enabled = prefs->javascript_enabled = javascript_;
IsEnabled(options::kJavaScript, true /* default_value */); prefs->images_enabled = images_;
prefs->images_enabled = IsEnabled(options::kImages, true /* default_value */); // GetImageAnimationPolicy(&preference_, &prefs->animation_policy);
GetImageAnimationPolicy(&preference_, &prefs->animation_policy); prefs->text_areas_are_resizable = text_areas_are_resizable_;
prefs->text_areas_are_resizable = prefs->navigate_on_drag_drop = navigate_on_drag_drop_;
IsEnabled(options::kTextAreasAreResizable, true /* default_value */); prefs->autoplay_policy = autoplay_policy_;
prefs->navigate_on_drag_drop =
IsEnabled(options::kNavigateOnDragDrop, false /* default_value */);
if (!GetAsAutoplayPolicy(&preference_, "autoplayPolicy",
&prefs->autoplay_policy)) {
prefs->autoplay_policy =
blink::mojom::AutoplayPolicy::kNoUserGestureRequired;
}
// Check if webgl should be enabled. // Check if webgl should be enabled.
bool is_webgl_enabled = IsEnabled(options::kWebGL, true /* default_value */); prefs->webgl1_enabled = webgl_;
prefs->webgl1_enabled = is_webgl_enabled; prefs->webgl2_enabled = webgl_;
prefs->webgl2_enabled = is_webgl_enabled;
// Check if web security should be enabled. // Check if web security should be enabled.
bool is_web_security_enabled = prefs->web_security_enabled = web_security_;
IsEnabled(options::kWebSecurity, true /* default_value */); prefs->allow_running_insecure_content = allow_running_insecure_content_;
prefs->web_security_enabled = is_web_security_enabled;
prefs->allow_running_insecure_content =
IsEnabled(options::kAllowRunningInsecureContent,
!is_web_security_enabled /* default_value */);
auto* fonts_dict = preference_.FindKeyOfType("defaultFontFamily", if (auto font =
base::Value::Type::DICTIONARY); default_font_family_.find("standard") != default_font_family_.end())
if (fonts_dict) { prefs->standard_font_family_map[blink::web_pref::kCommonScript] = font;
std::u16string font; if (auto font =
if (GetAsString(fonts_dict, "standard", &font)) default_font_family_.find("serif") != default_font_family_.end())
prefs->standard_font_family_map[blink::web_pref::kCommonScript] = font; prefs->serif_font_family_map[blink::web_pref::kCommonScript] = font;
if (GetAsString(fonts_dict, "serif", &font)) if (auto font =
prefs->serif_font_family_map[blink::web_pref::kCommonScript] = font; default_font_family_.find("sansSerif") != default_font_family_.end())
if (GetAsString(fonts_dict, "sansSerif", &font)) prefs->sans_serif_font_family_map[blink::web_pref::kCommonScript] = font;
prefs->sans_serif_font_family_map[blink::web_pref::kCommonScript] = font; if (auto font =
if (GetAsString(fonts_dict, "monospace", &font)) default_font_family_.find("monospace") != default_font_family_.end())
prefs->fixed_font_family_map[blink::web_pref::kCommonScript] = font; prefs->fixed_font_family_map[blink::web_pref::kCommonScript] = font;
if (GetAsString(fonts_dict, "cursive", &font)) if (auto font =
prefs->cursive_font_family_map[blink::web_pref::kCommonScript] = font; default_font_family_.find("cursive") != default_font_family_.end())
if (GetAsString(fonts_dict, "fantasy", &font)) prefs->cursive_font_family_map[blink::web_pref::kCommonScript] = font;
prefs->fantasy_font_family_map[blink::web_pref::kCommonScript] = font; if (auto font =
} default_font_family_.find("fantasy") != default_font_family_.end())
prefs->fantasy_font_family_map[blink::web_pref::kCommonScript] = font;
int size; if (default_font_size_)
if (GetAsInteger(&preference_, "defaultFontSize", &size)) prefs->default_font_size = *default_font_size_;
prefs->default_font_size = size; if (default_monospace_font_size_)
if (GetAsInteger(&preference_, "defaultMonospaceFontSize", &size)) prefs->default_fixed_font_size = *default_monospace_font_size_;
prefs->default_fixed_font_size = size; if (minimum_font_size_)
if (GetAsInteger(&preference_, "minimumFontSize", &size)) prefs->minimum_font_size = *minimum_font_size_;
prefs->minimum_font_size = size; if (default_encoding_)
std::string encoding; prefs->default_encoding = *default_encoding_;
if (GetAsString(&preference_, "defaultEncoding", &encoding))
prefs->default_encoding = encoding;
// Pass the opener's window id. // Pass the opener's window id.
int opener_id; prefs->opener_id = opener_id_;
if (GetAsInteger(&preference_, options::kOpenerID, &opener_id))
prefs->opener_id = opener_id;
// Run Electron APIs and preload script in isolated world // Run Electron APIs and preload script in isolated world
prefs->context_isolation = IsEnabled(options::kContextIsolation, true); prefs->context_isolation = context_isolation_;
prefs->guest_instance_id = guest_instance_id_;
int guest_instance_id = 0;
if (GetAsInteger(&preference_, options::kGuestInstanceID, &guest_instance_id))
prefs->guest_instance_id = guest_instance_id;
prefs->hidden_page = false; prefs->hidden_page = false;
if (guest_instance_id) { if (guest_instance_id_) {
// Webview `document.visibilityState` tracks window visibility so we need // Webview `document.visibilityState` tracks window visibility so we need
// to let it know if the window happens to be hidden right now. // 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) { if (manager) {
auto* embedder = manager->GetEmbedder(guest_instance_id); auto* embedder = manager->GetEmbedder(guest_instance_id_);
if (embedder) { if (embedder) {
auto* relay = NativeWindowRelay::FromWebContents(embedder); auto* relay = NativeWindowRelay::FromWebContents(embedder);
if (relay) { if (relay) {
@ -475,53 +483,26 @@ void WebContentsPreferences::OverrideWebkitPrefs(
} }
} }
prefs->offscreen = IsEnabled(options::kOffscreen); prefs->offscreen = offscreen_;
// The preload script. // The preload script.
GetPreloadPath(&prefs->preload); if (preload_path_)
prefs->preload = *preload_path_;
// Check if nativeWindowOpen is enabled. prefs->native_window_open = native_window_open_;
prefs->native_window_open = IsEnabled(options::kNativeWindowOpen, true); prefs->node_integration = node_integration_;
prefs->node_integration_in_worker = node_integration_in_worker_;
// Check if we have node integration specified. prefs->node_integration_in_sub_frames = node_integration_in_sub_frames_;
prefs->node_integration = IsEnabled(options::kNodeIntegration);
// Whether to enable node integration in Worker.
prefs->node_integration_in_worker =
IsEnabled(options::kNodeIntegrationInWorker);
prefs->node_integration_in_sub_frames =
IsEnabled(options::kNodeIntegrationInSubFrames);
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
prefs->enable_spellcheck = IsEnabled(options::kSpellcheck); prefs->enable_spellcheck = spellcheck_;
#endif #endif
// Check if plugins are enabled. prefs->enable_plugins = plugins_;
prefs->enable_plugins = IsEnabled(options::kPlugins); prefs->webview_tag = webview_tag_;
prefs->enable_websql = enable_websql_;
// Check if webview tag creation is enabled, default to nodeIntegration value. prefs->v8_cache_options = v8_cache_options_;
prefs->webview_tag = IsEnabled(options::kWebviewTag);
// Whether to allow the WebSQL api
prefs->enable_websql = IsEnabled(options::kEnableWebSQL);
std::string v8_cache_options;
if (GetAsString(&preference_, "v8CacheOptions", &v8_cache_options)) {
if (v8_cache_options == "none") {
prefs->v8_cache_options = blink::mojom::V8CacheOptions::kNone;
} else if (v8_cache_options == "code") {
prefs->v8_cache_options = blink::mojom::V8CacheOptions::kCode;
} else if (v8_cache_options == "bypassHeatCheck") {
prefs->v8_cache_options =
blink::mojom::V8CacheOptions::kCodeWithoutHeatCheck;
} else if (v8_cache_options == "bypassHeatCheckAndEagerCompile") {
prefs->v8_cache_options =
blink::mojom::V8CacheOptions::kFullCodeWithoutHeatCheck;
} else {
prefs->v8_cache_options = blink::mojom::V8CacheOptions::kDefault;
}
}
} }
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPreferences) WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPreferences)

View file

@ -5,11 +5,15 @@
#ifndef SHELL_BROWSER_WEB_CONTENTS_PREFERENCES_H_ #ifndef SHELL_BROWSER_WEB_CONTENTS_PREFERENCES_H_
#define SHELL_BROWSER_WEB_CONTENTS_PREFERENCES_H_ #define SHELL_BROWSER_WEB_CONTENTS_PREFERENCES_H_
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/values.h" #include "base/values.h"
#include "content/public/browser/web_contents_user_data.h" #include "content/public/browser/web_contents_user_data.h"
#include "electron/buildflags/buildflags.h"
#include "third_party/blink/public/mojom/v8_cache_options.mojom-forward.h"
#include "third_party/blink/public/mojom/webpreferences/web_preferences.mojom-forward.h"
namespace base { namespace base {
class CommandLine; class CommandLine;
@ -32,14 +36,9 @@ class WebContentsPreferences
const gin_helper::Dictionary& web_preferences); const gin_helper::Dictionary& web_preferences);
~WebContentsPreferences() override; ~WebContentsPreferences() override;
// Set WebPreferences defaults onto the JS object. void Merge(const gin_helper::Dictionary& new_web_preferences);
void SetDefaults();
// A simple way to know whether a Boolean property is enabled. void SetFromDictionary(const gin_helper::Dictionary& new_web_preferences);
bool IsEnabled(base::StringPiece name, bool default_value = false) const;
// $.extend(|web_preferences|, |new_web_preferences|).
void Merge(const base::DictionaryValue& new_web_preferences);
// Append command paramters according to preferences. // Append command paramters according to preferences.
void AppendCommandLineSwitches(base::CommandLine* command_line, void AppendCommandLineSwitches(base::CommandLine* command_line,
@ -48,19 +47,30 @@ class WebContentsPreferences
// Modify the WebPreferences according to preferences. // Modify the WebPreferences according to preferences.
void OverrideWebkitPrefs(blink::web_pref::WebPreferences* prefs); void OverrideWebkitPrefs(blink::web_pref::WebPreferences* prefs);
// Clear the current WebPreferences. base::Value* last_preference() { return &last_web_preferences_; }
void Clear();
// Return true if the particular preference value exists. bool IsOffscreen() const { return offscreen_; }
bool GetPreference(base::StringPiece name, std::string* value) const; SkColor GetBackgroundColor() const { return background_color_; }
void SetBackgroundColor(SkColor color) { background_color_ = color; }
// Returns the preload script path. bool ShouldUsePreferredSizeMode() const {
return enable_preferred_size_mode_;
}
void SetIgnoreMenuShortcuts(bool ignore_menu_shortcuts) {
ignore_menu_shortcuts_ = ignore_menu_shortcuts;
}
bool ShouldIgnoreMenuShortcuts() const { return ignore_menu_shortcuts_; }
bool SetImageAnimationPolicy(std::string policy);
bool ShouldDisableHtmlFullscreenWindowResize() const {
return disable_html_fullscreen_window_resize_;
}
bool ShouldDisableDialogs() const { return disable_dialogs_; }
bool ShouldUseSafeDialogs() const { return safe_dialogs_; }
bool GetSafeDialogsMessage(std::string* message) const;
bool ShouldDisablePopups() const { return disable_popups_; }
bool ShouldUseNativeWindowOpen() const { return native_window_open_; }
bool IsWebSecurityEnabled() const { return web_security_; }
bool GetPreloadPath(base::FilePath* path) const; bool GetPreloadPath(base::FilePath* path) const;
// Returns the web preferences.
base::Value* preference() { return &preference_; }
base::Value* last_preference() { return &last_preference_; }
private: private:
friend class content::WebContentsUserData<WebContentsPreferences>; friend class content::WebContentsUserData<WebContentsPreferences>;
friend class ElectronBrowserClient; friend class ElectronBrowserClient;
@ -68,21 +78,63 @@ class WebContentsPreferences
// Get WebContents according to process ID. // Get WebContents according to process ID.
static content::WebContents* GetWebContentsFromProcessID(int process_id); static content::WebContents* GetWebContentsFromProcessID(int process_id);
// Checks if the key is not defined void Clear();
bool IsUndefined(base::StringPiece key); void SaveLastPreferences();
// Set preference value to given bool if user did not provide value
bool SetDefaultBoolIfUndefined(base::StringPiece key, bool val);
// Set preference value to given bool
void SetBool(base::StringPiece key, bool value);
static std::vector<WebContentsPreferences*> instances_;
content::WebContents* web_contents_; content::WebContents* web_contents_;
base::Value preference_ = base::Value(base::Value::Type::DICTIONARY); bool plugins_;
base::Value last_preference_ = base::Value(base::Value::Type::DICTIONARY); bool experimental_features_;
bool node_integration_;
bool node_integration_in_sub_frames_;
bool node_integration_in_worker_;
bool disable_html_fullscreen_window_resize_;
bool webview_tag_;
bool sandbox_;
bool native_window_open_;
bool context_isolation_;
bool javascript_;
bool images_;
bool text_areas_are_resizable_;
bool webgl_;
bool enable_websql_;
bool enable_preferred_size_mode_;
bool web_security_;
bool allow_running_insecure_content_;
bool offscreen_;
bool navigate_on_drag_drop_;
blink::mojom::AutoplayPolicy autoplay_policy_;
std::map<std::string, std::u16string> default_font_family_;
absl::optional<int> default_font_size_;
absl::optional<int> default_monospace_font_size_;
absl::optional<int> minimum_font_size_;
absl::optional<std::string> default_encoding_;
int opener_id_;
int guest_instance_id_;
std::vector<std::string> custom_args_;
std::vector<std::string> custom_switches_;
absl::optional<std::string> enable_blink_features_;
absl::optional<std::string> disable_blink_features_;
bool disable_popups_;
bool disable_dialogs_;
bool safe_dialogs_;
absl::optional<std::string> safe_dialogs_message_;
bool ignore_menu_shortcuts_;
SkColor background_color_;
blink::mojom::ImageAnimationPolicy image_animation_policy_;
absl::optional<base::FilePath> preload_path_;
blink::mojom::V8CacheOptions v8_cache_options_;
#if defined(OS_MAC)
bool scroll_bounce_;
#endif
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
bool spellcheck_;
#endif
// This is a snapshot of some relevant preferences at the time the renderer
// was launched.
base::Value last_web_preferences_;
WEB_CONTENTS_USER_DATA_KEY_DECL(); WEB_CONTENTS_USER_DATA_KEY_DECL();

View file

@ -2420,14 +2420,14 @@ describe('BrowserWindow module', () => {
}); });
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js'); const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js');
w.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { preload: preloadPath, foo: 'bar' } } })); w.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { preload: preloadPath, contextIsolation: false } } }));
w.loadFile(path.join(fixtures, 'api', 'new-window.html')); w.loadFile(path.join(fixtures, 'api', 'new-window.html'));
const [[, childWebContents]] = await Promise.all([ const [[, childWebContents]] = await Promise.all([
emittedOnce(app, 'web-contents-created'), emittedOnce(app, 'web-contents-created'),
emittedOnce(ipcMain, 'answer') emittedOnce(ipcMain, 'answer')
]); ]);
const webPreferences = childWebContents.getLastWebPreferences(); const webPreferences = childWebContents.getLastWebPreferences();
expect(webPreferences.foo).to.equal('bar'); expect(webPreferences.contextIsolation).to.equal(false);
}); });
it('should set ipc event sender correctly', async () => { it('should set ipc event sender correctly', async () => {
@ -2689,7 +2689,7 @@ describe('BrowserWindow module', () => {
overrideBrowserWindowOptions: { overrideBrowserWindowOptions: {
webPreferences: { webPreferences: {
preload: preloadPath, preload: preloadPath,
foo: 'bar' contextIsolation: false
} }
} }
})); }));
@ -2699,7 +2699,7 @@ describe('BrowserWindow module', () => {
emittedOnce(ipcMain, 'answer') emittedOnce(ipcMain, 'answer')
]); ]);
const webPreferences = childWebContents.getLastWebPreferences(); const webPreferences = childWebContents.getLastWebPreferences();
expect(webPreferences.foo).to.equal('bar'); expect(webPreferences.contextIsolation).to.equal(false);
}); });
describe('window.location', () => { describe('window.location', () => {