Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / gesture_config_ui.cc
bloba84cf66fac4d83cb870dfe62db3cec17cd0869a6
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.
5 #include "chrome/browser/ui/webui/gesture_config_ui.h"
7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/url_constants.h"
13 #include "content/public/browser/web_ui.h"
14 #include "content/public/browser/web_ui_data_source.h"
15 #include "grit/browser_resources.h"
17 /**
18 * WebUI for configuring 'gesture.*' preference values used by
19 * Chrome's gesture recognition system.
21 GestureConfigUI::GestureConfigUI(content::WebUI* web_ui)
22 : content::WebUIController(web_ui) {
23 // Set up the chrome://gesture-config source.
24 content::WebUIDataSource* html_source =
25 content::WebUIDataSource::Create(chrome::kChromeUIGestureConfigHost);
27 // Register callback handlers.
28 web_ui->RegisterMessageCallback(
29 "updatePreferenceValue",
30 base::Bind(&GestureConfigUI::UpdatePreferenceValue,
31 base::Unretained(this)));
32 web_ui->RegisterMessageCallback(
33 "resetPreferenceValue",
34 base::Bind(&GestureConfigUI::ResetPreferenceValue,
35 base::Unretained(this)));
36 web_ui->RegisterMessageCallback(
37 "setPreferenceValue",
38 base::Bind(&GestureConfigUI::SetPreferenceValue,
39 base::Unretained(this)));
41 // Add required resources.
42 html_source->AddResourcePath("gesture_config.css", IDR_GESTURE_CONFIG_CSS);
43 html_source->AddResourcePath("gesture_config.js", IDR_GESTURE_CONFIG_JS);
44 html_source->SetDefaultResource(IDR_GESTURE_CONFIG_HTML);
46 Profile* profile = Profile::FromWebUI(web_ui);
47 content::WebUIDataSource::Add(profile, html_source);
50 GestureConfigUI::~GestureConfigUI() {
53 void GestureConfigUI::UpdatePreferenceValue(const base::ListValue* args) {
54 std::string pref_name;
56 if (!args->GetString(0, &pref_name)) return;
58 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
59 const PrefService::Preference* pref =
60 prefs->FindPreference(pref_name.c_str());
62 base::StringValue js_pref_name(pref_name);
63 base::FundamentalValue js_pref_value(prefs->GetDouble(pref_name.c_str()));
64 base::FundamentalValue js_pref_default(pref->IsDefaultValue());
66 web_ui()->CallJavascriptFunction(
67 "gesture_config.updatePreferenceValueResult",
68 js_pref_name,
69 js_pref_value,
70 js_pref_default);
73 void GestureConfigUI::ResetPreferenceValue(const base::ListValue* args) {
74 std::string pref_name;
76 if (!args->GetString(0, &pref_name)) return;
78 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
80 prefs->ClearPref(pref_name.c_str());
81 UpdatePreferenceValue(args);
84 void GestureConfigUI::SetPreferenceValue(const base::ListValue* args) {
85 std::string pref_name;
86 double value;
88 if (!args->GetString(0, &pref_name) || !args->GetDouble(1, &value)) return;
90 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
92 const PrefService::Preference* pref =
93 prefs->FindPreference(pref_name.c_str());
94 switch (pref->GetType()) {
95 case base::Value::TYPE_INTEGER:
96 prefs->SetInteger(pref_name.c_str(), static_cast<int>(value));
97 break;
98 case base::Value::TYPE_DOUBLE:
99 prefs->SetDouble(pref_name.c_str(), value);
100 break;
101 default:
102 NOTREACHED();