1 // Copyright (c) 2011 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 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFERENCE_API_H__ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFERENCE_API_H__ 7 #pragma once 8 9 #include <string> 10 11 #include "chrome/browser/extensions/extension_function.h" 12 #include "chrome/browser/prefs/pref_change_registrar.h" 13 #include "content/common/notification_observer.h" 14 15 class ExtensionPreferenceEventRouter : public NotificationObserver { 16 public: 17 explicit ExtensionPreferenceEventRouter(Profile* profile); 18 virtual ~ExtensionPreferenceEventRouter(); 19 20 private: 21 // NotificationObserver implementation. 22 virtual void Observe(NotificationType type, 23 const NotificationSource& source, 24 const NotificationDetails& details); 25 26 void OnPrefChanged(PrefService* pref_service, const std::string& pref_key); 27 28 // This method dispatches events to the extension message service. 29 void DispatchEvent(const std::string& extension_id, 30 const std::string& event_name, 31 const std::string& json_args); 32 33 PrefChangeRegistrar registrar_; 34 PrefChangeRegistrar incognito_registrar_; 35 36 // Weak, owns us (transitively via ExtensionService). 37 Profile* profile_; 38 39 DISALLOW_COPY_AND_ASSIGN(ExtensionPreferenceEventRouter); 40 }; 41 42 class Value; 43 44 class PrefTransformerInterface { 45 public: 46 virtual ~PrefTransformerInterface() {} 47 48 // Converts the representation of a preference as seen by the extension 49 // into a representation that is used in the pref stores of the browser. 50 // Returns the pref store representation in case of success or sets 51 // |error| and returns NULL otherwise. 52 // The ownership of the returned value is passed to the caller. 53 virtual Value* ExtensionToBrowserPref(const Value* extension_pref, 54 std::string* error) = 0; 55 56 // Converts the representation of the preference as stored in the browser 57 // into a representation that is used by the extension. 58 // Returns the extension representation in case of success or NULL otherwise. 59 // The ownership of the returned value is passed to the caller. 60 virtual Value* BrowserToExtensionPref(const Value* browser_pref) = 0; 61 }; 62 63 class GetPreferenceFunction : public SyncExtensionFunction { 64 public: 65 virtual ~GetPreferenceFunction(); 66 virtual bool RunImpl(); 67 DECLARE_EXTENSION_FUNCTION_NAME("experimental.preferences.get") 68 }; 69 70 class SetPreferenceFunction : public SyncExtensionFunction { 71 public: 72 virtual ~SetPreferenceFunction(); 73 virtual bool RunImpl(); 74 DECLARE_EXTENSION_FUNCTION_NAME("experimental.preferences.set") 75 }; 76 77 class ClearPreferenceFunction : public SyncExtensionFunction { 78 public: 79 virtual ~ClearPreferenceFunction(); 80 virtual bool RunImpl(); 81 DECLARE_EXTENSION_FUNCTION_NAME("experimental.preferences.clear") 82 }; 83 84 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFERENCE_API_H__ 85