web_contents_preferences.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef ELECTRON_SHELL_BROWSER_WEB_CONTENTS_PREFERENCES_H_
  5. #define ELECTRON_SHELL_BROWSER_WEB_CONTENTS_PREFERENCES_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/values.h"
  11. #include "content/public/browser/web_contents_user_data.h"
  12. #include "electron/buildflags/buildflags.h"
  13. #include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h"
  14. #include "third_party/blink/public/mojom/v8_cache_options.mojom-forward.h"
  15. #include "third_party/blink/public/mojom/webpreferences/web_preferences.mojom-forward.h"
  16. namespace base {
  17. class CommandLine;
  18. }
  19. namespace gin_helper {
  20. class Dictionary;
  21. }
  22. namespace electron {
  23. // Stores and applies the preferences of WebContents.
  24. class WebContentsPreferences
  25. : public content::WebContentsUserData<WebContentsPreferences> {
  26. public:
  27. // Get self from WebContents.
  28. static WebContentsPreferences* From(content::WebContents* web_contents);
  29. WebContentsPreferences(content::WebContents* web_contents,
  30. const gin_helper::Dictionary& web_preferences);
  31. ~WebContentsPreferences() override;
  32. // disable copy
  33. WebContentsPreferences(const WebContentsPreferences&) = delete;
  34. WebContentsPreferences& operator=(const WebContentsPreferences&) = delete;
  35. void SetFromDictionary(const gin_helper::Dictionary& new_web_preferences);
  36. // Append command parameters according to preferences.
  37. void AppendCommandLineSwitches(base::CommandLine* command_line,
  38. bool is_subframe);
  39. // Modify the WebPreferences according to preferences.
  40. void OverrideWebkitPrefs(blink::web_pref::WebPreferences* prefs,
  41. blink::RendererPreferences* renderer_prefs);
  42. base::Value* last_preference() { return &last_web_preferences_; }
  43. bool IsOffscreen() const { return offscreen_; }
  44. absl::optional<SkColor> GetBackgroundColor() const {
  45. return background_color_;
  46. }
  47. void SetBackgroundColor(absl::optional<SkColor> color) {
  48. background_color_ = color;
  49. }
  50. bool ShouldUsePreferredSizeMode() const {
  51. return enable_preferred_size_mode_;
  52. }
  53. void SetIgnoreMenuShortcuts(bool ignore_menu_shortcuts) {
  54. ignore_menu_shortcuts_ = ignore_menu_shortcuts;
  55. }
  56. bool ShouldIgnoreMenuShortcuts() const { return ignore_menu_shortcuts_; }
  57. bool SetImageAnimationPolicy(std::string policy);
  58. bool ShouldDisableHtmlFullscreenWindowResize() const {
  59. return disable_html_fullscreen_window_resize_;
  60. }
  61. bool AllowsNodeIntegrationInSubFrames() const {
  62. return node_integration_in_sub_frames_;
  63. }
  64. bool ShouldDisableDialogs() const { return disable_dialogs_; }
  65. bool ShouldUseSafeDialogs() const { return safe_dialogs_; }
  66. bool GetSafeDialogsMessage(std::string* message) const;
  67. bool ShouldDisablePopups() const { return disable_popups_; }
  68. bool IsWebSecurityEnabled() const { return web_security_; }
  69. bool GetPreloadPath(base::FilePath* path) const;
  70. bool IsSandboxed() const;
  71. private:
  72. friend class content::WebContentsUserData<WebContentsPreferences>;
  73. friend class ElectronBrowserClient;
  74. // Get WebContents according to process ID.
  75. static content::WebContents* GetWebContentsFromProcessID(int process_id);
  76. void Clear();
  77. void SaveLastPreferences();
  78. // TODO(clavin): refactor to use the WebContents provided by the
  79. // WebContentsUserData base class instead of storing a duplicate ref
  80. raw_ptr<content::WebContents> web_contents_;
  81. bool plugins_;
  82. bool experimental_features_;
  83. bool node_integration_;
  84. bool node_integration_in_sub_frames_;
  85. bool node_integration_in_worker_;
  86. bool disable_html_fullscreen_window_resize_;
  87. bool webview_tag_;
  88. absl::optional<bool> sandbox_;
  89. bool context_isolation_;
  90. bool javascript_;
  91. bool images_;
  92. bool text_areas_are_resizable_;
  93. bool webgl_;
  94. bool enable_websql_;
  95. bool enable_preferred_size_mode_;
  96. bool web_security_;
  97. bool allow_running_insecure_content_;
  98. bool offscreen_;
  99. bool navigate_on_drag_drop_;
  100. blink::mojom::AutoplayPolicy autoplay_policy_;
  101. std::map<std::string, std::u16string> default_font_family_;
  102. absl::optional<int> default_font_size_;
  103. absl::optional<int> default_monospace_font_size_;
  104. absl::optional<int> minimum_font_size_;
  105. absl::optional<std::string> default_encoding_;
  106. bool is_webview_;
  107. std::vector<std::string> custom_args_;
  108. std::vector<std::string> custom_switches_;
  109. absl::optional<std::string> enable_blink_features_;
  110. absl::optional<std::string> disable_blink_features_;
  111. bool disable_popups_;
  112. bool disable_dialogs_;
  113. bool safe_dialogs_;
  114. absl::optional<std::string> safe_dialogs_message_;
  115. bool ignore_menu_shortcuts_;
  116. absl::optional<SkColor> background_color_;
  117. blink::mojom::ImageAnimationPolicy image_animation_policy_;
  118. absl::optional<base::FilePath> preload_path_;
  119. blink::mojom::V8CacheOptions v8_cache_options_;
  120. #if BUILDFLAG(IS_MAC)
  121. bool scroll_bounce_;
  122. #endif
  123. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  124. bool spellcheck_;
  125. #endif
  126. // This is a snapshot of some relevant preferences at the time the renderer
  127. // was launched.
  128. base::Value last_web_preferences_;
  129. WEB_CONTENTS_USER_DATA_KEY_DECL();
  130. };
  131. } // namespace electron
  132. #endif // ELECTRON_SHELL_BROWSER_WEB_CONTENTS_PREFERENCES_H_