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/message_loop/message_loop_proxy.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/sequenced_task_runner.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/thread_task_runner_handle.h" |
12 #include "base/value_conversions.h" | 15 #include "base/value_conversions.h" |
13 | 16 |
14 using base::MessageLoopProxy; | |
15 using base::SingleThreadTaskRunner; | |
16 | |
17 namespace subtle { | 17 namespace subtle { |
18 | 18 |
19 PrefMemberBase::PrefMemberBase() | 19 PrefMemberBase::PrefMemberBase() |
20 : prefs_(NULL), | 20 : prefs_(NULL), |
21 setting_value_(false) { | 21 setting_value_(false) { |
22 } | 22 } |
23 | 23 |
24 PrefMemberBase::~PrefMemberBase() { | 24 PrefMemberBase::~PrefMemberBase() { |
25 Destroy(); | 25 Destroy(); |
26 } | 26 } |
(...skipping 20 matching lines...) Expand all Loading... |
47 prefs_->AddPrefObserver(pref_name, this); | 47 prefs_->AddPrefObserver(pref_name, this); |
48 } | 48 } |
49 | 49 |
50 void PrefMemberBase::Destroy() { | 50 void PrefMemberBase::Destroy() { |
51 if (prefs_ && !pref_name_.empty()) { | 51 if (prefs_ && !pref_name_.empty()) { |
52 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | 52 prefs_->RemovePrefObserver(pref_name_.c_str(), this); |
53 prefs_ = NULL; | 53 prefs_ = NULL; |
54 } | 54 } |
55 } | 55 } |
56 | 56 |
57 void PrefMemberBase::MoveToThread( | 57 void PrefMemberBase::UseAlternateTaskRunner( |
58 const scoped_refptr<SingleThreadTaskRunner>& task_runner) { | 58 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
59 VerifyValuePrefName(); | 59 VerifyValuePrefName(); |
60 // 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. |
61 if (!internal()) | 61 if (!internal()) |
62 UpdateValueFromPref(base::Closure()); | 62 UpdateValueFromPref(base::Closure()); |
63 internal()->MoveToThread(task_runner); | 63 internal()->UseAlternateTaskRunner(task_runner); |
64 } | 64 } |
65 | 65 |
66 void PrefMemberBase::OnPreferenceChanged(PrefService* service, | 66 void PrefMemberBase::OnPreferenceChanged(PrefService* service, |
67 const std::string& pref_name) { | 67 const std::string& pref_name) { |
68 VerifyValuePrefName(); | 68 VerifyValuePrefName(); |
69 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ? | 69 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ? |
70 base::Bind(observer_, pref_name) : base::Closure()); | 70 base::Bind(observer_, pref_name) : base::Closure()); |
71 } | 71 } |
72 | 72 |
73 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const { | 73 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const { |
(...skipping 14 matching lines...) Expand all Loading... |
88 if (!internal()) | 88 if (!internal()) |
89 UpdateValueFromPref(base::Closure()); | 89 UpdateValueFromPref(base::Closure()); |
90 } | 90 } |
91 | 91 |
92 void PrefMemberBase::InvokeUnnamedCallback(const base::Closure& callback, | 92 void PrefMemberBase::InvokeUnnamedCallback(const base::Closure& callback, |
93 const std::string& pref_name) { | 93 const std::string& pref_name) { |
94 callback.Run(); | 94 callback.Run(); |
95 } | 95 } |
96 | 96 |
97 PrefMemberBase::Internal::Internal() | 97 PrefMemberBase::Internal::Internal() |
98 : thread_loop_(MessageLoopProxy::current()), | 98 : is_managed_(false), is_user_modifiable_(false) { |
99 is_managed_(false), | 99 // In unit tests, there may not be a message loop. |
100 is_user_modifiable_(false) { | 100 if (base::ThreadTaskRunnerHandle::IsSet()) |
| 101 task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
101 } | 102 } |
102 PrefMemberBase::Internal::~Internal() { } | 103 PrefMemberBase::Internal::~Internal() { } |
103 | 104 |
104 bool PrefMemberBase::Internal::IsOnCorrectThread() const { | 105 bool PrefMemberBase::Internal::IsOnCorrectThread() const { |
105 // In unit tests, there may not be a message loop. | 106 // In unit tests, there may not be a message loop. |
106 return thread_loop_.get() == NULL || thread_loop_->BelongsToCurrentThread(); | 107 return task_runner_.get() == NULL || |
| 108 task_runner_->RunsTasksOnCurrentThread(); |
107 } | 109 } |
108 | 110 |
109 void PrefMemberBase::Internal::UpdateValue( | 111 void PrefMemberBase::Internal::UpdateValue( |
110 base::Value* v, | 112 base::Value* v, |
111 bool is_managed, | 113 bool is_managed, |
112 bool is_user_modifiable, | 114 bool is_user_modifiable, |
113 const base::Closure& callback) const { | 115 const base::Closure& callback) const { |
114 scoped_ptr<base::Value> value(v); | 116 scoped_ptr<base::Value> value(v); |
115 base::ScopedClosureRunner closure_runner(callback); | 117 base::ScopedClosureRunner closure_runner(callback); |
116 if (IsOnCorrectThread()) { | 118 if (IsOnCorrectThread()) { |
117 bool rv = UpdateValueInternal(*value); | 119 bool rv = UpdateValueInternal(*value); |
118 DCHECK(rv); | 120 DCHECK(rv); |
119 is_managed_ = is_managed; | 121 is_managed_ = is_managed; |
120 is_user_modifiable_ = is_user_modifiable; | 122 is_user_modifiable_ = is_user_modifiable; |
121 } else { | 123 } else { |
122 bool may_run = thread_loop_->PostTask( | 124 bool may_run = task_runner_->PostTask( |
123 FROM_HERE, | 125 FROM_HERE, |
124 base::Bind(&PrefMemberBase::Internal::UpdateValue, this, | 126 base::Bind(&PrefMemberBase::Internal::UpdateValue, |
125 value.release(), is_managed, is_user_modifiable, | 127 this, |
| 128 value.release(), |
| 129 is_managed, |
| 130 is_user_modifiable, |
126 closure_runner.Release())); | 131 closure_runner.Release())); |
127 DCHECK(may_run); | 132 DCHECK(may_run); |
128 } | 133 } |
129 } | 134 } |
130 | 135 |
131 void PrefMemberBase::Internal::MoveToThread( | 136 void PrefMemberBase::Internal::UseAlternateTaskRunner( |
132 const scoped_refptr<SingleThreadTaskRunner>& task_runner) { | 137 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
133 CheckOnCorrectThread(); | 138 CheckOnCorrectThread(); |
134 thread_loop_ = task_runner; | 139 task_runner_ = task_runner; |
135 } | 140 } |
136 | 141 |
137 bool PrefMemberVectorStringUpdate(const base::Value& value, | 142 bool PrefMemberVectorStringUpdate(const base::Value& value, |
138 std::vector<std::string>* string_vector) { | 143 std::vector<std::string>* string_vector) { |
139 if (!value.IsType(base::Value::TYPE_LIST)) | 144 if (!value.IsType(base::Value::TYPE_LIST)) |
140 return false; | 145 return false; |
141 const base::ListValue* list = static_cast<const base::ListValue*>(&value); | 146 const base::ListValue* list = static_cast<const base::ListValue*>(&value); |
142 | 147 |
143 std::vector<std::string> local_vector; | 148 std::vector<std::string> local_vector; |
144 for (base::ListValue::const_iterator it = list->begin(); | 149 for (base::ListValue::const_iterator it = list->begin(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 base::ListValue list_value; | 224 base::ListValue list_value; |
220 list_value.AppendStrings(value); | 225 list_value.AppendStrings(value); |
221 prefs()->Set(pref_name().c_str(), list_value); | 226 prefs()->Set(pref_name().c_str(), list_value); |
222 } | 227 } |
223 | 228 |
224 template <> | 229 template <> |
225 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( | 230 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( |
226 const base::Value& value) const { | 231 const base::Value& value) const { |
227 return subtle::PrefMemberVectorStringUpdate(value, &value_); | 232 return subtle::PrefMemberVectorStringUpdate(value, &value_); |
228 } | 233 } |
OLD | NEW |