Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / chromeos / keyboard_handler.cc
blob23f4045003846083ef37dd1cee9de47cee034361
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/options/chromeos/keyboard_handler.h"
7 #include "ash/new_window_delegate.h"
8 #include "ash/shell.h"
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/values.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "chromeos/chromeos_switches.h"
15 #include "content/public/browser/web_ui.h"
16 #include "ui/base/ime/chromeos/ime_keyboard.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/events/devices/device_data_manager.h"
19 #include "ui/events/devices/keyboard_device.h"
21 namespace {
22 const struct ModifierKeysSelectItem {
23 int message_id;
24 chromeos::input_method::ModifierKey value;
25 } kModifierKeysSelectItems[] = {
26 { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_SEARCH,
27 chromeos::input_method::kSearchKey },
28 { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_LEFT_CTRL,
29 chromeos::input_method::kControlKey },
30 { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_LEFT_ALT,
31 chromeos::input_method::kAltKey },
32 { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_VOID,
33 chromeos::input_method::kVoidKey },
34 { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_CAPS_LOCK,
35 chromeos::input_method::kCapsLockKey },
36 { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_ESCAPE,
37 chromeos::input_method::kEscapeKey },
40 const char* kDataValuesNames[] = {
41 "remapSearchKeyToValue",
42 "remapControlKeyToValue",
43 "remapAltKeyToValue",
44 "remapCapsLockKeyToValue",
45 "remapDiamondKeyToValue",
48 bool HasExternalKeyboard() {
49 for (const ui::KeyboardDevice& keyboard :
50 ui::DeviceDataManager::GetInstance()->keyboard_devices()) {
51 if (keyboard.type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL)
52 return true;
55 return false;
57 } // namespace
59 namespace chromeos {
60 namespace options {
62 KeyboardHandler::KeyboardHandler() {
63 ui::DeviceDataManager::GetInstance()->AddObserver(this);
66 KeyboardHandler::~KeyboardHandler() {
67 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
70 void KeyboardHandler::GetLocalizedValues(
71 base::DictionaryValue* localized_strings) {
72 DCHECK(localized_strings);
73 RegisterTitle(localized_strings, "keyboardOverlay",
74 IDS_OPTIONS_KEYBOARD_OVERLAY_TITLE);
76 localized_strings->SetString("remapSearchKeyToContent",
77 l10n_util::GetStringUTF16(
78 IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_SEARCH_LABEL));
79 localized_strings->SetString("remapControlKeyToContent",
80 l10n_util::GetStringUTF16(
81 IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_LEFT_CTRL_LABEL));
82 localized_strings->SetString("remapAltKeyToContent",
83 l10n_util::GetStringUTF16(
84 IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_LEFT_ALT_LABEL));
85 localized_strings->SetString("remapCapsLockKeyToContent",
86 l10n_util::GetStringUTF16(
87 IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_CAPS_LOCK_LABEL));
88 localized_strings->SetString("remapDiamondKeyToContent",
89 l10n_util::GetStringUTF16(
90 IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_DIAMOND_KEY_LABEL));
91 localized_strings->SetString("sendFunctionKeys",
92 l10n_util::GetStringUTF16(
93 IDS_OPTIONS_SETTINGS_LANGUAGES_SEND_FUNCTION_KEYS));
94 localized_strings->SetString("sendFunctionKeysDescription",
95 l10n_util::GetStringUTF16(
96 IDS_OPTIONS_SETTINGS_LANGUAGES_SEND_FUNCTION_KEYS_DESCRIPTION));
97 localized_strings->SetString("enableAutoRepeat",
98 l10n_util::GetStringUTF16(
99 IDS_OPTIONS_SETTINGS_LANGUAGES_AUTO_REPEAT_ENABLE));
100 localized_strings->SetString("autoRepeatDelay",
101 l10n_util::GetStringUTF16(
102 IDS_OPTIONS_SETTINGS_LANGUAGES_AUTO_REPEAT_DELAY));
103 localized_strings->SetString("autoRepeatDelayLong",
104 l10n_util::GetStringUTF16(
105 IDS_OPTIONS_SETTINGS_LANGUAGES_AUTO_REPEAT_DELAY_LONG));
106 localized_strings->SetString("autoRepeatDelayShort",
107 l10n_util::GetStringUTF16(
108 IDS_OPTIONS_SETTINGS_LANGUAGES_AUTO_REPEAT_DELAY_SHORT));
109 localized_strings->SetString("autoRepeatRate",
110 l10n_util::GetStringUTF16(
111 IDS_OPTIONS_SETTINGS_LANGUAGES_AUTO_REPEAT_RATE));
112 localized_strings->SetString("autoRepeatRateSlow",
113 l10n_util::GetStringUTF16(
114 IDS_OPTIONS_SETTINGS_LANGUAGES_AUTO_REPEAT_RATE_SLOW));
115 localized_strings->SetString("autoRepeatRateFast",
116 l10n_util::GetStringUTF16(
117 IDS_OPTIONS_SETTINGS_LANGUAGES_AUTO_REPEAT_RATE_FAST));
118 localized_strings->SetString("changeLanguageAndInputSettings",
119 l10n_util::GetStringUTF16(
120 IDS_OPTIONS_SETTINGS_CHANGE_LANGUAGE_AND_INPUT_SETTINGS));
121 localized_strings->SetString("showKeyboardShortcuts",
122 l10n_util::GetStringUTF16(
123 IDS_OPTIONS_SETTINGS_SHOW_KEYBOARD_SHORTCUTS));
125 for (size_t i = 0; i < arraysize(kDataValuesNames); ++i) {
126 base::ListValue* list_value = new base::ListValue();
127 for (size_t j = 0; j < arraysize(kModifierKeysSelectItems); ++j) {
128 const input_method::ModifierKey value =
129 kModifierKeysSelectItems[j].value;
130 const int message_id = kModifierKeysSelectItems[j].message_id;
131 // Only the seach key can be remapped to the caps lock key.
132 if (kDataValuesNames[i] != std::string("remapSearchKeyToValue") &&
133 kDataValuesNames[i] != std::string("remapCapsLockKeyToValue") &&
134 value == input_method::kCapsLockKey) {
135 continue;
137 base::ListValue* option = new base::ListValue();
138 option->Append(new base::FundamentalValue(value));
139 option->Append(new base::StringValue(l10n_util::GetStringUTF16(
140 message_id)));
141 list_value->Append(option);
143 localized_strings->Set(kDataValuesNames[i], list_value);
147 void KeyboardHandler::InitializePage() {
148 bool has_diamond_key = base::CommandLine::ForCurrentProcess()->HasSwitch(
149 chromeos::switches::kHasChromeOSDiamondKey);
150 const base::FundamentalValue show_diamond_key_options(has_diamond_key);
152 web_ui()->CallJavascriptFunction(
153 "options.KeyboardOverlay.showDiamondKeyOptions",
154 show_diamond_key_options);
156 UpdateCapsLockOptions();
159 void KeyboardHandler::RegisterMessages() {
160 // Callback to show keyboard overlay.
161 web_ui()->RegisterMessageCallback(
162 "showKeyboardShortcuts",
163 base::Bind(&KeyboardHandler::HandleShowKeyboardShortcuts,
164 base::Unretained(this)));
167 void KeyboardHandler::OnKeyboardDeviceConfigurationChanged() {
168 UpdateCapsLockOptions();
171 void KeyboardHandler::HandleShowKeyboardShortcuts(const base::ListValue* args) {
172 ash::Shell::GetInstance()->new_window_delegate()->ShowKeyboardOverlay();
175 void KeyboardHandler::UpdateCapsLockOptions() const {
176 const base::FundamentalValue show_caps_lock_options(HasExternalKeyboard());
177 web_ui()->CallJavascriptFunction(
178 "options.KeyboardOverlay.showCapsLockOptions",
179 show_caps_lock_options);
182 } // namespace options
183 } // namespace chromeos