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"
23 #include "ui/wm/core/shadow.h"
27 // The WebContentsDelegate for the keyboard.
28 // The delegate deletes itself when the keyboard is destroyed.
29 class KeyboardContentsDelegate
: public content::WebContentsDelegate
,
30 public content::WebContentsObserver
{
32 KeyboardContentsDelegate(keyboard::KeyboardControllerProxy
* proxy
)
34 ~KeyboardContentsDelegate() override
{}
37 // Overridden from content::WebContentsDelegate:
38 content::WebContents
* OpenURLFromTab(
39 content::WebContents
* source
,
40 const content::OpenURLParams
& params
) override
{
41 source
->GetController().LoadURL(
42 params
.url
, params
.referrer
, params
.transition
, params
.extra_headers
);
47 bool CanDragEnter(content::WebContents
* source
,
48 const content::DropData
& data
,
49 blink::WebDragOperationsMask operations_allowed
) override
{
53 bool ShouldCreateWebContents(
54 content::WebContents
* web_contents
,
56 int main_frame_route_id
,
57 WindowContainerType window_container_type
,
58 const base::string16
& frame_name
,
59 const GURL
& target_url
,
60 const std::string
& partition_id
,
61 content::SessionStorageNamespace
* session_storage_namespace
) override
{
65 bool IsPopupOrPanel(const content::WebContents
* source
) const override
{
69 void MoveContents(content::WebContents
* source
,
70 const gfx::Rect
& pos
) override
{
71 aura::Window
* keyboard
= proxy_
->GetKeyboardWindow();
72 // keyboard window must have been added to keyboard container window at this
73 // point. Otherwise, wrong keyboard bounds is used and may cause problem as
74 // described in crbug.com/367788.
75 DCHECK(keyboard
->parent());
76 // keyboard window bounds may not set to |pos| after this call. If keyboard
77 // is in FULL_WIDTH mode, only the height of keyboard window will be
79 keyboard
->SetBounds(pos
);
82 // Overridden from content::WebContentsDelegate:
83 void RequestMediaAccessPermission(
84 content::WebContents
* web_contents
,
85 const content::MediaStreamRequest
& request
,
86 const content::MediaResponseCallback
& callback
) override
{
87 proxy_
->RequestAudioInput(web_contents
, request
, callback
);
90 // Overridden from content::WebContentsObserver:
91 void WebContentsDestroyed() override
{ delete this; }
93 keyboard::KeyboardControllerProxy
* proxy_
;
95 DISALLOW_COPY_AND_ASSIGN(KeyboardContentsDelegate
);
102 KeyboardControllerProxy::KeyboardControllerProxy(
103 content::BrowserContext
* context
)
104 : browser_context_(context
), default_url_(kKeyboardURL
) {
107 KeyboardControllerProxy::~KeyboardControllerProxy() {
110 const GURL
& KeyboardControllerProxy::GetVirtualKeyboardUrl() {
111 if (keyboard::IsInputViewEnabled()) {
112 const GURL
& override_url
= GetOverrideContentUrl();
113 return override_url
.is_valid() ? override_url
: default_url_
;
119 void KeyboardControllerProxy::LoadContents(const GURL
& url
) {
120 if (keyboard_contents_
) {
121 content::OpenURLParams
params(
125 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
127 keyboard_contents_
->OpenURL(params
);
131 aura::Window
* KeyboardControllerProxy::GetKeyboardWindow() {
132 if (!keyboard_contents_
) {
133 content::BrowserContext
* context
= browser_context();
134 keyboard_contents_
.reset(content::WebContents::Create(
135 content::WebContents::CreateParams(context
,
136 content::SiteInstance::CreateForURL(context
,
137 GetVirtualKeyboardUrl()))));
138 keyboard_contents_
->SetDelegate(new KeyboardContentsDelegate(this));
139 SetupWebContents(keyboard_contents_
.get());
140 LoadContents(GetVirtualKeyboardUrl());
141 keyboard_contents_
->GetNativeView()->AddObserver(this);
144 return keyboard_contents_
->GetNativeView();
147 bool KeyboardControllerProxy::HasKeyboardWindow() const {
148 return keyboard_contents_
;
151 void KeyboardControllerProxy::ShowKeyboardContainer(aura::Window
* container
) {
152 GetKeyboardWindow()->Show();
156 void KeyboardControllerProxy::HideKeyboardContainer(aura::Window
* container
) {
158 GetKeyboardWindow()->Hide();
161 void KeyboardControllerProxy::SetUpdateInputType(ui::TextInputType type
) {
164 void KeyboardControllerProxy::EnsureCaretInWorkArea() {
165 if (GetInputMethod()->GetTextInputClient()) {
166 aura::Window
* keyboard_window
= GetKeyboardWindow();
167 aura::Window
* root_window
= keyboard_window
->GetRootWindow();
168 gfx::Rect available_bounds
= root_window
->bounds();
169 gfx::Rect keyboard_bounds
= keyboard_window
->bounds();
170 available_bounds
.set_height(available_bounds
.height() -
171 keyboard_bounds
.height());
172 GetInputMethod()->GetTextInputClient()->EnsureCaretInRect(available_bounds
);
176 void KeyboardControllerProxy::LoadSystemKeyboard() {
177 DCHECK(keyboard_contents_
);
178 if (keyboard_contents_
->GetURL() != default_url_
) {
179 // TODO(bshe): The height of system virtual keyboard and IME virtual
180 // keyboard may different. The height needs to be restored too.
181 LoadContents(default_url_
);
185 void KeyboardControllerProxy::ReloadKeyboardIfNeeded() {
186 DCHECK(keyboard_contents_
);
187 if (keyboard_contents_
->GetURL() != GetVirtualKeyboardUrl()) {
188 if (keyboard_contents_
->GetURL().GetOrigin() !=
189 GetVirtualKeyboardUrl().GetOrigin()) {
190 // Sets keyboard window height to 0 before navigate to a keyboard in a
191 // different extension. This keeps the UX the same as Android.
192 gfx::Rect bounds
= GetKeyboardWindow()->bounds();
193 bounds
.set_y(bounds
.y() + bounds
.height());
194 bounds
.set_height(0);
195 GetKeyboardWindow()->SetBounds(bounds
);
197 LoadContents(GetVirtualKeyboardUrl());
201 void KeyboardControllerProxy::SetupWebContents(content::WebContents
* contents
) {
204 void KeyboardControllerProxy::OnWindowBoundsChanged(
205 aura::Window
* window
,
206 const gfx::Rect
& old_bounds
,
207 const gfx::Rect
& new_bounds
) {
209 shadow_
.reset(new wm::Shadow());
210 shadow_
->Init(wm::Shadow::STYLE_ACTIVE
);
211 shadow_
->layer()->SetVisible(true);
212 DCHECK(keyboard_contents_
->GetNativeView()->parent());
213 keyboard_contents_
->GetNativeView()->parent()->layer()->Add(
217 shadow_
->SetContentBounds(new_bounds
);
220 void KeyboardControllerProxy::OnWindowDestroyed(aura::Window
* window
) {
221 window
->RemoveObserver(this);
224 } // namespace keyboard