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/dom_ui/options/core_options_handler.h" | 5 #include "chrome/browser/dom_ui/options/core_options_handler.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 dom_ui_->RegisterMessageCallback("observePrefs", | 110 dom_ui_->RegisterMessageCallback("observePrefs", |
111 NewCallback(this, &CoreOptionsHandler::HandleObservePrefs)); | 111 NewCallback(this, &CoreOptionsHandler::HandleObservePrefs)); |
112 dom_ui_->RegisterMessageCallback("setBooleanPref", | 112 dom_ui_->RegisterMessageCallback("setBooleanPref", |
113 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); | 113 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); |
114 dom_ui_->RegisterMessageCallback("setIntegerPref", | 114 dom_ui_->RegisterMessageCallback("setIntegerPref", |
115 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); | 115 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); |
116 dom_ui_->RegisterMessageCallback("setStringPref", | 116 dom_ui_->RegisterMessageCallback("setStringPref", |
117 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); | 117 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); |
118 dom_ui_->RegisterMessageCallback("setObjectPref", | 118 dom_ui_->RegisterMessageCallback("setObjectPref", |
119 NewCallback(this, &CoreOptionsHandler::HandleSetObjectPref)); | 119 NewCallback(this, &CoreOptionsHandler::HandleSetObjectPref)); |
| 120 dom_ui_->RegisterMessageCallback("clearPref", |
| 121 NewCallback(this, &CoreOptionsHandler::HandleClearPref)); |
120 dom_ui_->RegisterMessageCallback("coreOptionsUserMetricsAction", | 122 dom_ui_->RegisterMessageCallback("coreOptionsUserMetricsAction", |
121 NewCallback(this, &CoreOptionsHandler::HandleUserMetricsAction)); | 123 NewCallback(this, &CoreOptionsHandler::HandleUserMetricsAction)); |
122 } | 124 } |
123 | 125 |
124 void CoreOptionsHandler::HandleInitialize(const ListValue* args) { | 126 void CoreOptionsHandler::HandleInitialize(const ListValue* args) { |
125 static_cast<OptionsUI*>(dom_ui_)->InitializeHandlers(); | 127 static_cast<OptionsUI*>(dom_ui_)->InitializeHandlers(); |
126 } | 128 } |
127 | 129 |
128 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { | 130 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { |
129 DCHECK(dom_ui_); | |
130 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 131 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
131 | 132 |
132 const PrefService::Preference* pref = | 133 const PrefService::Preference* pref = |
133 pref_service->FindPreference(pref_name.c_str()); | 134 pref_service->FindPreference(pref_name.c_str()); |
134 | 135 |
135 Value* return_value; | 136 Value* return_value; |
136 if (pref) { | 137 if (pref) { |
137 DictionaryValue* dict = new DictionaryValue; | 138 DictionaryValue* dict = new DictionaryValue; |
138 dict->Set("value", pref->GetValue()->DeepCopy()); | 139 dict->Set("value", pref->GetValue()->DeepCopy()); |
139 dict->SetBoolean("managed", pref->IsManaged()); | 140 dict->SetBoolean("managed", pref->IsManaged()); |
140 return_value = dict; | 141 return_value = dict; |
141 } else { | 142 } else { |
142 return_value = Value::CreateNullValue(); | 143 return_value = Value::CreateNullValue(); |
143 } | 144 } |
144 return return_value; | 145 return return_value; |
145 } | 146 } |
146 | 147 |
147 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { | 148 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { |
148 registrar_.Add(pref_name.c_str(), this); | 149 registrar_.Add(pref_name.c_str(), this); |
149 } | 150 } |
150 | 151 |
151 void CoreOptionsHandler::SetPref(const std::string& pref_name, | 152 void CoreOptionsHandler::SetPref(const std::string& pref_name, |
152 Value::ValueType pref_type, | 153 Value::ValueType pref_type, |
153 const std::string& value_string, | 154 const std::string& value_string, |
154 const std::string& metric) { | 155 const std::string& metric) { |
155 DCHECK(dom_ui_); | |
156 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 156 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
157 | 157 |
158 switch (pref_type) { | 158 switch (pref_type) { |
159 case Value::TYPE_BOOLEAN: | 159 case Value::TYPE_BOOLEAN: |
160 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); | 160 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); |
161 break; | 161 break; |
162 case Value::TYPE_INTEGER: | 162 case Value::TYPE_INTEGER: |
163 int int_value; | 163 int int_value; |
164 if (base::StringToInt(value_string, &int_value)) | 164 if (base::StringToInt(value_string, &int_value)) |
165 pref_service->SetInteger(pref_name.c_str(), int_value); | 165 pref_service->SetInteger(pref_name.c_str(), int_value); |
166 break; | 166 break; |
167 case Value::TYPE_STRING: | 167 case Value::TYPE_STRING: |
168 pref_service->SetString(pref_name.c_str(), value_string); | 168 pref_service->SetString(pref_name.c_str(), value_string); |
169 break; | 169 break; |
170 default: | 170 default: |
171 NOTREACHED(); | 171 NOTREACHED(); |
172 return; | 172 return; |
173 } | 173 } |
174 | 174 |
175 pref_service->ScheduleSavePersistentPrefs(); | 175 pref_service->ScheduleSavePersistentPrefs(); |
176 ProcessUserMetric(pref_type, value_string, metric); | 176 ProcessUserMetric(pref_type, value_string, metric); |
177 } | 177 } |
178 | 178 |
| 179 void CoreOptionsHandler::ClearPref(const std::string& pref_name, |
| 180 const std::string& metric) { |
| 181 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 182 pref_service->ClearPref(pref_name.c_str()); |
| 183 pref_service->ScheduleSavePersistentPrefs(); |
| 184 |
| 185 if (!metric.empty()) |
| 186 UserMetricsRecordAction(UserMetricsAction(metric.c_str())); |
| 187 } |
| 188 |
179 void CoreOptionsHandler::ProcessUserMetric(Value::ValueType pref_type, | 189 void CoreOptionsHandler::ProcessUserMetric(Value::ValueType pref_type, |
180 const std::string& value_string, | 190 const std::string& value_string, |
181 const std::string& metric) { | 191 const std::string& metric) { |
182 if (metric.empty()) | 192 if (metric.empty()) |
183 return; | 193 return; |
184 | 194 |
185 std::string metric_string = metric; | 195 std::string metric_string = metric; |
186 if (pref_type == Value::TYPE_BOOLEAN) | 196 if (pref_type == Value::TYPE_BOOLEAN) |
187 metric_string += (value_string == "true" ? "_Enable" : "_Disable"); | 197 metric_string += (value_string == "true" ? "_Enable" : "_Disable"); |
188 | 198 |
189 UserMetricsRecordAction(UserMetricsAction(metric_string.c_str())); | 199 UserMetricsRecordAction(UserMetricsAction(metric_string.c_str())); |
190 } | 200 } |
191 | 201 |
192 void CoreOptionsHandler::StopObservingPref(const std::string& path) { | 202 void CoreOptionsHandler::StopObservingPref(const std::string& path) { |
193 registrar_.Remove(path.c_str(), this); | 203 registrar_.Remove(path.c_str(), this); |
194 } | 204 } |
195 | 205 |
196 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { | 206 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { |
197 // First param is name of callback function, so, there needs to be at least | 207 // First param is name of callback function, so, there needs to be at least |
198 // one more element for the actual preference identifier. | 208 // one more element for the actual preference identifier. |
199 const size_t kMinFetchPrefsParamCount = 2; | 209 DCHECK_GE(static_cast<int>(args->GetSize()), 2); |
200 if (args->GetSize() < kMinFetchPrefsParamCount) | |
201 return; | |
202 | 210 |
203 // Get callback JS function name. | 211 // Get callback JS function name. |
204 Value* callback; | 212 Value* callback; |
205 if (!args->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) | 213 if (!args->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) |
206 return; | 214 return; |
207 | 215 |
208 string16 callback_function; | 216 string16 callback_function; |
209 if (!callback->GetAsString(&callback_function)) | 217 if (!callback->GetAsString(&callback_function)) |
210 return; | 218 return; |
211 | 219 |
(...skipping 14 matching lines...) Expand all Loading... |
226 | 234 |
227 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); | 235 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); |
228 } | 236 } |
229 dom_ui_->CallJavascriptFunction(UTF16ToWideHack(callback_function).c_str(), | 237 dom_ui_->CallJavascriptFunction(UTF16ToWideHack(callback_function).c_str(), |
230 result_value); | 238 result_value); |
231 } | 239 } |
232 | 240 |
233 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) { | 241 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) { |
234 // First param is name is JS callback function name, the rest are pref | 242 // First param is name is JS callback function name, the rest are pref |
235 // identifiers that we are observing. | 243 // identifiers that we are observing. |
236 const size_t kMinObservePrefsParamCount = 2; | 244 DCHECK_GE(static_cast<int>(args->GetSize()), 2); |
237 if (args->GetSize() < kMinObservePrefsParamCount) | |
238 return; | |
239 | 245 |
240 // Get preference change callback function name. | 246 // Get preference change callback function name. |
241 string16 callback_func_name; | 247 string16 callback_func_name; |
242 if (!args->GetString(0, &callback_func_name)) | 248 if (!args->GetString(0, &callback_func_name)) |
243 return; | 249 return; |
244 | 250 |
245 // Get all other parameters - pref identifiers. | 251 // Get all other parameters - pref identifiers. |
246 for (size_t i = 1; i < args->GetSize(); i++) { | 252 for (size_t i = 1; i < args->GetSize(); i++) { |
247 Value* list_member; | 253 Value* list_member; |
248 if (!args->Get(i, &list_member)) | 254 if (!args->Get(i, &list_member)) |
(...skipping 25 matching lines...) Expand all Loading... |
274 void CoreOptionsHandler::HandleSetStringPref(const ListValue* args) { | 280 void CoreOptionsHandler::HandleSetStringPref(const ListValue* args) { |
275 HandleSetPref(args, Value::TYPE_STRING); | 281 HandleSetPref(args, Value::TYPE_STRING); |
276 } | 282 } |
277 | 283 |
278 void CoreOptionsHandler::HandleSetObjectPref(const ListValue* args) { | 284 void CoreOptionsHandler::HandleSetObjectPref(const ListValue* args) { |
279 HandleSetPref(args, Value::TYPE_NULL); | 285 HandleSetPref(args, Value::TYPE_NULL); |
280 } | 286 } |
281 | 287 |
282 void CoreOptionsHandler::HandleSetPref(const ListValue* args, | 288 void CoreOptionsHandler::HandleSetPref(const ListValue* args, |
283 Value::ValueType type) { | 289 Value::ValueType type) { |
284 if (args->GetSize() < 2) | 290 DCHECK_GT(static_cast<int>(args->GetSize()), 1); |
285 return; | |
286 | 291 |
287 std::string pref_name; | 292 std::string pref_name; |
288 if (!args->GetString(0, &pref_name)) | 293 if (!args->GetString(0, &pref_name)) |
289 return; | 294 return; |
290 | 295 |
291 std::string value_string; | 296 std::string value_string; |
292 if (!args->GetString(1, &value_string)) | 297 if (!args->GetString(1, &value_string)) |
293 return; | 298 return; |
294 | 299 |
295 std::string metric; | 300 std::string metric; |
296 if (args->GetSize() > 2) | 301 if (args->GetSize() > 2) |
297 args->GetString(2, &metric); | 302 args->GetString(2, &metric); |
298 | 303 |
299 SetPref(pref_name, type, value_string, metric); | 304 SetPref(pref_name, type, value_string, metric); |
300 } | 305 } |
301 | 306 |
| 307 void CoreOptionsHandler::HandleClearPref(const ListValue* args) { |
| 308 DCHECK_GT(static_cast<int>(args->GetSize()), 0); |
| 309 |
| 310 std::string pref_name; |
| 311 if (!args->GetString(0, &pref_name)) |
| 312 return; |
| 313 |
| 314 std::string metric; |
| 315 if (args->GetSize() > 1) |
| 316 args->GetString(1, &metric); |
| 317 |
| 318 ClearPref(pref_name, metric); |
| 319 } |
| 320 |
302 void CoreOptionsHandler::HandleUserMetricsAction(const ListValue* args) { | 321 void CoreOptionsHandler::HandleUserMetricsAction(const ListValue* args) { |
303 std::string metric = WideToUTF8(ExtractStringValue(args)); | 322 std::string metric = WideToUTF8(ExtractStringValue(args)); |
304 if (!metric.empty()) | 323 if (!metric.empty()) |
305 UserMetricsRecordAction(UserMetricsAction(metric.c_str())); | 324 UserMetricsRecordAction(UserMetricsAction(metric.c_str())); |
306 } | 325 } |
307 | 326 |
308 void CoreOptionsHandler::NotifyPrefChanged(const std::string* pref_name) { | 327 void CoreOptionsHandler::NotifyPrefChanged(const std::string* pref_name) { |
309 DCHECK(pref_name); | |
310 DCHECK(dom_ui_); | |
311 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 328 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
312 const PrefService::Preference* pref = | 329 const PrefService::Preference* pref = |
313 pref_service->FindPreference(pref_name->c_str()); | 330 pref_service->FindPreference(pref_name->c_str()); |
314 if (pref) { | 331 if (pref) { |
315 for (PreferenceCallbackMap::const_iterator iter = | 332 for (PreferenceCallbackMap::const_iterator iter = |
316 pref_callback_map_.find(*pref_name); | 333 pref_callback_map_.find(*pref_name); |
317 iter != pref_callback_map_.end(); ++iter) { | 334 iter != pref_callback_map_.end(); ++iter) { |
318 const std::wstring& callback_function = iter->second; | 335 const std::wstring& callback_function = iter->second; |
319 ListValue result_value; | 336 ListValue result_value; |
320 result_value.Append(Value::CreateStringValue(pref_name->c_str())); | 337 result_value.Append(Value::CreateStringValue(pref_name->c_str())); |
321 | 338 |
322 DictionaryValue* dict = new DictionaryValue; | 339 DictionaryValue* dict = new DictionaryValue; |
323 dict->Set("value", pref->GetValue()->DeepCopy()); | 340 dict->Set("value", pref->GetValue()->DeepCopy()); |
324 dict->SetBoolean("managed", pref->IsManaged()); | 341 dict->SetBoolean("managed", pref->IsManaged()); |
325 result_value.Append(dict); | 342 result_value.Append(dict); |
326 | 343 |
327 dom_ui_->CallJavascriptFunction(callback_function, result_value); | 344 dom_ui_->CallJavascriptFunction(callback_function, result_value); |
328 } | 345 } |
329 } | 346 } |
330 } | 347 } |
331 | 348 |
OLD | NEW |