• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/keyboard/keyboard_ui_handler.h"
6 
7 #include <string>
8 
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"
22 
23 namespace keyboard {
24 
KeyboardUIHandler()25 KeyboardUIHandler::KeyboardUIHandler() {
26 }
27 
~KeyboardUIHandler()28 KeyboardUIHandler::~KeyboardUIHandler() {
29 }
30 
RegisterMessages()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)));
48 }
49 
HandleInsertTextMessage(const base::ListValue * args)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;
55   }
56 
57   aura::Window* root_window =
58       web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
59   if (!root_window) {
60     LOG(ERROR) << "insertText failed: no root window";
61     return;
62   }
63 
64   if (!keyboard::InsertText(text, root_window))
65     LOG(ERROR) << "insertText failed";
66 }
67 
HandleGetInputContextMessage(const base::ListValue * args)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;
74   }
75   base::DictionaryValue results;
76   results.SetInteger("requestId", request_id);
77 
78   aura::Window* root_window =
79       web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
80   if (!root_window) {
81     LOG(ERROR) << "getInputContext failed: no root window";
82     return;
83   }
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;
89   }
90 
91   ui::TextInputClient* tic = input_method->GetTextInputClient();
92   results.SetInteger("type",
93                      tic ? tic->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE);
94 
95   web_ui()->CallJavascriptFunction("GetInputContextCallback",
96                                    results);
97 }
98 
HandleSendKeyEventMessage(const base::ListValue * args)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   std::string key_name;
106   int modifiers;
107 
108   if (!args->GetDictionary(0, &params) ||
109       !params->GetString("type", &type) ||
110       !params->GetInteger("charValue", &char_value) ||
111       !params->GetInteger("keyCode", &key_code) ||
112       !params->GetString("keyName", &key_name) ||
113       !params->GetInteger("modifiers", &modifiers)) {
114     LOG(ERROR) << "SendKeyEvent failed: bad argument";
115     return;
116   }
117 
118   aura::WindowEventDispatcher* dispatcher =
119       web_ui()->GetWebContents()->GetView()->GetNativeView()->GetDispatcher();
120   if (!dispatcher) {
121     LOG(ERROR) << "sendKeyEvent failed: no dispatcher";
122     return;
123   }
124 
125   if (!keyboard::SendKeyEvent(type,
126                               char_value,
127                               key_code,
128                               key_name,
129                               modifiers,
130                               dispatcher)) {
131     LOG(ERROR) << "sendKeyEvent failed";
132   }
133 }
134 
HandleHideKeyboard(const base::ListValue * args)135 void KeyboardUIHandler::HandleHideKeyboard(const base::ListValue* args) {
136   // TODO(stevet): Call into the keyboard controller to hide the keyboard
137   // directly.
138   NOTIMPLEMENTED();
139   return;
140 }
141 
142 }  // namespace keyboard
143