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 "chrome/browser/ui/ash/ash_keyboard_controller_proxy.h"
7 #include "ash/display/display_controller.h"
9 #include "chrome/browser/extensions/event_names.h"
10 #include "chrome/browser/extensions/extension_function_dispatcher.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_web_contents_observer.h"
13 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/common/extensions/api/virtual_keyboard_private.h"
17 #include "content/public/browser/site_instance.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_contents_view.h"
20 #include "extensions/browser/event_router.h"
21 #include "extensions/browser/extension_system.h"
22 #include "extensions/browser/view_type_utils.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/extension_messages.h"
25 #include "ipc/ipc_message_macros.h"
26 #include "ui/aura/client/aura_constants.h"
27 #include "ui/aura/window.h"
28 #include "ui/aura/window_event_dispatcher.h"
29 #include "ui/base/ime/input_method.h"
30 #include "ui/base/ime/text_input_client.h"
31 #include "ui/compositor/scoped_layer_animation_settings.h"
32 #include "ui/keyboard/keyboard_controller.h"
34 namespace virtual_keyboard_private
= extensions::api::virtual_keyboard_private
;
35 typedef virtual_keyboard_private::OnTextInputBoxFocused::Context Context
;
39 const char* kVirtualKeyboardExtensionID
= "mppnpdlheglhdfmldimlhpnegondlapf";
41 Context::Type
TextInputTypeToGeneratedInputTypeEnum(ui::TextInputType type
) {
43 case ui::TEXT_INPUT_TYPE_NONE
:
44 return Context::TYPE_NONE
;
45 case ui::TEXT_INPUT_TYPE_PASSWORD
:
46 return Context::TYPE_PASSWORD
;
47 case ui::TEXT_INPUT_TYPE_EMAIL
:
48 return Context::TYPE_EMAIL
;
49 case ui::TEXT_INPUT_TYPE_NUMBER
:
50 return Context::TYPE_NUMBER
;
51 case ui::TEXT_INPUT_TYPE_TELEPHONE
:
52 return Context::TYPE_TEL
;
53 case ui::TEXT_INPUT_TYPE_URL
:
54 return Context::TYPE_URL
;
55 case ui::TEXT_INPUT_TYPE_DATE
:
56 return Context::TYPE_DATE
;
57 case ui::TEXT_INPUT_TYPE_TEXT
:
58 case ui::TEXT_INPUT_TYPE_SEARCH
:
59 case ui::TEXT_INPUT_TYPE_DATE_TIME
:
60 case ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL
:
61 case ui::TEXT_INPUT_TYPE_MONTH
:
62 case ui::TEXT_INPUT_TYPE_TIME
:
63 case ui::TEXT_INPUT_TYPE_WEEK
:
64 case ui::TEXT_INPUT_TYPE_TEXT_AREA
:
65 case ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE
:
66 case ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD
:
67 return Context::TYPE_TEXT
;
70 return Context::TYPE_NONE
;
75 AshKeyboardControllerProxy::AshKeyboardControllerProxy() {}
77 AshKeyboardControllerProxy::~AshKeyboardControllerProxy() {}
79 void AshKeyboardControllerProxy::OnRequest(
80 const ExtensionHostMsg_Request_Params
& params
) {
81 extension_function_dispatcher_
->Dispatch(
82 params
, web_contents()->GetRenderViewHost());
85 content::BrowserContext
* AshKeyboardControllerProxy::GetBrowserContext() {
86 return ProfileManager::GetActiveUserProfile();
89 ui::InputMethod
* AshKeyboardControllerProxy::GetInputMethod() {
90 aura::Window
* root_window
= ash::Shell::GetInstance()->GetPrimaryRootWindow();
92 return root_window
->GetProperty(aura::client::kRootWindowInputMethodKey
);
95 void AshKeyboardControllerProxy::RequestAudioInput(
96 content::WebContents
* web_contents
,
97 const content::MediaStreamRequest
& request
,
98 const content::MediaResponseCallback
& callback
) {
99 const extensions::Extension
* extension
= NULL
;
100 GURL
origin(request
.security_origin
);
101 if (origin
.SchemeIs(extensions::kExtensionScheme
)) {
102 ExtensionService
* extensions_service
=
103 extensions::ExtensionSystem::Get(
104 GetBrowserContext())->extension_service();
105 extension
= extensions_service
->extensions()->GetByID(origin
.host());
109 MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
110 web_contents
, request
, callback
, extension
);
113 void AshKeyboardControllerProxy::SetupWebContents(
114 content::WebContents
* contents
) {
115 extension_function_dispatcher_
.reset(
116 new ExtensionFunctionDispatcher(GetBrowserContext(), this));
117 extensions::SetViewType(contents
, extensions::VIEW_TYPE_VIRTUAL_KEYBOARD
);
118 extensions::ExtensionWebContentsObserver::CreateForWebContents(contents
);
122 extensions::WindowController
*
123 AshKeyboardControllerProxy::GetExtensionWindowController() const {
124 // The keyboard doesn't have a window controller.
128 content::WebContents
*
129 AshKeyboardControllerProxy::GetAssociatedWebContents() const {
130 return web_contents();
133 bool AshKeyboardControllerProxy::OnMessageReceived(
134 const IPC::Message
& message
) {
136 IPC_BEGIN_MESSAGE_MAP(AshKeyboardControllerProxy
, message
)
137 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
138 IPC_MESSAGE_UNHANDLED(handled
= false)
139 IPC_END_MESSAGE_MAP()
143 void AshKeyboardControllerProxy::ShowKeyboardContainer(
144 aura::Window
* container
) {
145 // TODO(bshe): Implement logic to decide which root window should display
146 // virtual keyboard. http://crbug.com/303429
147 if (container
->GetRootWindow() != ash::Shell::GetPrimaryRootWindow())
150 KeyboardControllerProxy::ShowKeyboardContainer(container
);
153 void AshKeyboardControllerProxy::EnsureCaretInWorkArea() {
154 // GetTextInputClient may return NULL when keyboard-usability-experiment
156 if (GetInputMethod()->GetTextInputClient()) {
157 gfx::Rect showing_area
=
158 ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
159 GetInputMethod()->GetTextInputClient()->EnsureCaretInRect(showing_area
);
163 void AshKeyboardControllerProxy::SetUpdateInputType(ui::TextInputType type
) {
164 // TODO(bshe): Need to check the affected window's profile once multi-profile
166 content::BrowserContext
* context
= GetBrowserContext();
167 extensions::EventRouter
* router
=
168 extensions::ExtensionSystem::Get(context
)->event_router();
170 if (!router
->HasEventListener(
171 virtual_keyboard_private::OnTextInputBoxFocused::kEventName
)) {
175 scoped_ptr
<base::ListValue
> event_args(new base::ListValue());
176 scoped_ptr
<base::DictionaryValue
> input_context(new base::DictionaryValue());
177 input_context
->SetString("type",
178 Context::ToString(TextInputTypeToGeneratedInputTypeEnum(type
)));
179 event_args
->Append(input_context
.release());
181 scoped_ptr
<extensions::Event
> event(new extensions::Event(
182 virtual_keyboard_private::OnTextInputBoxFocused::kEventName
,
184 event
->restrict_to_browser_context
= context
;
185 router
->DispatchEventToExtension(kVirtualKeyboardExtensionID
, event
.Pass());