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_helpers.h" |
| 8 #include "base/callback.h" |
8 #include "base/location.h" | 9 #include "base/location.h" |
9 #include "base/prefs/public/pref_service_base.h" | 10 #include "base/prefs/public/pref_service_base.h" |
10 #include "base/value_conversions.h" | 11 #include "base/value_conversions.h" |
11 | 12 |
12 using base::MessageLoopProxy; | 13 using base::MessageLoopProxy; |
13 | 14 |
14 namespace subtle { | 15 namespace subtle { |
15 | 16 |
16 PrefMemberBase::PrefMemberBase() | 17 PrefMemberBase::PrefMemberBase() |
17 : observer_(NULL), | 18 : prefs_(NULL), |
18 prefs_(NULL), | |
19 setting_value_(false) { | 19 setting_value_(false) { |
20 } | 20 } |
21 | 21 |
22 PrefMemberBase::~PrefMemberBase() { | 22 PrefMemberBase::~PrefMemberBase() { |
23 Destroy(); | 23 Destroy(); |
24 } | 24 } |
25 | 25 |
26 void PrefMemberBase::Init(const char* pref_name, | 26 void PrefMemberBase::Init(const char* pref_name, |
27 PrefServiceBase* prefs, | 27 PrefServiceBase* prefs, |
28 PrefObserver* observer) { | 28 const NamedChangeCallback& observer) { |
29 DCHECK(pref_name); | 29 DCHECK(pref_name); |
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; | |
33 prefs_ = prefs; | 32 prefs_ = prefs; |
34 pref_name_ = pref_name; | 33 pref_name_ = pref_name; |
| 34 observer_ = observer; |
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, this); |
41 } | 41 } |
42 | 42 |
43 void PrefMemberBase::Destroy() { | 43 void PrefMemberBase::Destroy() { |
44 if (prefs_ && !pref_name_.empty()) { | 44 if (prefs_ && !pref_name_.empty()) { |
45 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | 45 prefs_->RemovePrefObserver(pref_name_.c_str(), this); |
46 prefs_ = NULL; | 46 prefs_ = NULL; |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 void PrefMemberBase::MoveToThread( | 50 void PrefMemberBase::MoveToThread( |
51 const scoped_refptr<MessageLoopProxy>& message_loop) { | 51 const scoped_refptr<MessageLoopProxy>& message_loop) { |
52 VerifyValuePrefName(); | 52 VerifyValuePrefName(); |
53 // Load the value from preferences if it hasn't been loaded so far. | 53 // Load the value from preferences if it hasn't been loaded so far. |
54 if (!internal()) | 54 if (!internal()) |
55 UpdateValueFromPref(); | 55 UpdateValueFromPref(); |
56 internal()->MoveToThread(message_loop); | 56 internal()->MoveToThread(message_loop); |
57 } | 57 } |
58 | 58 |
59 void PrefMemberBase::OnPreferenceChanged(PrefServiceBase* service, | 59 void PrefMemberBase::OnPreferenceChanged(PrefServiceBase* service, |
60 const std::string& pref_name) { | 60 const std::string& pref_name) { |
61 VerifyValuePrefName(); | 61 VerifyValuePrefName(); |
62 UpdateValueFromPref(); | 62 UpdateValueFromPref(); |
63 if (!setting_value_ && observer_) | 63 if (!setting_value_ && !observer_.is_null()) |
64 observer_->OnPreferenceChanged(service, pref_name); | 64 observer_.Run(pref_name); |
65 } | 65 } |
66 | 66 |
67 void PrefMemberBase::UpdateValueFromPref() const { | 67 void PrefMemberBase::UpdateValueFromPref() const { |
68 VerifyValuePrefName(); | 68 VerifyValuePrefName(); |
69 const PrefServiceBase::Preference* pref = | 69 const PrefServiceBase::Preference* pref = |
70 prefs_->FindPreference(pref_name_.c_str()); | 70 prefs_->FindPreference(pref_name_.c_str()); |
71 DCHECK(pref); | 71 DCHECK(pref); |
72 if (!internal()) | 72 if (!internal()) |
73 CreateInternal(); | 73 CreateInternal(); |
74 internal()->UpdateValue(pref->GetValue()->DeepCopy(), | 74 internal()->UpdateValue(pref->GetValue()->DeepCopy(), |
75 pref->IsManaged(), | 75 pref->IsManaged(), |
76 pref->IsUserModifiable()); | 76 pref->IsUserModifiable()); |
77 } | 77 } |
78 | 78 |
79 void PrefMemberBase::VerifyPref() const { | 79 void PrefMemberBase::VerifyPref() const { |
80 VerifyValuePrefName(); | 80 VerifyValuePrefName(); |
81 if (!internal()) | 81 if (!internal()) |
82 UpdateValueFromPref(); | 82 UpdateValueFromPref(); |
83 } | 83 } |
84 | 84 |
| 85 void PrefMemberBase::InvokeUnnamedCallback(const base::Closure& callback, |
| 86 const std::string& pref_name) { |
| 87 callback.Run(); |
| 88 } |
| 89 |
85 PrefMemberBase::Internal::Internal() | 90 PrefMemberBase::Internal::Internal() |
86 : thread_loop_(MessageLoopProxy::current()), | 91 : thread_loop_(MessageLoopProxy::current()), |
87 is_managed_(false) { | 92 is_managed_(false) { |
88 } | 93 } |
89 PrefMemberBase::Internal::~Internal() { } | 94 PrefMemberBase::Internal::~Internal() { } |
90 | 95 |
91 bool PrefMemberBase::Internal::IsOnCorrectThread() const { | 96 bool PrefMemberBase::Internal::IsOnCorrectThread() const { |
92 // In unit tests, there may not be a message loop. | 97 // In unit tests, there may not be a message loop. |
93 return thread_loop_ == NULL || thread_loop_->BelongsToCurrentThread(); | 98 return thread_loop_ == NULL || thread_loop_->BelongsToCurrentThread(); |
94 } | 99 } |
(...skipping 102 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 |