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 "base/prefs/pref_member.h" | 5 #include "base/prefs/pref_member.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
11 #include "base/value_conversions.h" | 12 #include "base/value_conversions.h" |
12 | 13 |
13 using base::MessageLoopProxy; | 14 using base::MessageLoopProxy; |
| 15 using base::SingleThreadTaskRunner; |
14 | 16 |
15 namespace subtle { | 17 namespace subtle { |
16 | 18 |
17 PrefMemberBase::PrefMemberBase() | 19 PrefMemberBase::PrefMemberBase() |
18 : prefs_(NULL), | 20 : prefs_(NULL), |
19 setting_value_(false) { | 21 setting_value_(false) { |
20 } | 22 } |
21 | 23 |
22 PrefMemberBase::~PrefMemberBase() { | 24 PrefMemberBase::~PrefMemberBase() { |
23 Destroy(); | 25 Destroy(); |
(...skipping 22 matching lines...) Expand all Loading... |
46 } | 48 } |
47 | 49 |
48 void PrefMemberBase::Destroy() { | 50 void PrefMemberBase::Destroy() { |
49 if (prefs_ && !pref_name_.empty()) { | 51 if (prefs_ && !pref_name_.empty()) { |
50 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | 52 prefs_->RemovePrefObserver(pref_name_.c_str(), this); |
51 prefs_ = NULL; | 53 prefs_ = NULL; |
52 } | 54 } |
53 } | 55 } |
54 | 56 |
55 void PrefMemberBase::MoveToThread( | 57 void PrefMemberBase::MoveToThread( |
56 const scoped_refptr<MessageLoopProxy>& message_loop) { | 58 const scoped_refptr<SingleThreadTaskRunner>& task_runner) { |
57 VerifyValuePrefName(); | 59 VerifyValuePrefName(); |
58 // Load the value from preferences if it hasn't been loaded so far. | 60 // Load the value from preferences if it hasn't been loaded so far. |
59 if (!internal()) | 61 if (!internal()) |
60 UpdateValueFromPref(base::Closure()); | 62 UpdateValueFromPref(base::Closure()); |
61 internal()->MoveToThread(message_loop); | 63 internal()->MoveToThread(task_runner); |
62 } | 64 } |
63 | 65 |
64 void PrefMemberBase::OnPreferenceChanged(PrefService* service, | 66 void PrefMemberBase::OnPreferenceChanged(PrefService* service, |
65 const std::string& pref_name) { | 67 const std::string& pref_name) { |
66 VerifyValuePrefName(); | 68 VerifyValuePrefName(); |
67 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ? | 69 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ? |
68 base::Bind(observer_, pref_name) : base::Closure()); | 70 base::Bind(observer_, pref_name) : base::Closure()); |
69 } | 71 } |
70 | 72 |
71 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const { | 73 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 bool may_run = thread_loop_->PostTask( | 122 bool may_run = thread_loop_->PostTask( |
121 FROM_HERE, | 123 FROM_HERE, |
122 base::Bind(&PrefMemberBase::Internal::UpdateValue, this, | 124 base::Bind(&PrefMemberBase::Internal::UpdateValue, this, |
123 value.release(), is_managed, is_user_modifiable, | 125 value.release(), is_managed, is_user_modifiable, |
124 closure_runner.Release())); | 126 closure_runner.Release())); |
125 DCHECK(may_run); | 127 DCHECK(may_run); |
126 } | 128 } |
127 } | 129 } |
128 | 130 |
129 void PrefMemberBase::Internal::MoveToThread( | 131 void PrefMemberBase::Internal::MoveToThread( |
130 const scoped_refptr<MessageLoopProxy>& message_loop) { | 132 const scoped_refptr<SingleThreadTaskRunner>& task_runner) { |
131 CheckOnCorrectThread(); | 133 CheckOnCorrectThread(); |
132 thread_loop_ = message_loop; | 134 thread_loop_ = task_runner; |
133 } | 135 } |
134 | 136 |
135 bool PrefMemberVectorStringUpdate(const base::Value& value, | 137 bool PrefMemberVectorStringUpdate(const base::Value& value, |
136 std::vector<std::string>* string_vector) { | 138 std::vector<std::string>* string_vector) { |
137 if (!value.IsType(base::Value::TYPE_LIST)) | 139 if (!value.IsType(base::Value::TYPE_LIST)) |
138 return false; | 140 return false; |
139 const base::ListValue* list = static_cast<const base::ListValue*>(&value); | 141 const base::ListValue* list = static_cast<const base::ListValue*>(&value); |
140 | 142 |
141 std::vector<std::string> local_vector; | 143 std::vector<std::string> local_vector; |
142 for (base::ListValue::const_iterator it = list->begin(); | 144 for (base::ListValue::const_iterator it = list->begin(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 base::ListValue list_value; | 219 base::ListValue list_value; |
218 list_value.AppendStrings(value); | 220 list_value.AppendStrings(value); |
219 prefs()->Set(pref_name().c_str(), list_value); | 221 prefs()->Set(pref_name().c_str(), list_value); |
220 } | 222 } |
221 | 223 |
222 template <> | 224 template <> |
223 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( | 225 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( |
224 const base::Value& value) const { | 226 const base::Value& value) const { |
225 return subtle::PrefMemberVectorStringUpdate(value, &value_); | 227 return subtle::PrefMemberVectorStringUpdate(value, &value_); |
226 } | 228 } |
OLD | NEW |