| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SETTINGS_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SETTINGS_SERVICE_H_ |
| 6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SETTINGS_SERVICE_H_ | 6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SETTINGS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/prefs/pref_store.h" | 14 #include "base/prefs/pref_store.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/managed_mode/managed_users.h" | 16 #include "chrome/browser/supervised_user/supervised_users.h" |
| 17 #include "components/keyed_service/core/keyed_service.h" | 17 #include "components/keyed_service/core/keyed_service.h" |
| 18 #include "sync/api/syncable_service.h" | 18 #include "sync/api/syncable_service.h" |
| 19 | 19 |
| 20 class PersistentPrefStore; | 20 class PersistentPrefStore; |
| 21 class Profile; | 21 class Profile; |
| 22 | 22 |
| 23 namespace base { | 23 namespace base { |
| 24 class FilePath; | 24 class FilePath; |
| 25 class SequencedTaskRunner; | 25 class SequencedTaskRunner; |
| 26 } | 26 } |
| 27 | 27 |
| 28 // This class syncs managed user settings from a server, which are mapped to | 28 // This class syncs supervised user settings from a server, which are mapped to |
| 29 // preferences. The downloaded settings are persisted in a PrefStore (which is | 29 // preferences. The downloaded settings are persisted in a PrefStore (which is |
| 30 // not directly hooked up to the PrefService; it's just used internally). | 30 // not directly hooked up to the PrefService; it's just used internally). |
| 31 // Settings are key-value pairs, where the key uniquely identifies the setting. | 31 // Settings are key-value pairs, where the key uniquely identifies the setting. |
| 32 // The value is a string containing a JSON serialization of an arbitrary value, | 32 // The value is a string containing a JSON serialization of an arbitrary value, |
| 33 // which is the value of the setting. | 33 // which is the value of the setting. |
| 34 // | 34 // |
| 35 // There are two kinds of settings handled by this class: Atomic and split | 35 // There are two kinds of settings handled by this class: Atomic and split |
| 36 // settings. | 36 // settings. |
| 37 // Atomic settings consist of a single key (which will be mapped to a pref key) | 37 // Atomic settings consist of a single key (which will be mapped to a pref key) |
| 38 // and a single (arbitrary) value. | 38 // and a single (arbitrary) value. |
| 39 // Split settings encode a dictionary value and are stored as multiple Sync | 39 // Split settings encode a dictionary value and are stored as multiple Sync |
| 40 // items, one for each dictionary entry. The key for each of these Sync items | 40 // items, one for each dictionary entry. The key for each of these Sync items |
| 41 // is the key of the split setting, followed by a separator (':') and the key | 41 // is the key of the split setting, followed by a separator (':') and the key |
| 42 // for the dictionary entry. The value of the Sync item is the value of the | 42 // for the dictionary entry. The value of the Sync item is the value of the |
| 43 // dictionary entry. | 43 // dictionary entry. |
| 44 // | 44 // |
| 45 // As an example, a split setting with key "Moose" and value | 45 // As an example, a split setting with key "Moose" and value |
| 46 // { | 46 // { |
| 47 // "foo": "bar", | 47 // "foo": "bar", |
| 48 // "baz": "blurp" | 48 // "baz": "blurp" |
| 49 // } | 49 // } |
| 50 // would be encoded as two sync items, one with key "Moose:foo" and value "bar", | 50 // would be encoded as two sync items, one with key "Moose:foo" and value "bar", |
| 51 // and one with key "Moose:baz" and value "blurp". | 51 // and one with key "Moose:baz" and value "blurp". |
| 52 class ManagedUserSettingsService : public KeyedService, | 52 class SupervisedUserSettingsService : public KeyedService, |
| 53 public syncer::SyncableService, | 53 public syncer::SyncableService, |
| 54 public PrefStore::Observer { | 54 public PrefStore::Observer { |
| 55 public: | 55 public: |
| 56 // A callback whose first parameter is a dictionary containing all managed | 56 // A callback whose first parameter is a dictionary containing all supervised |
| 57 // user settings. If the dictionary is NULL, it means that the service is | 57 // user settings. If the dictionary is NULL, it means that the service is |
| 58 // inactive, i.e. the user is not managed. | 58 // inactive, i.e. the user is not supervised. |
| 59 typedef base::Callback<void(const base::DictionaryValue*)> SettingsCallback; | 59 typedef base::Callback<void(const base::DictionaryValue*)> SettingsCallback; |
| 60 | 60 |
| 61 ManagedUserSettingsService(); | 61 SupervisedUserSettingsService(); |
| 62 virtual ~ManagedUserSettingsService(); | 62 virtual ~SupervisedUserSettingsService(); |
| 63 | 63 |
| 64 // Initializes the service by loading its settings from a file underneath the | 64 // Initializes the service by loading its settings from a file underneath the |
| 65 // |profile_path|. File I/O will be serialized via the | 65 // |profile_path|. File I/O will be serialized via the |
| 66 // |sequenced_task_runner|. If |load_synchronously| is true, the settings will | 66 // |sequenced_task_runner|. If |load_synchronously| is true, the settings will |
| 67 // be loaded synchronously, otherwise asynchronously. | 67 // be loaded synchronously, otherwise asynchronously. |
| 68 void Init(base::FilePath profile_path, | 68 void Init(base::FilePath profile_path, |
| 69 base::SequencedTaskRunner* sequenced_task_runner, | 69 base::SequencedTaskRunner* sequenced_task_runner, |
| 70 bool load_synchronously); | 70 bool load_synchronously); |
| 71 | 71 |
| 72 // Initializes the service by loading its settings from the |pref_store|. | 72 // Initializes the service by loading its settings from the |pref_store|. |
| 73 // Use this method in tests to inject a different PrefStore than the | 73 // Use this method in tests to inject a different PrefStore than the |
| 74 // default one. | 74 // default one. |
| 75 void Init(scoped_refptr<PersistentPrefStore> pref_store); | 75 void Init(scoped_refptr<PersistentPrefStore> pref_store); |
| 76 | 76 |
| 77 // Adds a callback to be called when managed user settings are initially | 77 // Adds a callback to be called when supervised user settings are initially |
| 78 // available, or when they change. | 78 // available, or when they change. |
| 79 void Subscribe(const SettingsCallback& callback); | 79 void Subscribe(const SettingsCallback& callback); |
| 80 | 80 |
| 81 // Activates/deactivates the service. This is called by the ManagedUserService | 81 // Activates/deactivates the service. This is called by the |
| 82 // when it is (de)activated. | 82 // SupervisedUserService when it is (de)activated. |
| 83 void SetActive(bool active); | 83 void SetActive(bool active); |
| 84 | 84 |
| 85 // Whether managed user settings are available. | 85 // Whether supervised user settings are available. |
| 86 bool IsReady(); | 86 bool IsReady(); |
| 87 | 87 |
| 88 // Clears all managed user settings and items. | 88 // Clears all supervised user settings and items. |
| 89 void Clear(); | 89 void Clear(); |
| 90 | 90 |
| 91 // Constructs a key for a split managed user setting from a prefix and a | 91 // Constructs a key for a split supervised user setting from a prefix and a |
| 92 // variable key. | 92 // variable key. |
| 93 static std::string MakeSplitSettingKey(const std::string& prefix, | 93 static std::string MakeSplitSettingKey(const std::string& prefix, |
| 94 const std::string& key); | 94 const std::string& key); |
| 95 | 95 |
| 96 // Uploads an item to the Sync server. Items are the same data structure as | 96 // Uploads an item to the Sync server. Items are the same data structure as |
| 97 // managed user settings (i.e. key-value pairs, as described at the top of | 97 // supervised user settings (i.e. key-value pairs, as described at the top of |
| 98 // the file), but they are only uploaded (whereas managed user settings are | 98 // the file), but they are only uploaded (whereas supervised user settings are |
| 99 // only downloaded), and never passed to the preference system. | 99 // only downloaded), and never passed to the preference system. |
| 100 // An example of an uploaded item is an access request to a blocked URL. | 100 // An example of an uploaded item is an access request to a blocked URL. |
| 101 void UploadItem(const std::string& key, scoped_ptr<base::Value> value); | 101 void UploadItem(const std::string& key, scoped_ptr<base::Value> value); |
| 102 | 102 |
| 103 // Sets the setting with the given |key| to a copy of the given |value|. | 103 // Sets the setting with the given |key| to a copy of the given |value|. |
| 104 void SetLocalSettingForTesting(const std::string& key, | 104 void SetLocalSettingForTesting(const std::string& key, |
| 105 scoped_ptr<base::Value> value); | 105 scoped_ptr<base::Value> value); |
| 106 | 106 |
| 107 // Public for testing. | 107 // Public for testing. |
| 108 static syncer::SyncData CreateSyncDataForSetting(const std::string& name, | 108 static syncer::SyncData CreateSyncDataForSetting(const std::string& name, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 128 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; | 128 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
| 129 virtual void OnInitializationCompleted(bool success) OVERRIDE; | 129 virtual void OnInitializationCompleted(bool success) OVERRIDE; |
| 130 | 130 |
| 131 private: | 131 private: |
| 132 base::DictionaryValue* GetOrCreateDictionary(const std::string& key) const; | 132 base::DictionaryValue* GetOrCreateDictionary(const std::string& key) const; |
| 133 base::DictionaryValue* GetAtomicSettings() const; | 133 base::DictionaryValue* GetAtomicSettings() const; |
| 134 base::DictionaryValue* GetSplitSettings() const; | 134 base::DictionaryValue* GetSplitSettings() const; |
| 135 base::DictionaryValue* GetQueuedItems() const; | 135 base::DictionaryValue* GetQueuedItems() const; |
| 136 | 136 |
| 137 // Returns the dictionary where a given Sync item should be stored, depending | 137 // Returns the dictionary where a given Sync item should be stored, depending |
| 138 // on whether the managed user setting is atomic or split. In case of a split | 138 // on whether the supervised user setting is atomic or split. In case of a |
| 139 // setting, the split setting prefix of |key| is removed, so that |key| can | 139 // split setting, the split setting prefix of |key| is removed, so that |key| |
| 140 // be used to update the returned dictionary. | 140 // can be used to update the returned dictionary. |
| 141 base::DictionaryValue* GetDictionaryAndSplitKey(std::string* key) const; | 141 base::DictionaryValue* GetDictionaryAndSplitKey(std::string* key) const; |
| 142 | 142 |
| 143 // Returns a dictionary with all managed user settings if the service is | 143 // Returns a dictionary with all supervised user settings if the service is |
| 144 // active, or NULL otherwise. | 144 // active, or NULL otherwise. |
| 145 scoped_ptr<base::DictionaryValue> GetSettings(); | 145 scoped_ptr<base::DictionaryValue> GetSettings(); |
| 146 | 146 |
| 147 // Sends the settings to all subscribers. This method should be called by the | 147 // Sends the settings to all subscribers. This method should be called by the |
| 148 // subclass whenever the settings change. | 148 // subclass whenever the settings change. |
| 149 void InformSubscribers(); | 149 void InformSubscribers(); |
| 150 | 150 |
| 151 // Used for persisting the settings. Unlike other PrefStores, this one is not | 151 // Used for persisting the settings. Unlike other PrefStores, this one is not |
| 152 // directly hooked up to the PrefService. | 152 // directly hooked up to the PrefService. |
| 153 scoped_refptr<PersistentPrefStore> store_; | 153 scoped_refptr<PersistentPrefStore> store_; |
| 154 | 154 |
| 155 bool active_; | 155 bool active_; |
| 156 | 156 |
| 157 // A set of local settings that are fixed and not configured remotely. | 157 // A set of local settings that are fixed and not configured remotely. |
| 158 scoped_ptr<base::DictionaryValue> local_settings_; | 158 scoped_ptr<base::DictionaryValue> local_settings_; |
| 159 | 159 |
| 160 std::vector<SettingsCallback> subscribers_; | 160 std::vector<SettingsCallback> subscribers_; |
| 161 | 161 |
| 162 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 162 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
| 163 scoped_ptr<syncer::SyncErrorFactory> error_handler_; | 163 scoped_ptr<syncer::SyncErrorFactory> error_handler_; |
| 164 | 164 |
| 165 DISALLOW_COPY_AND_ASSIGN(ManagedUserSettingsService); | 165 DISALLOW_COPY_AND_ASSIGN(SupervisedUserSettingsService); |
| 166 }; | 166 }; |
| 167 | 167 |
| 168 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SETTINGS_SERVICE_H_ | 168 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SETTINGS_SERVICE_H_ |
| OLD | NEW |