/chromium-webcl/src/chrome/browser/extensions/api/preference/preference_api.h

https://bitbucket.org/peixuan/chromium_r197479_base · C Header · 148 lines · 83 code · 36 blank · 29 comment · 0 complexity · 5cf7b11452441f8ece61cb3d18aec5b7 MD5 · raw file

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__
  5. #define CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__
  6. #include <string>
  7. #include "base/prefs/pref_change_registrar.h"
  8. #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
  9. #include "chrome/browser/extensions/event_router.h"
  10. #include "chrome/browser/extensions/extension_function.h"
  11. #include "content/public/browser/notification_observer.h"
  12. class PrefService;
  13. namespace base {
  14. class Value;
  15. }
  16. namespace extensions {
  17. class PreferenceEventRouter {
  18. public:
  19. explicit PreferenceEventRouter(Profile* profile);
  20. virtual ~PreferenceEventRouter();
  21. private:
  22. void OnPrefChanged(PrefService* pref_service,
  23. const std::string& pref_key);
  24. PrefChangeRegistrar registrar_;
  25. PrefChangeRegistrar incognito_registrar_;
  26. // Weak, owns us (transitively via ExtensionService).
  27. Profile* profile_;
  28. DISALLOW_COPY_AND_ASSIGN(PreferenceEventRouter);
  29. };
  30. class PreferenceAPI : public ProfileKeyedAPI,
  31. public EventRouter::Observer {
  32. public:
  33. explicit PreferenceAPI(Profile* profile);
  34. virtual ~PreferenceAPI();
  35. // ProfileKeyedService implementation.
  36. virtual void Shutdown() OVERRIDE;
  37. // ProfileKeyedAPI implementation.
  38. static ProfileKeyedAPIFactory<PreferenceAPI>* GetFactoryInstance();
  39. // EventRouter::Observer implementation.
  40. virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
  41. private:
  42. friend class ProfileKeyedAPIFactory<PreferenceAPI>;
  43. Profile* profile_;
  44. // ProfileKeyedAPI implementation.
  45. static const char* service_name() {
  46. return "PreferenceAPI";
  47. }
  48. static const bool kServiceIsNULLWhileTesting = true;
  49. // Created lazily upon OnListenerAdded.
  50. scoped_ptr<PreferenceEventRouter> preference_event_router_;
  51. DISALLOW_COPY_AND_ASSIGN(PreferenceAPI);
  52. };
  53. class PrefTransformerInterface {
  54. public:
  55. virtual ~PrefTransformerInterface() {}
  56. // Converts the representation of a preference as seen by the extension
  57. // into a representation that is used in the pref stores of the browser.
  58. // Returns the pref store representation in case of success or sets
  59. // |error| and returns NULL otherwise. |bad_message| is passed to simulate
  60. // the behavior of EXTENSION_FUNCTION_VALIDATE. It is never NULL.
  61. // The ownership of the returned value is passed to the caller.
  62. virtual base::Value* ExtensionToBrowserPref(
  63. const base::Value* extension_pref,
  64. std::string* error,
  65. bool* bad_message) = 0;
  66. // Converts the representation of the preference as stored in the browser
  67. // into a representation that is used by the extension.
  68. // Returns the extension representation in case of success or NULL otherwise.
  69. // The ownership of the returned value is passed to the caller.
  70. virtual base::Value* BrowserToExtensionPref(
  71. const base::Value* browser_pref) = 0;
  72. };
  73. // A base class to provide functionality common to the other *PreferenceFunction
  74. // classes.
  75. class PreferenceFunction : public SyncExtensionFunction {
  76. protected:
  77. virtual ~PreferenceFunction();
  78. // Given an |extension_pref_key|, provides its |browser_pref_key| from the
  79. // static map in extension_preference.cc. Returns true if the corresponding
  80. // browser pref exists and the extension has the API permission needed to
  81. // modify that pref. Sets |error_| if the extension doesn't have the needed
  82. // permission.
  83. bool ValidateBrowserPref(const std::string& extension_pref_key,
  84. std::string* browser_pref_key);
  85. };
  86. class GetPreferenceFunction : public PreferenceFunction {
  87. public:
  88. DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.get", TYPES_CHROMESETTING_GET)
  89. protected:
  90. virtual ~GetPreferenceFunction();
  91. // ExtensionFunction:
  92. virtual bool RunImpl() OVERRIDE;
  93. };
  94. class SetPreferenceFunction : public PreferenceFunction {
  95. public:
  96. DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.set", TYPES_CHROMESETTING_SET)
  97. protected:
  98. virtual ~SetPreferenceFunction();
  99. // ExtensionFunction:
  100. virtual bool RunImpl() OVERRIDE;
  101. };
  102. class ClearPreferenceFunction : public PreferenceFunction {
  103. public:
  104. DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.clear",
  105. TYPES_CHROMESETTING_CLEAR)
  106. protected:
  107. virtual ~ClearPreferenceFunction();
  108. // ExtensionFunction:
  109. virtual bool RunImpl() OVERRIDE;
  110. };
  111. } // namespace extensions
  112. #endif // CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__