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/auto_reset.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "components/prefs/pref_change_registrar.h" | |
| 12 #include "components/prefs/pref_service.h" | |
| 13 | |
| 14 PreferencesManager::PreferencesManager(Profile* profile) | |
| 15 : preferences_change_registrar_(new PrefChangeRegistrar), | |
| 16 setting_preferences_(false) { | |
| 17 DCHECK(profile); | |
| 18 service_ = profile->GetPrefs(); | |
| 19 preferences_change_registrar_->Init(service_); | |
| 20 } | |
| 21 | |
| 22 PreferencesManager::~PreferencesManager() {} | |
| 23 | |
| 24 void PreferencesManager::PreferenceChanged(const std::string& preference_name) { | |
| 25 if (setting_preferences_) | |
| 26 return; | |
| 27 const PrefService::Preference* pref = | |
| 28 service_->FindPreference(preference_name); | |
| 29 base::DictionaryValue dictionary; | |
| 30 dictionary.Set(preference_name, pref->GetValue()->CreateDeepCopy()); | |
| 31 client_->OnPreferencesChanged(dictionary); | |
| 32 } | |
| 33 | |
| 34 void PreferencesManager::AddObserver( | |
| 35 prefs::mojom::PreferencesObserverPtr client) { | |
| 36 client_ = std::move(client); | |
|
dcheng
2016/12/08 23:13:01
Why just a single observer? That feels a bit surpr
jonross
2016/12/08 23:32:55
These will end up pairing 1:1 with connecting clie
dcheng
2016/12/09 05:26:41
If that's the case, should this always have exactl
jonross
2016/12/09 20:49:04
I'll look into the factory style
jonross
2016/12/13 01:30:18
Similar to the factory pattern (eg media/mojo/inte
| |
| 37 } | |
| 38 | |
| 39 void PreferencesManager::SetPreferences( | |
| 40 const base::DictionaryValue& preferences) { | |
| 41 DCHECK(!setting_preferences_); | |
| 42 // We ignore preference changes caused by us. | |
| 43 base::AutoReset<bool> setting_preferences(&setting_preferences_, true); | |
| 44 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd(); | |
| 45 it.Advance()) { | |
| 46 if (!preferences_change_registrar_->IsObserved(it.key())) | |
| 47 continue; | |
| 48 const PrefService::Preference* pref = service_->FindPreference(it.key()); | |
| 49 if (!pref) { | |
| 50 DLOG(ERROR) << "Preference " << it.key() << " not found.\n"; | |
| 51 continue; | |
| 52 } | |
| 53 if (it.value().Equals(pref->GetValue())) | |
| 54 continue; | |
| 55 service_->Set(it.key(), it.value()); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 void PreferencesManager::Subscribe( | |
| 60 const std::vector<std::string>& preferences) { | |
| 61 base::DictionaryValue dictionary; | |
| 62 for (auto& it : preferences) { | |
| 63 const PrefService::Preference* pref = service_->FindPreference(it); | |
| 64 if (!pref) { | |
| 65 DLOG(ERROR) << "Preference " << it << " not found.\n"; | |
| 66 continue; | |
| 67 } | |
| 68 // PreferenceManager lifetime is managed by a mojo::StrongBindingPtr owned | |
| 69 // by PreferenceConnectionManager. It will outlive | |
| 70 // |preferences_change_registrar_| which it owns. | |
| 71 preferences_change_registrar_->Add( | |
| 72 it, base::Bind(&PreferencesManager::PreferenceChanged, | |
| 73 base::Unretained(this))); | |
| 74 dictionary.Set(it, pref->GetValue()->CreateDeepCopy()); | |
| 75 } | |
| 76 | |
| 77 if (dictionary.empty()) | |
| 78 return; | |
| 79 client_->OnPreferencesChanged(dictionary); | |
| 80 } | |
| OLD | NEW |