| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/cros_settings.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/stl_util.h" | |
| 11 #include "base/string_util.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/chromeos/device_settings_provider.h" | |
| 14 #include "chrome/browser/chromeos/login/signed_settings_helper.h" | |
| 15 #include "chrome/browser/chromeos/stub_cros_settings_provider.h" | |
| 16 #include "chrome/browser/chromeos/system_settings_provider.h" | |
| 17 #include "chrome/common/chrome_notification_types.h" | |
| 18 #include "chrome/common/chrome_switches.h" | |
| 19 #include "chrome/common/net/gaia/gaia_auth_util.h" | |
| 20 #include "content/public/browser/notification_details.h" | |
| 21 #include "content/public/browser/notification_source.h" | |
| 22 #include "content/public/browser/notification_types.h" | |
| 23 | |
| 24 namespace chromeos { | |
| 25 | |
| 26 static base::LazyInstance<CrosSettings> g_cros_settings = | |
| 27 LAZY_INSTANCE_INITIALIZER; | |
| 28 | |
| 29 CrosSettings* CrosSettings::Get() { | |
| 30 // TODO(xiyaun): Use real stuff when underlying libcros is ready. | |
| 31 return g_cros_settings.Pointer(); | |
| 32 } | |
| 33 | |
| 34 bool CrosSettings::IsCrosSettings(const std::string& path) { | |
| 35 return StartsWithASCII(path, kCrosSettingsPrefix, true); | |
| 36 } | |
| 37 | |
| 38 void CrosSettings::FireObservers(const std::string& path) { | |
| 39 DCHECK(CalledOnValidThread()); | |
| 40 SettingsObserverMap::iterator observer_iterator = | |
| 41 settings_observers_.find(path); | |
| 42 if (observer_iterator == settings_observers_.end()) | |
| 43 return; | |
| 44 | |
| 45 NotificationObserverList::Iterator it(*(observer_iterator->second)); | |
| 46 content::NotificationObserver* observer; | |
| 47 while ((observer = it.GetNext()) != NULL) { | |
| 48 observer->Observe(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, | |
| 49 content::Source<CrosSettings>(this), | |
| 50 content::Details<const std::string>(&path)); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 void CrosSettings::Set(const std::string& path, const base::Value& in_value) { | |
| 55 DCHECK(CalledOnValidThread()); | |
| 56 CrosSettingsProvider* provider; | |
| 57 provider = GetProvider(path); | |
| 58 if (provider) | |
| 59 provider->Set(path, in_value); | |
| 60 } | |
| 61 | |
| 62 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { | |
| 63 DCHECK(CalledOnValidThread()); | |
| 64 base::FundamentalValue value(in_value); | |
| 65 Set(path, value); | |
| 66 } | |
| 67 | |
| 68 void CrosSettings::SetInteger(const std::string& path, int in_value) { | |
| 69 DCHECK(CalledOnValidThread()); | |
| 70 base::FundamentalValue value(in_value); | |
| 71 Set(path, value); | |
| 72 } | |
| 73 | |
| 74 void CrosSettings::SetDouble(const std::string& path, double in_value) { | |
| 75 DCHECK(CalledOnValidThread()); | |
| 76 base::FundamentalValue value(in_value); | |
| 77 Set(path, value); | |
| 78 } | |
| 79 | |
| 80 void CrosSettings::SetString(const std::string& path, | |
| 81 const std::string& in_value) { | |
| 82 DCHECK(CalledOnValidThread()); | |
| 83 base::StringValue value(in_value); | |
| 84 Set(path, value); | |
| 85 } | |
| 86 | |
| 87 void CrosSettings::AppendToList(const std::string& path, | |
| 88 const base::Value* value) { | |
| 89 DCHECK(CalledOnValidThread()); | |
| 90 const base::Value* old_value = GetPref(path); | |
| 91 scoped_ptr<base::Value> new_value( | |
| 92 old_value ? old_value->DeepCopy() : new base::ListValue()); | |
| 93 static_cast<base::ListValue*>(new_value.get())->Append(value->DeepCopy()); | |
| 94 Set(path, *new_value); | |
| 95 } | |
| 96 | |
| 97 void CrosSettings::RemoveFromList(const std::string& path, | |
| 98 const base::Value* value) { | |
| 99 DCHECK(CalledOnValidThread()); | |
| 100 const base::Value* old_value = GetPref(path); | |
| 101 scoped_ptr<base::Value> new_value( | |
| 102 old_value ? old_value->DeepCopy() : new base::ListValue()); | |
| 103 static_cast<base::ListValue*>(new_value.get())->Remove(*value, NULL); | |
| 104 Set(path, *new_value); | |
| 105 } | |
| 106 | |
| 107 bool CrosSettings::FindEmailInList(const std::string& path, | |
| 108 const std::string& email) const { | |
| 109 DCHECK(CalledOnValidThread()); | |
| 110 std::string canonicalized_email( | |
| 111 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email))); | |
| 112 std::string wildcard_email; | |
| 113 std::string::size_type at_pos = canonicalized_email.find('@'); | |
| 114 if (at_pos != std::string::npos) { | |
| 115 wildcard_email = | |
| 116 std::string("*").append(canonicalized_email.substr(at_pos)); | |
| 117 } | |
| 118 | |
| 119 const base::ListValue* list; | |
| 120 if (!GetList(path, &list)) | |
| 121 return false; | |
| 122 for (base::ListValue::const_iterator entry(list->begin()); | |
| 123 entry != list->end(); | |
| 124 ++entry) { | |
| 125 std::string entry_string; | |
| 126 if (!(*entry)->GetAsString(&entry_string)) { | |
| 127 NOTREACHED(); | |
| 128 continue; | |
| 129 } | |
| 130 std::string canonicalized_entry( | |
| 131 gaia::CanonicalizeEmail(gaia::SanitizeEmail(entry_string))); | |
| 132 | |
| 133 if (canonicalized_entry == canonicalized_email || | |
| 134 canonicalized_entry == wildcard_email) { | |
| 135 return true; | |
| 136 } | |
| 137 } | |
| 138 return false; | |
| 139 } | |
| 140 | |
| 141 bool CrosSettings::AddSettingsProvider(CrosSettingsProvider* provider) { | |
| 142 DCHECK(CalledOnValidThread()); | |
| 143 providers_.push_back(provider); | |
| 144 | |
| 145 // Allow the provider to notify this object when settings have changed. | |
| 146 // Providers instantiated inside this class will have the same callback | |
| 147 // passed to their constructor, but doing it here allows for providers | |
| 148 // to be instantiated outside this class. | |
| 149 CrosSettingsProvider::NotifyObserversCallback notify_cb( | |
| 150 base::Bind(&CrosSettings::FireObservers, base::Unretained(this))); | |
| 151 provider->SetNotifyObserversCallback(notify_cb); | |
| 152 return true; | |
| 153 } | |
| 154 | |
| 155 bool CrosSettings::RemoveSettingsProvider(CrosSettingsProvider* provider) { | |
| 156 DCHECK(CalledOnValidThread()); | |
| 157 std::vector<CrosSettingsProvider*>::iterator it = | |
| 158 std::find(providers_.begin(), providers_.end(), provider); | |
| 159 if (it != providers_.end()) { | |
| 160 providers_.erase(it); | |
| 161 return true; | |
| 162 } | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 void CrosSettings::AddSettingsObserver(const char* path, | |
| 167 content::NotificationObserver* obs) { | |
| 168 DCHECK(path); | |
| 169 DCHECK(obs); | |
| 170 DCHECK(CalledOnValidThread()); | |
| 171 | |
| 172 if (!GetProvider(std::string(path))) { | |
| 173 NOTREACHED() << "Trying to add an observer for an unregistered setting: " | |
| 174 << path; | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 // Get the settings observer list associated with the path. | |
| 179 NotificationObserverList* observer_list = NULL; | |
| 180 SettingsObserverMap::iterator observer_iterator = | |
| 181 settings_observers_.find(path); | |
| 182 if (observer_iterator == settings_observers_.end()) { | |
| 183 observer_list = new NotificationObserverList; | |
| 184 settings_observers_[path] = observer_list; | |
| 185 } else { | |
| 186 observer_list = observer_iterator->second; | |
| 187 } | |
| 188 | |
| 189 // Verify that this observer doesn't already exist. | |
| 190 NotificationObserverList::Iterator it(*observer_list); | |
| 191 content::NotificationObserver* existing_obs; | |
| 192 while ((existing_obs = it.GetNext()) != NULL) { | |
| 193 if (existing_obs == obs) | |
| 194 return; | |
| 195 } | |
| 196 | |
| 197 // Ok, safe to add the pref observer. | |
| 198 observer_list->AddObserver(obs); | |
| 199 } | |
| 200 | |
| 201 void CrosSettings::RemoveSettingsObserver(const char* path, | |
| 202 content::NotificationObserver* obs) { | |
| 203 DCHECK(CalledOnValidThread()); | |
| 204 | |
| 205 SettingsObserverMap::iterator observer_iterator = | |
| 206 settings_observers_.find(path); | |
| 207 if (observer_iterator == settings_observers_.end()) | |
| 208 return; | |
| 209 | |
| 210 NotificationObserverList* observer_list = observer_iterator->second; | |
| 211 observer_list->RemoveObserver(obs); | |
| 212 } | |
| 213 | |
| 214 CrosSettingsProvider* CrosSettings::GetProvider( | |
| 215 const std::string& path) const { | |
| 216 for (size_t i = 0; i < providers_.size(); ++i) { | |
| 217 if (providers_[i]->HandlesSetting(path)) | |
| 218 return providers_[i]; | |
| 219 } | |
| 220 return NULL; | |
| 221 } | |
| 222 | |
| 223 void CrosSettings::ReloadProviders() { | |
| 224 for (size_t i = 0; i < providers_.size(); ++i) | |
| 225 providers_[i]->Reload(); | |
| 226 } | |
| 227 | |
| 228 const base::Value* CrosSettings::GetPref(const std::string& path) const { | |
| 229 DCHECK(CalledOnValidThread()); | |
| 230 CrosSettingsProvider* provider = GetProvider(path); | |
| 231 if (provider) | |
| 232 return provider->Get(path); | |
| 233 NOTREACHED() << path << " preference was not found in the signed settings."; | |
| 234 return NULL; | |
| 235 } | |
| 236 | |
| 237 CrosSettingsProvider::TrustedStatus CrosSettings::PrepareTrustedValues( | |
| 238 const base::Closure& callback) const { | |
| 239 DCHECK(CalledOnValidThread()); | |
| 240 for (size_t i = 0; i < providers_.size(); ++i) { | |
| 241 CrosSettingsProvider::TrustedStatus status = | |
| 242 providers_[i]->PrepareTrustedValues(callback); | |
| 243 if (status != CrosSettingsProvider::TRUSTED) | |
| 244 return status; | |
| 245 } | |
| 246 return CrosSettingsProvider::TRUSTED; | |
| 247 } | |
| 248 | |
| 249 bool CrosSettings::GetBoolean(const std::string& path, | |
| 250 bool* bool_value) const { | |
| 251 DCHECK(CalledOnValidThread()); | |
| 252 const base::Value* value = GetPref(path); | |
| 253 if (value) | |
| 254 return value->GetAsBoolean(bool_value); | |
| 255 return false; | |
| 256 } | |
| 257 | |
| 258 bool CrosSettings::GetInteger(const std::string& path, | |
| 259 int* out_value) const { | |
| 260 DCHECK(CalledOnValidThread()); | |
| 261 const base::Value* value = GetPref(path); | |
| 262 if (value) | |
| 263 return value->GetAsInteger(out_value); | |
| 264 return false; | |
| 265 } | |
| 266 | |
| 267 bool CrosSettings::GetDouble(const std::string& path, | |
| 268 double* out_value) const { | |
| 269 DCHECK(CalledOnValidThread()); | |
| 270 const base::Value* value = GetPref(path); | |
| 271 if (value) | |
| 272 return value->GetAsDouble(out_value); | |
| 273 return false; | |
| 274 } | |
| 275 | |
| 276 bool CrosSettings::GetString(const std::string& path, | |
| 277 std::string* out_value) const { | |
| 278 DCHECK(CalledOnValidThread()); | |
| 279 const base::Value* value = GetPref(path); | |
| 280 if (value) | |
| 281 return value->GetAsString(out_value); | |
| 282 return false; | |
| 283 } | |
| 284 | |
| 285 bool CrosSettings::GetList(const std::string& path, | |
| 286 const base::ListValue** out_value) const { | |
| 287 DCHECK(CalledOnValidThread()); | |
| 288 const base::Value* value = GetPref(path); | |
| 289 if (value) | |
| 290 return value->GetAsList(out_value); | |
| 291 return false; | |
| 292 } | |
| 293 | |
| 294 CrosSettings::CrosSettings() { | |
| 295 CrosSettingsProvider::NotifyObserversCallback notify_cb( | |
| 296 base::Bind(&CrosSettings::FireObservers, | |
| 297 // This is safe since |this| is never deleted. | |
| 298 base::Unretained(this))); | |
| 299 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 300 switches::kStubCrosSettings)) { | |
| 301 AddSettingsProvider(new StubCrosSettingsProvider(notify_cb)); | |
| 302 } else { | |
| 303 AddSettingsProvider( | |
| 304 new DeviceSettingsProvider(notify_cb, SignedSettingsHelper::Get())); | |
| 305 } | |
| 306 // System settings are not mocked currently. | |
| 307 AddSettingsProvider(new SystemSettingsProvider(notify_cb)); | |
| 308 } | |
| 309 | |
| 310 CrosSettings::~CrosSettings() { | |
| 311 STLDeleteElements(&providers_); | |
| 312 STLDeleteValues(&settings_observers_); | |
| 313 } | |
| 314 | |
| 315 } // namespace chromeos | |
| OLD | NEW |