web_contents_preferences.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/web_contents_preferences.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include "atom/browser/native_window.h"
  9. #include "atom/browser/web_view_manager.h"
  10. #include "atom/common/native_mate_converters/value_converter.h"
  11. #include "atom/common/options_switches.h"
  12. #include "base/command_line.h"
  13. #include "base/memory/ptr_util.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "cc/base/switches.h"
  16. #include "content/public/browser/render_frame_host.h"
  17. #include "content/public/browser/render_process_host.h"
  18. #include "content/public/common/content_switches.h"
  19. #include "content/public/common/web_preferences.h"
  20. #include "native_mate/dictionary.h"
  21. #include "net/base/filename_util.h"
  22. #if defined(OS_WIN)
  23. #include "ui/gfx/switches.h"
  24. #endif
  25. DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::WebContentsPreferences);
  26. namespace atom {
  27. // static
  28. std::vector<WebContentsPreferences*> WebContentsPreferences::instances_;
  29. WebContentsPreferences::WebContentsPreferences(
  30. content::WebContents* web_contents,
  31. const mate::Dictionary& web_preferences)
  32. : web_contents_(web_contents) {
  33. v8::Isolate* isolate = web_preferences.isolate();
  34. mate::Dictionary copied(isolate, web_preferences.GetHandle()->Clone());
  35. // Following fields should not be stored.
  36. copied.Delete("embedder");
  37. copied.Delete("isGuest");
  38. copied.Delete("session");
  39. mate::ConvertFromV8(isolate, copied.GetHandle(), &dict_);
  40. web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
  41. instances_.push_back(this);
  42. // Set WebPreferences defaults onto the JS object
  43. SetDefaultBoolIfUndefined(options::kPlugins, false);
  44. SetDefaultBoolIfUndefined(options::kExperimentalFeatures, false);
  45. SetDefaultBoolIfUndefined(options::kExperimentalCanvasFeatures, false);
  46. bool node = SetDefaultBoolIfUndefined(options::kNodeIntegration, true);
  47. SetDefaultBoolIfUndefined(options::kNodeIntegrationInWorker, false);
  48. SetDefaultBoolIfUndefined(options::kWebviewTag, node);
  49. SetDefaultBoolIfUndefined(options::kSandbox, false);
  50. SetDefaultBoolIfUndefined(options::kNativeWindowOpen, false);
  51. SetDefaultBoolIfUndefined(options::kContextIsolation, false);
  52. SetDefaultBoolIfUndefined("javascript", true);
  53. SetDefaultBoolIfUndefined("images", true);
  54. SetDefaultBoolIfUndefined("textAreasAreResizable", true);
  55. SetDefaultBoolIfUndefined("webgl", true);
  56. bool webSecurity = true;
  57. SetDefaultBoolIfUndefined(options::kWebSecurity, webSecurity);
  58. // If webSecurity was explicity set to false, let's inherit that into
  59. // insecureContent
  60. if (web_preferences.Get(options::kWebSecurity, &webSecurity) &&
  61. !webSecurity) {
  62. SetDefaultBoolIfUndefined(options::kAllowRunningInsecureContent, true);
  63. } else {
  64. SetDefaultBoolIfUndefined(options::kAllowRunningInsecureContent, false);
  65. }
  66. #if defined(OS_MACOSX)
  67. SetDefaultBoolIfUndefined(options::kScrollBounce, false);
  68. #endif
  69. SetDefaultBoolIfUndefined(options::kOffscreen, false);
  70. last_dict_ = std::move(*dict_.CreateDeepCopy());
  71. }
  72. WebContentsPreferences::~WebContentsPreferences() {
  73. instances_.erase(std::remove(instances_.begin(), instances_.end(), this),
  74. instances_.end());
  75. }
  76. bool WebContentsPreferences::SetDefaultBoolIfUndefined(
  77. const base::StringPiece& key,
  78. bool val) {
  79. bool existing;
  80. if (!dict_.GetBoolean(key, &existing)) {
  81. dict_.SetBoolean(key, val);
  82. return val;
  83. }
  84. return existing;
  85. }
  86. bool WebContentsPreferences::IsEnabled(const base::StringPiece& name,
  87. bool default_value) {
  88. bool bool_value = default_value;
  89. dict_.GetBoolean(name, &bool_value);
  90. return bool_value;
  91. }
  92. void WebContentsPreferences::Merge(const base::DictionaryValue& extend) {
  93. dict_.MergeDictionary(&extend);
  94. }
  95. bool WebContentsPreferences::GetPreloadPath(
  96. base::FilePath::StringType* path) const {
  97. DCHECK(path);
  98. base::FilePath::StringType preload;
  99. if (dict_.GetString(options::kPreloadScript, &preload)) {
  100. if (base::FilePath(preload).IsAbsolute()) {
  101. *path = std::move(preload);
  102. return true;
  103. } else {
  104. LOG(ERROR) << "preload script must have absolute path.";
  105. }
  106. } else if (dict_.GetString(options::kPreloadURL, &preload)) {
  107. // Translate to file path if there is "preload-url" option.
  108. base::FilePath preload_path;
  109. if (net::FileURLToFilePath(GURL(preload), &preload_path)) {
  110. *path = std::move(preload_path.value());
  111. return true;
  112. } else {
  113. LOG(ERROR) << "preload url must be file:// protocol.";
  114. }
  115. }
  116. return false;
  117. }
  118. // static
  119. content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID(
  120. int process_id) {
  121. for (WebContentsPreferences* preferences : instances_) {
  122. content::WebContents* web_contents = preferences->web_contents_;
  123. if (web_contents->GetMainFrame()->GetProcess()->GetID() == process_id)
  124. return web_contents;
  125. }
  126. return nullptr;
  127. }
  128. // static
  129. WebContentsPreferences* WebContentsPreferences::From(
  130. content::WebContents* web_contents) {
  131. if (!web_contents)
  132. return nullptr;
  133. return FromWebContents(web_contents);
  134. }
  135. void WebContentsPreferences::AppendCommandLineSwitches(
  136. base::CommandLine* command_line) {
  137. bool b;
  138. // Check if plugins are enabled.
  139. if (dict_.GetBoolean(options::kPlugins, &b) && b)
  140. command_line->AppendSwitch(switches::kEnablePlugins);
  141. // Experimental flags.
  142. if (dict_.GetBoolean(options::kExperimentalFeatures, &b) && b)
  143. command_line->AppendSwitch(
  144. ::switches::kEnableExperimentalWebPlatformFeatures);
  145. if (dict_.GetBoolean(options::kExperimentalCanvasFeatures, &b) && b)
  146. command_line->AppendSwitch(::switches::kEnableExperimentalCanvasFeatures);
  147. // Check if we have node integration specified.
  148. bool node_integration = true;
  149. dict_.GetBoolean(options::kNodeIntegration, &node_integration);
  150. command_line->AppendSwitchASCII(switches::kNodeIntegration,
  151. node_integration ? "true" : "false");
  152. // Whether to enable node integration in Worker.
  153. if (dict_.GetBoolean(options::kNodeIntegrationInWorker, &b) && b)
  154. command_line->AppendSwitch(switches::kNodeIntegrationInWorker);
  155. // Check if webview tag creation is enabled, default to nodeIntegration value.
  156. // TODO(kevinsawicki): Default to false in 2.0
  157. bool webview_tag = node_integration;
  158. dict_.GetBoolean(options::kWebviewTag, &webview_tag);
  159. command_line->AppendSwitchASCII(switches::kWebviewTag,
  160. webview_tag ? "true" : "false");
  161. // If the `sandbox` option was passed to the BrowserWindow's webPreferences,
  162. // pass `--enable-sandbox` to the renderer so it won't have any node.js
  163. // integration.
  164. if (dict_.GetBoolean(options::kSandbox, &b) && b)
  165. command_line->AppendSwitch(switches::kEnableSandbox);
  166. else if (!command_line->HasSwitch(switches::kEnableSandbox))
  167. command_line->AppendSwitch(::switches::kNoSandbox);
  168. if (dict_.GetBoolean(options::kNativeWindowOpen, &b) && b)
  169. command_line->AppendSwitch(switches::kNativeWindowOpen);
  170. // The preload script.
  171. base::FilePath::StringType preload;
  172. if (GetPreloadPath(&preload))
  173. command_line->AppendSwitchNative(switches::kPreloadScript, preload);
  174. // Custom args for renderer process
  175. base::Value* customArgs;
  176. if (dict_.Get(options::kCustomArgs, &customArgs) && customArgs->is_list()) {
  177. for (const base::Value& customArg : customArgs->GetList()) {
  178. if (customArg.is_string())
  179. command_line->AppendArg(customArg.GetString());
  180. }
  181. }
  182. // Run Electron APIs and preload script in isolated world
  183. if (dict_.GetBoolean(options::kContextIsolation, &b) && b)
  184. command_line->AppendSwitch(switches::kContextIsolation);
  185. // --background-color.
  186. std::string s;
  187. if (dict_.GetString(options::kBackgroundColor, &s)) {
  188. command_line->AppendSwitchASCII(switches::kBackgroundColor, s);
  189. } else if (!(dict_.GetBoolean(options::kOffscreen, &b) && b)) {
  190. // For non-OSR WebContents, we expect to have white background, see
  191. // https://github.com/electron/electron/issues/13764 for more.
  192. command_line->AppendSwitchASCII(switches::kBackgroundColor, "#fff");
  193. }
  194. // --guest-instance-id, which is used to identify guest WebContents.
  195. int guest_instance_id = 0;
  196. if (dict_.GetInteger(options::kGuestInstanceID, &guest_instance_id))
  197. command_line->AppendSwitchASCII(switches::kGuestInstanceID,
  198. base::IntToString(guest_instance_id));
  199. // Pass the opener's window id.
  200. int opener_id;
  201. if (dict_.GetInteger(options::kOpenerID, &opener_id))
  202. command_line->AppendSwitchASCII(switches::kOpenerID,
  203. base::IntToString(opener_id));
  204. #if defined(OS_MACOSX)
  205. // Enable scroll bounce.
  206. if (dict_.GetBoolean(options::kScrollBounce, &b) && b)
  207. command_line->AppendSwitch(switches::kScrollBounce);
  208. #endif
  209. // Custom command line switches.
  210. const base::ListValue* args;
  211. if (dict_.GetList("commandLineSwitches", &args)) {
  212. for (size_t i = 0; i < args->GetSize(); ++i) {
  213. std::string arg;
  214. if (args->GetString(i, &arg) && !arg.empty())
  215. command_line->AppendSwitch(arg);
  216. }
  217. }
  218. // Enable blink features.
  219. if (dict_.GetString(options::kEnableBlinkFeatures, &s))
  220. command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures, s);
  221. // Disable blink features.
  222. if (dict_.GetString(options::kDisableBlinkFeatures, &s))
  223. command_line->AppendSwitchASCII(::switches::kDisableBlinkFeatures, s);
  224. if (guest_instance_id) {
  225. // Webview `document.visibilityState` tracks window visibility so we need
  226. // to let it know if the window happens to be hidden right now.
  227. auto* manager = WebViewManager::GetWebViewManager(web_contents_);
  228. if (manager) {
  229. auto* embedder = manager->GetEmbedder(guest_instance_id);
  230. if (embedder) {
  231. auto* relay = NativeWindowRelay::FromWebContents(embedder);
  232. if (relay) {
  233. auto* window = relay->window.get();
  234. if (window) {
  235. const bool visible = window->IsVisible() && !window->IsMinimized();
  236. if (!visible) {
  237. command_line->AppendSwitch(switches::kHiddenPage);
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. // We are appending args to a webContents so let's save the current state
  245. // of our preferences object so that during the lifetime of the WebContents
  246. // we can fetch the options used to initally configure the WebContents
  247. last_dict_ = std::move(*dict_.CreateDeepCopy());
  248. }
  249. void WebContentsPreferences::OverrideWebkitPrefs(
  250. content::WebPreferences* prefs) {
  251. bool b;
  252. if (dict_.GetBoolean("javascript", &b))
  253. prefs->javascript_enabled = b;
  254. if (dict_.GetBoolean("images", &b))
  255. prefs->images_enabled = b;
  256. if (dict_.GetBoolean("textAreasAreResizable", &b))
  257. prefs->text_areas_are_resizable = b;
  258. if (dict_.GetBoolean("webgl", &b)) {
  259. prefs->webgl1_enabled = b;
  260. prefs->webgl2_enabled = b;
  261. }
  262. if (dict_.GetBoolean(options::kWebSecurity, &b)) {
  263. prefs->web_security_enabled = b;
  264. prefs->allow_running_insecure_content = !b;
  265. }
  266. if (dict_.GetBoolean(options::kAllowRunningInsecureContent, &b))
  267. prefs->allow_running_insecure_content = b;
  268. if (dict_.GetBoolean("navigateOnDragDrop", &b))
  269. prefs->navigate_on_drag_drop = b;
  270. const base::DictionaryValue* fonts = nullptr;
  271. if (dict_.GetDictionary("defaultFontFamily", &fonts)) {
  272. base::string16 font;
  273. if (fonts->GetString("standard", &font))
  274. prefs->standard_font_family_map[content::kCommonScript] = font;
  275. if (fonts->GetString("serif", &font))
  276. prefs->serif_font_family_map[content::kCommonScript] = font;
  277. if (fonts->GetString("sansSerif", &font))
  278. prefs->sans_serif_font_family_map[content::kCommonScript] = font;
  279. if (fonts->GetString("monospace", &font))
  280. prefs->fixed_font_family_map[content::kCommonScript] = font;
  281. if (fonts->GetString("cursive", &font))
  282. prefs->cursive_font_family_map[content::kCommonScript] = font;
  283. if (fonts->GetString("fantasy", &font))
  284. prefs->fantasy_font_family_map[content::kCommonScript] = font;
  285. }
  286. int size;
  287. if (GetInteger("defaultFontSize", &size))
  288. prefs->default_font_size = size;
  289. if (GetInteger("defaultMonospaceFontSize", &size))
  290. prefs->default_fixed_font_size = size;
  291. if (GetInteger("minimumFontSize", &size))
  292. prefs->minimum_font_size = size;
  293. std::string encoding;
  294. if (dict_.GetString("defaultEncoding", &encoding))
  295. prefs->default_encoding = encoding;
  296. bool node_integration = false;
  297. dict_.GetBoolean(options::kNodeIntegration, &node_integration);
  298. prefs->node_integration = node_integration;
  299. }
  300. bool WebContentsPreferences::GetInteger(const base::StringPiece& attribute_name,
  301. int* val) {
  302. // if it is already an integer, no conversion needed
  303. if (dict_.GetInteger(attribute_name, val))
  304. return true;
  305. std::string str;
  306. if (dict_.GetString(attribute_name, &str))
  307. return base::StringToInt(str, val);
  308. return false;
  309. }
  310. } // namespace atom