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 "chrome/browser/ui/ash/ash_keyboard_controller_proxy.h"
6
7#include "ash/display/display_controller.h"
8#include "ash/shell.h"
9#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
10#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/media/media_capture_devices_dispatcher.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/profiles/profile_manager.h"
14#include "chrome/common/extensions/api/virtual_keyboard_private.h"
15#include "content/public/browser/site_instance.h"
16#include "content/public/browser/web_contents.h"
17#include "extensions/browser/event_router.h"
18#include "extensions/browser/extension_function_dispatcher.h"
19#include "extensions/browser/extension_system.h"
20#include "extensions/browser/view_type_utils.h"
21#include "extensions/common/constants.h"
22#include "extensions/common/extension_messages.h"
23#include "ipc/ipc_message_macros.h"
24#include "ui/aura/client/aura_constants.h"
25#include "ui/aura/window.h"
26#include "ui/aura/window_event_dispatcher.h"
27#include "ui/compositor/scoped_layer_animation_settings.h"
28#include "ui/keyboard/keyboard_controller.h"
29
30namespace virtual_keyboard_private = extensions::api::virtual_keyboard_private;
31typedef virtual_keyboard_private::OnTextInputBoxFocused::Context Context;
32
33namespace {
34
35const char* kVirtualKeyboardExtensionID = "mppnpdlheglhdfmldimlhpnegondlapf";
36
37Context::Type TextInputTypeToGeneratedInputTypeEnum(ui::TextInputType type) {
38  switch (type) {
39    case ui::TEXT_INPUT_TYPE_NONE:
40      return Context::TYPE_NONE;
41    case ui::TEXT_INPUT_TYPE_PASSWORD:
42      return Context::TYPE_PASSWORD;
43    case ui::TEXT_INPUT_TYPE_EMAIL:
44      return Context::TYPE_EMAIL;
45    case ui::TEXT_INPUT_TYPE_NUMBER:
46      return Context::TYPE_NUMBER;
47    case ui::TEXT_INPUT_TYPE_TELEPHONE:
48      return Context::TYPE_TEL;
49    case ui::TEXT_INPUT_TYPE_URL:
50      return Context::TYPE_URL;
51    case ui::TEXT_INPUT_TYPE_DATE:
52      return Context::TYPE_DATE;
53    case ui::TEXT_INPUT_TYPE_TEXT:
54    case ui::TEXT_INPUT_TYPE_SEARCH:
55    case ui::TEXT_INPUT_TYPE_DATE_TIME:
56    case ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL:
57    case ui::TEXT_INPUT_TYPE_MONTH:
58    case ui::TEXT_INPUT_TYPE_TIME:
59    case ui::TEXT_INPUT_TYPE_WEEK:
60    case ui::TEXT_INPUT_TYPE_TEXT_AREA:
61    case ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE:
62    case ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD:
63      return Context::TYPE_TEXT;
64  }
65  NOTREACHED();
66  return Context::TYPE_NONE;
67}
68
69}  // namespace
70
71AshKeyboardControllerProxy::AshKeyboardControllerProxy() {}
72
73AshKeyboardControllerProxy::~AshKeyboardControllerProxy() {}
74
75void AshKeyboardControllerProxy::OnRequest(
76    const ExtensionHostMsg_Request_Params& params) {
77  extension_function_dispatcher_->Dispatch(
78      params, web_contents()->GetRenderViewHost());
79}
80
81content::BrowserContext* AshKeyboardControllerProxy::GetBrowserContext() {
82  return ProfileManager::GetActiveUserProfile();
83}
84
85ui::InputMethod* AshKeyboardControllerProxy::GetInputMethod() {
86  aura::Window* root_window = ash::Shell::GetInstance()->GetPrimaryRootWindow();
87  DCHECK(root_window);
88  return root_window->GetProperty(aura::client::kRootWindowInputMethodKey);
89}
90
91void AshKeyboardControllerProxy::RequestAudioInput(
92      content::WebContents* web_contents,
93      const content::MediaStreamRequest& request,
94      const content::MediaResponseCallback& callback) {
95  const extensions::Extension* extension = NULL;
96  GURL origin(request.security_origin);
97  if (origin.SchemeIs(extensions::kExtensionScheme)) {
98    ExtensionService* extensions_service =
99        extensions::ExtensionSystem::Get(
100            GetBrowserContext())->extension_service();
101    extension = extensions_service->extensions()->GetByID(origin.host());
102    DCHECK(extension);
103  }
104
105  MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
106      web_contents, request, callback, extension);
107}
108
109void AshKeyboardControllerProxy::SetupWebContents(
110    content::WebContents* contents) {
111  extension_function_dispatcher_.reset(
112      new extensions::ExtensionFunctionDispatcher(GetBrowserContext(), this));
113  extensions::SetViewType(contents, extensions::VIEW_TYPE_VIRTUAL_KEYBOARD);
114  extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
115      contents);
116  Observe(contents);
117}
118
119extensions::WindowController*
120    AshKeyboardControllerProxy::GetExtensionWindowController() const {
121  // The keyboard doesn't have a window controller.
122  return NULL;
123}
124
125content::WebContents*
126    AshKeyboardControllerProxy::GetAssociatedWebContents() const {
127  return web_contents();
128}
129
130bool AshKeyboardControllerProxy::OnMessageReceived(
131    const IPC::Message& message) {
132  bool handled = true;
133  IPC_BEGIN_MESSAGE_MAP(AshKeyboardControllerProxy, message)
134    IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
135    IPC_MESSAGE_UNHANDLED(handled = false)
136  IPC_END_MESSAGE_MAP()
137  return handled;
138}
139
140void AshKeyboardControllerProxy::ShowKeyboardContainer(
141    aura::Window* container) {
142  // TODO(bshe): Implement logic to decide which root window should display
143  // virtual keyboard. http://crbug.com/303429
144  if (container->GetRootWindow() != ash::Shell::GetPrimaryRootWindow())
145    NOTIMPLEMENTED();
146
147  KeyboardControllerProxy::ShowKeyboardContainer(container);
148}
149
150void AshKeyboardControllerProxy::SetUpdateInputType(ui::TextInputType type) {
151  // TODO(bshe): Need to check the affected window's profile once multi-profile
152  // is supported.
153  content::BrowserContext* context = GetBrowserContext();
154  extensions::EventRouter* router = extensions::EventRouter::Get(context);
155
156  if (!router->HasEventListener(
157          virtual_keyboard_private::OnTextInputBoxFocused::kEventName)) {
158    return;
159  }
160
161  scoped_ptr<base::ListValue> event_args(new base::ListValue());
162  scoped_ptr<base::DictionaryValue> input_context(new base::DictionaryValue());
163  input_context->SetString("type",
164      Context::ToString(TextInputTypeToGeneratedInputTypeEnum(type)));
165  event_args->Append(input_context.release());
166
167  scoped_ptr<extensions::Event> event(new extensions::Event(
168      virtual_keyboard_private::OnTextInputBoxFocused::kEventName,
169      event_args.Pass()));
170  event->restrict_to_browser_context = context;
171  router->DispatchEventToExtension(kVirtualKeyboardExtensionID, event.Pass());
172}
173