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 "ui/keyboard/keyboard_controller_proxy.h"
7 #include "base/command_line.h"
8 #include "base/values.h"
9 #include "content/public/browser/site_instance.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_contents_delegate.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "content/public/browser/web_ui.h"
15 #include "content/public/common/bindings_policy.h"
16 #include "ui/aura/layout_manager.h"
17 #include "ui/aura/window.h"
18 #include "ui/base/ime/input_method.h"
19 #include "ui/base/ime/text_input_client.h"
20 #include "ui/keyboard/keyboard_constants.h"
21 #include "ui/keyboard/keyboard_switches.h"
22 #include "ui/keyboard/keyboard_util.h"
26 // The WebContentsDelegate for the keyboard.
27 // The delegate deletes itself when the keyboard is destroyed.
28 class KeyboardContentsDelegate : public content::WebContentsDelegate,
29 public content::WebContentsObserver {
31 KeyboardContentsDelegate(keyboard::KeyboardControllerProxy* proxy)
33 virtual ~KeyboardContentsDelegate() {}
36 // Overridden from content::WebContentsDelegate:
37 virtual content::WebContents* OpenURLFromTab(
38 content::WebContents* source,
39 const content::OpenURLParams& params) OVERRIDE {
40 source->GetController().LoadURL(
41 params.url, params.referrer, params.transition, params.extra_headers);
46 virtual bool IsPopupOrPanel(
47 const content::WebContents* source) const OVERRIDE {
51 virtual void MoveContents(content::WebContents* source,
52 const gfx::Rect& pos) OVERRIDE {
53 aura::Window* keyboard = proxy_->GetKeyboardWindow();
54 // keyboard window must have been added to keyboard container window at this
55 // point. Otherwise, wrong keyboard bounds is used and may cause problem as
56 // described in crbug.com/367788.
57 DCHECK(keyboard->parent());
58 gfx::Rect bounds = keyboard->bounds();
59 int new_height = pos.height();
60 bounds.set_y(bounds.y() + bounds.height() - new_height);
61 bounds.set_height(new_height);
62 keyboard->SetBounds(bounds);
65 // Overridden from content::WebContentsDelegate:
66 virtual void RequestMediaAccessPermission(content::WebContents* web_contents,
67 const content::MediaStreamRequest& request,
68 const content::MediaResponseCallback& callback) OVERRIDE {
69 proxy_->RequestAudioInput(web_contents, request, callback);
72 // Overridden from content::WebContentsObserver:
73 virtual void WebContentsDestroyed() OVERRIDE {
77 keyboard::KeyboardControllerProxy* proxy_;
79 DISALLOW_COPY_AND_ASSIGN(KeyboardContentsDelegate);
86 KeyboardControllerProxy::KeyboardControllerProxy()
87 : default_url_(kKeyboardURL) {
90 KeyboardControllerProxy::~KeyboardControllerProxy() {
93 const GURL& KeyboardControllerProxy::GetVirtualKeyboardUrl() {
94 if (keyboard::IsInputViewEnabled()) {
95 const GURL& override_url = GetOverrideContentUrl();
96 return override_url.is_valid() ? override_url : default_url_;
102 void KeyboardControllerProxy::LoadContents(const GURL& url) {
103 if (keyboard_contents_) {
104 content::OpenURLParams params(
108 content::PAGE_TRANSITION_AUTO_TOPLEVEL,
110 keyboard_contents_->OpenURL(params);
114 aura::Window* KeyboardControllerProxy::GetKeyboardWindow() {
115 if (!keyboard_contents_) {
116 content::BrowserContext* context = GetBrowserContext();
117 keyboard_contents_.reset(content::WebContents::Create(
118 content::WebContents::CreateParams(context,
119 content::SiteInstance::CreateForURL(context,
120 GetVirtualKeyboardUrl()))));
121 keyboard_contents_->SetDelegate(new KeyboardContentsDelegate(this));
122 SetupWebContents(keyboard_contents_.get());
123 LoadContents(GetVirtualKeyboardUrl());
126 return keyboard_contents_->GetNativeView();
129 bool KeyboardControllerProxy::HasKeyboardWindow() const {
130 return keyboard_contents_;
133 void KeyboardControllerProxy::ShowKeyboardContainer(aura::Window* container) {
134 GetKeyboardWindow()->Show();
138 void KeyboardControllerProxy::HideKeyboardContainer(aura::Window* container) {
140 GetKeyboardWindow()->Hide();
143 void KeyboardControllerProxy::SetUpdateInputType(ui::TextInputType type) {
146 void KeyboardControllerProxy::EnsureCaretInWorkArea() {
147 if (GetInputMethod()->GetTextInputClient()) {
148 aura::Window* keyboard_window = GetKeyboardWindow();
149 aura::Window* root_window = keyboard_window->GetRootWindow();
150 gfx::Rect available_bounds = root_window->bounds();
151 gfx::Rect keyboard_bounds = keyboard_window->bounds();
152 available_bounds.set_height(available_bounds.height() -
153 keyboard_bounds.height());
154 GetInputMethod()->GetTextInputClient()->EnsureCaretInRect(available_bounds);
158 void KeyboardControllerProxy::LoadSystemKeyboard() {
159 DCHECK(keyboard_contents_);
160 if (keyboard_contents_->GetURL() != default_url_) {
161 // TODO(bshe): The height of system virtual keyboard and IME virtual
162 // keyboard may different. The height needs to be restored too.
163 LoadContents(default_url_);
167 void KeyboardControllerProxy::ReloadKeyboardIfNeeded() {
168 DCHECK(keyboard_contents_);
169 if (keyboard_contents_->GetURL() != GetVirtualKeyboardUrl()) {
170 LoadContents(GetVirtualKeyboardUrl());
174 void KeyboardControllerProxy::SetupWebContents(content::WebContents* contents) {
177 } // namespace keyboard