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.
4
5#include "chrome/browser/ui/webui/gesture_config_ui.h"
6
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"
16
17/**
18 * WebUI for configuring 'gesture.*' preference values used by
19 * Chrome's gesture recognition system.
20 */
21GestureConfigUI::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);
26
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)));
40
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);
45
46  Profile* profile = Profile::FromWebUI(web_ui);
47  content::WebUIDataSource::Add(profile, html_source);
48}
49
50GestureConfigUI::~GestureConfigUI() {
51}
52
53void GestureConfigUI::UpdatePreferenceValue(const base::ListValue* args) {
54  std::string pref_name;
55
56  if (!args->GetString(0, &pref_name)) return;
57
58  PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
59  const PrefService::Preference* pref =
60      prefs->FindPreference(pref_name.c_str());
61
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());
65
66  web_ui()->CallJavascriptFunction(
67      "gesture_config.updatePreferenceValueResult",
68      js_pref_name,
69      js_pref_value,
70      js_pref_default);
71}
72
73void GestureConfigUI::ResetPreferenceValue(const base::ListValue* args) {
74  std::string pref_name;
75
76  if (!args->GetString(0, &pref_name)) return;
77
78  PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
79
80  prefs->ClearPref(pref_name.c_str());
81  UpdatePreferenceValue(args);
82}
83
84void GestureConfigUI::SetPreferenceValue(const base::ListValue* args) {
85  std::string pref_name;
86  double value;
87
88  if (!args->GetString(0, &pref_name) || !args->GetDouble(1, &value)) return;
89
90  PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
91
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();
103  }
104}
105
106