web_contents_preferences.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_
  5. #define ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/values.h"
  9. #include "content/public/browser/web_contents_user_data.h"
  10. namespace base {
  11. class CommandLine;
  12. }
  13. namespace content {
  14. struct WebPreferences;
  15. }
  16. namespace mate {
  17. class Dictionary;
  18. }
  19. namespace atom {
  20. // Stores and applies the preferences of WebContents.
  21. class WebContentsPreferences
  22. : public content::WebContentsUserData<WebContentsPreferences> {
  23. public:
  24. // Get self from WebContents.
  25. static WebContentsPreferences* From(content::WebContents* web_contents);
  26. WebContentsPreferences(content::WebContents* web_contents,
  27. const mate::Dictionary& web_preferences);
  28. ~WebContentsPreferences() override;
  29. // Set WebPreferences defaults onto the JS object.
  30. void SetDefaults();
  31. // A simple way to know whether a Boolean property is enabled.
  32. bool IsEnabled(const base::StringPiece& name,
  33. bool default_value = false) const;
  34. // $.extend(|web_preferences|, |new_web_preferences|).
  35. void Merge(const base::DictionaryValue& new_web_preferences);
  36. // Append command paramters according to preferences.
  37. void AppendCommandLineSwitches(base::CommandLine* command_line);
  38. // Modify the WebPreferences according to preferences.
  39. void OverrideWebkitPrefs(content::WebPreferences* prefs);
  40. // Clear the current WebPreferences.
  41. void Clear();
  42. // Return true if the particular preference value exists.
  43. bool GetPreference(const base::StringPiece& name, std::string* value) const;
  44. // Whether to enable the remote module
  45. bool IsRemoteModuleEnabled() const;
  46. // Returns the preload script path.
  47. bool GetPreloadPath(base::FilePath::StringType* path) const;
  48. // Returns the web preferences.
  49. base::Value* preference() { return &preference_; }
  50. base::Value* last_preference() { return &last_preference_; }
  51. private:
  52. friend class content::WebContentsUserData<WebContentsPreferences>;
  53. friend class AtomBrowserClient;
  54. // Get WebContents according to process ID.
  55. static content::WebContents* GetWebContentsFromProcessID(int process_id);
  56. // Set preference value to given bool if user did not provide value
  57. bool SetDefaultBoolIfUndefined(const base::StringPiece& key, bool val);
  58. // Set preference value to given bool
  59. void SetBool(const base::StringPiece& key, bool value);
  60. static std::vector<WebContentsPreferences*> instances_;
  61. content::WebContents* web_contents_;
  62. base::Value preference_ = base::Value(base::Value::Type::DICTIONARY);
  63. base::Value last_preference_ = base::Value(base::Value::Type::DICTIONARY);
  64. WEB_CONTENTS_USER_DATA_KEY_DECL();
  65. DISALLOW_COPY_AND_ASSIGN(WebContentsPreferences);
  66. };
  67. } // namespace atom
  68. #endif // ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_