summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/gesture_config_ui.cc
blob: 2446e37dec75153a596c85c82eab0aac815a4f8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/gesture_config_ui.h"

#include "base/bind.h"
#include "base/prefs/pref_service.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"

/**
 * WebUI for configuring 'gesture.*' preference values used by
 * Chrome's gesture recognition system.
 */
GestureConfigUI::GestureConfigUI(content::WebUI* web_ui)
    : content::WebUIController(web_ui) {
  // Set up the chrome://gesture-config source.
  content::WebUIDataSource* html_source =
      content::WebUIDataSource::Create(chrome::kChromeUIGestureConfigHost);

  // Register callback handlers.
  web_ui->RegisterMessageCallback(
      "getPreferenceValue",
      base::Bind(&GestureConfigUI::GetPreferenceValue,
                 base::Unretained(this)));
  web_ui->RegisterMessageCallback(
      "resetPreferenceValue",
      base::Bind(&GestureConfigUI::ResetPreferenceValue,
                 base::Unretained(this)));
  web_ui->RegisterMessageCallback(
      "setPreferenceValue",
      base::Bind(&GestureConfigUI::SetPreferenceValue,
                 base::Unretained(this)));

  // Add required resources.
  html_source->AddResourcePath("gesture_config.css", IDR_GESTURE_CONFIG_CSS);
  html_source->AddResourcePath("gesture_config.js", IDR_GESTURE_CONFIG_JS);
  html_source->SetDefaultResource(IDR_GESTURE_CONFIG_HTML);

  Profile* profile = Profile::FromWebUI(web_ui);
  content::WebUIDataSource::Add(profile, html_source);
}

GestureConfigUI::~GestureConfigUI() {
}

void GestureConfigUI::GetPreferenceValue(const base::ListValue* args) {
  std::string pref_name;

  if (!args->GetString(0, &pref_name)) return;

  Profile* profile = Profile::FromWebUI(web_ui());
  PrefService* prefs = profile->GetPrefs();

  base::StringValue js_pref_name(pref_name);
  base::FundamentalValue js_pref_value(prefs->GetDouble(pref_name.c_str()));

  web_ui()->CallJavascriptFunction(
      "gesture_config.getPreferenceValueResult",
      js_pref_name,
      js_pref_value);
}

void GestureConfigUI::ResetPreferenceValue(const base::ListValue* args) {
  std::string pref_name;

  if (!args->GetString(0, &pref_name)) return;

  Profile* profile = Profile::FromWebUI(web_ui());
  PrefService* prefs = profile->GetPrefs();

  base::StringValue js_pref_name(pref_name);
  double d;
  if (prefs->GetDefaultPrefValue(pref_name.c_str())->GetAsDouble(&d)) {
    base::FundamentalValue js_pref_value(d);
    const PrefService::Preference* pref =
        prefs->FindPreference(pref_name.c_str());
    switch (pref->GetType()) {
      case base::Value::TYPE_INTEGER:
        prefs->SetInteger(pref_name.c_str(), static_cast<int>(d));
        break;
      case base::Value::TYPE_DOUBLE:
        prefs->SetDouble(pref_name.c_str(), d);
        break;
      default:
        NOTREACHED();
    }
    web_ui()->CallJavascriptFunction(
        "gesture_config.getPreferenceValueResult",
        js_pref_name,
        js_pref_value);
  }
}

void GestureConfigUI::SetPreferenceValue(const base::ListValue* args) {
  std::string pref_name;
  double value;

  if (!args->GetString(0, &pref_name) || !args->GetDouble(1, &value)) return;

  Profile* profile = Profile::FromWebUI(web_ui());
  PrefService* prefs = profile->GetPrefs();

  const PrefService::Preference* pref =
      prefs->FindPreference(pref_name.c_str());
  switch (pref->GetType()) {
    case base::Value::TYPE_INTEGER:
      prefs->SetInteger(pref_name.c_str(), static_cast<int>(value));
      break;
    case base::Value::TYPE_DOUBLE:
      prefs->SetDouble(pref_name.c_str(), value);
      break;
    default:
      NOTREACHED();
  }
}