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/thread_task_runner_handle.h" | |
12 #include "base/value_conversions.h" | 12 #include "base/value_conversions.h" |
13 | 13 |
| 14 using base::MessageLoopProxy; |
14 using base::SingleThreadTaskRunner; | 15 using base::SingleThreadTaskRunner; |
15 | 16 |
16 namespace subtle { | 17 namespace subtle { |
17 | 18 |
18 PrefMemberBase::PrefMemberBase() | 19 PrefMemberBase::PrefMemberBase() |
19 : prefs_(NULL), | 20 : prefs_(NULL), |
20 setting_value_(false) { | 21 setting_value_(false) { |
21 } | 22 } |
22 | 23 |
23 PrefMemberBase::~PrefMemberBase() { | 24 PrefMemberBase::~PrefMemberBase() { |
(...skipping 20 matching lines...) Expand all Loading... |
44 } | 45 } |
45 | 46 |
46 void PrefMemberBase::Destroy() { | 47 void PrefMemberBase::Destroy() { |
47 if (prefs_ && !pref_name_.empty()) { | 48 if (prefs_ && !pref_name_.empty()) { |
48 prefs_->RemovePrefObserver(pref_name_, this); | 49 prefs_->RemovePrefObserver(pref_name_, this); |
49 prefs_ = NULL; | 50 prefs_ = NULL; |
50 } | 51 } |
51 } | 52 } |
52 | 53 |
53 void PrefMemberBase::MoveToThread( | 54 void PrefMemberBase::MoveToThread( |
54 scoped_refptr<SingleThreadTaskRunner> task_runner) { | 55 const scoped_refptr<SingleThreadTaskRunner>& task_runner) { |
55 VerifyValuePrefName(); | 56 VerifyValuePrefName(); |
56 // Load the value from preferences if it hasn't been loaded so far. | 57 // Load the value from preferences if it hasn't been loaded so far. |
57 if (!internal()) | 58 if (!internal()) |
58 UpdateValueFromPref(base::Closure()); | 59 UpdateValueFromPref(base::Closure()); |
59 internal()->MoveToThread(task_runner); | 60 internal()->MoveToThread(task_runner); |
60 } | 61 } |
61 | 62 |
62 void PrefMemberBase::OnPreferenceChanged(PrefService* service, | 63 void PrefMemberBase::OnPreferenceChanged(PrefService* service, |
63 const std::string& pref_name) { | 64 const std::string& pref_name) { |
64 VerifyValuePrefName(); | 65 VerifyValuePrefName(); |
(...skipping 18 matching lines...) Expand all Loading... |
83 if (!internal()) | 84 if (!internal()) |
84 UpdateValueFromPref(base::Closure()); | 85 UpdateValueFromPref(base::Closure()); |
85 } | 86 } |
86 | 87 |
87 void PrefMemberBase::InvokeUnnamedCallback(const base::Closure& callback, | 88 void PrefMemberBase::InvokeUnnamedCallback(const base::Closure& callback, |
88 const std::string& pref_name) { | 89 const std::string& pref_name) { |
89 callback.Run(); | 90 callback.Run(); |
90 } | 91 } |
91 | 92 |
92 PrefMemberBase::Internal::Internal() | 93 PrefMemberBase::Internal::Internal() |
93 : thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 94 : thread_loop_(MessageLoopProxy::current()), |
94 is_managed_(false), | 95 is_managed_(false), |
95 is_user_modifiable_(false) { | 96 is_user_modifiable_(false) { |
96 } | 97 } |
97 PrefMemberBase::Internal::~Internal() { } | 98 PrefMemberBase::Internal::~Internal() { } |
98 | 99 |
99 bool PrefMemberBase::Internal::IsOnCorrectThread() const { | 100 bool PrefMemberBase::Internal::IsOnCorrectThread() const { |
100 return thread_task_runner_->BelongsToCurrentThread(); | 101 // In unit tests, there may not be a message loop. |
| 102 return thread_loop_.get() == NULL || thread_loop_->BelongsToCurrentThread(); |
101 } | 103 } |
102 | 104 |
103 void PrefMemberBase::Internal::UpdateValue( | 105 void PrefMemberBase::Internal::UpdateValue( |
104 base::Value* v, | 106 base::Value* v, |
105 bool is_managed, | 107 bool is_managed, |
106 bool is_user_modifiable, | 108 bool is_user_modifiable, |
107 const base::Closure& callback) const { | 109 const base::Closure& callback) const { |
108 scoped_ptr<base::Value> value(v); | 110 scoped_ptr<base::Value> value(v); |
109 base::ScopedClosureRunner closure_runner(callback); | 111 base::ScopedClosureRunner closure_runner(callback); |
110 if (IsOnCorrectThread()) { | 112 if (IsOnCorrectThread()) { |
111 bool rv = UpdateValueInternal(*value); | 113 bool rv = UpdateValueInternal(*value); |
112 DCHECK(rv); | 114 DCHECK(rv); |
113 is_managed_ = is_managed; | 115 is_managed_ = is_managed; |
114 is_user_modifiable_ = is_user_modifiable; | 116 is_user_modifiable_ = is_user_modifiable; |
115 } else { | 117 } else { |
116 bool may_run = thread_task_runner_->PostTask( | 118 bool may_run = thread_loop_->PostTask( |
117 FROM_HERE, base::Bind(&PrefMemberBase::Internal::UpdateValue, this, | 119 FROM_HERE, |
118 value.release(), is_managed, is_user_modifiable, | 120 base::Bind(&PrefMemberBase::Internal::UpdateValue, this, |
119 closure_runner.Release())); | 121 value.release(), is_managed, is_user_modifiable, |
| 122 closure_runner.Release())); |
120 DCHECK(may_run); | 123 DCHECK(may_run); |
121 } | 124 } |
122 } | 125 } |
123 | 126 |
124 void PrefMemberBase::Internal::MoveToThread( | 127 void PrefMemberBase::Internal::MoveToThread( |
125 scoped_refptr<SingleThreadTaskRunner> task_runner) { | 128 const scoped_refptr<SingleThreadTaskRunner>& task_runner) { |
126 CheckOnCorrectThread(); | 129 CheckOnCorrectThread(); |
127 thread_task_runner_ = task_runner.Pass(); | 130 thread_loop_ = task_runner; |
128 } | 131 } |
129 | 132 |
130 bool PrefMemberVectorStringUpdate(const base::Value& value, | 133 bool PrefMemberVectorStringUpdate(const base::Value& value, |
131 std::vector<std::string>* string_vector) { | 134 std::vector<std::string>* string_vector) { |
132 if (!value.IsType(base::Value::TYPE_LIST)) | 135 if (!value.IsType(base::Value::TYPE_LIST)) |
133 return false; | 136 return false; |
134 const base::ListValue* list = static_cast<const base::ListValue*>(&value); | 137 const base::ListValue* list = static_cast<const base::ListValue*>(&value); |
135 | 138 |
136 std::vector<std::string> local_vector; | 139 std::vector<std::string> local_vector; |
137 for (base::ListValue::const_iterator it = list->begin(); | 140 for (base::ListValue::const_iterator it = list->begin(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 base::ListValue list_value; | 215 base::ListValue list_value; |
213 list_value.AppendStrings(value); | 216 list_value.AppendStrings(value); |
214 prefs()->Set(pref_name(), list_value); | 217 prefs()->Set(pref_name(), list_value); |
215 } | 218 } |
216 | 219 |
217 template <> | 220 template <> |
218 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( | 221 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( |
219 const base::Value& value) const { | 222 const base::Value& value) const { |
220 return subtle::PrefMemberVectorStringUpdate(value, &value_); | 223 return subtle::PrefMemberVectorStringUpdate(value, &value_); |
221 } | 224 } |
OLD | NEW |