1 // Copyright 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/extensions/api/input/input.h"
6
7 #include "base/command_line.h"
8 #include "base/lazy_instance.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/string16.h"
11 #include "chrome/browser/extensions/extension_function_registry.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/user_metrics.h"
14 #include "ui/events/event.h"
15 #include "ui/keyboard/keyboard_controller.h"
16 #include "ui/keyboard/keyboard_switches.h"
17
18 #if defined(USE_ASH)
19 #include "ash/root_window_controller.h"
20 #include "ash/shell.h"
21 #include "ui/keyboard/keyboard_util.h"
22 #endif
23
24 namespace {
25
26 const char kNotYetImplementedError[] =
27 "API is not implemented on this platform.";
28
29 } // namespace
30
31 namespace extensions {
32
RunImpl()33 bool VirtualKeyboardPrivateInsertTextFunction::RunImpl() {
34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
35 #if defined(USE_ASH)
36 base::string16 text;
37 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text));
38
39 return keyboard::InsertText(text, ash::Shell::GetPrimaryRootWindow());
40 #endif
41 error_ = kNotYetImplementedError;
42 return false;
43 }
44
RunImpl()45 bool VirtualKeyboardPrivateMoveCursorFunction::RunImpl() {
46 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
47 #if defined(USE_ASH)
48 if (!CommandLine::ForCurrentProcess()->HasSwitch(
49 keyboard::switches::kEnableSwipeSelection)) {
50 return false;
51 }
52
53 int swipe_direction;
54 int modifier_flags;
55 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &swipe_direction));
56 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &modifier_flags));
57
58 return keyboard::MoveCursor(
59 swipe_direction,
60 modifier_flags,
61 ash::Shell::GetPrimaryRootWindow()->GetDispatcher());
62 #endif
63 error_ = kNotYetImplementedError;
64 return false;
65 }
66
RunImpl()67 bool VirtualKeyboardPrivateSendKeyEventFunction::RunImpl() {
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
69 #if defined(USE_ASH)
70 base::Value* options_value = NULL;
71 base::DictionaryValue* params = NULL;
72 std::string type;
73 int char_value;
74 int key_code;
75 std::string key_name;
76 int modifiers;
77
78 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &options_value));
79 EXTENSION_FUNCTION_VALIDATE(options_value->GetAsDictionary(¶ms));
80 EXTENSION_FUNCTION_VALIDATE(params->GetString("type", &type));
81 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("charValue", &char_value));
82 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("keyCode", &key_code));
83 EXTENSION_FUNCTION_VALIDATE(params->GetString("keyName", &key_name));
84 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("modifiers", &modifiers));
85
86 return keyboard::SendKeyEvent(
87 type,
88 char_value,
89 key_code,
90 key_name,
91 modifiers,
92 ash::Shell::GetPrimaryRootWindow()->GetDispatcher());
93 #endif
94 error_ = kNotYetImplementedError;
95 return false;
96 }
97
RunImpl()98 bool VirtualKeyboardPrivateHideKeyboardFunction::RunImpl() {
99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
100 #if defined(USE_ASH)
101 UMA_HISTOGRAM_ENUMERATION(
102 "VirtualKeyboard.KeyboardControlEvent",
103 keyboard::KEYBOARD_CONTROL_HIDE_USER,
104 keyboard::KEYBOARD_CONTROL_MAX);
105
106 // Pass HIDE_REASON_MANUAL since calls to HideKeyboard as part of this API
107 // would be user generated.
108 ash::Shell::GetInstance()->keyboard_controller()->HideKeyboard(
109 keyboard::KeyboardController::HIDE_REASON_MANUAL);
110
111 return true;
112 #endif
113 error_ = kNotYetImplementedError;
114 return false;
115 }
116
RunImpl()117 bool VirtualKeyboardPrivateLockKeyboardFunction::RunImpl() {
118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
119 #if defined(USE_ASH)
120 bool lock;
121 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &lock));
122
123 ash::Shell::GetInstance()->keyboard_controller()->set_lock_keyboard(lock);
124
125 return true;
126 #endif
127 error_ = kNotYetImplementedError;
128 return false;
129 }
130
RunImpl()131 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunImpl() {
132 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
133 #if defined(USE_ASH)
134 keyboard::MarkKeyboardLoadFinished();
135
136 content::UserMetricsAction("VirtualKeyboardLoaded");
137
138 return true;
139 #endif
140 error_ = kNotYetImplementedError;
141 return false;
142 }
143
InputAPI(Profile * profile)144 InputAPI::InputAPI(Profile* profile) {
145 }
146
~InputAPI()147 InputAPI::~InputAPI() {
148 }
149
150 static base::LazyInstance<ProfileKeyedAPIFactory<InputAPI> >
151 g_factory = LAZY_INSTANCE_INITIALIZER;
152
153 // static
GetFactoryInstance()154 ProfileKeyedAPIFactory<InputAPI>* InputAPI::GetFactoryInstance() {
155 return &g_factory.Get();
156 }
157
158 } // namespace extensions
159