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"
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"
25 KeyboardUIHandler::KeyboardUIHandler() {
28 KeyboardUIHandler::~KeyboardUIHandler() {
31 void KeyboardUIHandler::RegisterMessages() {
32 web_ui()->RegisterMessageCallback(
34 base::Bind(&KeyboardUIHandler::HandleInsertTextMessage
,
35 base::Unretained(this)));
36 web_ui()->RegisterMessageCallback(
38 base::Bind(&KeyboardUIHandler::HandleGetInputContextMessage
,
39 base::Unretained(this)));
40 web_ui()->RegisterMessageCallback(
42 base::Bind(&KeyboardUIHandler::HandleSendKeyEventMessage
,
43 base::Unretained(this)));
44 web_ui()->RegisterMessageCallback(
46 base::Bind(&KeyboardUIHandler::HandleHideKeyboard
,
47 base::Unretained(this)));
50 void KeyboardUIHandler::HandleInsertTextMessage(const base::ListValue
* args
) {
52 if (!args
->GetString(0, &text
)) {
53 LOG(ERROR
) << "insertText failed: bad argument";
57 aura::RootWindow
* root_window
=
58 web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
60 LOG(ERROR
) << "insertText failed: no root window";
64 if (!keyboard::InsertText(text
, root_window
))
65 LOG(ERROR
) << "insertText failed";
68 void KeyboardUIHandler::HandleGetInputContextMessage(
69 const base::ListValue
* args
) {
71 if (!args
->GetInteger(0, &request_id
)) {
72 LOG(ERROR
) << "getInputContext failed: bad argument";
75 base::DictionaryValue results
;
76 results
.SetInteger("requestId", request_id
);
78 aura::RootWindow
* root_window
=
79 web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
81 LOG(ERROR
) << "getInputContext failed: no root window";
84 ui::InputMethod
* input_method
=
85 root_window
->GetProperty(aura::client::kRootWindowInputMethodKey
);
87 LOG(ERROR
) << "getInputContext failed: no input method";
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",
99 void KeyboardUIHandler::HandleSendKeyEventMessage(
100 const base::ListValue
* args
) {
101 const base::DictionaryValue
* params
= NULL
;
107 if (!args
->GetDictionary(0, ¶ms
) ||
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";
116 aura::RootWindow
* root_window
=
117 web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
119 LOG(ERROR
) << "sendKeyEvent failed: no root window";
123 if (!keyboard::SendKeyEvent(type
,
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
139 } // namespace keyboard