web_contents_preferences.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 "shell/browser/web_contents_preferences.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/command_line.h"
  10. #include "base/containers/fixed_flat_map.h"
  11. #include "base/memory/ptr_util.h"
  12. #include "base/strings/string_number_conversions.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "cc/base/switches.h"
  15. #include "content/public/browser/render_frame_host.h"
  16. #include "content/public/browser/render_process_host.h"
  17. #include "content/public/browser/web_contents_user_data.h"
  18. #include "content/public/common/content_switches.h"
  19. #include "net/base/filename_util.h"
  20. #include "sandbox/policy/switches.h"
  21. #include "shell/browser/api/electron_api_web_contents.h"
  22. #include "shell/browser/native_window.h"
  23. #include "shell/browser/session_preferences.h"
  24. #include "shell/common/color_util.h"
  25. #include "shell/common/gin_converters/value_converter.h"
  26. #include "shell/common/gin_helper/dictionary.h"
  27. #include "shell/common/options_switches.h"
  28. #include "shell/common/process_util.h"
  29. #include "third_party/blink/public/common/web_preferences/web_preferences.h"
  30. #include "third_party/blink/public/mojom/v8_cache_options.mojom.h"
  31. #include "third_party/blink/public/mojom/webpreferences/web_preferences.mojom.h"
  32. #if BUILDFLAG(IS_WIN)
  33. #include "ui/gfx/switches.h"
  34. #endif
  35. namespace gin {
  36. template <>
  37. struct Converter<blink::mojom::AutoplayPolicy> {
  38. static bool FromV8(v8::Isolate* isolate,
  39. v8::Local<v8::Value> val,
  40. blink::mojom::AutoplayPolicy* out) {
  41. using Val = blink::mojom::AutoplayPolicy;
  42. static constexpr auto Lookup =
  43. base::MakeFixedFlatMapSorted<base::StringPiece, Val>({
  44. {"document-user-activation-required",
  45. Val::kDocumentUserActivationRequired},
  46. {"no-user-gesture-required", Val::kNoUserGestureRequired},
  47. {"user-gesture-required", Val::kUserGestureRequired},
  48. });
  49. return FromV8WithLookup(isolate, val, Lookup, out);
  50. }
  51. };
  52. template <>
  53. struct Converter<blink::mojom::V8CacheOptions> {
  54. static bool FromV8(v8::Isolate* isolate,
  55. v8::Local<v8::Value> val,
  56. blink::mojom::V8CacheOptions* out) {
  57. using Val = blink::mojom::V8CacheOptions;
  58. static constexpr auto Lookup =
  59. base::MakeFixedFlatMapSorted<base::StringPiece, Val>({
  60. {"bypassHeatCheck", Val::kCodeWithoutHeatCheck},
  61. {"bypassHeatCheckAndEagerCompile", Val::kFullCodeWithoutHeatCheck},
  62. {"code", Val::kCode},
  63. {"none", Val::kNone},
  64. });
  65. return FromV8WithLookup(isolate, val, Lookup, out);
  66. }
  67. };
  68. } // namespace gin
  69. namespace electron {
  70. namespace {
  71. std::vector<WebContentsPreferences*>& Instances() {
  72. static base::NoDestructor<std::vector<WebContentsPreferences*>> g_instances;
  73. return *g_instances;
  74. }
  75. } // namespace
  76. WebContentsPreferences::WebContentsPreferences(
  77. content::WebContents* web_contents,
  78. const gin_helper::Dictionary& web_preferences)
  79. : content::WebContentsUserData<WebContentsPreferences>(*web_contents),
  80. web_contents_(web_contents) {
  81. web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
  82. Instances().push_back(this);
  83. SetFromDictionary(web_preferences);
  84. // If this is a <webview> tag, and the embedder is offscreen-rendered, then
  85. // this WebContents is also offscreen-rendered.
  86. if (auto* api_web_contents = api::WebContents::From(web_contents_)) {
  87. if (electron::api::WebContents* embedder = api_web_contents->embedder()) {
  88. auto* embedder_preferences =
  89. WebContentsPreferences::From(embedder->web_contents());
  90. if (embedder_preferences && embedder_preferences->IsOffscreen()) {
  91. offscreen_ = true;
  92. }
  93. }
  94. }
  95. }
  96. WebContentsPreferences::~WebContentsPreferences() {
  97. Instances().erase(std::remove(Instances().begin(), Instances().end(), this),
  98. Instances().end());
  99. }
  100. void WebContentsPreferences::Clear() {
  101. plugins_ = false;
  102. experimental_features_ = false;
  103. node_integration_ = false;
  104. node_integration_in_sub_frames_ = false;
  105. node_integration_in_worker_ = false;
  106. disable_html_fullscreen_window_resize_ = false;
  107. webview_tag_ = false;
  108. sandbox_ = absl::nullopt;
  109. context_isolation_ = true;
  110. javascript_ = true;
  111. images_ = true;
  112. text_areas_are_resizable_ = true;
  113. webgl_ = true;
  114. enable_websql_ = true;
  115. enable_preferred_size_mode_ = false;
  116. web_security_ = true;
  117. allow_running_insecure_content_ = false;
  118. offscreen_ = false;
  119. navigate_on_drag_drop_ = false;
  120. autoplay_policy_ = blink::mojom::AutoplayPolicy::kNoUserGestureRequired;
  121. default_font_family_.clear();
  122. default_font_size_ = absl::nullopt;
  123. default_monospace_font_size_ = absl::nullopt;
  124. minimum_font_size_ = absl::nullopt;
  125. default_encoding_ = absl::nullopt;
  126. is_webview_ = false;
  127. custom_args_.clear();
  128. custom_switches_.clear();
  129. enable_blink_features_ = absl::nullopt;
  130. disable_blink_features_ = absl::nullopt;
  131. disable_popups_ = false;
  132. disable_dialogs_ = false;
  133. safe_dialogs_ = false;
  134. safe_dialogs_message_ = absl::nullopt;
  135. ignore_menu_shortcuts_ = false;
  136. background_color_ = absl::nullopt;
  137. image_animation_policy_ =
  138. blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed;
  139. preload_path_ = absl::nullopt;
  140. v8_cache_options_ = blink::mojom::V8CacheOptions::kDefault;
  141. #if BUILDFLAG(IS_MAC)
  142. scroll_bounce_ = false;
  143. #endif
  144. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  145. spellcheck_ = true;
  146. #endif
  147. }
  148. void WebContentsPreferences::SetFromDictionary(
  149. const gin_helper::Dictionary& web_preferences) {
  150. Clear();
  151. web_preferences.Get(options::kPlugins, &plugins_);
  152. web_preferences.Get(options::kExperimentalFeatures, &experimental_features_);
  153. web_preferences.Get(options::kNodeIntegration, &node_integration_);
  154. web_preferences.Get(options::kNodeIntegrationInSubFrames,
  155. &node_integration_in_sub_frames_);
  156. web_preferences.Get(options::kNodeIntegrationInWorker,
  157. &node_integration_in_worker_);
  158. web_preferences.Get(options::kDisableHtmlFullscreenWindowResize,
  159. &disable_html_fullscreen_window_resize_);
  160. web_preferences.Get(options::kWebviewTag, &webview_tag_);
  161. bool sandbox;
  162. if (web_preferences.Get(options::kSandbox, &sandbox))
  163. sandbox_ = sandbox;
  164. web_preferences.Get(options::kContextIsolation, &context_isolation_);
  165. web_preferences.Get(options::kJavaScript, &javascript_);
  166. web_preferences.Get(options::kImages, &images_);
  167. web_preferences.Get(options::kTextAreasAreResizable,
  168. &text_areas_are_resizable_);
  169. web_preferences.Get(options::kWebGL, &webgl_);
  170. web_preferences.Get(options::kEnableWebSQL, &enable_websql_);
  171. web_preferences.Get(options::kEnablePreferredSizeMode,
  172. &enable_preferred_size_mode_);
  173. web_preferences.Get(options::kWebSecurity, &web_security_);
  174. if (!web_preferences.Get(options::kAllowRunningInsecureContent,
  175. &allow_running_insecure_content_) &&
  176. !web_security_)
  177. allow_running_insecure_content_ = true;
  178. web_preferences.Get(options::kOffscreen, &offscreen_);
  179. web_preferences.Get(options::kNavigateOnDragDrop, &navigate_on_drag_drop_);
  180. web_preferences.Get("autoplayPolicy", &autoplay_policy_);
  181. web_preferences.Get("defaultFontFamily", &default_font_family_);
  182. int size;
  183. if (web_preferences.Get("defaultFontSize", &size))
  184. default_font_size_ = size;
  185. if (web_preferences.Get("defaultMonospaceFontSize", &size))
  186. default_monospace_font_size_ = size;
  187. if (web_preferences.Get("minimumFontSize", &size))
  188. minimum_font_size_ = size;
  189. std::string encoding;
  190. if (web_preferences.Get("defaultEncoding", &encoding))
  191. default_encoding_ = encoding;
  192. web_preferences.Get(options::kCustomArgs, &custom_args_);
  193. web_preferences.Get("commandLineSwitches", &custom_switches_);
  194. web_preferences.Get("disablePopups", &disable_popups_);
  195. web_preferences.Get("disableDialogs", &disable_dialogs_);
  196. web_preferences.Get("safeDialogs", &safe_dialogs_);
  197. // preferences don't save a transparency option,
  198. // apply any existing transparency setting to background_color_
  199. bool transparent;
  200. if (web_preferences.Get(options::kTransparent, &transparent) && transparent) {
  201. background_color_ = SK_ColorTRANSPARENT;
  202. }
  203. std::string background_color;
  204. if (web_preferences.GetHidden(options::kBackgroundColor, &background_color))
  205. background_color_ = ParseCSSColor(background_color);
  206. std::string safe_dialogs_message;
  207. if (web_preferences.Get("safeDialogsMessage", &safe_dialogs_message))
  208. safe_dialogs_message_ = safe_dialogs_message;
  209. web_preferences.Get("ignoreMenuShortcuts", &ignore_menu_shortcuts_);
  210. std::string enable_blink_features;
  211. if (web_preferences.Get(options::kEnableBlinkFeatures,
  212. &enable_blink_features))
  213. enable_blink_features_ = enable_blink_features;
  214. std::string disable_blink_features;
  215. if (web_preferences.Get(options::kDisableBlinkFeatures,
  216. &disable_blink_features))
  217. disable_blink_features_ = disable_blink_features;
  218. base::FilePath::StringType preload_path;
  219. if (web_preferences.Get(options::kPreloadScript, &preload_path)) {
  220. base::FilePath preload(preload_path);
  221. if (preload.IsAbsolute()) {
  222. preload_path_ = preload;
  223. } else {
  224. LOG(ERROR) << "preload script must have absolute path.";
  225. }
  226. }
  227. std::string type;
  228. if (web_preferences.Get(options::kType, &type)) {
  229. is_webview_ = type == "webview";
  230. }
  231. web_preferences.Get("v8CacheOptions", &v8_cache_options_);
  232. #if BUILDFLAG(IS_MAC)
  233. web_preferences.Get(options::kScrollBounce, &scroll_bounce_);
  234. #endif
  235. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  236. web_preferences.Get(options::kSpellcheck, &spellcheck_);
  237. #endif
  238. SaveLastPreferences();
  239. }
  240. bool WebContentsPreferences::GetSafeDialogsMessage(std::string* message) const {
  241. if (safe_dialogs_message_) {
  242. *message = *safe_dialogs_message_;
  243. return true;
  244. }
  245. return false;
  246. }
  247. bool WebContentsPreferences::SetImageAnimationPolicy(std::string policy) {
  248. if (policy == "animate") {
  249. image_animation_policy_ =
  250. blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed;
  251. return true;
  252. } else if (policy == "animateOnce") {
  253. image_animation_policy_ =
  254. blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAnimateOnce;
  255. return true;
  256. } else if (policy == "noAnimation") {
  257. image_animation_policy_ =
  258. blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyNoAnimation;
  259. return true;
  260. }
  261. return false;
  262. }
  263. bool WebContentsPreferences::GetPreloadPath(base::FilePath* path) const {
  264. DCHECK(path);
  265. if (preload_path_) {
  266. *path = *preload_path_;
  267. return true;
  268. }
  269. return false;
  270. }
  271. bool WebContentsPreferences::IsSandboxed() const {
  272. if (sandbox_)
  273. return *sandbox_;
  274. bool sandbox_disabled_by_default =
  275. node_integration_ || node_integration_in_worker_;
  276. return !sandbox_disabled_by_default;
  277. }
  278. // static
  279. content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID(
  280. int process_id) {
  281. for (WebContentsPreferences* preferences : Instances()) {
  282. content::WebContents* web_contents = preferences->web_contents_;
  283. if (web_contents->GetPrimaryMainFrame()->GetProcess()->GetID() ==
  284. process_id)
  285. return web_contents;
  286. }
  287. return nullptr;
  288. }
  289. // static
  290. WebContentsPreferences* WebContentsPreferences::From(
  291. content::WebContents* web_contents) {
  292. if (!web_contents)
  293. return nullptr;
  294. return FromWebContents(web_contents);
  295. }
  296. void WebContentsPreferences::AppendCommandLineSwitches(
  297. base::CommandLine* command_line,
  298. bool is_subframe) {
  299. // Experimental flags.
  300. if (experimental_features_)
  301. command_line->AppendSwitch(
  302. ::switches::kEnableExperimentalWebPlatformFeatures);
  303. // Sandbox can be enabled for renderer processes hosting cross-origin frames
  304. // unless nodeIntegrationInSubFrames is enabled
  305. bool can_sandbox_frame = is_subframe && !node_integration_in_sub_frames_;
  306. if (IsSandboxed() || can_sandbox_frame) {
  307. command_line->AppendSwitch(switches::kEnableSandbox);
  308. } else if (!command_line->HasSwitch(switches::kEnableSandbox)) {
  309. command_line->AppendSwitch(sandbox::policy::switches::kNoSandbox);
  310. command_line->AppendSwitch(::switches::kNoZygote);
  311. }
  312. #if BUILDFLAG(IS_MAC)
  313. // Enable scroll bounce.
  314. if (scroll_bounce_)
  315. command_line->AppendSwitch(switches::kScrollBounce);
  316. #endif
  317. // Custom args for renderer process
  318. for (const auto& arg : custom_args_)
  319. if (!arg.empty())
  320. command_line->AppendArg(arg);
  321. // Custom command line switches.
  322. for (const auto& arg : custom_switches_)
  323. if (!arg.empty())
  324. command_line->AppendSwitch(arg);
  325. if (enable_blink_features_)
  326. command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures,
  327. *enable_blink_features_);
  328. if (disable_blink_features_)
  329. command_line->AppendSwitchASCII(::switches::kDisableBlinkFeatures,
  330. *disable_blink_features_);
  331. if (node_integration_in_worker_)
  332. command_line->AppendSwitch(switches::kNodeIntegrationInWorker);
  333. // We are appending args to a webContents so let's save the current state
  334. // of our preferences object so that during the lifetime of the WebContents
  335. // we can fetch the options used to initially configure the WebContents
  336. // last_preference_ = preference_.Clone();
  337. SaveLastPreferences();
  338. }
  339. void WebContentsPreferences::SaveLastPreferences() {
  340. base::Value::Dict dict;
  341. dict.Set(options::kNodeIntegration, node_integration_);
  342. dict.Set(options::kNodeIntegrationInSubFrames,
  343. node_integration_in_sub_frames_);
  344. dict.Set(options::kSandbox, IsSandboxed());
  345. dict.Set(options::kContextIsolation, context_isolation_);
  346. dict.Set(options::kJavaScript, javascript_);
  347. dict.Set(options::kEnableWebSQL, enable_websql_);
  348. dict.Set(options::kWebviewTag, webview_tag_);
  349. dict.Set("disablePopups", disable_popups_);
  350. dict.Set(options::kWebSecurity, web_security_);
  351. dict.Set(options::kAllowRunningInsecureContent,
  352. allow_running_insecure_content_);
  353. dict.Set(options::kExperimentalFeatures, experimental_features_);
  354. dict.Set(options::kEnableBlinkFeatures, enable_blink_features_.value_or(""));
  355. last_web_preferences_ = base::Value(std::move(dict));
  356. }
  357. void WebContentsPreferences::OverrideWebkitPrefs(
  358. blink::web_pref::WebPreferences* prefs) {
  359. prefs->javascript_enabled = javascript_;
  360. prefs->images_enabled = images_;
  361. prefs->animation_policy = image_animation_policy_;
  362. prefs->text_areas_are_resizable = text_areas_are_resizable_;
  363. prefs->navigate_on_drag_drop = navigate_on_drag_drop_;
  364. prefs->autoplay_policy = autoplay_policy_;
  365. // Check if webgl should be enabled.
  366. prefs->webgl1_enabled = webgl_;
  367. prefs->webgl2_enabled = webgl_;
  368. // Check if web security should be enabled.
  369. prefs->web_security_enabled = web_security_;
  370. prefs->allow_running_insecure_content = allow_running_insecure_content_;
  371. if (!default_font_family_.empty()) {
  372. if (auto iter = default_font_family_.find("standard");
  373. iter != default_font_family_.end())
  374. prefs->standard_font_family_map[blink::web_pref::kCommonScript] =
  375. iter->second;
  376. if (auto iter = default_font_family_.find("serif");
  377. iter != default_font_family_.end())
  378. prefs->serif_font_family_map[blink::web_pref::kCommonScript] =
  379. iter->second;
  380. if (auto iter = default_font_family_.find("sansSerif");
  381. iter != default_font_family_.end())
  382. prefs->sans_serif_font_family_map[blink::web_pref::kCommonScript] =
  383. iter->second;
  384. if (auto iter = default_font_family_.find("monospace");
  385. iter != default_font_family_.end())
  386. prefs->fixed_font_family_map[blink::web_pref::kCommonScript] =
  387. iter->second;
  388. if (auto iter = default_font_family_.find("cursive");
  389. iter != default_font_family_.end())
  390. prefs->cursive_font_family_map[blink::web_pref::kCommonScript] =
  391. iter->second;
  392. if (auto iter = default_font_family_.find("fantasy");
  393. iter != default_font_family_.end())
  394. prefs->fantasy_font_family_map[blink::web_pref::kCommonScript] =
  395. iter->second;
  396. if (auto iter = default_font_family_.find("math");
  397. iter != default_font_family_.end())
  398. prefs->math_font_family_map[blink::web_pref::kCommonScript] =
  399. iter->second;
  400. }
  401. if (default_font_size_)
  402. prefs->default_font_size = *default_font_size_;
  403. if (default_monospace_font_size_)
  404. prefs->default_fixed_font_size = *default_monospace_font_size_;
  405. if (minimum_font_size_)
  406. prefs->minimum_font_size = *minimum_font_size_;
  407. if (default_encoding_)
  408. prefs->default_encoding = *default_encoding_;
  409. // Run Electron APIs and preload script in isolated world
  410. prefs->context_isolation = context_isolation_;
  411. prefs->is_webview = is_webview_;
  412. prefs->hidden_page = false;
  413. // Webview `document.visibilityState` tracks window visibility so we need
  414. // to let it know if the window happens to be hidden right now.
  415. if (auto* api_web_contents = api::WebContents::From(web_contents_)) {
  416. if (electron::api::WebContents* embedder = api_web_contents->embedder()) {
  417. if (auto* relay =
  418. NativeWindowRelay::FromWebContents(embedder->web_contents())) {
  419. if (auto* window = relay->GetNativeWindow()) {
  420. const bool visible = window->IsVisible() && !window->IsMinimized();
  421. if (!visible) {
  422. prefs->hidden_page = true;
  423. }
  424. }
  425. }
  426. }
  427. }
  428. prefs->offscreen = offscreen_;
  429. prefs->node_integration = node_integration_;
  430. prefs->node_integration_in_worker = node_integration_in_worker_;
  431. prefs->node_integration_in_sub_frames = node_integration_in_sub_frames_;
  432. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  433. prefs->enable_spellcheck = spellcheck_;
  434. #endif
  435. prefs->enable_plugins = plugins_;
  436. prefs->webview_tag = webview_tag_;
  437. prefs->enable_websql = enable_websql_;
  438. prefs->v8_cache_options = v8_cache_options_;
  439. }
  440. WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPreferences);
  441. } // namespace electron