| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/singleton.h" | 7 #include "base/singleton.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/cros_settings_provider.h" | 10 #include "chrome/browser/chromeos/cros_settings_provider.h" |
| 11 #include "chrome/browser/chromeos/cros_settings_provider_user.h" | 11 #include "chrome/browser/chromeos/cros_settings_provider_user.h" |
| 12 #include "chrome/common/notification_service.h" |
| 12 | 13 |
| 13 namespace chromeos { | 14 namespace chromeos { |
| 14 | 15 |
| 15 CrosSettings* CrosSettings::Get() { | 16 CrosSettings* CrosSettings::Get() { |
| 16 // TODO(xiyaun): Use real stuff when underlying libcros is ready. | 17 // TODO(xiyaun): Use real stuff when underlying libcros is ready. |
| 17 return Singleton<CrosSettings>::get(); | 18 return Singleton<CrosSettings>::get(); |
| 18 } | 19 } |
| 19 | 20 |
| 20 bool CrosSettings::IsCrosSettings(const std::string& path) { | 21 bool CrosSettings::IsCrosSettings(const std::string& path) { |
| 21 return StartsWithASCII(path, kCrosSettingsPrefix, true); | 22 return StartsWithASCII(path, kCrosSettingsPrefix, true); |
| 22 } | 23 } |
| 23 | 24 |
| 25 void CrosSettings::FireObservers(const char* path) { |
| 26 DCHECK(CalledOnValidThread()); |
| 27 std::string path_str(path); |
| 28 SettingsObserverMap::iterator observer_iterator = |
| 29 settings_observers_.find(path_str); |
| 30 if (observer_iterator == settings_observers_.end()) |
| 31 return; |
| 32 |
| 33 NotificationObserverList::Iterator it(*(observer_iterator->second)); |
| 34 NotificationObserver* observer; |
| 35 while ((observer = it.GetNext()) != NULL) { |
| 36 observer->Observe(NotificationType::SYSTEM_SETTING_CHANGED, |
| 37 Source<CrosSettings>(this), |
| 38 Details<std::string>(&path_str)); |
| 39 } |
| 40 } |
| 41 |
| 24 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { | 42 void CrosSettings::SetBoolean(const std::string& path, bool in_value) { |
| 43 DCHECK(CalledOnValidThread()); |
| 25 Set(path, Value::CreateBooleanValue(in_value)); | 44 Set(path, Value::CreateBooleanValue(in_value)); |
| 26 } | 45 } |
| 27 | 46 |
| 28 void CrosSettings::SetInteger(const std::string& path, int in_value) { | 47 void CrosSettings::SetInteger(const std::string& path, int in_value) { |
| 48 DCHECK(CalledOnValidThread()); |
| 29 Set(path, Value::CreateIntegerValue(in_value)); | 49 Set(path, Value::CreateIntegerValue(in_value)); |
| 30 } | 50 } |
| 31 | 51 |
| 32 void CrosSettings::SetReal(const std::string& path, double in_value) { | 52 void CrosSettings::SetReal(const std::string& path, double in_value) { |
| 53 DCHECK(CalledOnValidThread()); |
| 33 Set(path, Value::CreateRealValue(in_value)); | 54 Set(path, Value::CreateRealValue(in_value)); |
| 34 } | 55 } |
| 35 | 56 |
| 36 void CrosSettings::SetString(const std::string& path, | 57 void CrosSettings::SetString(const std::string& path, |
| 37 const std::string& in_value) { | 58 const std::string& in_value) { |
| 59 DCHECK(CalledOnValidThread()); |
| 38 Set(path, Value::CreateStringValue(in_value)); | 60 Set(path, Value::CreateStringValue(in_value)); |
| 39 } | 61 } |
| 40 | 62 |
| 41 bool CrosSettings::GetBoolean(const std::string& path, | 63 bool CrosSettings::GetBoolean(const std::string& path, |
| 42 bool* bool_value) const { | 64 bool* bool_value) const { |
| 65 DCHECK(CalledOnValidThread()); |
| 43 Value* value; | 66 Value* value; |
| 44 if (!Get(path, &value)) | 67 if (!Get(path, &value)) |
| 45 return false; | 68 return false; |
| 46 | 69 |
| 47 return value->GetAsBoolean(bool_value); | 70 return value->GetAsBoolean(bool_value); |
| 48 } | 71 } |
| 49 | 72 |
| 50 bool CrosSettings::AddProvider(CrosSettingsProvider* provider) { | 73 bool CrosSettings::AddSettingsProvider(CrosSettingsProvider* provider) { |
| 74 DCHECK(CalledOnValidThread()); |
| 51 providers_.push_back(provider); | 75 providers_.push_back(provider); |
| 52 return true; | 76 return true; |
| 53 } | 77 } |
| 54 | 78 |
| 55 bool CrosSettings::RemoveProvider(CrosSettingsProvider* provider) { | 79 bool CrosSettings::RemoveSettingsProvider(CrosSettingsProvider* provider) { |
| 80 DCHECK(CalledOnValidThread()); |
| 56 std::vector<CrosSettingsProvider*>::iterator it = | 81 std::vector<CrosSettingsProvider*>::iterator it = |
| 57 std::find(providers_.begin(), providers_.end(), provider); | 82 std::find(providers_.begin(), providers_.end(), provider); |
| 58 if (it != providers_.end()) { | 83 if (it != providers_.end()) { |
| 59 providers_.erase(it); | 84 providers_.erase(it); |
| 60 return true; | 85 return true; |
| 61 } | 86 } |
| 62 return false; | 87 return false; |
| 63 } | 88 } |
| 64 | 89 |
| 90 void CrosSettings::AddSettingsObserver(const char* path, |
| 91 NotificationObserver* obs) { |
| 92 DCHECK(path); |
| 93 DCHECK(obs); |
| 94 DCHECK(CalledOnValidThread()); |
| 95 |
| 96 if (!GetProvider(std::string(path))) { |
| 97 NOTREACHED() << "Trying to add an observer for an unregistered setting: " |
| 98 << path; |
| 99 return; |
| 100 } |
| 101 |
| 102 // Get the settings observer list associated with the path. |
| 103 NotificationObserverList* observer_list = NULL; |
| 104 SettingsObserverMap::iterator observer_iterator = |
| 105 settings_observers_.find(path); |
| 106 if (observer_iterator == settings_observers_.end()) { |
| 107 observer_list = new NotificationObserverList; |
| 108 settings_observers_[path] = observer_list; |
| 109 } else { |
| 110 observer_list = observer_iterator->second; |
| 111 } |
| 112 |
| 113 // Verify that this observer doesn't already exist. |
| 114 NotificationObserverList::Iterator it(*observer_list); |
| 115 NotificationObserver* existing_obs; |
| 116 while ((existing_obs = it.GetNext()) != NULL) { |
| 117 DCHECK(existing_obs != obs) << path << " observer already registered"; |
| 118 if (existing_obs == obs) |
| 119 return; |
| 120 } |
| 121 |
| 122 // Ok, safe to add the pref observer. |
| 123 observer_list->AddObserver(obs); |
| 124 } |
| 125 |
| 126 void CrosSettings::RemoveSettingsObserver(const char* path, |
| 127 NotificationObserver* obs) { |
| 128 DCHECK(CalledOnValidThread()); |
| 129 |
| 130 SettingsObserverMap::iterator observer_iterator = |
| 131 settings_observers_.find(path); |
| 132 if (observer_iterator == settings_observers_.end()) { |
| 133 return; |
| 134 } |
| 135 |
| 136 NotificationObserverList* observer_list = observer_iterator->second; |
| 137 observer_list->RemoveObserver(obs); |
| 138 } |
| 139 |
| 65 CrosSettingsProvider* CrosSettings::GetProvider( | 140 CrosSettingsProvider* CrosSettings::GetProvider( |
| 66 const std::string& path) const { | 141 const std::string& path) const { |
| 67 for (size_t i = 0; i < providers_.size(); ++i) { | 142 for (size_t i = 0; i < providers_.size(); ++i) { |
| 68 if (providers_[i]->HandlesSetting(path)){ | 143 if (providers_[i]->HandlesSetting(path)){ |
| 69 return providers_[i]; | 144 return providers_[i]; |
| 70 } | 145 } |
| 71 } | 146 } |
| 72 return NULL; | 147 return NULL; |
| 73 } | 148 } |
| 74 | 149 |
| 75 void CrosSettings::Set(const std::string& path, Value* in_value) { | 150 void CrosSettings::Set(const std::string& path, Value* in_value) { |
| 151 DCHECK(CalledOnValidThread()); |
| 76 CrosSettingsProvider* provider; | 152 CrosSettingsProvider* provider; |
| 77 provider = GetProvider(path); | 153 provider = GetProvider(path); |
| 78 if (provider) { | 154 if (provider) { |
| 79 provider->Set(path, in_value); | 155 provider->Set(path, in_value); |
| 80 } | 156 } |
| 81 } | 157 } |
| 82 | 158 |
| 83 bool CrosSettings::Get(const std::string& path, Value** out_value) const { | 159 bool CrosSettings::Get(const std::string& path, Value** out_value) const { |
| 160 DCHECK(CalledOnValidThread()); |
| 84 CrosSettingsProvider* provider; | 161 CrosSettingsProvider* provider; |
| 85 provider = GetProvider(path); | 162 provider = GetProvider(path); |
| 86 if (provider) { | 163 if (provider) { |
| 87 return provider->Get(path, out_value); | 164 return provider->Get(path, out_value); |
| 88 } | 165 } |
| 89 return false; | 166 return false; |
| 90 } | 167 } |
| 91 | 168 |
| 92 bool CrosSettings::GetInteger(const std::string& path, | 169 bool CrosSettings::GetInteger(const std::string& path, |
| 93 int* out_value) const { | 170 int* out_value) const { |
| 171 DCHECK(CalledOnValidThread()); |
| 94 Value* value; | 172 Value* value; |
| 95 if (!Get(path, &value)) | 173 if (!Get(path, &value)) |
| 96 return false; | 174 return false; |
| 97 | 175 |
| 98 return value->GetAsInteger(out_value); | 176 return value->GetAsInteger(out_value); |
| 99 } | 177 } |
| 100 | 178 |
| 101 bool CrosSettings::GetReal(const std::string& path, | 179 bool CrosSettings::GetReal(const std::string& path, |
| 102 double* out_value) const { | 180 double* out_value) const { |
| 181 DCHECK(CalledOnValidThread()); |
| 103 Value* value; | 182 Value* value; |
| 104 if (!Get(path, &value)) | 183 if (!Get(path, &value)) |
| 105 return false; | 184 return false; |
| 106 | 185 |
| 107 return value->GetAsReal(out_value); | 186 return value->GetAsReal(out_value); |
| 108 } | 187 } |
| 109 | 188 |
| 110 bool CrosSettings::GetString(const std::string& path, | 189 bool CrosSettings::GetString(const std::string& path, |
| 111 std::string* out_value) const { | 190 std::string* out_value) const { |
| 191 DCHECK(CalledOnValidThread()); |
| 112 Value* value; | 192 Value* value; |
| 113 if (!Get(path, &value)) | 193 if (!Get(path, &value)) |
| 114 return false; | 194 return false; |
| 115 | 195 |
| 116 return value->GetAsString(out_value); | 196 return value->GetAsString(out_value); |
| 117 } | 197 } |
| 118 | 198 |
| 119 CrosSettings::CrosSettings() { | 199 CrosSettings::CrosSettings() { |
| 120 UserCrosSettingsProvider* user = new UserCrosSettingsProvider(); | |
| 121 AddProvider(user); | |
| 122 } | 200 } |
| 123 | 201 |
| 124 CrosSettings::~CrosSettings() { | 202 CrosSettings::~CrosSettings() { |
| 125 for (size_t i = 0; i < providers_.size(); ++i) { | 203 DCHECK(!providers_.size()); |
| 126 delete providers_[i]; | |
| 127 } | |
| 128 } | 204 } |
| 129 | 205 |
| 130 } // namespace chromeos | 206 } // namespace chromeos |
| OLD | NEW |