2015-09-04 16:44:22 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/web_contents_preferences.h"
|
2015-09-04 16:44:22 +00:00
|
|
|
|
2016-03-09 12:34:51 +00:00
|
|
|
#include <algorithm>
|
2015-09-05 11:47:44 +00:00
|
|
|
#include <string>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
2016-03-09 12:34:51 +00:00
|
|
|
#include <vector>
|
2015-09-05 11:47:44 +00:00
|
|
|
|
2015-09-04 17:04:09 +00:00
|
|
|
#include "base/command_line.h"
|
2017-08-07 20:30:03 +00:00
|
|
|
#include "base/memory/ptr_util.h"
|
2015-09-04 17:33:13 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2018-08-03 21:23:07 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "cc/base/switches.h"
|
2018-03-12 08:27:43 +00:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2016-03-09 12:34:51 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2016-01-07 04:49:00 +00:00
|
|
|
#include "content/public/common/content_switches.h"
|
2015-09-04 17:12:32 +00:00
|
|
|
#include "content/public/common/web_preferences.h"
|
2019-09-18 16:52:06 +00:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2015-09-05 01:57:39 +00:00
|
|
|
#include "net/base/filename_util.h"
|
2018-09-15 00:07:49 +00:00
|
|
|
#include "services/service_manager/sandbox/switches.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window.h"
|
|
|
|
#include "shell/browser/web_view_manager.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_converters/value_converter.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/options_switches.h"
|
2015-09-04 17:04:09 +00:00
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "ui/gfx/switches.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-03 21:23:07 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool GetAsString(const base::Value* val,
|
2019-07-25 15:19:04 +00:00
|
|
|
base::StringPiece path,
|
2018-08-03 21:23:07 +00:00
|
|
|
std::string* out) {
|
|
|
|
if (val) {
|
|
|
|
auto* found = val->FindKeyOfType(path, base::Value::Type::STRING);
|
|
|
|
if (found) {
|
|
|
|
*out = found->GetString();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetAsString(const base::Value* val,
|
2019-07-25 15:19:04 +00:00
|
|
|
base::StringPiece path,
|
2018-08-03 21:23:07 +00:00
|
|
|
base::string16* out) {
|
|
|
|
if (val) {
|
|
|
|
auto* found = val->FindKeyOfType(path, base::Value::Type::STRING);
|
|
|
|
if (found) {
|
|
|
|
*out = base::UTF8ToUTF16(found->GetString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:19:04 +00:00
|
|
|
bool GetAsInteger(const base::Value* val, base::StringPiece path, int* out) {
|
2018-08-03 21:23:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:30:34 +00:00
|
|
|
bool GetAsAutoplayPolicy(const base::Value* val,
|
2019-07-25 15:19:04 +00:00
|
|
|
base::StringPiece path,
|
2019-01-15 19:30:34 +00:00
|
|
|
content::AutoplayPolicy* out) {
|
|
|
|
std::string policy_str;
|
|
|
|
if (GetAsString(val, path, &policy_str)) {
|
|
|
|
if (policy_str == "no-user-gesture-required") {
|
|
|
|
*out = content::AutoplayPolicy::kNoUserGestureRequired;
|
|
|
|
return true;
|
|
|
|
} else if (policy_str == "user-gesture-required") {
|
|
|
|
*out = content::AutoplayPolicy::kUserGestureRequired;
|
|
|
|
return true;
|
|
|
|
} else if (policy_str == "document-user-activation-required") {
|
|
|
|
*out = content::AutoplayPolicy::kDocumentUserActivationRequired;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:23:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2015-09-04 16:44:22 +00:00
|
|
|
|
2016-03-09 12:34:51 +00:00
|
|
|
// static
|
|
|
|
std::vector<WebContentsPreferences*> WebContentsPreferences::instances_;
|
|
|
|
|
2015-09-04 16:44:22 +00:00
|
|
|
WebContentsPreferences::WebContentsPreferences(
|
|
|
|
content::WebContents* web_contents,
|
2019-10-25 13:03:28 +00:00
|
|
|
const gin_helper::Dictionary& web_preferences)
|
2016-03-09 12:34:51 +00:00
|
|
|
: web_contents_(web_contents) {
|
2015-11-11 02:32:25 +00:00
|
|
|
v8::Isolate* isolate = web_preferences.isolate();
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Dictionary copied(isolate, web_preferences.GetHandle()->Clone());
|
2015-11-11 02:32:25 +00:00
|
|
|
// Following fields should not be stored.
|
|
|
|
copied.Delete("embedder");
|
|
|
|
copied.Delete("session");
|
2019-05-20 10:55:46 +00:00
|
|
|
copied.Delete("type");
|
2015-11-11 02:32:25 +00:00
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
gin::ConvertFromV8(isolate, copied.GetHandle(), &preference_);
|
2017-08-07 20:30:03 +00:00
|
|
|
web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
|
2016-03-09 12:34:51 +00:00
|
|
|
|
|
|
|
instances_.push_back(this);
|
2018-03-15 04:56:46 +00:00
|
|
|
|
|
|
|
// Set WebPreferences defaults onto the JS object
|
2018-05-29 08:09:51 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kPlugins, false);
|
2018-03-15 04:56:46 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kExperimentalFeatures, false);
|
2019-01-07 19:19:27 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kNodeIntegration, false);
|
2019-01-22 19:24:46 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kNodeIntegrationInSubFrames, false);
|
2018-03-15 04:56:46 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kNodeIntegrationInWorker, false);
|
2019-03-07 23:29:37 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kDisableHtmlFullscreenWindowResize, false);
|
2019-01-07 19:19:27 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kWebviewTag, false);
|
2018-05-29 08:09:51 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kSandbox, false);
|
|
|
|
SetDefaultBoolIfUndefined(options::kNativeWindowOpen, false);
|
2018-08-29 21:57:49 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kContextIsolation, false);
|
2019-04-23 16:14:18 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kJavaScript, true);
|
|
|
|
SetDefaultBoolIfUndefined(options::kImages, true);
|
|
|
|
SetDefaultBoolIfUndefined(options::kTextAreasAreResizable, true);
|
|
|
|
SetDefaultBoolIfUndefined(options::kWebGL, true);
|
2018-04-16 13:28:54 +00:00
|
|
|
bool webSecurity = true;
|
2018-05-29 08:09:51 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kWebSecurity, webSecurity);
|
2018-04-16 13:28:54 +00:00
|
|
|
// If webSecurity was explicity set to false, let's inherit that into
|
|
|
|
// insecureContent
|
2018-05-29 08:09:51 +00:00
|
|
|
if (web_preferences.Get(options::kWebSecurity, &webSecurity) &&
|
|
|
|
!webSecurity) {
|
|
|
|
SetDefaultBoolIfUndefined(options::kAllowRunningInsecureContent, true);
|
2018-04-16 13:28:54 +00:00
|
|
|
} else {
|
2018-05-29 08:09:51 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kAllowRunningInsecureContent, false);
|
2018-04-16 13:28:54 +00:00
|
|
|
}
|
2018-04-18 01:55:30 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2018-03-15 04:56:46 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kScrollBounce, false);
|
2018-04-18 01:55:30 +00:00
|
|
|
#endif
|
2018-05-29 08:09:51 +00:00
|
|
|
SetDefaultBoolIfUndefined(options::kOffscreen, false);
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
SetDefaultBoolIfUndefined(options::kSpellcheck, true);
|
|
|
|
#endif
|
2018-03-08 07:05:12 +00:00
|
|
|
|
2019-04-19 19:55:20 +00:00
|
|
|
// If this is a <webview> tag, and the embedder is offscreen-rendered, then
|
|
|
|
// this WebContents is also offscreen-rendered.
|
|
|
|
int guest_instance_id = 0;
|
|
|
|
if (web_preferences.Get(options::kGuestInstanceID, &guest_instance_id)) {
|
|
|
|
auto* manager = WebViewManager::GetWebViewManager(web_contents);
|
|
|
|
if (manager) {
|
|
|
|
auto* embedder = manager->GetEmbedder(guest_instance_id);
|
|
|
|
if (embedder) {
|
|
|
|
auto* embedder_preferences = WebContentsPreferences::From(embedder);
|
|
|
|
if (embedder_preferences &&
|
|
|
|
embedder_preferences->IsEnabled(options::kOffscreen)) {
|
|
|
|
preference_.SetKey(options::kOffscreen, base::Value(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 01:56:04 +00:00
|
|
|
SetDefaults();
|
2015-09-04 16:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebContentsPreferences::~WebContentsPreferences() {
|
2018-04-18 01:55:30 +00:00
|
|
|
instances_.erase(std::remove(instances_.begin(), instances_.end(), this),
|
|
|
|
instances_.end());
|
2015-09-04 16:44:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 00:44:54 +00:00
|
|
|
void WebContentsPreferences::SetDefaults() {
|
|
|
|
if (IsEnabled(options::kSandbox)) {
|
|
|
|
SetBool(options::kNativeWindowOpen, true);
|
|
|
|
}
|
2019-07-12 01:56:04 +00:00
|
|
|
|
|
|
|
last_preference_ = preference_.Clone();
|
2019-05-27 00:44:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 15:19:04 +00:00
|
|
|
bool WebContentsPreferences::SetDefaultBoolIfUndefined(base::StringPiece key,
|
|
|
|
bool val) {
|
2018-08-03 21:23:07 +00:00
|
|
|
auto* current_value =
|
|
|
|
preference_.FindKeyOfType(key, base::Value::Type::BOOLEAN);
|
|
|
|
if (current_value) {
|
|
|
|
return current_value->GetBool();
|
|
|
|
} else {
|
|
|
|
preference_.SetKey(key, base::Value(val));
|
2018-08-29 21:57:49 +00:00
|
|
|
return val;
|
2018-03-15 04:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:19:04 +00:00
|
|
|
void WebContentsPreferences::SetBool(base::StringPiece key, bool value) {
|
2019-05-27 00:44:54 +00:00
|
|
|
preference_.SetKey(key, base::Value(value));
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:19:04 +00:00
|
|
|
bool WebContentsPreferences::IsEnabled(base::StringPiece name,
|
2018-08-03 21:23:07 +00:00
|
|
|
bool default_value) const {
|
|
|
|
auto* current_value =
|
|
|
|
preference_.FindKeyOfType(name, base::Value::Type::BOOLEAN);
|
|
|
|
if (current_value)
|
|
|
|
return current_value->GetBool();
|
|
|
|
return default_value;
|
2018-03-08 07:36:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 02:43:30 +00:00
|
|
|
void WebContentsPreferences::Merge(const base::DictionaryValue& extend) {
|
2018-08-03 21:23:07 +00:00
|
|
|
if (preference_.is_dict())
|
|
|
|
static_cast<base::DictionaryValue*>(&preference_)->MergeDictionary(&extend);
|
2019-05-27 00:44:54 +00:00
|
|
|
|
|
|
|
SetDefaults();
|
2018-08-03 21:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsPreferences::Clear() {
|
|
|
|
if (preference_.is_dict())
|
|
|
|
static_cast<base::DictionaryValue*>(&preference_)->Clear();
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:19:04 +00:00
|
|
|
bool WebContentsPreferences::GetPreference(base::StringPiece name,
|
2018-08-03 21:23:07 +00:00
|
|
|
std::string* value) const {
|
|
|
|
return GetAsString(&preference_, name, value);
|
2015-09-05 02:43:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 22:19:49 +00:00
|
|
|
bool WebContentsPreferences::GetPreloadPath(
|
|
|
|
base::FilePath::StringType* path) const {
|
|
|
|
DCHECK(path);
|
|
|
|
base::FilePath::StringType preload;
|
|
|
|
if (GetAsString(&preference_, options::kPreloadScript, &preload)) {
|
|
|
|
if (base::FilePath(preload).IsAbsolute()) {
|
|
|
|
*path = std::move(preload);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "preload script must have absolute path.";
|
|
|
|
}
|
|
|
|
} else if (GetAsString(&preference_, options::kPreloadURL, &preload)) {
|
|
|
|
// Translate to file path if there is "preload-url" option.
|
|
|
|
base::FilePath preload_path;
|
|
|
|
if (net::FileURLToFilePath(GURL(preload), &preload_path)) {
|
|
|
|
*path = std::move(preload_path.value());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "preload url must be file:// protocol.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-09 12:34:51 +00:00
|
|
|
// static
|
|
|
|
content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID(
|
|
|
|
int process_id) {
|
|
|
|
for (WebContentsPreferences* preferences : instances_) {
|
|
|
|
content::WebContents* web_contents = preferences->web_contents_;
|
2017-12-18 02:46:23 +00:00
|
|
|
if (web_contents->GetMainFrame()->GetProcess()->GetID() == process_id)
|
2016-03-09 12:34:51 +00:00
|
|
|
return web_contents;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-08 07:12:45 +00:00
|
|
|
// static
|
|
|
|
WebContentsPreferences* WebContentsPreferences::From(
|
|
|
|
content::WebContents* web_contents) {
|
|
|
|
if (!web_contents)
|
|
|
|
return nullptr;
|
|
|
|
return FromWebContents(web_contents);
|
|
|
|
}
|
|
|
|
|
2018-03-08 08:01:54 +00:00
|
|
|
void WebContentsPreferences::AppendCommandLineSwitches(
|
2019-06-20 10:10:56 +00:00
|
|
|
base::CommandLine* command_line,
|
|
|
|
bool is_subframe) {
|
2015-09-04 17:04:09 +00:00
|
|
|
// Check if plugins are enabled.
|
2018-08-03 21:23:07 +00:00
|
|
|
if (IsEnabled(options::kPlugins))
|
2015-09-04 17:04:09 +00:00
|
|
|
command_line->AppendSwitch(switches::kEnablePlugins);
|
|
|
|
|
2016-01-07 04:49:00 +00:00
|
|
|
// Experimental flags.
|
2018-08-03 21:23:07 +00:00
|
|
|
if (IsEnabled(options::kExperimentalFeatures))
|
2016-01-07 04:49:00 +00:00
|
|
|
command_line->AppendSwitch(
|
|
|
|
::switches::kEnableExperimentalWebPlatformFeatures);
|
2015-09-04 17:33:13 +00:00
|
|
|
|
|
|
|
// Check if we have node integration specified.
|
2019-01-03 16:22:34 +00:00
|
|
|
if (IsEnabled(options::kNodeIntegration))
|
|
|
|
command_line->AppendSwitch(switches::kNodeIntegration);
|
2015-09-04 17:33:13 +00:00
|
|
|
|
2017-03-15 09:51:21 +00:00
|
|
|
// Whether to enable node integration in Worker.
|
2018-08-03 21:23:07 +00:00
|
|
|
if (IsEnabled(options::kNodeIntegrationInWorker))
|
2017-03-15 09:51:21 +00:00
|
|
|
command_line->AppendSwitch(switches::kNodeIntegrationInWorker);
|
|
|
|
|
2017-05-17 20:09:24 +00:00
|
|
|
// Check if webview tag creation is enabled, default to nodeIntegration value.
|
2019-01-03 16:22:34 +00:00
|
|
|
if (IsEnabled(options::kWebviewTag))
|
|
|
|
command_line->AppendSwitch(switches::kWebviewTag);
|
2017-05-07 05:10:42 +00:00
|
|
|
|
2019-06-20 10:10:56 +00:00
|
|
|
// Sandbox can be enabled for renderer processes hosting cross-origin frames
|
|
|
|
// unless nodeIntegrationInSubFrames is enabled
|
|
|
|
bool can_sandbox_frame =
|
|
|
|
is_subframe && !IsEnabled(options::kNodeIntegrationInSubFrames);
|
|
|
|
|
2016-08-15 10:59:08 +00:00
|
|
|
// 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
|
2019-06-20 10:10:56 +00:00
|
|
|
// integration. Otherwise disable Chromium sandbox, unless app.enableSandbox()
|
|
|
|
// was called.
|
|
|
|
if (IsEnabled(options::kSandbox) || can_sandbox_frame) {
|
2016-08-15 10:59:08 +00:00
|
|
|
command_line->AppendSwitch(switches::kEnableSandbox);
|
2018-12-06 01:42:12 +00:00
|
|
|
} else if (!command_line->HasSwitch(switches::kEnableSandbox)) {
|
2018-09-15 00:07:49 +00:00
|
|
|
command_line->AppendSwitch(service_manager::switches::kNoSandbox);
|
2018-12-06 01:42:12 +00:00
|
|
|
command_line->AppendSwitch(::switches::kNoZygote);
|
|
|
|
}
|
2018-08-03 21:23:07 +00:00
|
|
|
|
|
|
|
// Check if nativeWindowOpen is enabled.
|
|
|
|
if (IsEnabled(options::kNativeWindowOpen))
|
2017-03-19 08:41:20 +00:00
|
|
|
command_line->AppendSwitch(switches::kNativeWindowOpen);
|
2016-08-15 10:59:08 +00:00
|
|
|
|
2015-09-04 17:33:13 +00:00
|
|
|
// The preload script.
|
|
|
|
base::FilePath::StringType preload;
|
2018-08-10 22:19:49 +00:00
|
|
|
if (GetPreloadPath(&preload))
|
|
|
|
command_line->AppendSwitchNative(switches::kPreloadScript, preload);
|
2015-09-04 17:33:13 +00:00
|
|
|
|
2018-02-12 17:54:31 +00:00
|
|
|
// Custom args for renderer process
|
2018-08-03 21:23:07 +00:00
|
|
|
auto* customArgs =
|
|
|
|
preference_.FindKeyOfType(options::kCustomArgs, base::Value::Type::LIST);
|
|
|
|
if (customArgs) {
|
|
|
|
for (const auto& customArg : customArgs->GetList()) {
|
2018-03-08 08:01:54 +00:00
|
|
|
if (customArg.is_string())
|
2018-02-12 17:54:31 +00:00
|
|
|
command_line->AppendArg(customArg.GetString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:52:06 +00:00
|
|
|
#if BUILDFLAG(ENABLE_REMOTE_MODULE)
|
2018-10-13 17:50:07 +00:00
|
|
|
// Whether to enable the remote module
|
2020-01-13 06:23:03 +00:00
|
|
|
if (IsEnabled(options::kEnableRemoteModule, true))
|
2019-08-28 22:57:03 +00:00
|
|
|
command_line->AppendSwitch(switches::kEnableRemoteModule);
|
2019-09-18 16:52:06 +00:00
|
|
|
#endif
|
2018-10-13 17:50:07 +00:00
|
|
|
|
2016-12-08 23:33:51 +00:00
|
|
|
// Run Electron APIs and preload script in isolated world
|
2018-08-03 21:23:07 +00:00
|
|
|
if (IsEnabled(options::kContextIsolation))
|
2016-12-15 21:20:17 +00:00
|
|
|
command_line->AppendSwitch(switches::kContextIsolation);
|
2016-12-08 23:33:51 +00:00
|
|
|
|
2016-04-02 11:35:57 +00:00
|
|
|
// --background-color.
|
2018-03-08 08:01:54 +00:00
|
|
|
std::string s;
|
2018-10-03 03:09:18 +00:00
|
|
|
if (GetAsString(&preference_, options::kBackgroundColor, &s)) {
|
2018-03-08 08:01:54 +00:00
|
|
|
command_line->AppendSwitchASCII(switches::kBackgroundColor, s);
|
2018-10-03 03:09:18 +00:00
|
|
|
} else if (!IsEnabled(options::kOffscreen)) {
|
|
|
|
// For non-OSR WebContents, we expect to have white background, see
|
|
|
|
// https://github.com/electron/electron/issues/13764 for more.
|
|
|
|
command_line->AppendSwitchASCII(switches::kBackgroundColor, "#fff");
|
|
|
|
}
|
2016-04-02 11:35:57 +00:00
|
|
|
|
2019-04-19 19:55:20 +00:00
|
|
|
// --offscreen
|
|
|
|
if (IsEnabled(options::kOffscreen)) {
|
|
|
|
command_line->AppendSwitch(options::kOffscreen);
|
|
|
|
}
|
|
|
|
|
2015-09-05 02:43:30 +00:00
|
|
|
// --guest-instance-id, which is used to identify guest WebContents.
|
2016-05-26 01:05:37 +00:00
|
|
|
int guest_instance_id = 0;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsInteger(&preference_, options::kGuestInstanceID, &guest_instance_id))
|
2016-04-13 13:56:11 +00:00
|
|
|
command_line->AppendSwitchASCII(switches::kGuestInstanceID,
|
2019-04-20 17:20:37 +00:00
|
|
|
base::NumberToString(guest_instance_id));
|
2015-12-17 13:27:14 +00:00
|
|
|
|
|
|
|
// Pass the opener's window id.
|
|
|
|
int opener_id;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsInteger(&preference_, options::kOpenerID, &opener_id))
|
2016-04-13 13:56:11 +00:00
|
|
|
command_line->AppendSwitchASCII(switches::kOpenerID,
|
2019-04-20 17:20:37 +00:00
|
|
|
base::NumberToString(opener_id));
|
2016-01-07 06:10:18 +00:00
|
|
|
|
2016-05-06 05:57:43 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
// Enable scroll bounce.
|
2018-08-03 21:23:07 +00:00
|
|
|
if (IsEnabled(options::kScrollBounce))
|
2016-05-06 05:57:43 +00:00
|
|
|
command_line->AppendSwitch(switches::kScrollBounce);
|
|
|
|
#endif
|
|
|
|
|
2016-05-29 01:34:53 +00:00
|
|
|
// Custom command line switches.
|
2018-08-03 21:23:07 +00:00
|
|
|
auto* args =
|
|
|
|
preference_.FindKeyOfType("commandLineSwitches", base::Value::Type::LIST);
|
|
|
|
if (args) {
|
|
|
|
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);
|
|
|
|
}
|
2016-05-29 01:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 06:10:18 +00:00
|
|
|
// Enable blink features.
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(&preference_, options::kEnableBlinkFeatures, &s))
|
2018-03-08 08:01:54 +00:00
|
|
|
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures, s);
|
2016-04-13 13:56:11 +00:00
|
|
|
|
2016-06-07 20:42:42 +00:00
|
|
|
// Disable blink features.
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(&preference_, options::kDisableBlinkFeatures, &s))
|
2018-03-08 08:01:54 +00:00
|
|
|
command_line->AppendSwitchASCII(::switches::kDisableBlinkFeatures, s);
|
2016-06-07 20:42:42 +00:00
|
|
|
|
2017-05-22 18:10:10 +00:00
|
|
|
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.
|
2018-03-08 08:01:54 +00:00
|
|
|
auto* manager = WebViewManager::GetWebViewManager(web_contents_);
|
2016-05-25 17:13:12 +00:00
|
|
|
if (manager) {
|
2018-03-08 08:01:54 +00:00
|
|
|
auto* embedder = manager->GetEmbedder(guest_instance_id);
|
2017-05-22 18:10:10 +00:00
|
|
|
if (embedder) {
|
2018-03-08 08:01:54 +00:00
|
|
|
auto* relay = NativeWindowRelay::FromWebContents(embedder);
|
2018-03-06 06:18:45 +00:00
|
|
|
if (relay) {
|
2018-10-02 22:14:43 +00:00
|
|
|
auto* window = relay->GetNativeWindow();
|
2018-03-06 06:18:45 +00:00
|
|
|
if (window) {
|
|
|
|
const bool visible = window->IsVisible() && !window->IsMinimized();
|
|
|
|
if (!visible) {
|
|
|
|
command_line->AppendSwitch(switches::kHiddenPage);
|
|
|
|
}
|
2017-06-07 00:02:23 +00:00
|
|
|
}
|
2017-05-22 18:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 17:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-08 08:01:54 +00:00
|
|
|
|
2019-01-22 19:24:46 +00:00
|
|
|
if (IsEnabled(options::kNodeIntegrationInSubFrames))
|
|
|
|
command_line->AppendSwitch(switches::kNodeIntegrationInSubFrames);
|
|
|
|
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
if (IsEnabled(options::kSpellcheck)) {
|
|
|
|
command_line->AppendSwitch(switches::kEnableSpellcheck);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-08 08:01:54 +00:00
|
|
|
// 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
|
2018-08-03 21:23:07 +00:00
|
|
|
last_preference_ = preference_.Clone();
|
2015-09-04 17:04:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:12:32 +00:00
|
|
|
void WebContentsPreferences::OverrideWebkitPrefs(
|
2018-03-08 08:01:54 +00:00
|
|
|
content::WebPreferences* prefs) {
|
2019-04-23 16:14:18 +00:00
|
|
|
prefs->javascript_enabled =
|
|
|
|
IsEnabled(options::kJavaScript, true /* default_value */);
|
|
|
|
prefs->images_enabled = IsEnabled(options::kImages, true /* default_value */);
|
2018-08-03 21:23:07 +00:00
|
|
|
prefs->text_areas_are_resizable =
|
2019-04-23 16:14:18 +00:00
|
|
|
IsEnabled(options::kTextAreasAreResizable, true /* default_value */);
|
2018-08-03 21:23:07 +00:00
|
|
|
prefs->navigate_on_drag_drop =
|
2019-04-23 16:14:18 +00:00
|
|
|
IsEnabled(options::kNavigateOnDragDrop, false /* default_value */);
|
2019-01-15 19:30:34 +00:00
|
|
|
if (!GetAsAutoplayPolicy(&preference_, "autoplayPolicy",
|
|
|
|
&prefs->autoplay_policy)) {
|
|
|
|
prefs->autoplay_policy = content::AutoplayPolicy::kNoUserGestureRequired;
|
|
|
|
}
|
2018-08-03 21:23:07 +00:00
|
|
|
|
|
|
|
// Check if webgl should be enabled.
|
2019-04-23 16:14:18 +00:00
|
|
|
bool is_webgl_enabled = IsEnabled(options::kWebGL, true /* default_value */);
|
2018-08-03 21:23:07 +00:00
|
|
|
prefs->webgl1_enabled = is_webgl_enabled;
|
|
|
|
prefs->webgl2_enabled = is_webgl_enabled;
|
|
|
|
|
|
|
|
// Check if web security should be enabled.
|
|
|
|
bool is_web_security_enabled =
|
|
|
|
IsEnabled(options::kWebSecurity, true /* default_value */);
|
|
|
|
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",
|
|
|
|
base::Value::Type::DICTIONARY);
|
|
|
|
if (fonts_dict) {
|
2016-01-19 14:13:23 +00:00
|
|
|
base::string16 font;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(fonts_dict, "standard", &font))
|
2016-01-19 14:13:23 +00:00
|
|
|
prefs->standard_font_family_map[content::kCommonScript] = font;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(fonts_dict, "serif", &font))
|
2016-01-19 14:13:23 +00:00
|
|
|
prefs->serif_font_family_map[content::kCommonScript] = font;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(fonts_dict, "sansSerif", &font))
|
2016-01-19 14:13:23 +00:00
|
|
|
prefs->sans_serif_font_family_map[content::kCommonScript] = font;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(fonts_dict, "monospace", &font))
|
2016-01-19 14:13:23 +00:00
|
|
|
prefs->fixed_font_family_map[content::kCommonScript] = font;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(fonts_dict, "cursive", &font))
|
2016-12-28 18:29:55 +00:00
|
|
|
prefs->cursive_font_family_map[content::kCommonScript] = font;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(fonts_dict, "fantasy", &font))
|
2016-12-28 18:29:55 +00:00
|
|
|
prefs->fantasy_font_family_map[content::kCommonScript] = font;
|
2016-01-19 14:13:23 +00:00
|
|
|
}
|
2018-08-03 21:23:07 +00:00
|
|
|
|
2016-01-19 14:13:23 +00:00
|
|
|
int size;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsInteger(&preference_, "defaultFontSize", &size))
|
2016-01-19 14:13:23 +00:00
|
|
|
prefs->default_font_size = size;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsInteger(&preference_, "defaultMonospaceFontSize", &size))
|
2016-01-19 14:13:23 +00:00
|
|
|
prefs->default_fixed_font_size = size;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsInteger(&preference_, "minimumFontSize", &size))
|
2016-01-19 14:13:23 +00:00
|
|
|
prefs->minimum_font_size = size;
|
|
|
|
std::string encoding;
|
2018-08-03 21:23:07 +00:00
|
|
|
if (GetAsString(&preference_, "defaultEncoding", &encoding))
|
2016-01-19 14:13:23 +00:00
|
|
|
prefs->default_encoding = encoding;
|
2015-09-04 17:12:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 16:56:54 +00:00
|
|
|
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPreferences)
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|