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 namespace chrome { |
| 15 |
| 16 PreferencesManager::PreferencesManager(Profile* profile) |
| 17 : preferences_change_registrar_(new PrefChangeRegistrar), |
| 18 setting_preferences_(false) { |
| 19 DCHECK(profile); |
| 20 service_ = profile->GetPrefs(); |
| 21 preferences_change_registrar_->Init(service_); |
| 22 } |
| 23 |
| 24 PreferencesManager::~PreferencesManager() {} |
| 25 |
| 26 void PreferencesManager::OnProfileDestroyed() { |
| 27 preferences_change_registrar_.reset(); |
| 28 } |
| 29 |
| 30 void PreferencesManager::PreferenceChanged(const std::string& preference_name) { |
| 31 if (setting_preferences_) |
| 32 return; |
| 33 const PrefService::Preference* pref = |
| 34 service_->FindPreference(preference_name); |
| 35 base::DictionaryValue dictionary; |
| 36 dictionary.Set(preference_name, pref->GetValue()->CreateDeepCopy()); |
| 37 client_->OnPreferencesChanged(dictionary); |
| 38 } |
| 39 |
| 40 void PreferencesManager::AddObserver( |
| 41 prefs::mojom::PreferencesObserverPtr client) { |
| 42 client_ = std::move(client); |
| 43 } |
| 44 |
| 45 void PreferencesManager::SetPreferences( |
| 46 const base::DictionaryValue& preferences) { |
| 47 DCHECK(!setting_preferences_); |
| 48 // We ignore preference changes caused by us. |
| 49 base::AutoReset<bool> setting_preferences(&setting_preferences_, true); |
| 50 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd(); |
| 51 it.Advance()) { |
| 52 if (!preferences_change_registrar_->IsObserved(it.key())) |
| 53 continue; |
| 54 const PrefService::Preference* pref = service_->FindPreference(it.key()); |
| 55 if (!pref) |
| 56 continue; |
| 57 if (it.value().Equals(pref->GetValue())) |
| 58 continue; |
| 59 service_->Set(it.key(), it.value()); |
| 60 } |
| 61 } |
| 62 |
| 63 void PreferencesManager::Subscribe( |
| 64 const std::vector<std::string>& preferences) { |
| 65 base::DictionaryValue dictionary; |
| 66 for (auto& it : preferences) { |
| 67 const PrefService::Preference* pref = service_->FindPreference(it); |
| 68 if (!pref) |
| 69 continue; |
| 70 // PreferenceManager lifetime is managed by a mojo::StrongBindingPtr owned |
| 71 // by PreferenceConnectionManager. It will outlive |
| 72 // |preferences_change_registrar_| which it owns. |
| 73 preferences_change_registrar_->Add( |
| 74 it, base::Bind(&PreferencesManager::PreferenceChanged, |
| 75 base::Unretained(this))); |
| 76 dictionary.Set(it, pref->GetValue()->CreateDeepCopy()); |
| 77 } |
| 78 |
| 79 if (dictionary.empty()) |
| 80 return; |
| 81 client_->OnPreferencesChanged(dictionary); |
| 82 } |
| 83 |
| 84 } // namespace chrome |
OLD | NEW |