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