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 #include "grit/generated_resources.h" 17 18 /** 19 * WebUI for configuring 'gesture.*' preference values used by 20 * Chrome's gesture recognition system. 21 */ 22 GestureConfigUI::GestureConfigUI(content::WebUI* web_ui) 23 : content::WebUIController(web_ui) { 24 // Set up the chrome://gesture-config source. 25 content::WebUIDataSource* html_source = 26 content::WebUIDataSource::Create(chrome::kChromeUIGestureConfigHost); 27 28 // Register callback handlers. 29 web_ui->RegisterMessageCallback( 30 "updatePreferenceValue", 31 base::Bind(&GestureConfigUI::UpdatePreferenceValue, 32 base::Unretained(this))); 33 web_ui->RegisterMessageCallback( 34 "resetPreferenceValue", 35 base::Bind(&GestureConfigUI::ResetPreferenceValue, 36 base::Unretained(this))); 37 web_ui->RegisterMessageCallback( 38 "setPreferenceValue", 39 base::Bind(&GestureConfigUI::SetPreferenceValue, 40 base::Unretained(this))); 41 42 // Add required resources. 43 html_source->AddResourcePath("gesture_config.css", IDR_GESTURE_CONFIG_CSS); 44 html_source->AddResourcePath("gesture_config.js", IDR_GESTURE_CONFIG_JS); 45 html_source->SetDefaultResource(IDR_GESTURE_CONFIG_HTML); 46 47 Profile* profile = Profile::FromWebUI(web_ui); 48 content::WebUIDataSource::Add(profile, html_source); 49 } 50 51 GestureConfigUI::~GestureConfigUI() { 52 } 53 54 void GestureConfigUI::UpdatePreferenceValue(const base::ListValue* args) { 55 std::string pref_name; 56 57 if (!args->GetString(0, &pref_name)) return; 58 59 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); 60 const PrefService::Preference* pref = 61 prefs->FindPreference(pref_name.c_str()); 62 63 base::StringValue js_pref_name(pref_name); 64 base::FundamentalValue js_pref_value(prefs->GetDouble(pref_name.c_str())); 65 base::FundamentalValue js_pref_default(pref->IsDefaultValue()); 66 67 web_ui()->CallJavascriptFunction( 68 "gesture_config.updatePreferenceValueResult", 69 js_pref_name, 70 js_pref_value, 71 js_pref_default); 72 } 73 74 void GestureConfigUI::ResetPreferenceValue(const base::ListValue* args) { 75 std::string pref_name; 76 77 if (!args->GetString(0, &pref_name)) return; 78 79 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); 80 81 prefs->ClearPref(pref_name.c_str()); 82 UpdatePreferenceValue(args); 83 } 84 85 void GestureConfigUI::SetPreferenceValue(const base::ListValue* args) { 86 std::string pref_name; 87 double value; 88 89 if (!args->GetString(0, &pref_name) || !args->GetDouble(1, &value)) return; 90 91 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); 92 93 const PrefService::Preference* pref = 94 prefs->FindPreference(pref_name.c_str()); 95 switch (pref->GetType()) { 96 case base::Value::TYPE_INTEGER: 97 prefs->SetInteger(pref_name.c_str(), static_cast<int>(value)); 98 break; 99 case base::Value::TYPE_DOUBLE: 100 prefs->SetDouble(pref_name.c_str(), value); 101 break; 102 default: 103 NOTREACHED(); 104 } 105 } 106 107