Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/supervised_user/supervised_user_settings_service.h" | 5 #include "chrome/browser/supervised_user/supervised_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" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 | 43 |
| 44 namespace { | 44 namespace { |
| 45 | 45 |
| 46 bool SettingShouldApplyToPrefs(const std::string& name) { | 46 bool SettingShouldApplyToPrefs(const std::string& name) { |
| 47 return !StartsWithASCII(name, kSupervisedUserInternalItemPrefix, false); | 47 return !StartsWithASCII(name, kSupervisedUserInternalItemPrefix, false); |
| 48 } | 48 } |
| 49 | 49 |
| 50 } // namespace | 50 } // namespace |
| 51 | 51 |
| 52 SupervisedUserSettingsService::SupervisedUserSettingsService() | 52 SupervisedUserSettingsService::SupervisedUserSettingsService() |
| 53 : active_(false), local_settings_(new base::DictionaryValue) {} | 53 : active_(false), |
| 54 initialization_failed_(false), | |
| 55 local_settings_(new base::DictionaryValue) { | |
| 56 } | |
| 54 | 57 |
| 55 SupervisedUserSettingsService::~SupervisedUserSettingsService() {} | 58 SupervisedUserSettingsService::~SupervisedUserSettingsService() {} |
| 56 | 59 |
| 57 void SupervisedUserSettingsService::Init( | 60 void SupervisedUserSettingsService::Init( |
| 58 base::FilePath profile_path, | 61 base::FilePath profile_path, |
| 59 base::SequencedTaskRunner* sequenced_task_runner, | 62 base::SequencedTaskRunner* sequenced_task_runner, |
| 60 bool load_synchronously) { | 63 bool load_synchronously) { |
| 61 base::FilePath path = | 64 base::FilePath path = |
| 62 profile_path.Append(chrome::kSupervisedUserSettingsFilename); | 65 profile_path.Append(chrome::kSupervisedUserSettingsFilename); |
| 63 PersistentPrefStore* store = new JsonPrefStore( | 66 PersistentPrefStore* store = new JsonPrefStore( |
| 64 path, sequenced_task_runner, scoped_ptr<PrefFilter>()); | 67 path, sequenced_task_runner, scoped_ptr<PrefFilter>()); |
| 65 Init(store); | 68 Init(store); |
| 66 if (load_synchronously) { | 69 if (load_synchronously) { |
| 67 store_->ReadPrefs(); | 70 store_->ReadPrefs(); |
| 68 // TODO(bauerb): Temporary CHECK while investigating | 71 // TODO(bauerb): Temporary CHECK while investigating |
| 69 // https://crbug.com/425785. Remove (or change to DCHECK) once the bug | 72 // https://crbug.com/425785. Remove (or change to DCHECK) once the bug |
| 70 // is fixed. | 73 // is fixed. |
| 71 CHECK(store_->IsInitializationComplete()); | 74 CHECK(store_->IsInitializationComplete()); |
|
Marc Treib
2015/02/27 15:51:43
Do you need to handle a failing PrefStore initiali
Bernhard Bauer
2015/02/27 16:03:44
Interestingly enough, this CHECK does not seem to
Marc Treib
2015/02/27 16:08:32
Okay, fair enough. LGTM then.
| |
| 72 } else { | 75 } else { |
| 73 store_->ReadPrefsAsync(NULL); | 76 store_->ReadPrefsAsync(NULL); |
| 74 } | 77 } |
| 75 } | 78 } |
| 76 | 79 |
| 77 void SupervisedUserSettingsService::Init( | 80 void SupervisedUserSettingsService::Init( |
| 78 scoped_refptr<PersistentPrefStore> store) { | 81 scoped_refptr<PersistentPrefStore> store) { |
| 79 DCHECK(!store_.get()); | 82 DCHECK(!store_.get()); |
| 80 store_ = store; | 83 store_ = store; |
| 81 store_->AddObserver(this); | 84 store_->AddObserver(this); |
| 82 } | 85 } |
| 83 | 86 |
| 84 void SupervisedUserSettingsService::Subscribe( | 87 void SupervisedUserSettingsService::Subscribe( |
| 85 const SettingsCallback& callback) { | 88 const SettingsCallback& callback) { |
| 86 if (IsReady()) { | 89 if (IsReady()) { |
| 87 scoped_ptr<base::DictionaryValue> settings = GetSettings(); | 90 scoped_ptr<base::DictionaryValue> settings = GetSettings(); |
| 88 callback.Run(settings.get()); | 91 callback.Run(settings.get()); |
| 89 } | 92 } |
| 90 | 93 |
| 91 subscribers_.push_back(callback); | 94 subscribers_.push_back(callback); |
| 92 } | 95 } |
| 93 | 96 |
| 94 void SupervisedUserSettingsService::SetActive(bool active) { | 97 void SupervisedUserSettingsService::SetActive(bool active) { |
| 95 active_ = active; | 98 active_ = active; |
| 96 InformSubscribers(); | 99 InformSubscribers(); |
| 97 } | 100 } |
| 98 | 101 |
| 99 bool SupervisedUserSettingsService::IsReady() { | 102 bool SupervisedUserSettingsService::IsReady() { |
| 100 return store_->IsInitializationComplete(); | 103 // Initialization cannot be complete but have failed at the same time. |
| 104 DCHECK(!(store_->IsInitializationComplete() && initialization_failed_)); | |
| 105 return initialization_failed_ || store_->IsInitializationComplete(); | |
| 101 } | 106 } |
| 102 | 107 |
| 103 void SupervisedUserSettingsService::Clear() { | 108 void SupervisedUserSettingsService::Clear() { |
| 104 store_->RemoveValue(kAtomicSettings); | 109 store_->RemoveValue(kAtomicSettings); |
| 105 store_->RemoveValue(kSplitSettings); | 110 store_->RemoveValue(kSplitSettings); |
| 106 } | 111 } |
| 107 | 112 |
| 108 // static | 113 // static |
| 109 std::string SupervisedUserSettingsService::MakeSplitSettingKey( | 114 std::string SupervisedUserSettingsService::MakeSplitSettingKey( |
| 110 const std::string& prefix, | 115 const std::string& prefix, |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 292 InformSubscribers(); | 297 InformSubscribers(); |
| 293 | 298 |
| 294 SyncError error; | 299 SyncError error; |
| 295 return error; | 300 return error; |
| 296 } | 301 } |
| 297 | 302 |
| 298 void SupervisedUserSettingsService::OnPrefValueChanged(const std::string& key) { | 303 void SupervisedUserSettingsService::OnPrefValueChanged(const std::string& key) { |
| 299 } | 304 } |
| 300 | 305 |
| 301 void SupervisedUserSettingsService::OnInitializationCompleted(bool success) { | 306 void SupervisedUserSettingsService::OnInitializationCompleted(bool success) { |
| 307 if (!success) { | |
| 308 // If this happens, it means the profile directory was not found. There is | |
| 309 // not much we can do, but the whole profile will probably be useless | |
| 310 // anyway. Just mark initialization as failed and continue otherwise, | |
| 311 // because subscribers might still expect to be called back. | |
| 312 initialization_failed_ = true; | |
| 313 } | |
| 314 | |
| 302 // TODO(bauerb): Temporary CHECK while investigating https://crbug.com/425785. | 315 // TODO(bauerb): Temporary CHECK while investigating https://crbug.com/425785. |
| 303 // Remove (or change back to DCHECK) once the bug is fixed. | 316 // Remove (or change back to DCHECK) once the bug is fixed. |
| 304 CHECK(success); | |
| 305 CHECK(IsReady()); | 317 CHECK(IsReady()); |
| 306 InformSubscribers(); | 318 InformSubscribers(); |
| 307 } | 319 } |
| 308 | 320 |
| 309 base::DictionaryValue* SupervisedUserSettingsService::GetOrCreateDictionary( | 321 base::DictionaryValue* SupervisedUserSettingsService::GetOrCreateDictionary( |
| 310 const std::string& key) const { | 322 const std::string& key) const { |
| 311 base::Value* value = NULL; | 323 base::Value* value = NULL; |
| 312 base::DictionaryValue* dict = NULL; | 324 base::DictionaryValue* dict = NULL; |
| 313 if (store_->GetMutableValue(key, &value)) { | 325 if (store_->GetMutableValue(key, &value)) { |
| 314 bool success = value->GetAsDictionary(&dict); | 326 bool success = value->GetAsDictionary(&dict); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 dict = new base::DictionaryValue; | 359 dict = new base::DictionaryValue; |
| 348 DCHECK(!split_settings->HasKey(prefix)); | 360 DCHECK(!split_settings->HasKey(prefix)); |
| 349 split_settings->Set(prefix, dict); | 361 split_settings->Set(prefix, dict); |
| 350 } | 362 } |
| 351 key->erase(0, pos + 1); | 363 key->erase(0, pos + 1); |
| 352 return dict; | 364 return dict; |
| 353 } | 365 } |
| 354 | 366 |
| 355 scoped_ptr<base::DictionaryValue> SupervisedUserSettingsService::GetSettings() { | 367 scoped_ptr<base::DictionaryValue> SupervisedUserSettingsService::GetSettings() { |
| 356 DCHECK(IsReady()); | 368 DCHECK(IsReady()); |
| 357 if (!active_) | 369 if (!active_ || initialization_failed_) |
| 358 return scoped_ptr<base::DictionaryValue>(); | 370 return scoped_ptr<base::DictionaryValue>(); |
| 359 | 371 |
| 360 scoped_ptr<base::DictionaryValue> settings(local_settings_->DeepCopy()); | 372 scoped_ptr<base::DictionaryValue> settings(local_settings_->DeepCopy()); |
| 361 | 373 |
| 362 base::DictionaryValue* atomic_settings = GetAtomicSettings(); | 374 base::DictionaryValue* atomic_settings = GetAtomicSettings(); |
| 363 for (base::DictionaryValue::Iterator it(*atomic_settings); !it.IsAtEnd(); | 375 for (base::DictionaryValue::Iterator it(*atomic_settings); !it.IsAtEnd(); |
| 364 it.Advance()) { | 376 it.Advance()) { |
| 365 if (!SettingShouldApplyToPrefs(it.key())) | 377 if (!SettingShouldApplyToPrefs(it.key())) |
| 366 continue; | 378 continue; |
| 367 | 379 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 381 } | 393 } |
| 382 | 394 |
| 383 void SupervisedUserSettingsService::InformSubscribers() { | 395 void SupervisedUserSettingsService::InformSubscribers() { |
| 384 if (!IsReady()) | 396 if (!IsReady()) |
| 385 return; | 397 return; |
| 386 | 398 |
| 387 scoped_ptr<base::DictionaryValue> settings = GetSettings(); | 399 scoped_ptr<base::DictionaryValue> settings = GetSettings(); |
| 388 for (const auto& callback : subscribers_) | 400 for (const auto& callback : subscribers_) |
| 389 callback.Run(settings.get()); | 401 callback.Run(settings.get()); |
| 390 } | 402 } |
| OLD | NEW |