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.
|
|
|
|
|
|
|
|
#include "atom/browser/web_contents_preferences.h"
|
|
|
|
|
2016-03-09 12:34:51 +00:00
|
|
|
#include <algorithm>
|
2015-09-05 11:47:44 +00:00
|
|
|
#include <string>
|
2016-03-09 12:34:51 +00:00
|
|
|
#include <vector>
|
2015-09-05 11:47:44 +00:00
|
|
|
|
2015-11-11 02:32:25 +00:00
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
2015-09-04 17:04:09 +00:00
|
|
|
#include "atom/common/options_switches.h"
|
|
|
|
#include "base/command_line.h"
|
2015-09-04 17:33:13 +00:00
|
|
|
#include "base/strings/string_number_conversions.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"
|
2015-11-11 02:32:25 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-09-05 01:57:39 +00:00
|
|
|
#include "net/base/filename_util.h"
|
2015-09-04 17:04:09 +00:00
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "ui/gfx/switches.h"
|
|
|
|
#endif
|
|
|
|
|
2015-09-07 07:55:08 +00:00
|
|
|
DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::WebContentsPreferences);
|
|
|
|
|
2015-09-04 16:44:22 +00:00
|
|
|
namespace atom {
|
|
|
|
|
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,
|
2016-03-09 12:34:51 +00:00
|
|
|
const mate::Dictionary& web_preferences)
|
|
|
|
: web_contents_(web_contents) {
|
2015-11-11 02:32:25 +00:00
|
|
|
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(), &web_preferences_);
|
2015-09-07 07:55:08 +00:00
|
|
|
web_contents->SetUserData(UserDataKey(), this);
|
2016-03-09 12:34:51 +00:00
|
|
|
|
|
|
|
instances_.push_back(this);
|
2015-09-04 16:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebContentsPreferences::~WebContentsPreferences() {
|
2016-03-09 12:34:51 +00:00
|
|
|
instances_.erase(
|
|
|
|
std::remove(instances_.begin(), instances_.end(), this),
|
|
|
|
instances_.end());
|
2015-09-04 16:44:22 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 02:43:30 +00:00
|
|
|
void WebContentsPreferences::Merge(const base::DictionaryValue& extend) {
|
|
|
|
web_preferences_.MergeDictionary(&extend);
|
|
|
|
}
|
|
|
|
|
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_;
|
|
|
|
if (web_contents->GetRenderProcessHost()->GetID() == process_id)
|
|
|
|
return web_contents;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:04:09 +00:00
|
|
|
// static
|
|
|
|
void WebContentsPreferences::AppendExtraCommandLineSwitches(
|
|
|
|
content::WebContents* web_contents, base::CommandLine* command_line) {
|
2015-09-07 07:55:08 +00:00
|
|
|
WebContentsPreferences* self = FromWebContents(web_contents);
|
2015-09-05 02:43:30 +00:00
|
|
|
if (!self)
|
|
|
|
return;
|
|
|
|
|
2015-09-04 17:33:13 +00:00
|
|
|
base::DictionaryValue& web_preferences = self->web_preferences_;
|
2015-09-04 17:04:09 +00:00
|
|
|
|
|
|
|
bool b;
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
// Check if DirectWrite is disabled.
|
2015-11-13 05:58:31 +00:00
|
|
|
if (web_preferences.GetBoolean(options::kDirectWrite, &b) && !b)
|
2015-09-04 17:04:09 +00:00
|
|
|
command_line->AppendSwitch(::switches::kDisableDirectWrite);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Check if plugins are enabled.
|
2015-09-04 17:33:13 +00:00
|
|
|
if (web_preferences.GetBoolean("plugins", &b) && b)
|
2015-09-04 17:04:09 +00:00
|
|
|
command_line->AppendSwitch(switches::kEnablePlugins);
|
|
|
|
|
2016-01-07 04:49:00 +00:00
|
|
|
// Experimental flags.
|
|
|
|
if (web_preferences.GetBoolean(options::kExperimentalFeatures, &b) && b)
|
|
|
|
command_line->AppendSwitch(
|
|
|
|
::switches::kEnableExperimentalWebPlatformFeatures);
|
|
|
|
if (web_preferences.GetBoolean(options::kExperimentalCanvasFeatures, &b) && b)
|
|
|
|
command_line->AppendSwitch(::switches::kEnableExperimentalCanvasFeatures);
|
2015-09-04 17:33:13 +00:00
|
|
|
|
|
|
|
// Check if we have node integration specified.
|
|
|
|
bool node_integration = true;
|
2015-11-13 05:58:31 +00:00
|
|
|
web_preferences.GetBoolean(options::kNodeIntegration, &node_integration);
|
2015-09-04 17:33:13 +00:00
|
|
|
command_line->AppendSwitchASCII(switches::kNodeIntegration,
|
|
|
|
node_integration ? "true" : "false");
|
|
|
|
|
|
|
|
// The preload script.
|
|
|
|
base::FilePath::StringType preload;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (web_preferences.GetString(options::kPreloadScript, &preload)) {
|
2015-09-05 01:57:39 +00:00
|
|
|
if (base::FilePath(preload).IsAbsolute())
|
|
|
|
command_line->AppendSwitchNative(switches::kPreloadScript, preload);
|
|
|
|
else
|
2015-09-19 21:11:29 +00:00
|
|
|
LOG(ERROR) << "preload script must have absolute path.";
|
2015-11-13 08:03:40 +00:00
|
|
|
} else if (web_preferences.GetString(options::kPreloadURL, &preload)) {
|
2015-09-05 01:57:39 +00:00
|
|
|
// Translate to file path if there is "preload-url" option.
|
|
|
|
base::FilePath preload_path;
|
|
|
|
if (net::FileURLToFilePath(GURL(preload), &preload_path))
|
|
|
|
command_line->AppendSwitchPath(switches::kPreloadScript, preload_path);
|
|
|
|
else
|
|
|
|
LOG(ERROR) << "preload url must be file:// protocol.";
|
|
|
|
}
|
2015-09-04 17:33:13 +00:00
|
|
|
|
2016-04-02 11:35:57 +00:00
|
|
|
// --background-color.
|
|
|
|
std::string color;
|
|
|
|
if (web_preferences.GetString(options::kBackgroundColor, &color))
|
|
|
|
command_line->AppendSwitchASCII(switches::kBackgroundColor, color);
|
|
|
|
|
2015-09-04 17:33:13 +00:00
|
|
|
// The zoom factor.
|
|
|
|
double zoom_factor = 1.0;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (web_preferences.GetDouble(options::kZoomFactor, &zoom_factor) &&
|
2015-09-04 17:33:13 +00:00
|
|
|
zoom_factor != 1.0)
|
|
|
|
command_line->AppendSwitchASCII(switches::kZoomFactor,
|
|
|
|
base::DoubleToString(zoom_factor));
|
2015-09-05 02:43:30 +00:00
|
|
|
|
|
|
|
// --guest-instance-id, which is used to identify guest WebContents.
|
|
|
|
int guest_instance_id;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (web_preferences.GetInteger(options::kGuestInstanceID, &guest_instance_id))
|
2015-09-05 02:43:30 +00:00
|
|
|
command_line->AppendSwitchASCII(switches::kGuestInstanceID,
|
|
|
|
base::IntToString(guest_instance_id));
|
2015-12-17 13:27:14 +00:00
|
|
|
|
|
|
|
// Pass the opener's window id.
|
|
|
|
int opener_id;
|
|
|
|
if (web_preferences.GetInteger(options::kOpenerID, &opener_id))
|
|
|
|
command_line->AppendSwitchASCII(switches::kOpenerID,
|
|
|
|
base::IntToString(opener_id));
|
2016-01-07 06:10:18 +00:00
|
|
|
|
|
|
|
// Enable blink features.
|
|
|
|
std::string blink_features;
|
|
|
|
if (web_preferences.GetString(options::kBlinkFeatures, &blink_features))
|
|
|
|
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures,
|
|
|
|
blink_features);
|
2015-09-04 17:04:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 02:43:30 +00:00
|
|
|
// static
|
2015-09-04 17:12:32 +00:00
|
|
|
void WebContentsPreferences::OverrideWebkitPrefs(
|
|
|
|
content::WebContents* web_contents, content::WebPreferences* prefs) {
|
2015-09-07 07:55:08 +00:00
|
|
|
WebContentsPreferences* self = FromWebContents(web_contents);
|
2015-09-05 02:43:30 +00:00
|
|
|
if (!self)
|
|
|
|
return;
|
2015-09-04 17:12:32 +00:00
|
|
|
|
|
|
|
bool b;
|
|
|
|
if (self->web_preferences_.GetBoolean("javascript", &b))
|
|
|
|
prefs->javascript_enabled = b;
|
|
|
|
if (self->web_preferences_.GetBoolean("images", &b))
|
|
|
|
prefs->images_enabled = b;
|
2015-11-10 14:23:21 +00:00
|
|
|
if (self->web_preferences_.GetBoolean("textAreasAreResizable", &b))
|
2015-09-04 17:12:32 +00:00
|
|
|
prefs->text_areas_are_resizable = b;
|
|
|
|
if (self->web_preferences_.GetBoolean("webgl", &b))
|
|
|
|
prefs->experimental_webgl_enabled = b;
|
|
|
|
if (self->web_preferences_.GetBoolean("webaudio", &b))
|
|
|
|
prefs->webaudio_enabled = b;
|
2015-11-10 14:23:21 +00:00
|
|
|
if (self->web_preferences_.GetBoolean("webSecurity", &b)) {
|
2015-09-04 17:12:32 +00:00
|
|
|
prefs->web_security_enabled = b;
|
|
|
|
prefs->allow_displaying_insecure_content = !b;
|
|
|
|
prefs->allow_running_insecure_content = !b;
|
|
|
|
}
|
2015-11-13 05:58:31 +00:00
|
|
|
if (self->web_preferences_.GetBoolean("allowDisplayingInsecureContent", &b))
|
2015-09-04 17:12:32 +00:00
|
|
|
prefs->allow_displaying_insecure_content = b;
|
2015-11-10 14:23:21 +00:00
|
|
|
if (self->web_preferences_.GetBoolean("allowRunningInsecureContent", &b))
|
2015-09-04 17:12:32 +00:00
|
|
|
prefs->allow_running_insecure_content = b;
|
2016-01-19 14:13:23 +00:00
|
|
|
const base::DictionaryValue* fonts = nullptr;
|
|
|
|
if (self->web_preferences_.GetDictionary("defaultFontFamily", &fonts)) {
|
|
|
|
base::string16 font;
|
|
|
|
if (fonts->GetString("standard", &font))
|
|
|
|
prefs->standard_font_family_map[content::kCommonScript] = font;
|
|
|
|
if (fonts->GetString("serif", &font))
|
|
|
|
prefs->serif_font_family_map[content::kCommonScript] = font;
|
|
|
|
if (fonts->GetString("sansSerif", &font))
|
|
|
|
prefs->sans_serif_font_family_map[content::kCommonScript] = font;
|
|
|
|
if (fonts->GetString("monospace", &font))
|
|
|
|
prefs->fixed_font_family_map[content::kCommonScript] = font;
|
|
|
|
}
|
|
|
|
int size;
|
|
|
|
if (self->web_preferences_.GetInteger("defaultFontSize", &size))
|
|
|
|
prefs->default_font_size = size;
|
|
|
|
if (self->web_preferences_.GetInteger("defaultMonospaceFontSize", &size))
|
|
|
|
prefs->default_fixed_font_size = size;
|
|
|
|
if (self->web_preferences_.GetInteger("minimumFontSize", &size))
|
|
|
|
prefs->minimum_font_size = size;
|
|
|
|
std::string encoding;
|
|
|
|
if (self->web_preferences_.GetString("defaultEncoding", &encoding))
|
|
|
|
prefs->default_encoding = encoding;
|
2015-09-04 17:12:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 16:44:22 +00:00
|
|
|
} // namespace atom
|