| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/cros_settings.h" | 5 #include "chrome/browser/chromeos/cros_settings.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/chromeos/user_cros_settings_provider.h" | 11 #include "chrome/browser/chromeos/device_settings_provider.h" |
| 12 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h" | 12 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "content/public/browser/notification_details.h" | 14 #include "content/public/browser/notification_details.h" |
| 15 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
| 16 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
| 17 | 17 |
| 18 namespace chromeos { | 18 namespace chromeos { |
| 19 | 19 |
| 20 static base::LazyInstance<CrosSettings> g_cros_settings = | 20 static base::LazyInstance<CrosSettings> g_cros_settings = |
| 21 LAZY_INSTANCE_INITIALIZER; | 21 LAZY_INSTANCE_INITIALIZER; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 43 observer->Observe(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, | 43 observer->Observe(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, |
| 44 content::Source<CrosSettings>(this), | 44 content::Source<CrosSettings>(this), |
| 45 content::Details<std::string>(&path_str)); | 45 content::Details<std::string>(&path_str)); |
| 46 } | 46 } |
| 47 } | 47 } |
| 48 | 48 |
| 49 void CrosSettings::Set(const std::string& path, const base::Value& in_value) { | 49 void CrosSettings::Set(const std::string& path, const base::Value& in_value) { |
| 50 DCHECK(CalledOnValidThread()); | 50 DCHECK(CalledOnValidThread()); |
| 51 CrosSettingsProvider* provider; | 51 CrosSettingsProvider* provider; |
| 52 provider = GetProvider(path); | 52 provider = GetProvider(path); |
| 53 if (provider) { | 53 if (provider) |
| 54 provider->Set(path, in_value); | 54 provider->Set(path, in_value); |
| 55 } | |
| 56 } | 55 } |
| 57 | 56 |
| 58 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { | 57 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { |
| 59 DCHECK(CalledOnValidThread()); | 58 DCHECK(CalledOnValidThread()); |
| 60 base::FundamentalValue value(in_value); | 59 base::FundamentalValue value(in_value); |
| 61 Set(path, value); | 60 Set(path, value); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void CrosSettings::SetInteger(const std::string& path, int in_value) { | 63 void CrosSettings::SetInteger(const std::string& path, int in_value) { |
| 65 DCHECK(CalledOnValidThread()); | 64 DCHECK(CalledOnValidThread()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 scoped_ptr<base::Value> new_value( | 96 scoped_ptr<base::Value> new_value( |
| 98 old_value ? old_value->DeepCopy() : new base::ListValue()); | 97 old_value ? old_value->DeepCopy() : new base::ListValue()); |
| 99 static_cast<base::ListValue*>(new_value.get())->Remove(*value, NULL); | 98 static_cast<base::ListValue*>(new_value.get())->Remove(*value, NULL); |
| 100 Set(path, *new_value); | 99 Set(path, *new_value); |
| 101 } | 100 } |
| 102 | 101 |
| 103 bool CrosSettings::FindEmailInList(const std::string& path, | 102 bool CrosSettings::FindEmailInList(const std::string& path, |
| 104 const std::string& email) const { | 103 const std::string& email) const { |
| 105 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
| 106 base::StringValue email_value(email); | 105 base::StringValue email_value(email); |
| 107 const base::ListValue* value = | 106 const base::ListValue* value( |
| 108 static_cast<const base::ListValue*>(GetPref(path)); | 107 static_cast<const base::ListValue*>(GetPref(path))); |
| 109 if (value) { | 108 if (value) { |
| 110 if (value->Find(email_value) != value->end()) | 109 if (value->Find(email_value) != value->end()) |
| 111 return true; | 110 return true; |
| 112 std::string::size_type at_pos = email.find('@'); | 111 std::string::size_type at_pos = email.find('@'); |
| 113 if (at_pos != std::string::npos) { | 112 if (at_pos != std::string::npos) { |
| 114 base::StringValue wildcarded_value( | 113 base::StringValue wildcarded_value( |
| 115 std::string("*").append(email.substr(at_pos))); | 114 std::string("*").append(email.substr(at_pos))); |
| 116 return value->Find(wildcarded_value) != value->end(); | 115 return value->Find(wildcarded_value) != value->end(); |
| 117 } | 116 } |
| 118 } | 117 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 137 } | 136 } |
| 138 | 137 |
| 139 void CrosSettings::AddSettingsObserver(const char* path, | 138 void CrosSettings::AddSettingsObserver(const char* path, |
| 140 content::NotificationObserver* obs) { | 139 content::NotificationObserver* obs) { |
| 141 DCHECK(path); | 140 DCHECK(path); |
| 142 DCHECK(obs); | 141 DCHECK(obs); |
| 143 DCHECK(CalledOnValidThread()); | 142 DCHECK(CalledOnValidThread()); |
| 144 | 143 |
| 145 if (!GetProvider(std::string(path))) { | 144 if (!GetProvider(std::string(path))) { |
| 146 NOTREACHED() << "Trying to add an observer for an unregistered setting: " | 145 NOTREACHED() << "Trying to add an observer for an unregistered setting: " |
| 147 << path; | 146 << path; |
| 148 return; | 147 return; |
| 149 } | 148 } |
| 150 | 149 |
| 151 // Get the settings observer list associated with the path. | 150 // Get the settings observer list associated with the path. |
| 152 NotificationObserverList* observer_list = NULL; | 151 NotificationObserverList* observer_list = NULL; |
| 153 SettingsObserverMap::iterator observer_iterator = | 152 SettingsObserverMap::iterator observer_iterator = |
| 154 settings_observers_.find(path); | 153 settings_observers_.find(path); |
| 155 if (observer_iterator == settings_observers_.end()) { | 154 if (observer_iterator == settings_observers_.end()) { |
| 156 observer_list = new NotificationObserverList; | 155 observer_list = new NotificationObserverList; |
| 157 settings_observers_[path] = observer_list; | 156 settings_observers_[path] = observer_list; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 171 // Ok, safe to add the pref observer. | 170 // Ok, safe to add the pref observer. |
| 172 observer_list->AddObserver(obs); | 171 observer_list->AddObserver(obs); |
| 173 } | 172 } |
| 174 | 173 |
| 175 void CrosSettings::RemoveSettingsObserver(const char* path, | 174 void CrosSettings::RemoveSettingsObserver(const char* path, |
| 176 content::NotificationObserver* obs) { | 175 content::NotificationObserver* obs) { |
| 177 DCHECK(CalledOnValidThread()); | 176 DCHECK(CalledOnValidThread()); |
| 178 | 177 |
| 179 SettingsObserverMap::iterator observer_iterator = | 178 SettingsObserverMap::iterator observer_iterator = |
| 180 settings_observers_.find(path); | 179 settings_observers_.find(path); |
| 181 if (observer_iterator == settings_observers_.end()) { | 180 if (observer_iterator == settings_observers_.end()) |
| 182 return; | 181 return; |
| 183 } | |
| 184 | 182 |
| 185 NotificationObserverList* observer_list = observer_iterator->second; | 183 NotificationObserverList* observer_list = observer_iterator->second; |
| 186 observer_list->RemoveObserver(obs); | 184 observer_list->RemoveObserver(obs); |
| 187 } | 185 } |
| 188 | 186 |
| 189 CrosSettingsProvider* CrosSettings::GetProvider( | 187 CrosSettingsProvider* CrosSettings::GetProvider( |
| 190 const std::string& path) const { | 188 const std::string& path) const { |
| 191 for (size_t i = 0; i < providers_.size(); ++i) { | 189 for (size_t i = 0; i < providers_.size(); ++i) { |
| 192 if (providers_[i]->HandlesSetting(path)) | 190 if (providers_[i]->HandlesSetting(path)) |
| 193 return providers_[i]; | 191 return providers_[i]; |
| 194 } | 192 } |
| 195 return NULL; | 193 return NULL; |
| 196 } | 194 } |
| 197 | 195 |
| 196 void CrosSettings::ReloadProviders() { |
| 197 for (size_t i = 0; i < providers_.size(); ++i) |
| 198 providers_[i]->Reload(); |
| 199 } |
| 200 |
| 198 const base::Value* CrosSettings::GetPref(const std::string& path) const { | 201 const base::Value* CrosSettings::GetPref(const std::string& path) const { |
| 199 DCHECK(CalledOnValidThread()); | 202 DCHECK(CalledOnValidThread()); |
| 200 CrosSettingsProvider* provider = GetProvider(path); | 203 CrosSettingsProvider* provider = GetProvider(path); |
| 201 if (provider) | 204 if (provider) |
| 202 return provider->Get(path); | 205 return provider->Get(path); |
| 203 NOTREACHED() << path << " preference was not found in the signed settings."; | 206 NOTREACHED() << path << " preference was not found in the signed settings."; |
| 204 return NULL; | 207 return NULL; |
| 205 } | 208 } |
| 206 | 209 |
| 207 bool CrosSettings::GetTrusted(const std::string& path, | 210 bool CrosSettings::GetTrusted(const std::string& path, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 const base::ListValue** out_value) const { | 257 const base::ListValue** out_value) const { |
| 255 DCHECK(CalledOnValidThread()); | 258 DCHECK(CalledOnValidThread()); |
| 256 const base::Value* value = GetPref(path); | 259 const base::Value* value = GetPref(path); |
| 257 if (value) | 260 if (value) |
| 258 return value->GetAsList(out_value); | 261 return value->GetAsList(out_value); |
| 259 return false; | 262 return false; |
| 260 } | 263 } |
| 261 | 264 |
| 262 CrosSettings::CrosSettings() { | 265 CrosSettings::CrosSettings() { |
| 263 AddSettingsProvider(new SystemSettingsProvider()); | 266 AddSettingsProvider(new SystemSettingsProvider()); |
| 264 AddSettingsProvider(new UserCrosSettingsProvider()); | 267 AddSettingsProvider(new DeviceSettingsProvider()); |
| 265 } | 268 } |
| 266 | 269 |
| 267 CrosSettings::~CrosSettings() { | 270 CrosSettings::~CrosSettings() { |
| 268 STLDeleteElements(&providers_); | 271 STLDeleteElements(&providers_); |
| 269 STLDeleteValues(&settings_observers_); | 272 STLDeleteValues(&settings_observers_); |
| 270 } | 273 } |
| 271 | 274 |
| 272 } // namespace chromeos | 275 } // namespace chromeos |
| OLD | NEW |