| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/prefs/preferences_manager.h" | 5 #include "chrome/browser/prefs/preferences_service.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "components/prefs/pref_change_registrar.h" | 12 #include "components/prefs/pref_change_registrar.h" |
| 13 #include "components/prefs/pref_service.h" | 13 #include "components/prefs/pref_service.h" |
| 14 | 14 |
| 15 PreferencesManager::PreferencesManager( | 15 PreferencesService::PreferencesService( |
| 16 prefs::mojom::PreferencesObserverPtr client, | 16 prefs::mojom::PreferencesServiceClientPtr client, |
| 17 Profile* profile) | 17 Profile* profile) |
| 18 : preferences_change_registrar_(new PrefChangeRegistrar), | 18 : preferences_change_registrar_(new PrefChangeRegistrar), |
| 19 client_(std::move(client)), | 19 client_(std::move(client)), |
| 20 setting_preferences_(false) { | 20 setting_preferences_(false) { |
| 21 DCHECK(profile); | 21 DCHECK(profile); |
| 22 DCHECK(client_.is_bound()); | 22 DCHECK(client_.is_bound()); |
| 23 service_ = profile->GetPrefs(); | 23 service_ = profile->GetPrefs(); |
| 24 preferences_change_registrar_->Init(service_); | 24 preferences_change_registrar_->Init(service_); |
| 25 } | 25 } |
| 26 | 26 |
| 27 PreferencesManager::~PreferencesManager() {} | 27 PreferencesService::~PreferencesService() {} |
| 28 | 28 |
| 29 void PreferencesManager::PreferenceChanged(const std::string& preference_name) { | 29 void PreferencesService::PreferenceChanged(const std::string& preference_name) { |
| 30 if (setting_preferences_) | 30 if (setting_preferences_) |
| 31 return; | 31 return; |
| 32 const PrefService::Preference* pref = | 32 const PrefService::Preference* pref = |
| 33 service_->FindPreference(preference_name); | 33 service_->FindPreference(preference_name); |
| 34 std::unique_ptr<base::DictionaryValue> dictionary = | 34 std::unique_ptr<base::DictionaryValue> dictionary = |
| 35 base::MakeUnique<base::DictionaryValue>(); | 35 base::MakeUnique<base::DictionaryValue>(); |
| 36 dictionary->Set(preference_name, pref->GetValue()->CreateDeepCopy()); | 36 dictionary->Set(preference_name, pref->GetValue()->CreateDeepCopy()); |
| 37 client_->OnPreferencesChanged(std::move(dictionary)); | 37 client_->OnPreferencesChanged(std::move(dictionary)); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void PreferencesManager::SetPreferences( | 40 void PreferencesService::SetPreferences( |
| 41 std::unique_ptr<base::DictionaryValue> preferences) { | 41 std::unique_ptr<base::DictionaryValue> preferences) { |
| 42 DCHECK(!setting_preferences_); | 42 DCHECK(!setting_preferences_); |
| 43 // We ignore preference changes caused by us. | 43 // We ignore preference changes caused by us. |
| 44 base::AutoReset<bool> setting_preferences(&setting_preferences_, true); | 44 base::AutoReset<bool> setting_preferences(&setting_preferences_, true); |
| 45 for (base::DictionaryValue::Iterator it(*preferences); !it.IsAtEnd(); | 45 for (base::DictionaryValue::Iterator it(*preferences); !it.IsAtEnd(); |
| 46 it.Advance()) { | 46 it.Advance()) { |
| 47 if (!preferences_change_registrar_->IsObserved(it.key())) | 47 if (!preferences_change_registrar_->IsObserved(it.key())) |
| 48 continue; | 48 continue; |
| 49 const PrefService::Preference* pref = service_->FindPreference(it.key()); | 49 const PrefService::Preference* pref = service_->FindPreference(it.key()); |
| 50 if (!pref) { | 50 if (!pref) { |
| 51 DLOG(ERROR) << "Preference " << it.key() << " not found.\n"; | 51 DLOG(ERROR) << "Preference " << it.key() << " not found.\n"; |
| 52 continue; | 52 continue; |
| 53 } | 53 } |
| 54 if (it.value().Equals(pref->GetValue())) | 54 if (it.value().Equals(pref->GetValue())) |
| 55 continue; | 55 continue; |
| 56 service_->Set(it.key(), it.value()); | 56 service_->Set(it.key(), it.value()); |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 void PreferencesManager::Subscribe( | 60 void PreferencesService::Subscribe( |
| 61 const std::vector<std::string>& preferences) { | 61 const std::vector<std::string>& preferences) { |
| 62 std::unique_ptr<base::DictionaryValue> dictionary = | 62 std::unique_ptr<base::DictionaryValue> dictionary = |
| 63 base::MakeUnique<base::DictionaryValue>(); | 63 base::MakeUnique<base::DictionaryValue>(); |
| 64 for (auto& it : preferences) { | 64 for (auto& it : preferences) { |
| 65 const PrefService::Preference* pref = service_->FindPreference(it); | 65 const PrefService::Preference* pref = service_->FindPreference(it); |
| 66 if (!pref) { | 66 if (!pref) { |
| 67 DLOG(ERROR) << "Preference " << it << " not found.\n"; | 67 DLOG(ERROR) << "Preference " << it << " not found.\n"; |
| 68 continue; | 68 continue; |
| 69 } | 69 } |
| 70 // PreferenceManager lifetime is managed by a mojo::StrongBindingPtr owned | 70 // PreferenceManager lifetime is managed by a mojo::StrongBindingPtr owned |
| 71 // by PreferenceConnectionManager. It will outlive | 71 // by PreferenceConnectionManager. It will outlive |
| 72 // |preferences_change_registrar_| which it owns. | 72 // |preferences_change_registrar_| which it owns. |
| 73 preferences_change_registrar_->Add( | 73 preferences_change_registrar_->Add( |
| 74 it, base::Bind(&PreferencesManager::PreferenceChanged, | 74 it, base::Bind(&PreferencesService::PreferenceChanged, |
| 75 base::Unretained(this))); | 75 base::Unretained(this))); |
| 76 dictionary->Set(it, pref->GetValue()->CreateDeepCopy()); | 76 dictionary->Set(it, pref->GetValue()->CreateDeepCopy()); |
| 77 } | 77 } |
| 78 | 78 |
| 79 if (dictionary->empty()) | 79 if (dictionary->empty()) |
| 80 return; | 80 return; |
| 81 client_->OnPreferencesChanged(std::move(dictionary)); | 81 client_->OnPreferencesChanged(std::move(dictionary)); |
| 82 } | 82 } |
| OLD | NEW |