Minimum support of partial request on Drive API v2.
[chromium-blink-merge.git] / ui / keyboard / keyboard_ui_handler.cc
blob26909c2d49ee20d6def07a787d85d3ff4193447f
1 // Copyright (c) 2013 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 "ui/keyboard/keyboard_ui_handler.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/values.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_view.h"
14 #include "content/public/browser/web_ui.h"
15 #include "ui/aura/client/aura_constants.h"
16 #include "ui/aura/root_window.h"
17 #include "ui/aura/window.h"
18 #include "ui/base/ime/input_method.h"
19 #include "ui/base/ime/text_input_client.h"
20 #include "ui/keyboard/keyboard_controller.h"
21 #include "ui/keyboard/keyboard_util.h"
23 namespace keyboard {
25 KeyboardUIHandler::KeyboardUIHandler() {
28 KeyboardUIHandler::~KeyboardUIHandler() {
31 void KeyboardUIHandler::RegisterMessages() {
32 web_ui()->RegisterMessageCallback(
33 "insertText",
34 base::Bind(&KeyboardUIHandler::HandleInsertTextMessage,
35 base::Unretained(this)));
36 web_ui()->RegisterMessageCallback(
37 "getInputContext",
38 base::Bind(&KeyboardUIHandler::HandleGetInputContextMessage,
39 base::Unretained(this)));
40 web_ui()->RegisterMessageCallback(
41 "sendKeyEvent",
42 base::Bind(&KeyboardUIHandler::HandleSendKeyEventMessage,
43 base::Unretained(this)));
44 web_ui()->RegisterMessageCallback(
45 "hideKeyboard",
46 base::Bind(&KeyboardUIHandler::HandleHideKeyboard,
47 base::Unretained(this)));
50 void KeyboardUIHandler::HandleInsertTextMessage(const base::ListValue* args) {
51 string16 text;
52 if (!args->GetString(0, &text)) {
53 LOG(ERROR) << "insertText failed: bad argument";
54 return;
57 aura::RootWindow* root_window =
58 web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
59 if (!root_window) {
60 LOG(ERROR) << "insertText failed: no root window";
61 return;
64 if (!keyboard::InsertText(text, root_window))
65 LOG(ERROR) << "insertText failed";
68 void KeyboardUIHandler::HandleGetInputContextMessage(
69 const base::ListValue* args) {
70 int request_id;
71 if (!args->GetInteger(0, &request_id)) {
72 LOG(ERROR) << "getInputContext failed: bad argument";
73 return;
75 base::DictionaryValue results;
76 results.SetInteger("requestId", request_id);
78 aura::RootWindow* root_window =
79 web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
80 if (!root_window) {
81 LOG(ERROR) << "getInputContext failed: no root window";
82 return;
84 ui::InputMethod* input_method =
85 root_window->GetProperty(aura::client::kRootWindowInputMethodKey);
86 if (!input_method) {
87 LOG(ERROR) << "getInputContext failed: no input method";
88 return;
91 ui::TextInputClient* tic = input_method->GetTextInputClient();
92 results.SetInteger("type",
93 tic ? tic->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE);
95 web_ui()->CallJavascriptFunction("GetInputContextCallback",
96 results);
99 void KeyboardUIHandler::HandleSendKeyEventMessage(
100 const base::ListValue* args) {
101 const base::DictionaryValue* params = NULL;
102 std::string type;
103 int char_value;
104 int key_code;
105 bool shift_modifier;
107 if (!args->GetDictionary(0, &params) ||
108 !params->GetString("type", &type) ||
109 !params->GetInteger("charValue", &char_value) ||
110 !params->GetInteger("keyCode", &key_code) ||
111 !params->GetBoolean("shiftKey", &shift_modifier)) {
112 LOG(ERROR) << "SendKeyEvent failed: bad argument";
113 return;
116 aura::RootWindow* root_window =
117 web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
118 if (!root_window) {
119 LOG(ERROR) << "sendKeyEvent failed: no root window";
120 return;
123 if (!keyboard::SendKeyEvent(type,
124 char_value,
125 key_code,
126 shift_modifier,
127 root_window)) {
128 LOG(ERROR) << "sendKeyEvent failed";
132 void KeyboardUIHandler::HandleHideKeyboard(const base::ListValue* args) {
133 // TODO(stevet): Call into the keyboard controller to hide the keyboard
134 // directly.
135 NOTIMPLEMENTED();
136 return;
139 } // namespace keyboard