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 std::unique_ptr<base::DictionaryValue> dictionary = |
| 30 base::MakeUnique<base::DictionaryValue>(); |
| 31 dictionary->Set(preference_name, pref->GetValue()->CreateDeepCopy()); |
| 32 client_->OnPreferencesChanged(std::move(dictionary)); |
| 33 } |
| 34 |
| 35 void PreferencesManager::AddObserver( |
| 36 prefs::mojom::PreferencesObserverPtr client) { |
| 37 // TODO(jonross): once service_manager::Connector supports enforcing two-way |
| 38 // binding at connection time, update PreferencesManager to use that approach. |
| 39 // After which enforcing bind checks will not be needed (crbug.com/674140) |
| 40 client_ = std::move(client); |
| 41 } |
| 42 |
| 43 void PreferencesManager::SetPreferences( |
| 44 std::unique_ptr<base::DictionaryValue> preferences) { |
| 45 if (!client_.is_bound()) |
| 46 return; |
| 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 DLOG(ERROR) << "Preference " << it.key() << " not found.\n"; |
| 57 continue; |
| 58 } |
| 59 if (it.value().Equals(pref->GetValue())) |
| 60 continue; |
| 61 service_->Set(it.key(), it.value()); |
| 62 } |
| 63 } |
| 64 |
| 65 void PreferencesManager::Subscribe( |
| 66 const std::vector<std::string>& preferences) { |
| 67 if (!client_.is_bound()) |
| 68 return; |
| 69 std::unique_ptr<base::DictionaryValue> dictionary = |
| 70 base::MakeUnique<base::DictionaryValue>(); |
| 71 for (auto& it : preferences) { |
| 72 const PrefService::Preference* pref = service_->FindPreference(it); |
| 73 if (!pref) { |
| 74 DLOG(ERROR) << "Preference " << it << " not found.\n"; |
| 75 continue; |
| 76 } |
| 77 // PreferenceManager lifetime is managed by a mojo::StrongBindingPtr owned |
| 78 // by PreferenceConnectionManager. It will outlive |
| 79 // |preferences_change_registrar_| which it owns. |
| 80 preferences_change_registrar_->Add( |
| 81 it, base::Bind(&PreferencesManager::PreferenceChanged, |
| 82 base::Unretained(this))); |
| 83 dictionary->Set(it, pref->GetValue()->CreateDeepCopy()); |
| 84 } |
| 85 |
| 86 if (dictionary->empty()) |
| 87 return; |
| 88 client_->OnPreferencesChanged(std::move(dictionary)); |
| 89 } |
OLD | NEW |