web_contents_preferences.h 5.1 KB

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