Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: base/prefs/pref_member.cc

Issue 369703003: Reduce usage of MessageLoopProxy in base/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Explicit Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
11 #include "base/value_conversions.h" 14 #include "base/value_conversions.h"
12 15
13 using base::MessageLoopProxy;
14
15 namespace subtle { 16 namespace subtle {
16 17
17 PrefMemberBase::PrefMemberBase() 18 PrefMemberBase::PrefMemberBase()
18 : prefs_(NULL), 19 : prefs_(NULL),
19 setting_value_(false) { 20 setting_value_(false) {
20 } 21 }
21 22
22 PrefMemberBase::~PrefMemberBase() { 23 PrefMemberBase::~PrefMemberBase() {
23 Destroy(); 24 Destroy();
24 } 25 }
(...skipping 20 matching lines...) Expand all
45 prefs_->AddPrefObserver(pref_name, this); 46 prefs_->AddPrefObserver(pref_name, this);
46 } 47 }
47 48
48 void PrefMemberBase::Destroy() { 49 void PrefMemberBase::Destroy() {
49 if (prefs_ && !pref_name_.empty()) { 50 if (prefs_ && !pref_name_.empty()) {
50 prefs_->RemovePrefObserver(pref_name_.c_str(), this); 51 prefs_->RemovePrefObserver(pref_name_.c_str(), this);
51 prefs_ = NULL; 52 prefs_ = NULL;
52 } 53 }
53 } 54 }
54 55
55 void PrefMemberBase::MoveToThread( 56 void PrefMemberBase::MoveToTaskRunner(
56 const scoped_refptr<MessageLoopProxy>& message_loop) { 57 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
57 VerifyValuePrefName(); 58 VerifyValuePrefName();
58 // Load the value from preferences if it hasn't been loaded so far. 59 // Load the value from preferences if it hasn't been loaded so far.
59 if (!internal()) 60 if (!internal())
60 UpdateValueFromPref(base::Closure()); 61 UpdateValueFromPref(base::Closure());
61 internal()->MoveToThread(message_loop); 62 internal()->MoveToTaskRunner(task_runner);
62 } 63 }
63 64
64 void PrefMemberBase::OnPreferenceChanged(PrefService* service, 65 void PrefMemberBase::OnPreferenceChanged(PrefService* service,
65 const std::string& pref_name) { 66 const std::string& pref_name) {
66 VerifyValuePrefName(); 67 VerifyValuePrefName();
67 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ? 68 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ?
68 base::Bind(observer_, pref_name) : base::Closure()); 69 base::Bind(observer_, pref_name) : base::Closure());
69 } 70 }
70 71
71 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const { 72 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const {
(...skipping 14 matching lines...) Expand all
86 if (!internal()) 87 if (!internal())
87 UpdateValueFromPref(base::Closure()); 88 UpdateValueFromPref(base::Closure());
88 } 89 }
89 90
90 void PrefMemberBase::InvokeUnnamedCallback(const base::Closure& callback, 91 void PrefMemberBase::InvokeUnnamedCallback(const base::Closure& callback,
91 const std::string& pref_name) { 92 const std::string& pref_name) {
92 callback.Run(); 93 callback.Run();
93 } 94 }
94 95
95 PrefMemberBase::Internal::Internal() 96 PrefMemberBase::Internal::Internal()
96 : thread_loop_(MessageLoopProxy::current()), 97 : is_managed_(false),
97 is_managed_(false),
98 is_user_modifiable_(false) { 98 is_user_modifiable_(false) {
99 // In unit tests, there may not be a message loop.
100 if (base::ThreadTaskRunnerHandle::IsSet())
101 task_runner_ = base::ThreadTaskRunnerHandle::Get();
99 } 102 }
100 PrefMemberBase::Internal::~Internal() { } 103 PrefMemberBase::Internal::~Internal() { }
101 104
102 bool PrefMemberBase::Internal::IsOnCorrectThread() const { 105 bool PrefMemberBase::Internal::IsOnCorrectThread() const {
103 // In unit tests, there may not be a message loop. 106 // In unit tests, there may not be a message loop.
104 return thread_loop_.get() == NULL || thread_loop_->BelongsToCurrentThread(); 107 return task_runner_.get() == NULL || task_runner_->RunsTasksOnCurrentThread();
105 } 108 }
106 109
107 void PrefMemberBase::Internal::UpdateValue( 110 void PrefMemberBase::Internal::UpdateValue(
108 base::Value* v, 111 base::Value* v,
109 bool is_managed, 112 bool is_managed,
110 bool is_user_modifiable, 113 bool is_user_modifiable,
111 const base::Closure& callback) const { 114 const base::Closure& callback) const {
112 scoped_ptr<base::Value> value(v); 115 scoped_ptr<base::Value> value(v);
113 base::ScopedClosureRunner closure_runner(callback); 116 base::ScopedClosureRunner closure_runner(callback);
114 if (IsOnCorrectThread()) { 117 if (IsOnCorrectThread()) {
115 bool rv = UpdateValueInternal(*value); 118 bool rv = UpdateValueInternal(*value);
116 DCHECK(rv); 119 DCHECK(rv);
117 is_managed_ = is_managed; 120 is_managed_ = is_managed;
118 is_user_modifiable_ = is_user_modifiable; 121 is_user_modifiable_ = is_user_modifiable;
119 } else { 122 } else {
120 bool may_run = thread_loop_->PostTask( 123 bool may_run = task_runner_->PostTask(
121 FROM_HERE, 124 FROM_HERE,
122 base::Bind(&PrefMemberBase::Internal::UpdateValue, this, 125 base::Bind(&PrefMemberBase::Internal::UpdateValue,
123 value.release(), is_managed, is_user_modifiable, 126 this,
127 value.release(),
128 is_managed,
129 is_user_modifiable,
124 closure_runner.Release())); 130 closure_runner.Release()));
125 DCHECK(may_run); 131 DCHECK(may_run);
126 } 132 }
127 } 133 }
128 134
129 void PrefMemberBase::Internal::MoveToThread( 135 void PrefMemberBase::Internal::MoveToTaskRunner(
130 const scoped_refptr<MessageLoopProxy>& message_loop) { 136 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
131 CheckOnCorrectThread(); 137 CheckOnCorrectThread();
132 thread_loop_ = message_loop; 138 task_runner_ = task_runner;
133 } 139 }
134 140
135 bool PrefMemberVectorStringUpdate(const base::Value& value, 141 bool PrefMemberVectorStringUpdate(const base::Value& value,
136 std::vector<std::string>* string_vector) { 142 std::vector<std::string>* string_vector) {
137 if (!value.IsType(base::Value::TYPE_LIST)) 143 if (!value.IsType(base::Value::TYPE_LIST))
138 return false; 144 return false;
139 const base::ListValue* list = static_cast<const base::ListValue*>(&value); 145 const base::ListValue* list = static_cast<const base::ListValue*>(&value);
140 146
141 std::vector<std::string> local_vector; 147 std::vector<std::string> local_vector;
142 for (base::ListValue::const_iterator it = list->begin(); 148 for (base::ListValue::const_iterator it = list->begin();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 base::ListValue list_value; 223 base::ListValue list_value;
218 list_value.AppendStrings(value); 224 list_value.AppendStrings(value);
219 prefs()->Set(pref_name().c_str(), list_value); 225 prefs()->Set(pref_name().c_str(), list_value);
220 } 226 }
221 227
222 template <> 228 template <>
223 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( 229 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal(
224 const base::Value& value) const { 230 const base::Value& value) const {
225 return subtle::PrefMemberVectorStringUpdate(value, &value_); 231 return subtle::PrefMemberVectorStringUpdate(value, &value_);
226 } 232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698