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/extension_function_dispatcher.h" 10 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_system.h" 12 #include "chrome/browser/media/media_capture_devices_dispatcher.h" 13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile_manager.h" 15 #include "chrome/common/extensions/extension_messages.h" 16 #include "content/public/browser/site_instance.h" 17 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents_view.h" 19 #include "extensions/browser/view_type_utils.h" 20 #include "extensions/common/constants.h" 21 #include "ipc/ipc_message_macros.h" 22 #include "ui/aura/client/aura_constants.h" 23 #include "ui/aura/root_window.h" 24 #include "ui/aura/window.h" 25 #include "ui/base/ime/input_method.h" 26 #include "ui/base/ime/text_input_client.h" 27 #include "ui/keyboard/keyboard_controller.h" 28 29 AshKeyboardControllerProxy::AshKeyboardControllerProxy() {} 30 31 AshKeyboardControllerProxy::~AshKeyboardControllerProxy() {} 32 33 void AshKeyboardControllerProxy::OnRequest( 34 const ExtensionHostMsg_Request_Params& params) { 35 extension_function_dispatcher_->Dispatch( 36 params, web_contents()->GetRenderViewHost()); 37 } 38 39 content::BrowserContext* AshKeyboardControllerProxy::GetBrowserContext() { 40 return ProfileManager::GetDefaultProfile(); 41 } 42 43 ui::InputMethod* AshKeyboardControllerProxy::GetInputMethod() { 44 aura::Window* root_window = ash::Shell::GetInstance()->GetPrimaryRootWindow(); 45 DCHECK(root_window); 46 return root_window->GetProperty(aura::client::kRootWindowInputMethodKey); 47 } 48 49 void AshKeyboardControllerProxy::RequestAudioInput( 50 content::WebContents* web_contents, 51 const content::MediaStreamRequest& request, 52 const content::MediaResponseCallback& callback) { 53 const extensions::Extension* extension = NULL; 54 GURL origin(request.security_origin); 55 if (origin.SchemeIs(extensions::kExtensionScheme)) { 56 ExtensionService* extensions_service = 57 extensions::ExtensionSystem::Get(ProfileManager::GetDefaultProfile())-> 58 extension_service(); 59 extension = extensions_service->extensions()->GetByID(origin.host()); 60 DCHECK(extension); 61 } 62 63 MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest( 64 web_contents, request, callback, extension); 65 } 66 67 void AshKeyboardControllerProxy::SetupWebContents( 68 content::WebContents* contents) { 69 extension_function_dispatcher_.reset( 70 new ExtensionFunctionDispatcher(ProfileManager::GetDefaultProfile(), 71 this)); 72 extensions::SetViewType(contents, extensions::VIEW_TYPE_VIRTUAL_KEYBOARD); 73 Observe(contents); 74 } 75 76 extensions::WindowController* 77 AshKeyboardControllerProxy::GetExtensionWindowController() const { 78 // The keyboard doesn't have a window controller. 79 return NULL; 80 } 81 82 content::WebContents* 83 AshKeyboardControllerProxy::GetAssociatedWebContents() const { 84 return web_contents(); 85 } 86 87 bool AshKeyboardControllerProxy::OnMessageReceived( 88 const IPC::Message& message) { 89 bool handled = true; 90 IPC_BEGIN_MESSAGE_MAP(AshKeyboardControllerProxy, message) 91 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) 92 IPC_MESSAGE_UNHANDLED(handled = false) 93 IPC_END_MESSAGE_MAP() 94 return handled; 95 } 96 97 void AshKeyboardControllerProxy::ShowKeyboardContainer( 98 aura::Window* container) { 99 KeyboardControllerProxy::ShowKeyboardContainer(container); 100 gfx::Rect showing_area = 101 ash::DisplayController::GetPrimaryDisplay().work_area(); 102 GetInputMethod()->GetTextInputClient()->EnsureCaretInRect(showing_area); 103 } 104