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