web_contents_preferences.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // A simple way to know whether a Boolean property is enabled.
  30. bool IsEnabled(const base::StringPiece& name, bool default_value = false);
  31. // $.extend(|web_preferences|, |new_web_preferences|).
  32. void Merge(const base::DictionaryValue& new_web_preferences);
  33. // Append command paramters according to preferences.
  34. void AppendCommandLineSwitches(base::CommandLine* command_line);
  35. // Modify the WebPreferences according to preferences.
  36. void OverrideWebkitPrefs(content::WebPreferences* prefs);
  37. // Returns the web preferences.
  38. base::DictionaryValue* dict() { return &dict_; }
  39. const base::DictionaryValue* dict() const { return &dict_; }
  40. base::DictionaryValue* last_dict() { return &last_dict_; }
  41. private:
  42. friend class content::WebContentsUserData<WebContentsPreferences>;
  43. friend class AtomBrowserClient;
  44. // Get WebContents according to process ID.
  45. static content::WebContents* GetWebContentsFromProcessID(int process_id);
  46. // Set preference value to given bool if user did not provide value
  47. bool SetDefaultBoolIfUndefined(const base::StringPiece& key, bool val);
  48. // Get preferences value as integer possibly coercing it from a string
  49. bool GetInteger(const base::StringPiece& attribute_name, int* val);
  50. static std::vector<WebContentsPreferences*> instances_;
  51. content::WebContents* web_contents_;
  52. base::DictionaryValue dict_;
  53. base::DictionaryValue last_dict_;
  54. DISALLOW_COPY_AND_ASSIGN(WebContentsPreferences);
  55. };
  56. } // namespace atom
  57. #endif // ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_