Chromium Code Reviews| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 const scoped_refptr<SingleThreadTaskRunner>& task_runner) { |
| 129 CheckOnCorrectThread(); | 126 CheckOnCorrectThread(); |
| 130 thread_loop_ = task_runner; | 127 thread_task_runner_ = task_runner; |
|
danakj
2015/04/21 20:16:30
nit: pass it by value to the method and .Pass() it
Sami
2015/04/23 17:48:24
Done.
| |
| 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 |