Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #include "chrome/browser/prefs/preferences_manager.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "components/prefs/pref_change_registrar.h" | |
| 11 #include "components/prefs/pref_service.h" | |
| 12 | |
| 13 namespace chrome { | |
| 14 | |
| 15 PreferencesManager::PreferencesManager(Profile* profile) | |
| 16 : preferences_change_registrar_(new PrefChangeRegistrar), | |
| 17 setting_preferences_(false) { | |
| 18 DCHECK(profile); | |
| 19 service_ = profile->GetPrefs(); | |
| 20 preferences_change_registrar_->Init(service_); | |
| 21 } | |
| 22 | |
| 23 PreferencesManager::~PreferencesManager() {} | |
| 24 | |
| 25 void PreferencesManager::OnProfileDestroyed() { | |
| 26 preferences_change_registrar_.reset(); | |
| 27 } | |
| 28 | |
| 29 void PreferencesManager::AddObserver( | |
| 30 const std::vector<std::string>& preferences, | |
| 31 prefs::mojom::PreferencesObserverPtr client) { | |
| 32 client_ = std::move(client); | |
| 33 base::DictionaryValue dictionary; | |
| 34 for (auto it : preferences) { | |
|
James Cook
2016/11/18 00:50:34
ditto
jonross
2016/11/18 20:31:24
Done.
| |
| 35 const PrefService::Preference* pref = service_->FindPreference(it); | |
| 36 if (!pref) | |
| 37 continue; | |
| 38 // PreferenceManager lifetime is managed by PreferenceConnectionManager it | |
| 39 // will outlive |preferences_change_registrar_| which it owns. | |
| 40 preferences_change_registrar_->Add( | |
| 41 it, base::Bind(&PreferencesManager::PreferenceChanged, | |
| 42 base::Unretained(this))); | |
| 43 dictionary.Set(it, pref->GetValue()->CreateDeepCopy()); | |
| 44 } | |
| 45 | |
| 46 if (dictionary.empty()) | |
| 47 return; | |
| 48 client_->OnPreferencesChanged(dictionary); | |
| 49 } | |
| 50 | |
| 51 void PreferencesManager::SetPreferences( | |
| 52 const base::DictionaryValue& preferences) { | |
| 53 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd(); | |
| 54 it.Advance()) { | |
| 55 if (!preferences_change_registrar_->IsObserved(it.key())) | |
| 56 continue; | |
| 57 const PrefService::Preference* pref = service_->FindPreference(it.key()); | |
| 58 if (!pref) | |
| 59 continue; | |
| 60 if (it.value().Equals(pref->GetValue())) | |
| 61 continue; | |
| 62 // We ignore preference changes caused by us. | |
| 63 setting_preferences_ = true; | |
| 64 service_->Set(it.key(), it.value()); | |
| 65 setting_preferences_ = false; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void PreferencesManager::PreferenceChanged(const std::string& preference_name) { | |
| 70 if (setting_preferences_) | |
| 71 return; | |
| 72 const PrefService::Preference* pref = | |
| 73 service_->FindPreference(preference_name); | |
| 74 if (!pref) | |
| 75 return; | |
| 76 base::DictionaryValue dictionary; | |
| 77 dictionary.Set(preference_name, pref->GetValue()->CreateDeepCopy()); | |
| 78 client_->OnPreferencesChanged(dictionary); | |
| 79 } | |
| 80 | |
| 81 } // namespace chrome | |
| OLD | NEW |