Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: chrome/browser/ui/webui/options/core_options_handler.cc

Issue 8467012: Refactor proxy handling for ChromeOS to not go through the CrosSettings interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Centralized the proxy change notification. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/ui/webui/options/core_options_handler.h" 5 #include "chrome/browser/ui/webui/options/core_options_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 web_ui_->RegisterMessageCallback("coreOptionsUserMetricsAction", 155 web_ui_->RegisterMessageCallback("coreOptionsUserMetricsAction",
156 base::Bind(&CoreOptionsHandler::HandleUserMetricsAction, 156 base::Bind(&CoreOptionsHandler::HandleUserMetricsAction,
157 base::Unretained(this))); 157 base::Unretained(this)));
158 } 158 }
159 159
160 void CoreOptionsHandler::HandleInitialize(const ListValue* args) { 160 void CoreOptionsHandler::HandleInitialize(const ListValue* args) {
161 DCHECK(handlers_host_); 161 DCHECK(handlers_host_);
162 handlers_host_->InitializeHandlers(); 162 handlers_host_->InitializeHandlers();
163 } 163 }
164 164
165 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { 165 base::Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) {
166 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); 166 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs();
167 167
168 const PrefService::Preference* pref = 168 const PrefService::Preference* pref =
169 pref_service->FindPreference(pref_name.c_str()); 169 pref_service->FindPreference(pref_name.c_str());
170 if (!pref) 170 if (!pref)
171 return Value::CreateNullValue(); 171 return base::Value::CreateNullValue();
172 172
173 return CreateValueForPref(pref, NULL); 173 return CreateValueForPref(pref, NULL);
174 } 174 }
175 175
176 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { 176 void CoreOptionsHandler::ObservePref(const std::string& pref_name) {
177 registrar_.Add(pref_name.c_str(), this); 177 registrar_.Add(pref_name.c_str(), this);
178 } 178 }
179 179
180 void CoreOptionsHandler::SetPref(const std::string& pref_name, 180 void CoreOptionsHandler::SetPref(const std::string& pref_name,
181 const Value* value, 181 const base::Value* value,
182 const std::string& metric) { 182 const std::string& metric) {
183 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); 183 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs();
184 184
185 switch (value->GetType()) { 185 switch (value->GetType()) {
186 case Value::TYPE_BOOLEAN: 186 case base::Value::TYPE_BOOLEAN:
187 case Value::TYPE_INTEGER: 187 case base::Value::TYPE_INTEGER:
188 case Value::TYPE_DOUBLE: 188 case base::Value::TYPE_DOUBLE:
189 case Value::TYPE_STRING: 189 case base::Value::TYPE_STRING:
190 pref_service->Set(pref_name.c_str(), *value); 190 pref_service->Set(pref_name.c_str(), *value);
191 break; 191 break;
192 192
193 default: 193 default:
194 NOTREACHED(); 194 NOTREACHED();
195 return; 195 return;
196 } 196 }
197 197
198 pref_service->ScheduleSavePersistentPrefs(); 198 pref_service->ScheduleSavePersistentPrefs();
199 199
200 ProcessUserMetric(value, metric); 200 ProcessUserMetric(value, metric);
201 } 201 }
202 202
203 void CoreOptionsHandler::ClearPref(const std::string& pref_name, 203 void CoreOptionsHandler::ClearPref(const std::string& pref_name,
204 const std::string& metric) { 204 const std::string& metric) {
205 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); 205 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs();
206 pref_service->ClearPref(pref_name.c_str()); 206 pref_service->ClearPref(pref_name.c_str());
207 pref_service->ScheduleSavePersistentPrefs(); 207 pref_service->ScheduleSavePersistentPrefs();
208 208
209 if (!metric.empty()) 209 if (!metric.empty())
210 UserMetrics::RecordComputedAction(metric); 210 UserMetrics::RecordComputedAction(metric);
211 } 211 }
212 212
213 void CoreOptionsHandler::ProcessUserMetric(const Value* value, 213 void CoreOptionsHandler::ProcessUserMetric(const base::Value* value,
214 const std::string& metric) { 214 const std::string& metric) {
215 if (metric.empty()) 215 if (metric.empty())
216 return; 216 return;
217 217
218 std::string metric_string = metric; 218 std::string metric_string = metric;
219 if (value->IsType(Value::TYPE_BOOLEAN)) { 219 if (value->IsType(base::Value::TYPE_BOOLEAN)) {
220 bool bool_value; 220 bool bool_value;
221 CHECK(value->GetAsBoolean(&bool_value)); 221 CHECK(value->GetAsBoolean(&bool_value));
222 metric_string += bool_value ? "_Enable" : "_Disable"; 222 metric_string += bool_value ? "_Enable" : "_Disable";
223 } 223 }
224 224
225 UserMetrics::RecordComputedAction(metric_string); 225 UserMetrics::RecordComputedAction(metric_string);
226 } 226 }
227 227
228 void CoreOptionsHandler::NotifyPrefChanged( 228 void CoreOptionsHandler::NotifyPrefChanged(
229 const std::string& pref_name, 229 const std::string& pref_name,
230 const std::string& controlling_pref_name) { 230 const std::string& controlling_pref_name) {
231 const PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); 231 const PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs();
232 const PrefService::Preference* pref = 232 const PrefService::Preference* pref =
233 pref_service->FindPreference(pref_name.c_str()); 233 pref_service->FindPreference(pref_name.c_str());
234 if (!pref) 234 if (!pref)
235 return; 235 return;
236 const PrefService::Preference* controlling_pref = 236 const PrefService::Preference* controlling_pref =
237 !controlling_pref_name.empty() ? 237 !controlling_pref_name.empty() ?
238 pref_service->FindPreference(controlling_pref_name.c_str()) : NULL; 238 pref_service->FindPreference(controlling_pref_name.c_str()) : NULL;
239 std::pair<PreferenceCallbackMap::const_iterator, 239 std::pair<PreferenceCallbackMap::const_iterator,
240 PreferenceCallbackMap::const_iterator> range; 240 PreferenceCallbackMap::const_iterator> range;
241 range = pref_callback_map_.equal_range(pref_name); 241 range = pref_callback_map_.equal_range(pref_name);
242 for (PreferenceCallbackMap::const_iterator iter = range.first; 242 for (PreferenceCallbackMap::const_iterator iter = range.first;
243 iter != range.second; ++iter) { 243 iter != range.second; ++iter) {
244 const std::wstring& callback_function = iter->second; 244 const std::wstring& callback_function = iter->second;
245 ListValue result_value; 245 ListValue result_value;
246 result_value.Append(Value::CreateStringValue(pref_name.c_str())); 246 result_value.Append(base::Value::CreateStringValue(pref_name.c_str()));
247 result_value.Append(CreateValueForPref(pref, controlling_pref)); 247 result_value.Append(CreateValueForPref(pref, controlling_pref));
248 web_ui_->CallJavascriptFunction(WideToASCII(callback_function), 248 web_ui_->CallJavascriptFunction(WideToASCII(callback_function),
249 result_value); 249 result_value);
250 } 250 }
251 } 251 }
252 252
253 DictionaryValue* CoreOptionsHandler::CreateValueForPref( 253 DictionaryValue* CoreOptionsHandler::CreateValueForPref(
254 const PrefService::Preference* pref, 254 const PrefService::Preference* pref,
255 const PrefService::Preference* controlling_pref) { 255 const PrefService::Preference* controlling_pref) {
256 DictionaryValue* dict = new DictionaryValue; 256 DictionaryValue* dict = new DictionaryValue;
(...skipping 12 matching lines...) Expand all
269 void CoreOptionsHandler::StopObservingPref(const std::string& path) { 269 void CoreOptionsHandler::StopObservingPref(const std::string& path) {
270 registrar_.Remove(path.c_str(), this); 270 registrar_.Remove(path.c_str(), this);
271 } 271 }
272 272
273 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { 273 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) {
274 // First param is name of callback function, so, there needs to be at least 274 // First param is name of callback function, so, there needs to be at least
275 // one more element for the actual preference identifier. 275 // one more element for the actual preference identifier.
276 DCHECK_GE(static_cast<int>(args->GetSize()), 2); 276 DCHECK_GE(static_cast<int>(args->GetSize()), 2);
277 277
278 // Get callback JS function name. 278 // Get callback JS function name.
279 Value* callback; 279 base::Value* callback;
280 if (!args->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) 280 if (!args->Get(0, &callback) || !callback->IsType(base::Value::TYPE_STRING))
281 return; 281 return;
282 282
283 string16 callback_function; 283 string16 callback_function;
284 if (!callback->GetAsString(&callback_function)) 284 if (!callback->GetAsString(&callback_function))
285 return; 285 return;
286 286
287 // Get the list of name for prefs to build the response dictionary. 287 // Get the list of name for prefs to build the response dictionary.
288 DictionaryValue result_value; 288 DictionaryValue result_value;
289 Value* list_member; 289 base::Value* list_member;
290 290
291 for (size_t i = 1; i < args->GetSize(); i++) { 291 for (size_t i = 1; i < args->GetSize(); i++) {
292 if (!args->Get(i, &list_member)) 292 if (!args->Get(i, &list_member))
293 break; 293 break;
294 294
295 if (!list_member->IsType(Value::TYPE_STRING)) 295 if (!list_member->IsType(base::Value::TYPE_STRING))
296 continue; 296 continue;
297 297
298 std::string pref_name; 298 std::string pref_name;
299 if (!list_member->GetAsString(&pref_name)) 299 if (!list_member->GetAsString(&pref_name))
300 continue; 300 continue;
301 301
302 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); 302 result_value.Set(pref_name.c_str(), FetchPref(pref_name));
303 } 303 }
304 web_ui_->CallJavascriptFunction(UTF16ToASCII(callback_function), 304 web_ui_->CallJavascriptFunction(UTF16ToASCII(callback_function),
305 result_value); 305 result_value);
306 } 306 }
307 307
308 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) { 308 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) {
309 // First param is name is JS callback function name, the rest are pref 309 // First param is name is JS callback function name, the rest are pref
310 // identifiers that we are observing. 310 // identifiers that we are observing.
311 DCHECK_GE(static_cast<int>(args->GetSize()), 2); 311 DCHECK_GE(static_cast<int>(args->GetSize()), 2);
312 312
313 // Get preference change callback function name. 313 // Get preference change callback function name.
314 string16 callback_func_name; 314 string16 callback_func_name;
315 if (!args->GetString(0, &callback_func_name)) 315 if (!args->GetString(0, &callback_func_name))
316 return; 316 return;
317 317
318 // Get all other parameters - pref identifiers. 318 // Get all other parameters - pref identifiers.
319 for (size_t i = 1; i < args->GetSize(); i++) { 319 for (size_t i = 1; i < args->GetSize(); i++) {
320 Value* list_member; 320 base::Value* list_member;
321 if (!args->Get(i, &list_member)) 321 if (!args->Get(i, &list_member))
322 break; 322 break;
323 323
324 // Just ignore bad pref identifiers for now. 324 // Just ignore bad pref identifiers for now.
325 std::string pref_name; 325 std::string pref_name;
326 if (!list_member->IsType(Value::TYPE_STRING) || 326 if (!list_member->IsType(base::Value::TYPE_STRING) ||
327 !list_member->GetAsString(&pref_name)) 327 !list_member->GetAsString(&pref_name))
328 continue; 328 continue;
329 329
330 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end()) 330 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end())
331 ObservePref(pref_name); 331 ObservePref(pref_name);
332 332
333 pref_callback_map_.insert( 333 pref_callback_map_.insert(
334 PreferenceCallbackMap::value_type(pref_name, 334 PreferenceCallbackMap::value_type(pref_name,
335 UTF16ToWideHack(callback_func_name))); 335 UTF16ToWideHack(callback_func_name)));
336 } 336 }
(...skipping 23 matching lines...) Expand all
360 HandleSetPref(args, TYPE_LIST); 360 HandleSetPref(args, TYPE_LIST);
361 } 361 }
362 362
363 void CoreOptionsHandler::HandleSetPref(const ListValue* args, PrefType type) { 363 void CoreOptionsHandler::HandleSetPref(const ListValue* args, PrefType type) {
364 DCHECK_GT(static_cast<int>(args->GetSize()), 1); 364 DCHECK_GT(static_cast<int>(args->GetSize()), 1);
365 365
366 std::string pref_name; 366 std::string pref_name;
367 if (!args->GetString(0, &pref_name)) 367 if (!args->GetString(0, &pref_name))
368 return; 368 return;
369 369
370 Value* value; 370 base::Value* value;
371 if (!args->Get(1, &value)) 371 if (!args->Get(1, &value))
372 return; 372 return;
373 373
374 scoped_ptr<Value> temp_value; 374 scoped_ptr<base::Value> temp_value;
375 375
376 switch (type) { 376 switch (type) {
377 case TYPE_BOOLEAN: 377 case TYPE_BOOLEAN:
378 CHECK_EQ(base::Value::TYPE_BOOLEAN, value->GetType()); 378 CHECK_EQ(base::Value::TYPE_BOOLEAN, value->GetType());
379 break; 379 break;
380 case TYPE_INTEGER: { 380 case TYPE_INTEGER: {
381 // In JS all numbers are doubles. 381 // In JS all numbers are doubles.
382 double double_value; 382 double double_value;
383 CHECK(value->GetAsDouble(&double_value)); 383 CHECK(value->GetAsDouble(&double_value));
384 int int_value = static_cast<int>(double_value); 384 int int_value = static_cast<int>(double_value);
385 temp_value.reset(Value::CreateIntegerValue(int_value)); 385 temp_value.reset(base::Value::CreateIntegerValue(int_value));
386 value = temp_value.get(); 386 value = temp_value.get();
387 break; 387 break;
388 } 388 }
389 case TYPE_DOUBLE: 389 case TYPE_DOUBLE:
390 CHECK_EQ(base::Value::TYPE_DOUBLE, value->GetType()); 390 CHECK_EQ(base::Value::TYPE_DOUBLE, value->GetType());
391 break; 391 break;
392 case TYPE_STRING: 392 case TYPE_STRING:
393 CHECK_EQ(base::Value::TYPE_STRING, value->GetType()); 393 CHECK_EQ(base::Value::TYPE_STRING, value->GetType());
394 break; 394 break;
395 case TYPE_URL: { 395 case TYPE_URL: {
396 std::string original; 396 std::string original;
397 CHECK(value->GetAsString(&original)); 397 CHECK(value->GetAsString(&original));
398 GURL fixed = URLFixerUpper::FixupURL(original, std::string()); 398 GURL fixed = URLFixerUpper::FixupURL(original, std::string());
399 temp_value.reset(Value::CreateStringValue(fixed.spec())); 399 temp_value.reset(base::Value::CreateStringValue(fixed.spec()));
400 value = temp_value.get(); 400 value = temp_value.get();
401 break; 401 break;
402 } 402 }
403 case TYPE_LIST: { 403 case TYPE_LIST: {
404 // In case we have a List pref we got a JSON string. 404 // In case we have a List pref we got a JSON string.
405 std::string json_string; 405 std::string json_string;
406 CHECK(value->GetAsString(&json_string)); 406 CHECK(value->GetAsString(&json_string));
407 temp_value.reset( 407 temp_value.reset(
408 base::JSONReader().JsonToValue(json_string, 408 base::JSONReader().JsonToValue(json_string,
409 false, // no check_root 409 false, // no check_root
(...skipping 26 matching lines...) Expand all
436 ClearPref(pref_name, metric); 436 ClearPref(pref_name, metric);
437 } 437 }
438 438
439 void CoreOptionsHandler::HandleUserMetricsAction(const ListValue* args) { 439 void CoreOptionsHandler::HandleUserMetricsAction(const ListValue* args) {
440 std::string metric = UTF16ToUTF8(ExtractStringValue(args)); 440 std::string metric = UTF16ToUTF8(ExtractStringValue(args));
441 if (!metric.empty()) 441 if (!metric.empty())
442 UserMetrics::RecordComputedAction(metric); 442 UserMetrics::RecordComputedAction(metric);
443 } 443 }
444 444
445 void CoreOptionsHandler::UpdateClearPluginLSOData() { 445 void CoreOptionsHandler::UpdateClearPluginLSOData() {
446 scoped_ptr<Value> enabled( 446 scoped_ptr<base::Value> enabled(
447 Value::CreateBooleanValue(clear_plugin_lso_data_enabled_.GetValue())); 447 base::Value::CreateBooleanValue(
448 clear_plugin_lso_data_enabled_.GetValue()));
448 web_ui_->CallJavascriptFunction( 449 web_ui_->CallJavascriptFunction(
449 "OptionsPage.setClearPluginLSODataEnabled", *enabled); 450 "OptionsPage.setClearPluginLSODataEnabled", *enabled);
450 } 451 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698