Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/prefs/pref_member.h" | 5 #include "chrome/browser/api/prefs/pref_member.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/prefs/public/pref_service_base.h" | 9 #include "base/prefs/public/pref_service_base.h" |
| 10 #include "base/value_conversions.h" | 10 #include "base/value_conversions.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 DCHECK(prefs); | 30 DCHECK(prefs); |
| 31 DCHECK(pref_name_.empty()); // Check that Init is only called once. | 31 DCHECK(pref_name_.empty()); // Check that Init is only called once. |
| 32 observer_ = observer; | 32 observer_ = observer; |
| 33 prefs_ = prefs; | 33 prefs_ = prefs; |
| 34 pref_name_ = pref_name; | 34 pref_name_ = pref_name; |
| 35 // Check that the preference is registered. | 35 // Check that the preference is registered. |
| 36 DCHECK(prefs_->FindPreference(pref_name_.c_str())) | 36 DCHECK(prefs_->FindPreference(pref_name_.c_str())) |
| 37 << pref_name << " not registered."; | 37 << pref_name << " not registered."; |
| 38 | 38 |
| 39 // Add ourselves as a pref observer so we can keep our local value in sync. | 39 // Add ourselves as a pref observer so we can keep our local value in sync. |
| 40 prefs_->AddPrefObserver(pref_name, this); | 40 prefs_->AddPrefObserver(pref_name, |
| 41 base::Bind(&PrefMemberBase::OnPreferenceChanged, | |
| 42 base::Unretained(this), prefs, pref_name)); | |
| 41 } | 43 } |
| 42 | 44 |
| 43 void PrefMemberBase::Destroy() { | 45 void PrefMemberBase::Destroy() { |
| 44 if (prefs_ && !pref_name_.empty()) { | 46 if (prefs_ && !pref_name_.empty()) { |
| 45 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | 47 prefs_->RemovePrefObserver(pref_name_.c_str(), |
| 48 base::Bind(&PrefMemberBase::OnPreferenceChanged, | |
| 49 base::Unretained(this), | |
| 50 prefs_, pref_name_.c_str())); | |
| 46 prefs_ = NULL; | 51 prefs_ = NULL; |
| 47 } | 52 } |
| 48 } | 53 } |
| 49 | 54 |
| 50 void PrefMemberBase::MoveToThread( | 55 void PrefMemberBase::MoveToThread( |
| 51 const scoped_refptr<MessageLoopProxy>& message_loop) { | 56 const scoped_refptr<MessageLoopProxy>& message_loop) { |
| 52 VerifyValuePrefName(); | 57 VerifyValuePrefName(); |
| 53 // Load the value from preferences if it hasn't been loaded so far. | 58 // Load the value from preferences if it hasn't been loaded so far. |
| 54 if (!internal()) | 59 if (!internal()) |
| 55 UpdateValueFromPref(); | 60 UpdateValueFromPref(); |
| 56 internal()->MoveToThread(message_loop); | 61 internal()->MoveToThread(message_loop); |
| 57 } | 62 } |
| 58 | 63 |
| 59 void PrefMemberBase::OnPreferenceChanged(PrefServiceBase* service, | 64 void PrefMemberBase::OnPreferenceChanged(PrefServiceBase* service, |
| 60 const std::string& pref_name) { | 65 const std::string& pref_name) { |
|
Mattias Nissler (ping if slow)
2012/11/06 13:26:48
doesn't require these parameters any longer, can j
| |
| 61 VerifyValuePrefName(); | 66 VerifyValuePrefName(); |
| 62 UpdateValueFromPref(); | 67 UpdateValueFromPref(); |
| 63 if (!setting_value_ && observer_) | 68 if (!setting_value_ && observer_) |
| 64 observer_->OnPreferenceChanged(service, pref_name); | 69 observer_->OnPreferenceChanged(service, pref_name); |
| 65 } | 70 } |
| 66 | 71 |
| 67 void PrefMemberBase::UpdateValueFromPref() const { | 72 void PrefMemberBase::UpdateValueFromPref() const { |
| 68 VerifyValuePrefName(); | 73 VerifyValuePrefName(); |
| 69 const PrefServiceBase::Preference* pref = | 74 const PrefServiceBase::Preference* pref = |
| 70 prefs_->FindPreference(pref_name_.c_str()); | 75 prefs_->FindPreference(pref_name_.c_str()); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 ListValue list_value; | 202 ListValue list_value; |
| 198 list_value.AppendStrings(value); | 203 list_value.AppendStrings(value); |
| 199 prefs()->Set(pref_name().c_str(), list_value); | 204 prefs()->Set(pref_name().c_str(), list_value); |
| 200 } | 205 } |
| 201 | 206 |
| 202 template <> | 207 template <> |
| 203 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( | 208 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( |
| 204 const Value& value) const { | 209 const Value& value) const { |
| 205 return subtle::PrefMemberVectorStringUpdate(value, &value_); | 210 return subtle::PrefMemberVectorStringUpdate(value, &value_); |
| 206 } | 211 } |
| OLD | NEW |