| 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 "chrome/browser/chromeos/settings/cros_settings.h" | 5 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 AddSettingsProvider(base::MakeUnique<StubCrosSettingsProvider>(notify_cb)); | 76 AddSettingsProvider(base::MakeUnique<StubCrosSettingsProvider>(notify_cb)); |
| 77 } else { | 77 } else { |
| 78 AddSettingsProvider(base::MakeUnique<DeviceSettingsProvider>( | 78 AddSettingsProvider(base::MakeUnique<DeviceSettingsProvider>( |
| 79 notify_cb, device_settings_service)); | 79 notify_cb, device_settings_service)); |
| 80 } | 80 } |
| 81 // System settings are not mocked currently. | 81 // System settings are not mocked currently. |
| 82 AddSettingsProvider(base::MakeUnique<SystemSettingsProvider>(notify_cb)); | 82 AddSettingsProvider(base::MakeUnique<SystemSettingsProvider>(notify_cb)); |
| 83 } | 83 } |
| 84 | 84 |
| 85 CrosSettings::~CrosSettings() { | 85 CrosSettings::~CrosSettings() { |
| 86 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 86 } | 87 } |
| 87 | 88 |
| 88 bool CrosSettings::IsCrosSettings(const std::string& path) { | 89 bool CrosSettings::IsCrosSettings(const std::string& path) { |
| 89 return base::StartsWith(path, kCrosSettingsPrefix, | 90 return base::StartsWith(path, kCrosSettingsPrefix, |
| 90 base::CompareCase::SENSITIVE); | 91 base::CompareCase::SENSITIVE); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void CrosSettings::Set(const std::string& path, const base::Value& in_value) { | 94 void CrosSettings::Set(const std::string& path, const base::Value& in_value) { |
| 94 DCHECK(CalledOnValidThread()); | 95 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 95 CrosSettingsProvider* provider; | 96 CrosSettingsProvider* provider; |
| 96 provider = GetProvider(path); | 97 provider = GetProvider(path); |
| 97 if (provider) | 98 if (provider) |
| 98 provider->Set(path, in_value); | 99 provider->Set(path, in_value); |
| 99 } | 100 } |
| 100 | 101 |
| 101 const base::Value* CrosSettings::GetPref(const std::string& path) const { | 102 const base::Value* CrosSettings::GetPref(const std::string& path) const { |
| 102 DCHECK(CalledOnValidThread()); | 103 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 103 CrosSettingsProvider* provider = GetProvider(path); | 104 CrosSettingsProvider* provider = GetProvider(path); |
| 104 if (provider) | 105 if (provider) |
| 105 return provider->Get(path); | 106 return provider->Get(path); |
| 106 NOTREACHED() << path << " preference was not found in the signed settings."; | 107 NOTREACHED() << path << " preference was not found in the signed settings."; |
| 107 return nullptr; | 108 return nullptr; |
| 108 } | 109 } |
| 109 | 110 |
| 110 CrosSettingsProvider::TrustedStatus CrosSettings::PrepareTrustedValues( | 111 CrosSettingsProvider::TrustedStatus CrosSettings::PrepareTrustedValues( |
| 111 const base::Closure& callback) const { | 112 const base::Closure& callback) const { |
| 112 DCHECK(CalledOnValidThread()); | 113 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 113 for (size_t i = 0; i < providers_.size(); ++i) { | 114 for (size_t i = 0; i < providers_.size(); ++i) { |
| 114 CrosSettingsProvider::TrustedStatus status = | 115 CrosSettingsProvider::TrustedStatus status = |
| 115 providers_[i]->PrepareTrustedValues(callback); | 116 providers_[i]->PrepareTrustedValues(callback); |
| 116 if (status != CrosSettingsProvider::TRUSTED) | 117 if (status != CrosSettingsProvider::TRUSTED) |
| 117 return status; | 118 return status; |
| 118 } | 119 } |
| 119 return CrosSettingsProvider::TRUSTED; | 120 return CrosSettingsProvider::TRUSTED; |
| 120 } | 121 } |
| 121 | 122 |
| 122 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { | 123 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { |
| 123 DCHECK(CalledOnValidThread()); | 124 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 124 base::Value value(in_value); | 125 base::Value value(in_value); |
| 125 Set(path, value); | 126 Set(path, value); |
| 126 } | 127 } |
| 127 | 128 |
| 128 void CrosSettings::SetInteger(const std::string& path, int in_value) { | 129 void CrosSettings::SetInteger(const std::string& path, int in_value) { |
| 129 DCHECK(CalledOnValidThread()); | 130 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 130 base::Value value(in_value); | 131 base::Value value(in_value); |
| 131 Set(path, value); | 132 Set(path, value); |
| 132 } | 133 } |
| 133 | 134 |
| 134 void CrosSettings::SetDouble(const std::string& path, double in_value) { | 135 void CrosSettings::SetDouble(const std::string& path, double in_value) { |
| 135 DCHECK(CalledOnValidThread()); | 136 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 136 base::Value value(in_value); | 137 base::Value value(in_value); |
| 137 Set(path, value); | 138 Set(path, value); |
| 138 } | 139 } |
| 139 | 140 |
| 140 void CrosSettings::SetString(const std::string& path, | 141 void CrosSettings::SetString(const std::string& path, |
| 141 const std::string& in_value) { | 142 const std::string& in_value) { |
| 142 DCHECK(CalledOnValidThread()); | 143 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 143 base::Value value(in_value); | 144 base::Value value(in_value); |
| 144 Set(path, value); | 145 Set(path, value); |
| 145 } | 146 } |
| 146 | 147 |
| 147 void CrosSettings::AppendToList(const std::string& path, | 148 void CrosSettings::AppendToList(const std::string& path, |
| 148 const base::Value* value) { | 149 const base::Value* value) { |
| 149 DCHECK(CalledOnValidThread()); | 150 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 150 const base::Value* old_value = GetPref(path); | 151 const base::Value* old_value = GetPref(path); |
| 151 std::unique_ptr<base::Value> new_value(old_value ? old_value->DeepCopy() | 152 std::unique_ptr<base::Value> new_value(old_value ? old_value->DeepCopy() |
| 152 : new base::ListValue()); | 153 : new base::ListValue()); |
| 153 static_cast<base::ListValue*>(new_value.get()) | 154 static_cast<base::ListValue*>(new_value.get()) |
| 154 ->Append(value->CreateDeepCopy()); | 155 ->Append(value->CreateDeepCopy()); |
| 155 Set(path, *new_value); | 156 Set(path, *new_value); |
| 156 } | 157 } |
| 157 | 158 |
| 158 void CrosSettings::RemoveFromList(const std::string& path, | 159 void CrosSettings::RemoveFromList(const std::string& path, |
| 159 const base::Value* value) { | 160 const base::Value* value) { |
| 160 DCHECK(CalledOnValidThread()); | 161 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 161 const base::Value* old_value = GetPref(path); | 162 const base::Value* old_value = GetPref(path); |
| 162 std::unique_ptr<base::Value> new_value(old_value ? old_value->DeepCopy() | 163 std::unique_ptr<base::Value> new_value(old_value ? old_value->DeepCopy() |
| 163 : new base::ListValue()); | 164 : new base::ListValue()); |
| 164 static_cast<base::ListValue*>(new_value.get())->Remove(*value, nullptr); | 165 static_cast<base::ListValue*>(new_value.get())->Remove(*value, nullptr); |
| 165 Set(path, *new_value); | 166 Set(path, *new_value); |
| 166 } | 167 } |
| 167 | 168 |
| 168 bool CrosSettings::GetBoolean(const std::string& path, | 169 bool CrosSettings::GetBoolean(const std::string& path, |
| 169 bool* bool_value) const { | 170 bool* bool_value) const { |
| 170 DCHECK(CalledOnValidThread()); | 171 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 171 const base::Value* value = GetPref(path); | 172 const base::Value* value = GetPref(path); |
| 172 if (value) | 173 if (value) |
| 173 return value->GetAsBoolean(bool_value); | 174 return value->GetAsBoolean(bool_value); |
| 174 return false; | 175 return false; |
| 175 } | 176 } |
| 176 | 177 |
| 177 bool CrosSettings::GetInteger(const std::string& path, | 178 bool CrosSettings::GetInteger(const std::string& path, |
| 178 int* out_value) const { | 179 int* out_value) const { |
| 179 DCHECK(CalledOnValidThread()); | 180 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 180 const base::Value* value = GetPref(path); | 181 const base::Value* value = GetPref(path); |
| 181 if (value) | 182 if (value) |
| 182 return value->GetAsInteger(out_value); | 183 return value->GetAsInteger(out_value); |
| 183 return false; | 184 return false; |
| 184 } | 185 } |
| 185 | 186 |
| 186 bool CrosSettings::GetDouble(const std::string& path, | 187 bool CrosSettings::GetDouble(const std::string& path, |
| 187 double* out_value) const { | 188 double* out_value) const { |
| 188 DCHECK(CalledOnValidThread()); | 189 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 189 const base::Value* value = GetPref(path); | 190 const base::Value* value = GetPref(path); |
| 190 if (value) | 191 if (value) |
| 191 return value->GetAsDouble(out_value); | 192 return value->GetAsDouble(out_value); |
| 192 return false; | 193 return false; |
| 193 } | 194 } |
| 194 | 195 |
| 195 bool CrosSettings::GetString(const std::string& path, | 196 bool CrosSettings::GetString(const std::string& path, |
| 196 std::string* out_value) const { | 197 std::string* out_value) const { |
| 197 DCHECK(CalledOnValidThread()); | 198 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 198 const base::Value* value = GetPref(path); | 199 const base::Value* value = GetPref(path); |
| 199 if (value) | 200 if (value) |
| 200 return value->GetAsString(out_value); | 201 return value->GetAsString(out_value); |
| 201 return false; | 202 return false; |
| 202 } | 203 } |
| 203 | 204 |
| 204 bool CrosSettings::GetList(const std::string& path, | 205 bool CrosSettings::GetList(const std::string& path, |
| 205 const base::ListValue** out_value) const { | 206 const base::ListValue** out_value) const { |
| 206 DCHECK(CalledOnValidThread()); | 207 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 207 const base::Value* value = GetPref(path); | 208 const base::Value* value = GetPref(path); |
| 208 if (value) | 209 if (value) |
| 209 return value->GetAsList(out_value); | 210 return value->GetAsList(out_value); |
| 210 return false; | 211 return false; |
| 211 } | 212 } |
| 212 | 213 |
| 213 bool CrosSettings::GetDictionary( | 214 bool CrosSettings::GetDictionary( |
| 214 const std::string& path, | 215 const std::string& path, |
| 215 const base::DictionaryValue** out_value) const { | 216 const base::DictionaryValue** out_value) const { |
| 216 DCHECK(CalledOnValidThread()); | 217 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 217 const base::Value* value = GetPref(path); | 218 const base::Value* value = GetPref(path); |
| 218 if (value) | 219 if (value) |
| 219 return value->GetAsDictionary(out_value); | 220 return value->GetAsDictionary(out_value); |
| 220 return false; | 221 return false; |
| 221 } | 222 } |
| 222 | 223 |
| 223 bool CrosSettings::FindEmailInList(const std::string& path, | 224 bool CrosSettings::FindEmailInList(const std::string& path, |
| 224 const std::string& email, | 225 const std::string& email, |
| 225 bool* wildcard_match) const { | 226 bool* wildcard_match) const { |
| 226 DCHECK(CalledOnValidThread()); | 227 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 227 std::string canonicalized_email( | 228 std::string canonicalized_email( |
| 228 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email))); | 229 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email))); |
| 229 std::string wildcard_email; | 230 std::string wildcard_email; |
| 230 std::string::size_type at_pos = canonicalized_email.find('@'); | 231 std::string::size_type at_pos = canonicalized_email.find('@'); |
| 231 if (at_pos != std::string::npos) { | 232 if (at_pos != std::string::npos) { |
| 232 wildcard_email = | 233 wildcard_email = |
| 233 std::string("*").append(canonicalized_email.substr(at_pos)); | 234 std::string("*").append(canonicalized_email.substr(at_pos)); |
| 234 } | 235 } |
| 235 | 236 |
| 236 if (wildcard_match) | 237 if (wildcard_match) |
| (...skipping 27 matching lines...) Expand all Loading... |
| 264 } | 265 } |
| 265 | 266 |
| 266 if (wildcard_match) | 267 if (wildcard_match) |
| 267 *wildcard_match = found_wildcard_match; | 268 *wildcard_match = found_wildcard_match; |
| 268 | 269 |
| 269 return found_wildcard_match; | 270 return found_wildcard_match; |
| 270 } | 271 } |
| 271 | 272 |
| 272 bool CrosSettings::AddSettingsProvider( | 273 bool CrosSettings::AddSettingsProvider( |
| 273 std::unique_ptr<CrosSettingsProvider> provider) { | 274 std::unique_ptr<CrosSettingsProvider> provider) { |
| 274 DCHECK(CalledOnValidThread()); | 275 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 275 CrosSettingsProvider* provider_ptr = provider.get(); | 276 CrosSettingsProvider* provider_ptr = provider.get(); |
| 276 providers_.push_back(std::move(provider)); | 277 providers_.push_back(std::move(provider)); |
| 277 | 278 |
| 278 // Allow the provider to notify this object when settings have changed. | 279 // Allow the provider to notify this object when settings have changed. |
| 279 // Providers instantiated inside this class will have the same callback | 280 // Providers instantiated inside this class will have the same callback |
| 280 // passed to their constructor, but doing it here allows for providers | 281 // passed to their constructor, but doing it here allows for providers |
| 281 // to be instantiated outside this class. | 282 // to be instantiated outside this class. |
| 282 CrosSettingsProvider::NotifyObserversCallback notify_cb( | 283 CrosSettingsProvider::NotifyObserversCallback notify_cb( |
| 283 base::Bind(&CrosSettings::FireObservers, base::Unretained(this))); | 284 base::Bind(&CrosSettings::FireObservers, base::Unretained(this))); |
| 284 provider_ptr->SetNotifyObserversCallback(notify_cb); | 285 provider_ptr->SetNotifyObserversCallback(notify_cb); |
| 285 return true; | 286 return true; |
| 286 } | 287 } |
| 287 | 288 |
| 288 std::unique_ptr<CrosSettingsProvider> CrosSettings::RemoveSettingsProvider( | 289 std::unique_ptr<CrosSettingsProvider> CrosSettings::RemoveSettingsProvider( |
| 289 CrosSettingsProvider* provider) { | 290 CrosSettingsProvider* provider) { |
| 290 DCHECK(CalledOnValidThread()); | 291 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 291 auto it = std::find_if( | 292 auto it = std::find_if( |
| 292 providers_.begin(), providers_.end(), | 293 providers_.begin(), providers_.end(), |
| 293 [provider](const std::unique_ptr<CrosSettingsProvider>& ptr) { | 294 [provider](const std::unique_ptr<CrosSettingsProvider>& ptr) { |
| 294 return ptr.get() == provider; | 295 return ptr.get() == provider; |
| 295 }); | 296 }); |
| 296 if (it != providers_.end()) { | 297 if (it != providers_.end()) { |
| 297 std::unique_ptr<CrosSettingsProvider> ptr = std::move(*it); | 298 std::unique_ptr<CrosSettingsProvider> ptr = std::move(*it); |
| 298 providers_.erase(it); | 299 providers_.erase(it); |
| 299 return ptr; | 300 return ptr; |
| 300 } | 301 } |
| 301 return nullptr; | 302 return nullptr; |
| 302 } | 303 } |
| 303 | 304 |
| 304 std::unique_ptr<CrosSettings::ObserverSubscription> | 305 std::unique_ptr<CrosSettings::ObserverSubscription> |
| 305 CrosSettings::AddSettingsObserver(const std::string& path, | 306 CrosSettings::AddSettingsObserver(const std::string& path, |
| 306 const base::Closure& callback) { | 307 const base::Closure& callback) { |
| 307 DCHECK(!path.empty()); | 308 DCHECK(!path.empty()); |
| 308 DCHECK(!callback.is_null()); | 309 DCHECK(!callback.is_null()); |
| 309 DCHECK(CalledOnValidThread()); | 310 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 310 | 311 |
| 311 if (!GetProvider(path)) { | 312 if (!GetProvider(path)) { |
| 312 NOTREACHED() << "Trying to add an observer for an unregistered setting: " | 313 NOTREACHED() << "Trying to add an observer for an unregistered setting: " |
| 313 << path; | 314 << path; |
| 314 return std::unique_ptr<CrosSettings::ObserverSubscription>(); | 315 return std::unique_ptr<CrosSettings::ObserverSubscription>(); |
| 315 } | 316 } |
| 316 | 317 |
| 317 // Get the callback registry associated with the path. | 318 // Get the callback registry associated with the path. |
| 318 base::CallbackList<void(void)>* registry = nullptr; | 319 base::CallbackList<void(void)>* registry = nullptr; |
| 319 auto observer_iterator = settings_observers_.find(path); | 320 auto observer_iterator = settings_observers_.find(path); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 331 CrosSettingsProvider* CrosSettings::GetProvider( | 332 CrosSettingsProvider* CrosSettings::GetProvider( |
| 332 const std::string& path) const { | 333 const std::string& path) const { |
| 333 for (size_t i = 0; i < providers_.size(); ++i) { | 334 for (size_t i = 0; i < providers_.size(); ++i) { |
| 334 if (providers_[i]->HandlesSetting(path)) | 335 if (providers_[i]->HandlesSetting(path)) |
| 335 return providers_[i].get(); | 336 return providers_[i].get(); |
| 336 } | 337 } |
| 337 return nullptr; | 338 return nullptr; |
| 338 } | 339 } |
| 339 | 340 |
| 340 void CrosSettings::FireObservers(const std::string& path) { | 341 void CrosSettings::FireObservers(const std::string& path) { |
| 341 DCHECK(CalledOnValidThread()); | 342 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 342 auto observer_iterator = settings_observers_.find(path); | 343 auto observer_iterator = settings_observers_.find(path); |
| 343 if (observer_iterator == settings_observers_.end()) | 344 if (observer_iterator == settings_observers_.end()) |
| 344 return; | 345 return; |
| 345 | 346 |
| 346 observer_iterator->second->Notify(); | 347 observer_iterator->second->Notify(); |
| 347 } | 348 } |
| 348 | 349 |
| 349 ScopedTestCrosSettings::ScopedTestCrosSettings() { | 350 ScopedTestCrosSettings::ScopedTestCrosSettings() { |
| 350 CrosSettings::Initialize(); | 351 CrosSettings::Initialize(); |
| 351 } | 352 } |
| 352 | 353 |
| 353 ScopedTestCrosSettings::~ScopedTestCrosSettings() { | 354 ScopedTestCrosSettings::~ScopedTestCrosSettings() { |
| 354 CrosSettings::Shutdown(); | 355 CrosSettings::Shutdown(); |
| 355 } | 356 } |
| 356 | 357 |
| 357 } // namespace chromeos | 358 } // namespace chromeos |
| OLD | NEW |