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 #include "chrome/browser/managed_mode/managed_user_settings_service.h" | 5 #include "chrome/browser/managed_mode/managed_user_settings_service.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/prefs/json_pref_store.h" | 10 #include "base/prefs/json_pref_store.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
13 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" | 13 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" |
14 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 14 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
15 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/user_metrics.h" | 17 #include "content/public/browser/user_metrics.h" |
| 18 #include "sync/api/sync_change.h" |
| 19 #include "sync/api/sync_error_factory.h" |
| 20 #include "sync/protocol/sync.pb.h" |
18 | 21 |
19 using base::DictionaryValue; | 22 using base::DictionaryValue; |
| 23 using base::JSONReader; |
20 using base::Value; | 24 using base::Value; |
21 using content::BrowserThread; | 25 using content::BrowserThread; |
22 using content::UserMetricsAction; | 26 using content::UserMetricsAction; |
| 27 using syncer::MANAGED_USER_SETTINGS; |
| 28 using syncer::ModelType; |
| 29 using syncer::SyncChange; |
| 30 using syncer::SyncChangeList; |
| 31 using syncer::SyncChangeProcessor; |
| 32 using syncer::SyncData; |
| 33 using syncer::SyncDataList; |
| 34 using syncer::SyncError; |
| 35 using syncer::SyncErrorFactory; |
| 36 using syncer::SyncMergeResult; |
23 | 37 |
24 const char kAtomicSettings[] = "atomic_settings"; | 38 const char kAtomicSettings[] = "atomic_settings"; |
25 const char kManagedUserInternalItemPrefix[] = "X-"; | 39 const char kManagedUserInternalItemPrefix[] = "X-"; |
26 const char kQueuedItems[] = "queued_items"; | 40 const char kQueuedItems[] = "queued_items"; |
27 const char kSplitSettingKeySeparator = ':'; | 41 const char kSplitSettingKeySeparator = ':'; |
28 const char kSplitSettings[] = "split_settings"; | 42 const char kSplitSettings[] = "split_settings"; |
29 | 43 |
30 namespace { | 44 namespace { |
31 | 45 |
32 bool SettingShouldApplyToPrefs(const std::string& name) { | 46 bool SettingShouldApplyToPrefs(const std::string& name) { |
33 return !StartsWithASCII(name, kManagedUserInternalItemPrefix, false); | 47 return !StartsWithASCII(name, kManagedUserInternalItemPrefix, false); |
34 } | 48 } |
35 | 49 |
36 } // namespace | 50 } // namespace |
37 | 51 |
38 ManagedUserSettingsService::ManagedUserSettingsService() | 52 ManagedUserSettingsService::ManagedUserSettingsService() |
39 : active_(false), local_settings_(new DictionaryValue) {} | 53 : active_(false), local_settings_(new DictionaryValue) {} |
40 | 54 |
41 ManagedUserSettingsService::~ManagedUserSettingsService() {} | 55 ManagedUserSettingsService::~ManagedUserSettingsService() {} |
42 | 56 |
43 void ManagedUserSettingsService::Init( | 57 void ManagedUserSettingsService::Init( |
| 58 base::FilePath profile_path, |
| 59 base::SequencedTaskRunner* sequenced_task_runner, |
| 60 bool load_synchronously) { |
| 61 base::FilePath path = |
| 62 profile_path.Append(chrome::kManagedUserSettingsFilename); |
| 63 PersistentPrefStore* store = new JsonPrefStore(path, sequenced_task_runner); |
| 64 Init(store); |
| 65 if (load_synchronously) |
| 66 store_->ReadPrefs(); |
| 67 else |
| 68 store_->ReadPrefsAsync(NULL); |
| 69 } |
| 70 |
| 71 void ManagedUserSettingsService::Init( |
44 scoped_refptr<PersistentPrefStore> store) { | 72 scoped_refptr<PersistentPrefStore> store) { |
45 DCHECK(!store_); | 73 DCHECK(!store_); |
46 store_ = store; | 74 store_ = store; |
47 store_->AddObserver(this); | 75 store_->AddObserver(this); |
48 } | 76 } |
49 | 77 |
50 void ManagedUserSettingsService::Subscribe(const SettingsCallback& callback) { | 78 void ManagedUserSettingsService::Subscribe(const SettingsCallback& callback) { |
51 if (IsReady()) { | 79 if (IsReady()) { |
52 scoped_ptr<base::DictionaryValue> settings = GetSettings(); | 80 scoped_ptr<base::DictionaryValue> settings = GetSettings(); |
53 callback.Run(settings.get()); | 81 callback.Run(settings.get()); |
54 } | 82 } |
55 | 83 |
56 subscribers_.push_back(callback); | 84 subscribers_.push_back(callback); |
57 } | 85 } |
58 | 86 |
59 void ManagedUserSettingsService::Activate() { | 87 void ManagedUserSettingsService::Activate() { |
60 active_ = true; | 88 active_ = true; |
61 InformSubscribers(); | 89 InformSubscribers(); |
62 } | 90 } |
63 | 91 |
64 bool ManagedUserSettingsService::IsReady() { | 92 bool ManagedUserSettingsService::IsReady() { |
65 return store_->IsInitializationComplete(); | 93 return store_->IsInitializationComplete(); |
66 } | 94 } |
67 | 95 |
68 void ManagedUserSettingsService::Clear() { | 96 void ManagedUserSettingsService::Clear() { |
69 store_->RemoveValue(kAtomicSettings); | 97 store_->RemoveValue(kAtomicSettings); |
70 store_->RemoveValue(kSplitSettings); | 98 store_->RemoveValue(kSplitSettings); |
71 } | 99 } |
| 100 |
| 101 // static |
| 102 std::string ManagedUserSettingsService::MakeSplitSettingKey( |
| 103 const std::string& prefix, |
| 104 const std::string& key) { |
| 105 return prefix + kSplitSettingKeySeparator + key; |
| 106 } |
| 107 |
| 108 void ManagedUserSettingsService::UploadItem(const std::string& key, |
| 109 scoped_ptr<Value> value) { |
| 110 DCHECK(!SettingShouldApplyToPrefs(key)); |
| 111 |
| 112 std::string key_suffix = key; |
| 113 DictionaryValue* dict = NULL; |
| 114 if (sync_processor_) { |
| 115 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Syncing")); |
| 116 dict = GetDictionaryAndSplitKey(&key_suffix); |
| 117 DCHECK(GetQueuedItems()->empty()); |
| 118 SyncChangeList change_list; |
| 119 SyncData data = CreateSyncDataForSetting(key, *value); |
| 120 SyncChange::SyncChangeType change_type = |
| 121 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE |
| 122 : SyncChange::ACTION_ADD; |
| 123 change_list.push_back(SyncChange(FROM_HERE, change_type, data)); |
| 124 SyncError error = |
| 125 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); |
| 126 DCHECK(!error.IsSet()) << error.ToString(); |
| 127 } else { |
| 128 // Queue the item up to be uploaded when we start syncing |
| 129 // (in MergeDataAndStartSyncing()). |
| 130 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Queued")); |
| 131 dict = GetQueuedItems(); |
| 132 } |
| 133 dict->SetWithoutPathExpansion(key_suffix, value.release()); |
| 134 } |
| 135 |
72 void ManagedUserSettingsService::SetLocalSettingForTesting( | 136 void ManagedUserSettingsService::SetLocalSettingForTesting( |
73 const std::string& key, | 137 const std::string& key, |
74 scoped_ptr<Value> value) { | 138 scoped_ptr<Value> value) { |
75 if (value) | 139 if (value) |
76 local_settings_->SetWithoutPathExpansion(key, value.release()); | 140 local_settings_->SetWithoutPathExpansion(key, value.release()); |
77 else | 141 else |
78 local_settings_->RemoveWithoutPathExpansion(key, NULL); | 142 local_settings_->RemoveWithoutPathExpansion(key, NULL); |
79 | 143 |
80 InformSubscribers(); | 144 InformSubscribers(); |
81 } | 145 } |
82 | 146 |
| 147 // static |
| 148 SyncData ManagedUserSettingsService::CreateSyncDataForSetting( |
| 149 const std::string& name, |
| 150 const Value& value) { |
| 151 std::string json_value; |
| 152 base::JSONWriter::Write(&value, &json_value); |
| 153 ::sync_pb::EntitySpecifics specifics; |
| 154 specifics.mutable_managed_user_setting()->set_name(name); |
| 155 specifics.mutable_managed_user_setting()->set_value(json_value); |
| 156 return SyncData::CreateLocalData(name, name, specifics); |
| 157 } |
| 158 |
83 void ManagedUserSettingsService::Shutdown() { | 159 void ManagedUserSettingsService::Shutdown() { |
84 store_->RemoveObserver(this); | 160 store_->RemoveObserver(this); |
85 } | 161 } |
| 162 |
| 163 SyncMergeResult ManagedUserSettingsService::MergeDataAndStartSyncing( |
| 164 ModelType type, |
| 165 const SyncDataList& initial_sync_data, |
| 166 scoped_ptr<SyncChangeProcessor> sync_processor, |
| 167 scoped_ptr<SyncErrorFactory> error_handler) { |
| 168 DCHECK_EQ(MANAGED_USER_SETTINGS, type); |
| 169 sync_processor_ = sync_processor.Pass(); |
| 170 error_handler_ = error_handler.Pass(); |
| 171 |
| 172 // Clear all atomic and split settings, then recreate them from Sync data. |
| 173 Clear(); |
| 174 for (SyncDataList::const_iterator it = initial_sync_data.begin(); |
| 175 it != initial_sync_data.end(); ++it) { |
| 176 DCHECK_EQ(MANAGED_USER_SETTINGS, it->GetDataType()); |
| 177 const ::sync_pb::ManagedUserSettingSpecifics& managed_user_setting = |
| 178 it->GetSpecifics().managed_user_setting(); |
| 179 scoped_ptr<Value> value(JSONReader::Read(managed_user_setting.value())); |
| 180 std::string name_suffix = managed_user_setting.name(); |
| 181 DictionaryValue* dict = GetDictionaryAndSplitKey(&name_suffix); |
| 182 dict->SetWithoutPathExpansion(name_suffix, value.release()); |
| 183 } |
| 184 store_->ReportValueChanged(kAtomicSettings); |
| 185 store_->ReportValueChanged(kSplitSettings); |
| 186 InformSubscribers(); |
| 187 |
| 188 // Upload all the queued up items (either with an ADD or an UPDATE action, |
| 189 // depending on whether they already exist) and move them to split settings. |
| 190 SyncChangeList change_list; |
| 191 DictionaryValue* queued_items = GetQueuedItems(); |
| 192 for (DictionaryValue::Iterator it(*queued_items); !it.IsAtEnd(); |
| 193 it.Advance()) { |
| 194 std::string key_suffix = it.key(); |
| 195 DictionaryValue* dict = GetDictionaryAndSplitKey(&key_suffix); |
| 196 SyncData data = CreateSyncDataForSetting(it.key(), it.value()); |
| 197 SyncChange::SyncChangeType change_type = |
| 198 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE |
| 199 : SyncChange::ACTION_ADD; |
| 200 change_list.push_back(SyncChange(FROM_HERE, change_type, data)); |
| 201 dict->SetWithoutPathExpansion(key_suffix, it.value().DeepCopy()); |
| 202 } |
| 203 queued_items->Clear(); |
| 204 |
| 205 SyncMergeResult result(MANAGED_USER_SETTINGS); |
| 206 // Process all the accumulated changes from the queued items. |
| 207 if (change_list.size() > 0) { |
| 208 store_->ReportValueChanged(kQueuedItems); |
| 209 result.set_error( |
| 210 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); |
| 211 } |
| 212 |
| 213 // TODO(bauerb): Statistics? |
| 214 return result; |
| 215 } |
| 216 |
| 217 void ManagedUserSettingsService::StopSyncing(ModelType type) { |
| 218 DCHECK_EQ(syncer::MANAGED_USER_SETTINGS, type); |
| 219 sync_processor_.reset(); |
| 220 error_handler_.reset(); |
| 221 } |
| 222 |
| 223 SyncDataList ManagedUserSettingsService::GetAllSyncData( |
| 224 ModelType type) const { |
| 225 DCHECK_EQ(syncer::MANAGED_USER_SETTINGS, type); |
| 226 SyncDataList data; |
| 227 for (DictionaryValue::Iterator it(*GetAtomicSettings()); !it.IsAtEnd(); |
| 228 it.Advance()) { |
| 229 data.push_back(CreateSyncDataForSetting(it.key(), it.value())); |
| 230 } |
| 231 for (DictionaryValue::Iterator it(*GetSplitSettings()); !it.IsAtEnd(); |
| 232 it.Advance()) { |
| 233 const DictionaryValue* dict = NULL; |
| 234 it.value().GetAsDictionary(&dict); |
| 235 for (DictionaryValue::Iterator jt(*dict); !jt.IsAtEnd(); jt.Advance()) { |
| 236 data.push_back(CreateSyncDataForSetting( |
| 237 MakeSplitSettingKey(it.key(), jt.key()), jt.value())); |
| 238 } |
| 239 } |
| 240 DCHECK_EQ(0u, GetQueuedItems()->size()); |
| 241 return data; |
| 242 } |
| 243 |
| 244 SyncError ManagedUserSettingsService::ProcessSyncChanges( |
| 245 const tracked_objects::Location& from_here, |
| 246 const SyncChangeList& change_list) { |
| 247 for (SyncChangeList::const_iterator it = change_list.begin(); |
| 248 it != change_list.end(); ++it) { |
| 249 SyncData data = it->sync_data(); |
| 250 DCHECK_EQ(MANAGED_USER_SETTINGS, data.GetDataType()); |
| 251 const ::sync_pb::ManagedUserSettingSpecifics& managed_user_setting = |
| 252 data.GetSpecifics().managed_user_setting(); |
| 253 std::string key = managed_user_setting.name(); |
| 254 DictionaryValue* dict = GetDictionaryAndSplitKey(&key); |
| 255 switch (it->change_type()) { |
| 256 case SyncChange::ACTION_ADD: |
| 257 case SyncChange::ACTION_UPDATE: { |
| 258 scoped_ptr<Value> value(JSONReader::Read(managed_user_setting.value())); |
| 259 if (dict->HasKey(key)) { |
| 260 DLOG_IF(WARNING, it->change_type() == SyncChange::ACTION_ADD) |
| 261 << "Value for key " << key << " already exists"; |
| 262 } else { |
| 263 DLOG_IF(WARNING, it->change_type() == SyncChange::ACTION_UPDATE) |
| 264 << "Value for key " << key << " doesn't exist yet"; |
| 265 } |
| 266 dict->SetWithoutPathExpansion(key, value.release()); |
| 267 break; |
| 268 } |
| 269 case SyncChange::ACTION_DELETE: { |
| 270 DLOG_IF(WARNING, !dict->HasKey(key)) << "Trying to delete nonexistent " |
| 271 << "key " << key; |
| 272 dict->RemoveWithoutPathExpansion(key, NULL); |
| 273 break; |
| 274 } |
| 275 case SyncChange::ACTION_INVALID: { |
| 276 NOTREACHED(); |
| 277 break; |
| 278 } |
| 279 } |
| 280 } |
| 281 store_->ReportValueChanged(kAtomicSettings); |
| 282 store_->ReportValueChanged(kSplitSettings); |
| 283 InformSubscribers(); |
| 284 |
| 285 SyncError error; |
| 286 return error; |
| 287 } |
| 288 |
86 void ManagedUserSettingsService::OnPrefValueChanged(const std::string& key) {} | 289 void ManagedUserSettingsService::OnPrefValueChanged(const std::string& key) {} |
87 | 290 |
88 void ManagedUserSettingsService::OnInitializationCompleted(bool success) { | 291 void ManagedUserSettingsService::OnInitializationCompleted(bool success) { |
89 DCHECK(success); | 292 DCHECK(success); |
90 DCHECK(IsReady()); | 293 DCHECK(IsReady()); |
91 InformSubscribers(); | 294 InformSubscribers(); |
92 } | 295 } |
93 | 296 |
94 DictionaryValue* ManagedUserSettingsService::GetOrCreateDictionary( | 297 DictionaryValue* ManagedUserSettingsService::GetOrCreateDictionary( |
95 const std::string& key) const { | 298 const std::string& key) const { |
(...skipping 15 matching lines...) Expand all Loading... |
111 } | 314 } |
112 | 315 |
113 DictionaryValue* ManagedUserSettingsService::GetSplitSettings() const { | 316 DictionaryValue* ManagedUserSettingsService::GetSplitSettings() const { |
114 return GetOrCreateDictionary(kSplitSettings); | 317 return GetOrCreateDictionary(kSplitSettings); |
115 } | 318 } |
116 | 319 |
117 DictionaryValue* ManagedUserSettingsService::GetQueuedItems() const { | 320 DictionaryValue* ManagedUserSettingsService::GetQueuedItems() const { |
118 return GetOrCreateDictionary(kQueuedItems); | 321 return GetOrCreateDictionary(kQueuedItems); |
119 } | 322 } |
120 | 323 |
| 324 DictionaryValue* ManagedUserSettingsService::GetDictionaryAndSplitKey( |
| 325 std::string* key) const { |
| 326 size_t pos = key->find_first_of(kSplitSettingKeySeparator); |
| 327 if (pos == std::string::npos) |
| 328 return GetAtomicSettings(); |
| 329 |
| 330 DictionaryValue* split_settings = GetSplitSettings(); |
| 331 std::string prefix = key->substr(0, pos); |
| 332 DictionaryValue* dict = NULL; |
| 333 if (!split_settings->GetDictionary(prefix, &dict)) { |
| 334 dict = new DictionaryValue; |
| 335 DCHECK(!split_settings->HasKey(prefix)); |
| 336 split_settings->Set(prefix, dict); |
| 337 } |
| 338 key->erase(0, pos + 1); |
| 339 return dict; |
| 340 } |
| 341 |
121 scoped_ptr<DictionaryValue> ManagedUserSettingsService::GetSettings() { | 342 scoped_ptr<DictionaryValue> ManagedUserSettingsService::GetSettings() { |
122 DCHECK(IsReady()); | 343 DCHECK(IsReady()); |
123 if (!active_) | 344 if (!active_) |
124 return scoped_ptr<base::DictionaryValue>(); | 345 return scoped_ptr<base::DictionaryValue>(); |
125 | 346 |
126 scoped_ptr<DictionaryValue> settings(local_settings_->DeepCopy()); | 347 scoped_ptr<DictionaryValue> settings(local_settings_->DeepCopy()); |
127 | 348 |
128 DictionaryValue* atomic_settings = GetAtomicSettings(); | 349 DictionaryValue* atomic_settings = GetAtomicSettings(); |
129 for (DictionaryValue::Iterator it(*atomic_settings); !it.IsAtEnd(); | 350 for (DictionaryValue::Iterator it(*atomic_settings); !it.IsAtEnd(); |
130 it.Advance()) { | 351 it.Advance()) { |
(...skipping 18 matching lines...) Expand all Loading... |
149 void ManagedUserSettingsService::InformSubscribers() { | 370 void ManagedUserSettingsService::InformSubscribers() { |
150 if (!IsReady()) | 371 if (!IsReady()) |
151 return; | 372 return; |
152 | 373 |
153 scoped_ptr<base::DictionaryValue> settings = GetSettings(); | 374 scoped_ptr<base::DictionaryValue> settings = GetSettings(); |
154 for (std::vector<SettingsCallback>::iterator it = subscribers_.begin(); | 375 for (std::vector<SettingsCallback>::iterator it = subscribers_.begin(); |
155 it != subscribers_.end(); ++it) { | 376 it != subscribers_.end(); ++it) { |
156 it->Run(settings.get()); | 377 it->Run(settings.get()); |
157 } | 378 } |
158 } | 379 } |
OLD | NEW |