web_contents_preferences.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. // static
  96. content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID(
  97. int process_id) {
  98. for (WebContentsPreferences* preferences : instances_) {
  99. content::WebContents* web_contents = preferences->web_contents_;
  100. if (web_contents->GetMainFrame()->GetProcess()->GetID() == process_id)
  101. return web_contents;
  102. }
  103. return nullptr;
  104. }
  105. // static
  106. WebContentsPreferences* WebContentsPreferences::From(
  107. content::WebContents* web_contents) {
  108. if (!web_contents)
  109. return nullptr;
  110. return FromWebContents(web_contents);
  111. }
  112. void WebContentsPreferences::AppendCommandLineSwitches(
  113. base::CommandLine* command_line) {
  114. bool b;
  115. // Check if plugins are enabled.
  116. if (dict_.GetBoolean(options::kPlugins, &b) && b)
  117. command_line->AppendSwitch(switches::kEnablePlugins);
  118. // Experimental flags.
  119. if (dict_.GetBoolean(options::kExperimentalFeatures, &b) && b)
  120. command_line->AppendSwitch(
  121. ::switches::kEnableExperimentalWebPlatformFeatures);
  122. if (dict_.GetBoolean(options::kExperimentalCanvasFeatures, &b) && b)
  123. command_line->AppendSwitch(::switches::kEnableExperimentalCanvasFeatures);
  124. // Check if we have node integration specified.
  125. bool node_integration = true;
  126. dict_.GetBoolean(options::kNodeIntegration, &node_integration);
  127. command_line->AppendSwitchASCII(switches::kNodeIntegration,
  128. node_integration ? "true" : "false");
  129. // Whether to enable node integration in Worker.
  130. if (dict_.GetBoolean(options::kNodeIntegrationInWorker, &b) && b)
  131. command_line->AppendSwitch(switches::kNodeIntegrationInWorker);
  132. // Check if webview tag creation is enabled, default to nodeIntegration value.
  133. // TODO(kevinsawicki): Default to false in 2.0
  134. bool webview_tag = node_integration;
  135. dict_.GetBoolean(options::kWebviewTag, &webview_tag);
  136. command_line->AppendSwitchASCII(switches::kWebviewTag,
  137. webview_tag ? "true" : "false");
  138. // If the `sandbox` option was passed to the BrowserWindow's webPreferences,
  139. // pass `--enable-sandbox` to the renderer so it won't have any node.js
  140. // integration.
  141. if (dict_.GetBoolean(options::kSandbox, &b) && b)
  142. command_line->AppendSwitch(switches::kEnableSandbox);
  143. else if (!command_line->HasSwitch(switches::kEnableSandbox))
  144. command_line->AppendSwitch(::switches::kNoSandbox);
  145. if (dict_.GetBoolean(options::kNativeWindowOpen, &b) && b)
  146. command_line->AppendSwitch(switches::kNativeWindowOpen);
  147. // The preload script.
  148. base::FilePath::StringType preload;
  149. if (dict_.GetString(options::kPreloadScript, &preload)) {
  150. if (base::FilePath(preload).IsAbsolute())
  151. command_line->AppendSwitchNative(switches::kPreloadScript, preload);
  152. else
  153. LOG(ERROR) << "preload script must have absolute path.";
  154. } else if (dict_.GetString(options::kPreloadURL, &preload)) {
  155. // Translate to file path if there is "preload-url" option.
  156. base::FilePath preload_path;
  157. if (net::FileURLToFilePath(GURL(preload), &preload_path))
  158. command_line->AppendSwitchPath(switches::kPreloadScript, preload_path);
  159. else
  160. LOG(ERROR) << "preload url must be file:// protocol.";
  161. }
  162. // Custom args for renderer process
  163. base::Value* customArgs;
  164. if (dict_.Get(options::kCustomArgs, &customArgs) && customArgs->is_list()) {
  165. for (const base::Value& customArg : customArgs->GetList()) {
  166. if (customArg.is_string())
  167. command_line->AppendArg(customArg.GetString());
  168. }
  169. }
  170. // Run Electron APIs and preload script in isolated world
  171. if (dict_.GetBoolean(options::kContextIsolation, &b) && b)
  172. command_line->AppendSwitch(switches::kContextIsolation);
  173. // --background-color.
  174. std::string s;
  175. if (dict_.GetString(options::kBackgroundColor, &s))
  176. command_line->AppendSwitchASCII(switches::kBackgroundColor, s);
  177. // --guest-instance-id, which is used to identify guest WebContents.
  178. int guest_instance_id = 0;
  179. if (dict_.GetInteger(options::kGuestInstanceID, &guest_instance_id))
  180. command_line->AppendSwitchASCII(switches::kGuestInstanceID,
  181. base::IntToString(guest_instance_id));
  182. // Pass the opener's window id.
  183. int opener_id;
  184. if (dict_.GetInteger(options::kOpenerID, &opener_id))
  185. command_line->AppendSwitchASCII(switches::kOpenerID,
  186. base::IntToString(opener_id));
  187. #if defined(OS_MACOSX)
  188. // Enable scroll bounce.
  189. if (dict_.GetBoolean(options::kScrollBounce, &b) && b)
  190. command_line->AppendSwitch(switches::kScrollBounce);
  191. #endif
  192. // Custom command line switches.
  193. const base::ListValue* args;
  194. if (dict_.GetList("commandLineSwitches", &args)) {
  195. for (size_t i = 0; i < args->GetSize(); ++i) {
  196. std::string arg;
  197. if (args->GetString(i, &arg) && !arg.empty())
  198. command_line->AppendSwitch(arg);
  199. }
  200. }
  201. // Enable blink features.
  202. if (dict_.GetString(options::kEnableBlinkFeatures, &s))
  203. command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures, s);
  204. // Disable blink features.
  205. if (dict_.GetString(options::kDisableBlinkFeatures, &s))
  206. command_line->AppendSwitchASCII(::switches::kDisableBlinkFeatures, s);
  207. if (guest_instance_id) {
  208. // Webview `document.visibilityState` tracks window visibility so we need
  209. // to let it know if the window happens to be hidden right now.
  210. auto* manager = WebViewManager::GetWebViewManager(web_contents_);
  211. if (manager) {
  212. auto* embedder = manager->GetEmbedder(guest_instance_id);
  213. if (embedder) {
  214. auto* relay = NativeWindowRelay::FromWebContents(embedder);
  215. if (relay) {
  216. auto* window = relay->window.get();
  217. if (window) {
  218. const bool visible = window->IsVisible() && !window->IsMinimized();
  219. if (!visible) {
  220. command_line->AppendSwitch(switches::kHiddenPage);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. // We are appending args to a webContents so let's save the current state
  228. // of our preferences object so that during the lifetime of the WebContents
  229. // we can fetch the options used to initally configure the WebContents
  230. last_dict_ = std::move(*dict_.CreateDeepCopy());
  231. }
  232. void WebContentsPreferences::OverrideWebkitPrefs(
  233. content::WebPreferences* prefs) {
  234. bool b;
  235. if (dict_.GetBoolean("javascript", &b))
  236. prefs->javascript_enabled = b;
  237. if (dict_.GetBoolean("images", &b))
  238. prefs->images_enabled = b;
  239. if (dict_.GetBoolean("textAreasAreResizable", &b))
  240. prefs->text_areas_are_resizable = b;
  241. if (dict_.GetBoolean("webgl", &b)) {
  242. prefs->webgl1_enabled = b;
  243. prefs->webgl2_enabled = b;
  244. }
  245. if (dict_.GetBoolean(options::kWebSecurity, &b)) {
  246. prefs->web_security_enabled = b;
  247. prefs->allow_running_insecure_content = !b;
  248. }
  249. if (dict_.GetBoolean(options::kAllowRunningInsecureContent, &b))
  250. prefs->allow_running_insecure_content = b;
  251. if (dict_.GetBoolean("navigateOnDragDrop", &b))
  252. prefs->navigate_on_drag_drop = b;
  253. const base::DictionaryValue* fonts = nullptr;
  254. if (dict_.GetDictionary("defaultFontFamily", &fonts)) {
  255. base::string16 font;
  256. if (fonts->GetString("standard", &font))
  257. prefs->standard_font_family_map[content::kCommonScript] = font;
  258. if (fonts->GetString("serif", &font))
  259. prefs->serif_font_family_map[content::kCommonScript] = font;
  260. if (fonts->GetString("sansSerif", &font))
  261. prefs->sans_serif_font_family_map[content::kCommonScript] = font;
  262. if (fonts->GetString("monospace", &font))
  263. prefs->fixed_font_family_map[content::kCommonScript] = font;
  264. if (fonts->GetString("cursive", &font))
  265. prefs->cursive_font_family_map[content::kCommonScript] = font;
  266. if (fonts->GetString("fantasy", &font))
  267. prefs->fantasy_font_family_map[content::kCommonScript] = font;
  268. }
  269. int size;
  270. if (GetInteger("defaultFontSize", &size))
  271. prefs->default_font_size = size;
  272. if (GetInteger("defaultMonospaceFontSize", &size))
  273. prefs->default_fixed_font_size = size;
  274. if (GetInteger("minimumFontSize", &size))
  275. prefs->minimum_font_size = size;
  276. std::string encoding;
  277. if (dict_.GetString("defaultEncoding", &encoding))
  278. prefs->default_encoding = encoding;
  279. }
  280. bool WebContentsPreferences::GetInteger(const base::StringPiece& attribute_name,
  281. int* val) {
  282. // if it is already an integer, no conversion needed
  283. if (dict_.GetInteger(attribute_name, val))
  284. return true;
  285. std::string str;
  286. if (dict_.GetString(attribute_name, &str))
  287. return base::StringToInt(str, val);
  288. return false;
  289. }
  290. } // namespace atom