/
web_contents_preferences.cc
407 lines (355 loc) · 14.2 KB
/
web_contents_preferences.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/web_contents_preferences.h"
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include "atom/browser/native_window.h"
#include "atom/browser/web_view_manager.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "cc/base/switches.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/web_preferences.h"
#include "native_mate/dictionary.h"
#include "net/base/filename_util.h"
#include "services/service_manager/sandbox/switches.h"
#if defined(OS_WIN)
#include "ui/gfx/switches.h"
#endif
namespace {
bool GetAsString(const base::Value* val,
const base::StringPiece& path,
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,
const base::StringPiece& path,
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;
}
bool GetAsInteger(const base::Value* val,
const 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;
}
} // namespace
namespace atom {
// static
std::vector<WebContentsPreferences*> WebContentsPreferences::instances_;
WebContentsPreferences::WebContentsPreferences(
content::WebContents* web_contents,
const mate::Dictionary& web_preferences)
: web_contents_(web_contents) {
v8::Isolate* isolate = web_preferences.isolate();
mate::Dictionary copied(isolate, web_preferences.GetHandle()->Clone());
// Following fields should not be stored.
copied.Delete("embedder");
copied.Delete("isGuest");
copied.Delete("session");
mate::ConvertFromV8(isolate, copied.GetHandle(), &preference_);
web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
instances_.push_back(this);
// Set WebPreferences defaults onto the JS object
SetDefaultBoolIfUndefined(options::kPlugins, false);
SetDefaultBoolIfUndefined(options::kExperimentalFeatures, false);
SetDefaultBoolIfUndefined(options::kNodeIntegration, false);
SetDefaultBoolIfUndefined(options::kNodeIntegrationInWorker, false);
SetDefaultBoolIfUndefined(options::kWebviewTag, false);
SetDefaultBoolIfUndefined(options::kSandbox, false);
SetDefaultBoolIfUndefined(options::kNativeWindowOpen, false);
SetDefaultBoolIfUndefined(options::kContextIsolation, false);
SetDefaultBoolIfUndefined("javascript", true);
SetDefaultBoolIfUndefined("images", true);
SetDefaultBoolIfUndefined("textAreasAreResizable", true);
SetDefaultBoolIfUndefined("webgl", true);
bool webSecurity = true;
SetDefaultBoolIfUndefined(options::kWebSecurity, webSecurity);
// If webSecurity was explicity 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_MACOSX)
SetDefaultBoolIfUndefined(options::kScrollBounce, false);
#endif
SetDefaultBoolIfUndefined(options::kOffscreen, false);
last_preference_ = preference_.Clone();
}
WebContentsPreferences::~WebContentsPreferences() {
instances_.erase(std::remove(instances_.begin(), instances_.end(), this),
instances_.end());
}
bool WebContentsPreferences::SetDefaultBoolIfUndefined(
const 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;
}
}
bool WebContentsPreferences::IsEnabled(const 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);
}
void WebContentsPreferences::Clear() {
if (preference_.is_dict())
static_cast<base::DictionaryValue*>(&preference_)->Clear();
}
bool WebContentsPreferences::GetPreference(const base::StringPiece& name,
std::string* value) const {
return GetAsString(&preference_, name, value);
}
bool WebContentsPreferences::IsRemoteModuleEnabled() const {
return IsEnabled(options::kEnableRemoteModule, true);
}
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;
}
// static
content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID(
int process_id) {
for (WebContentsPreferences* preferences : instances_) {
content::WebContents* web_contents = preferences->web_contents_;
if (web_contents->GetMainFrame()->GetProcess()->GetID() == process_id)
return web_contents;
}
return nullptr;
}
// static
WebContentsPreferences* WebContentsPreferences::From(
content::WebContents* web_contents) {
if (!web_contents)
return nullptr;
return FromWebContents(web_contents);
}
void WebContentsPreferences::AppendCommandLineSwitches(
base::CommandLine* command_line) {
// Check if plugins are enabled.
if (IsEnabled(options::kPlugins))
command_line->AppendSwitch(switches::kEnablePlugins);
// Experimental flags.
if (IsEnabled(options::kExperimentalFeatures))
command_line->AppendSwitch(
::switches::kEnableExperimentalWebPlatformFeatures);
// Check if we have node integration specified.
if (IsEnabled(options::kNodeIntegration))
command_line->AppendSwitch(switches::kNodeIntegration);
// Whether to enable node integration in Worker.
if (IsEnabled(options::kNodeIntegrationInWorker))
command_line->AppendSwitch(switches::kNodeIntegrationInWorker);
// Check if webview tag creation is enabled, default to nodeIntegration value.
if (IsEnabled(options::kWebviewTag))
command_line->AppendSwitch(switches::kWebviewTag);
// 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.
if (IsEnabled(options::kSandbox)) {
command_line->AppendSwitch(switches::kEnableSandbox);
} else if (!command_line->HasSwitch(switches::kEnableSandbox)) {
command_line->AppendSwitch(service_manager::switches::kNoSandbox);
command_line->AppendSwitch(::switches::kNoZygote);
}
// Check if nativeWindowOpen is enabled.
if (IsEnabled(options::kNativeWindowOpen))
command_line->AppendSwitch(switches::kNativeWindowOpen);
// The preload script.
base::FilePath::StringType preload;
if (GetPreloadPath(&preload))
command_line->AppendSwitchNative(switches::kPreloadScript, preload);
// 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());
}
}
// Whether to enable the remote module
if (!IsRemoteModuleEnabled())
command_line->AppendSwitch(switches::kDisableRemoteModule);
// Run Electron APIs and preload script in isolated world
if (IsEnabled(options::kContextIsolation))
command_line->AppendSwitch(switches::kContextIsolation);
// --background-color.
std::string s;
if (GetAsString(&preference_, options::kBackgroundColor, &s)) {
command_line->AppendSwitchASCII(switches::kBackgroundColor, s);
} 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");
}
// --guest-instance-id, which is used to identify guest WebContents.
int guest_instance_id = 0;
if (GetAsInteger(&preference_, 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 (GetAsInteger(&preference_, options::kOpenerID, &opener_id))
command_line->AppendSwitchASCII(switches::kOpenerID,
base::IntToString(opener_id));
#if defined(OS_MACOSX)
// Enable scroll bounce.
if (IsEnabled(options::kScrollBounce))
command_line->AppendSwitch(switches::kScrollBounce);
#endif
// Custom command line switches.
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);
}
}
}
// Enable blink features.
if (GetAsString(&preference_, options::kEnableBlinkFeatures, &s))
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures, s);
// Disable blink features.
if (GetAsString(&preference_, 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_);
if (manager) {
auto* embedder = manager->GetEmbedder(guest_instance_id);
if (embedder) {
auto* relay = NativeWindowRelay::FromWebContents(embedder);
if (relay) {
auto* window = relay->GetNativeWindow();
if (window) {
const bool visible = window->IsVisible() && !window->IsMinimized();
if (!visible) {
command_line->AppendSwitch(switches::kHiddenPage);
}
}
}
}
}
}
// 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_preference_ = preference_.Clone();
}
void WebContentsPreferences::OverrideWebkitPrefs(
content::WebPreferences* prefs) {
prefs->javascript_enabled = IsEnabled("javascript", true /* default_value */);
prefs->images_enabled = IsEnabled("images", true /* default_value */);
prefs->text_areas_are_resizable =
IsEnabled("textAreasAreResizable", true /* default_value */);
prefs->navigate_on_drag_drop =
IsEnabled("navigateOnDragDrop", false /* default_value */);
// Check if webgl should be enabled.
bool is_webgl_enabled = IsEnabled("webgl", true /* default_value */);
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) {
base::string16 font;
if (GetAsString(fonts_dict, "standard", &font))
prefs->standard_font_family_map[content::kCommonScript] = font;
if (GetAsString(fonts_dict, "serif", &font))
prefs->serif_font_family_map[content::kCommonScript] = font;
if (GetAsString(fonts_dict, "sansSerif", &font))
prefs->sans_serif_font_family_map[content::kCommonScript] = font;
if (GetAsString(fonts_dict, "monospace", &font))
prefs->fixed_font_family_map[content::kCommonScript] = font;
if (GetAsString(fonts_dict, "cursive", &font))
prefs->cursive_font_family_map[content::kCommonScript] = font;
if (GetAsString(fonts_dict, "fantasy", &font))
prefs->fantasy_font_family_map[content::kCommonScript] = font;
}
int size;
if (GetAsInteger(&preference_, "defaultFontSize", &size))
prefs->default_font_size = size;
if (GetAsInteger(&preference_, "defaultMonospaceFontSize", &size))
prefs->default_fixed_font_size = size;
if (GetAsInteger(&preference_, "minimumFontSize", &size))
prefs->minimum_font_size = size;
std::string encoding;
if (GetAsString(&preference_, "defaultEncoding", &encoding))
prefs->default_encoding = encoding;
}
} // namespace atom