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