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_MANAGED_MODE_MANAGED_USER_SETTINGS_SERVICE_H_ |
6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SETTINGS_SERVICE_H_ | 6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_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/managed_mode/managed_users.h" |
17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 18 #include "sync/api/syncable_service.h" |
18 | 19 |
19 class PersistentPrefStore; | 20 class PersistentPrefStore; |
20 class Profile; | 21 class Profile; |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 class FilePath; | 24 class FilePath; |
24 class SequencedTaskRunner; | 25 class SequencedTaskRunner; |
25 } | 26 } |
26 | 27 |
27 // TODO(bauerb): This class is not yet fully functional. | |
28 // This class syncs managed user settings from a server, which are mapped to | 28 // This class syncs managed 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 BrowserContextKeyedService, | 52 class ManagedUserSettingsService : public BrowserContextKeyedService, |
| 53 public syncer::SyncableService, |
53 public PrefStore::Observer { | 54 public PrefStore::Observer { |
54 public: | 55 public: |
55 // A callback whose first parameter is a dictionary containing all managed | 56 // A callback whose first parameter is a dictionary containing all managed |
56 // 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 |
57 // inactive, i.e. the user is not managed. | 58 // inactive, i.e. the user is not managed. |
58 typedef base::Callback<void(const base::DictionaryValue*)> SettingsCallback; | 59 typedef base::Callback<void(const base::DictionaryValue*)> SettingsCallback; |
59 | 60 |
60 ManagedUserSettingsService(); | 61 ManagedUserSettingsService(); |
61 virtual ~ManagedUserSettingsService(); | 62 virtual ~ManagedUserSettingsService(); |
62 | 63 |
| 64 // Initializes the service by loading its settings from a file underneath the |
| 65 // |profile_path|. File I/O will be serialized via the |
| 66 // |sequenced_task_runner|. If |load_synchronously| is true, the settings will |
| 67 // be loaded synchronously, otherwise asynchronously. |
| 68 void Init(base::FilePath profile_path, |
| 69 base::SequencedTaskRunner* sequenced_task_runner, |
| 70 bool load_synchronously); |
| 71 |
63 // Initializes the service by loading its settings from the |pref_store|. | 72 // Initializes the service by loading its settings from the |pref_store|. |
64 // 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 |
65 // default one. | 74 // default one. |
66 void Init(scoped_refptr<PersistentPrefStore> pref_store); | 75 void Init(scoped_refptr<PersistentPrefStore> pref_store); |
67 | 76 |
68 // Adds a callback to be called when managed user settings are initially | 77 // Adds a callback to be called when managed user settings are initially |
69 // available, or when they change. | 78 // available, or when they change. |
70 void Subscribe(const SettingsCallback& callback); | 79 void Subscribe(const SettingsCallback& callback); |
71 | 80 |
72 // Activates the service. This happens when the user is managed. | 81 // Activates the service. This happens when the user is managed. |
73 void Activate(); | 82 void Activate(); |
74 | 83 |
75 // Whether managed user settings are available. | 84 // Whether managed user settings are available. |
76 bool IsReady(); | 85 bool IsReady(); |
77 | 86 |
78 // Clears all managed user settings and items. | 87 // Clears all managed user settings and items. |
79 void Clear(); | 88 void Clear(); |
80 | 89 |
| 90 // Constructs a key for a split managed user setting from a prefix and a |
| 91 // variable key. |
| 92 static std::string MakeSplitSettingKey(const std::string& prefix, |
| 93 const std::string& key); |
| 94 |
| 95 // Uploads an item to the Sync server. Items are the same data structure as |
| 96 // managed user settings (i.e. key-value pairs, as described at the top of |
| 97 // the file), but they are only uploaded (whereas managed user settings are |
| 98 // only downloaded), and never passed to the preference system. |
| 99 // An example of an uploaded item is an access request to a blocked URL. |
| 100 void UploadItem(const std::string& key, scoped_ptr<base::Value> value); |
| 101 |
81 // Sets the setting with the given |key| to a copy of the given |value|. | 102 // Sets the setting with the given |key| to a copy of the given |value|. |
82 void SetLocalSettingForTesting(const std::string& key, | 103 void SetLocalSettingForTesting(const std::string& key, |
83 scoped_ptr<base::Value> value); | 104 scoped_ptr<base::Value> value); |
84 | 105 |
| 106 // Public for testing. |
| 107 static syncer::SyncData CreateSyncDataForSetting(const std::string& name, |
| 108 const base::Value& value); |
| 109 |
85 // BrowserContextKeyedService implementation: | 110 // BrowserContextKeyedService implementation: |
86 virtual void Shutdown() OVERRIDE; | 111 virtual void Shutdown() OVERRIDE; |
87 | 112 |
| 113 // SyncableService implementation: |
| 114 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( |
| 115 syncer::ModelType type, |
| 116 const syncer::SyncDataList& initial_sync_data, |
| 117 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 118 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE; |
| 119 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; |
| 120 virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const |
| 121 OVERRIDE; |
| 122 virtual syncer::SyncError ProcessSyncChanges( |
| 123 const tracked_objects::Location& from_here, |
| 124 const syncer::SyncChangeList& change_list) OVERRIDE; |
| 125 |
88 // PrefStore::Observer implementation: | 126 // PrefStore::Observer implementation: |
89 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; | 127 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
90 virtual void OnInitializationCompleted(bool success) OVERRIDE; | 128 virtual void OnInitializationCompleted(bool success) OVERRIDE; |
91 | 129 |
92 private: | 130 private: |
93 base::DictionaryValue* GetOrCreateDictionary(const std::string& key) const; | 131 base::DictionaryValue* GetOrCreateDictionary(const std::string& key) const; |
94 base::DictionaryValue* GetAtomicSettings() const; | 132 base::DictionaryValue* GetAtomicSettings() const; |
95 base::DictionaryValue* GetSplitSettings() const; | 133 base::DictionaryValue* GetSplitSettings() const; |
96 base::DictionaryValue* GetQueuedItems() const; | 134 base::DictionaryValue* GetQueuedItems() const; |
97 | 135 |
| 136 // Returns the dictionary where a given Sync item should be stored, depending |
| 137 // on whether the managed user setting is atomic or split. In case of a split |
| 138 // setting, the split setting prefix of |key| is removed, so that |key| can |
| 139 // be used to update the returned dictionary. |
| 140 base::DictionaryValue* GetDictionaryAndSplitKey(std::string* key) const; |
| 141 |
98 // Returns a dictionary with all managed user settings if the service is | 142 // Returns a dictionary with all managed user settings if the service is |
99 // active, or NULL otherwise. | 143 // active, or NULL otherwise. |
100 scoped_ptr<base::DictionaryValue> GetSettings(); | 144 scoped_ptr<base::DictionaryValue> GetSettings(); |
101 | 145 |
102 // Sends the settings to all subscribers. This method should be called by the | 146 // Sends the settings to all subscribers. This method should be called by the |
103 // subclass whenever the settings change. | 147 // subclass whenever the settings change. |
104 void InformSubscribers(); | 148 void InformSubscribers(); |
105 | 149 |
106 // Used for persisting the settings. Unlike other PrefStores, this one is not | 150 // Used for persisting the settings. Unlike other PrefStores, this one is not |
107 // directly hooked up to the PrefService. | 151 // directly hooked up to the PrefService. |
108 scoped_refptr<PersistentPrefStore> store_; | 152 scoped_refptr<PersistentPrefStore> store_; |
109 | 153 |
110 bool active_; | 154 bool active_; |
111 | 155 |
112 // A set of local settings that are fixed and not configured remotely. | 156 // A set of local settings that are fixed and not configured remotely. |
113 scoped_ptr<base::DictionaryValue> local_settings_; | 157 scoped_ptr<base::DictionaryValue> local_settings_; |
114 | 158 |
115 std::vector<SettingsCallback> subscribers_; | 159 std::vector<SettingsCallback> subscribers_; |
116 | 160 |
| 161 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
| 162 scoped_ptr<syncer::SyncErrorFactory> error_handler_; |
| 163 |
117 DISALLOW_COPY_AND_ASSIGN(ManagedUserSettingsService); | 164 DISALLOW_COPY_AND_ASSIGN(ManagedUserSettingsService); |
118 }; | 165 }; |
119 | 166 |
120 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SETTINGS_SERVICE_H_ | 167 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SETTINGS_SERVICE_H_ |
OLD | NEW |