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 prefs::mojom::PreferencesObserverPtr client) { | |
| 31 client_ = std::move(client); | |
| 32 } | |
| 33 | |
| 34 void PreferencesManager::SetPreferences( | |
| 35 const base::DictionaryValue& preferences) { | |
| 36 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd(); | |
| 37 it.Advance()) { | |
| 38 if (!preferences_change_registrar_->IsObserved(it.key())) | |
| 39 continue; | |
| 40 const PrefService::Preference* pref = service_->FindPreference(it.key()); | |
| 41 if (!pref) | |
| 42 continue; | |
| 43 if (it.value().Equals(pref->GetValue())) | |
| 44 continue; | |
| 45 // We ignore preference changes caused by us. | |
| 46 setting_preferences_ = true; | |
|
sadrul
2016/11/29 17:25:49
Move this out of the for loop, and use base::AutoR
jonross
2016/11/30 01:01:31
Done.
| |
| 47 service_->Set(it.key(), it.value()); | |
| 48 setting_preferences_ = false; | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 void PreferencesManager::Subscribe( | |
| 53 const std::vector<std::string>& preferences) { | |
| 54 base::DictionaryValue dictionary; | |
| 55 for (auto& it : preferences) { | |
| 56 const PrefService::Preference* pref = service_->FindPreference(it); | |
| 57 if (!pref) | |
| 58 continue; | |
| 59 // PreferenceManager lifetime is managed by PreferenceConnectionManager it | |
| 60 // will outlive |preferences_change_registrar_| which it owns. | |
|
sadrul
2016/11/29 17:25:49
This comment is slightly wrong: PCM does not actua
jonross
2016/11/30 01:01:31
Half true now with the wrapping mojo::StrongBindin
| |
| 61 preferences_change_registrar_->Add( | |
| 62 it, base::Bind(&PreferencesManager::PreferenceChanged, | |
| 63 base::Unretained(this))); | |
| 64 dictionary.Set(it, pref->GetValue()->CreateDeepCopy()); | |
| 65 } | |
| 66 | |
| 67 if (dictionary.empty()) | |
| 68 return; | |
| 69 client_->OnPreferencesChanged(dictionary); | |
| 70 } | |
| 71 | |
| 72 void PreferencesManager::PreferenceChanged(const std::string& preference_name) { | |
| 73 if (setting_preferences_) | |
| 74 return; | |
| 75 const PrefService::Preference* pref = | |
| 76 service_->FindPreference(preference_name); | |
| 77 if (!pref) | |
| 78 return; | |
|
sadrul
2016/11/29 17:25:49
Should |pref| ever be null?
jonross
2016/11/30 01:01:31
Actually no. We don't subscribe if the pref doesn'
| |
| 79 base::DictionaryValue dictionary; | |
| 80 dictionary.Set(preference_name, pref->GetValue()->CreateDeepCopy()); | |
| 81 client_->OnPreferencesChanged(dictionary); | |
|
sadrul
2016/11/29 17:25:49
If a lot of preferences are updating at the same t
jonross
2016/11/30 01:01:31
It would be nice to reduce the number of IPCs. I d
| |
| 82 } | |
| 83 | |
| 84 } // namespace chrome | |
| OLD | NEW |