MD Downloads: UI review feedback
[chromium-blink-merge.git] / chrome / browser / supervised_user / legacy / supervised_user_shared_settings_service.cc
blob9ce935e5972e9b3674289ac2e1dcc919cf6d3d21
1 // Copyright 2014 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.
5 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service.h"
7 #include <map>
8 #include <set>
10 #include "base/json/json_reader.h"
11 #include "base/json/json_writer.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/scoped_user_pref_update.h"
14 #include "base/values.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/pref_registry/pref_registry_syncable.h"
17 #include "sync/api/sync_change.h"
18 #include "sync/api/sync_data.h"
19 #include "sync/api/sync_error.h"
20 #include "sync/api/sync_error_factory.h"
21 #include "sync/api/sync_merge_result.h"
22 #include "sync/protocol/sync.pb.h"
24 using base::DictionaryValue;
25 using base::Value;
26 using syncer::ModelType;
27 using syncer::SUPERVISED_USER_SHARED_SETTINGS;
28 using syncer::SyncChange;
29 using syncer::SyncChangeList;
30 using syncer::SyncChangeProcessor;
31 using syncer::SyncData;
32 using syncer::SyncDataList;
33 using syncer::SyncError;
34 using syncer::SyncErrorFactory;
35 using syncer::SyncMergeResult;
37 namespace {
39 const char kAcknowledged[] = "acknowledged";
40 const char kValue[] = "value";
42 DictionaryValue* FindOrCreateDictionary(DictionaryValue* parent,
43 const std::string& key) {
44 DictionaryValue* dict = NULL;
45 if (!parent->GetDictionaryWithoutPathExpansion(key, &dict)) {
46 dict = new DictionaryValue;
47 parent->SetWithoutPathExpansion(key, dict);
49 return dict;
52 class ScopedSupervisedUserSharedSettingsUpdate {
53 public:
54 ScopedSupervisedUserSharedSettingsUpdate(PrefService* prefs,
55 const std::string& su_id)
56 : update_(prefs, prefs::kSupervisedUserSharedSettings), su_id_(su_id) {
57 DCHECK(!su_id.empty());
59 // A supervised user can only modify their own settings.
60 std::string id = prefs->GetString(prefs::kSupervisedUserId);
61 DCHECK(id.empty() || id == su_id);
64 DictionaryValue* Get() {
65 return FindOrCreateDictionary(update_.Get(), su_id_);
68 private:
69 DictionaryPrefUpdate update_;
70 std::string su_id_;
73 SyncData CreateSyncDataForValue(
74 const std::string& su_id,
75 const std::string& key,
76 const Value& dict_value) {
77 const DictionaryValue* dict = NULL;
78 if (!dict_value.GetAsDictionary(&dict))
79 return SyncData();
81 const Value* value = NULL;
82 if (!dict->Get(kValue, &value))
83 return SyncData();
85 bool acknowledged = false;
86 dict->GetBoolean(kAcknowledged, &acknowledged);
88 return SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
89 su_id, key, *value, acknowledged);
92 } // namespace
95 SupervisedUserSharedSettingsService::SupervisedUserSharedSettingsService(
96 PrefService* prefs)
97 : prefs_(prefs) {}
99 SupervisedUserSharedSettingsService::~SupervisedUserSharedSettingsService() {}
101 void SupervisedUserSharedSettingsService::SetValueInternal(
102 const std::string& su_id,
103 const std::string& key,
104 const Value& value,
105 bool acknowledged) {
106 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id);
107 DictionaryValue* update_dict = update.Get();
109 DictionaryValue* dict = NULL;
110 bool has_key = update_dict->GetDictionaryWithoutPathExpansion(key, &dict);
111 if (!has_key) {
112 dict = new DictionaryValue;
113 update_dict->SetWithoutPathExpansion(key, dict);
115 dict->SetWithoutPathExpansion(kValue, value.DeepCopy());
116 dict->SetBooleanWithoutPathExpansion(kAcknowledged, acknowledged);
118 if (!sync_processor_)
119 return;
121 SyncData data = CreateSyncDataForSetting(su_id, key, value, acknowledged);
122 SyncChange::SyncChangeType change_type =
123 has_key ? SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD;
124 SyncChangeList changes;
125 changes.push_back(SyncChange(FROM_HERE, change_type, data));
126 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
127 DCHECK(!error.IsSet()) << error.ToString();
130 const Value* SupervisedUserSharedSettingsService::GetValue(
131 const std::string& su_id,
132 const std::string& key) {
133 const DictionaryValue* data =
134 prefs_->GetDictionary(prefs::kSupervisedUserSharedSettings);
135 const DictionaryValue* dict = NULL;
136 if (!data->GetDictionaryWithoutPathExpansion(su_id, &dict))
137 return NULL;
139 const DictionaryValue* settings = NULL;
140 if (!dict->GetDictionaryWithoutPathExpansion(key, &settings))
141 return NULL;
143 const Value* value = NULL;
144 if (!settings->GetWithoutPathExpansion(kValue, &value))
145 return NULL;
147 return value;
150 void SupervisedUserSharedSettingsService::SetValue(
151 const std::string& su_id,
152 const std::string& key,
153 const Value& value) {
154 SetValueInternal(su_id, key, value, true);
157 scoped_ptr<
158 SupervisedUserSharedSettingsService::ChangeCallbackList::Subscription>
159 SupervisedUserSharedSettingsService::Subscribe(
160 const SupervisedUserSharedSettingsService::ChangeCallback& cb) {
161 return callbacks_.Add(cb);
164 // static
165 void SupervisedUserSharedSettingsService::RegisterProfilePrefs(
166 user_prefs::PrefRegistrySyncable* registry) {
167 registry->RegisterDictionaryPref(prefs::kSupervisedUserSharedSettings);
170 // static
171 SyncData SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
172 const std::string& su_id,
173 const std::string& key,
174 const Value& value,
175 bool acknowledged) {
176 std::string json_value;
177 base::JSONWriter::Write(value, &json_value);
178 ::sync_pb::EntitySpecifics specifics;
179 specifics.mutable_managed_user_shared_setting()->set_mu_id(su_id);
180 specifics.mutable_managed_user_shared_setting()->set_key(key);
181 specifics.mutable_managed_user_shared_setting()->set_value(json_value);
182 specifics.mutable_managed_user_shared_setting()->set_acknowledged(
183 acknowledged);
184 std::string title = su_id + ":" + key;
185 return SyncData::CreateLocalData(title, title, specifics);
188 void SupervisedUserSharedSettingsService::Shutdown() {}
190 syncer::SyncMergeResult
191 SupervisedUserSharedSettingsService::MergeDataAndStartSyncing(
192 syncer::ModelType type,
193 const syncer::SyncDataList& initial_sync_data,
194 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
195 scoped_ptr<syncer::SyncErrorFactory> error_handler) {
196 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type);
197 sync_processor_ = sync_processor.Pass();
198 error_handler_ = error_handler.Pass();
200 // We keep a map from MU ID to the set of keys that we have seen in the
201 // initial sync data.
202 std::map<std::string, std::set<std::string> > seen_keys;
204 // Iterate over all initial sync data, and update it locally. This means that
205 // the value from the server always wins over a local value.
206 for (const SyncData& sync_data : initial_sync_data) {
207 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, sync_data.GetDataType());
208 const ::sync_pb::ManagedUserSharedSettingSpecifics&
209 supervised_user_shared_setting =
210 sync_data.GetSpecifics().managed_user_shared_setting();
211 scoped_ptr<Value> value =
212 base::JSONReader::Read(supervised_user_shared_setting.value());
213 const std::string& su_id = supervised_user_shared_setting.mu_id();
214 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id);
215 const std::string& key = supervised_user_shared_setting.key();
216 DictionaryValue* dict = FindOrCreateDictionary(update.Get(), key);
217 dict->SetWithoutPathExpansion(kValue, value.release());
219 // Every setting we get from the server should have the acknowledged flag
220 // set.
221 DCHECK(supervised_user_shared_setting.acknowledged());
222 dict->SetBooleanWithoutPathExpansion(
223 kAcknowledged, supervised_user_shared_setting.acknowledged());
224 callbacks_.Notify(su_id, key);
226 seen_keys[su_id].insert(key);
229 // Iterate over all settings that we have locally, which includes settings
230 // that were just synced down. We filter those out using |seen_keys|.
231 SyncChangeList change_list;
232 const DictionaryValue* all_settings =
233 prefs_->GetDictionary(prefs::kSupervisedUserSharedSettings);
234 for (DictionaryValue::Iterator it(*all_settings); !it.IsAtEnd();
235 it.Advance()) {
236 const DictionaryValue* dict = NULL;
237 bool success = it.value().GetAsDictionary(&dict);
238 DCHECK(success);
240 const std::set<std::string>& seen = seen_keys[it.key()];
241 for (DictionaryValue::Iterator jt(*dict); !jt.IsAtEnd(); jt.Advance()) {
242 // We only need to upload settings that we haven't seen in the initial
243 // sync data (which means they were added locally).
244 if (seen.count(jt.key()) > 0)
245 continue;
247 SyncData data = CreateSyncDataForValue(it.key(), jt.key(), jt.value());
248 DCHECK(data.IsValid());
249 change_list.push_back(
250 SyncChange(FROM_HERE, SyncChange::ACTION_ADD, data));
254 SyncMergeResult result(SUPERVISED_USER_SHARED_SETTINGS);
255 // Process all the accumulated changes.
256 if (change_list.size() > 0) {
257 result.set_error(
258 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list));
261 // TODO(bauerb): Statistics?
262 return result;
265 void SupervisedUserSharedSettingsService::StopSyncing(syncer::ModelType type) {
266 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type);
267 sync_processor_.reset();
268 error_handler_.reset();
271 syncer::SyncDataList SupervisedUserSharedSettingsService::GetAllSyncData(
272 syncer::ModelType type) const {
273 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type);
274 SyncDataList data;
275 const DictionaryValue* all_settings =
276 prefs_->GetDictionary(prefs::kSupervisedUserSharedSettings);
277 for (DictionaryValue::Iterator it(*all_settings); !it.IsAtEnd();
278 it.Advance()) {
279 const DictionaryValue* dict = NULL;
280 bool success = it.value().GetAsDictionary(&dict);
281 DCHECK(success);
282 for (DictionaryValue::Iterator jt(*dict); !jt.IsAtEnd(); jt.Advance()) {
283 data.push_back(CreateSyncDataForValue(it.key(), jt.key(), jt.value()));
286 return data;
289 syncer::SyncError SupervisedUserSharedSettingsService::ProcessSyncChanges(
290 const tracked_objects::Location& from_here,
291 const syncer::SyncChangeList& change_list) {
292 for (const SyncChange& sync_change : change_list) {
293 SyncData data = sync_change.sync_data();
294 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, data.GetDataType());
295 const ::sync_pb::ManagedUserSharedSettingSpecifics&
296 supervised_user_shared_setting =
297 data.GetSpecifics().managed_user_shared_setting();
298 const std::string& key = supervised_user_shared_setting.key();
299 const std::string& su_id = supervised_user_shared_setting.mu_id();
300 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id);
301 DictionaryValue* update_dict = update.Get();
302 DictionaryValue* dict = NULL;
303 bool has_key = update_dict->GetDictionaryWithoutPathExpansion(key, &dict);
304 switch (sync_change.change_type()) {
305 case SyncChange::ACTION_ADD:
306 case SyncChange::ACTION_UPDATE: {
307 // Every setting we get from the server should have the acknowledged
308 // flag set.
309 DCHECK(supervised_user_shared_setting.acknowledged());
311 if (has_key) {
312 // If the supervised user already exists, it should be an update
313 // action.
314 DCHECK_EQ(SyncChange::ACTION_UPDATE, sync_change.change_type());
315 } else {
316 // Otherwise, it should be an add action.
317 DCHECK_EQ(SyncChange::ACTION_ADD, sync_change.change_type());
318 dict = new DictionaryValue;
319 update_dict->SetWithoutPathExpansion(key, dict);
321 scoped_ptr<Value> value =
322 base::JSONReader::Read(supervised_user_shared_setting.value());
323 dict->SetWithoutPathExpansion(kValue, value.release());
324 dict->SetBooleanWithoutPathExpansion(
325 kAcknowledged, supervised_user_shared_setting.acknowledged());
326 break;
328 case SyncChange::ACTION_DELETE: {
329 if (has_key)
330 update_dict->RemoveWithoutPathExpansion(key, NULL);
331 else
332 NOTREACHED() << "Trying to delete nonexistent key " << key;
333 break;
335 case SyncChange::ACTION_INVALID: {
336 NOTREACHED();
337 break;
340 callbacks_.Notify(su_id, key);
343 SyncError error;
344 return error;