Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / ui / keyboard / keyboard_controller_proxy.cc
blob4a0dd4f6fe2010ffc0e83b42ee05e488e4b5f9e5
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/keyboard/keyboard_constants.h"
19 #include "ui/keyboard/keyboard_switches.h"
20 #include "ui/keyboard/keyboard_util.h"
22 namespace {
24 // The WebContentsDelegate for the keyboard.
25 // The delegate deletes itself when the keyboard is destroyed.
26 class KeyboardContentsDelegate : public content::WebContentsDelegate,
27 public content::WebContentsObserver {
28 public:
29 KeyboardContentsDelegate(keyboard::KeyboardControllerProxy* proxy)
30 : proxy_(proxy) {}
31 virtual ~KeyboardContentsDelegate() {}
33 private:
34 // Overridden from content::WebContentsDelegate:
35 virtual content::WebContents* OpenURLFromTab(
36 content::WebContents* source,
37 const content::OpenURLParams& params) OVERRIDE {
38 source->GetController().LoadURL(
39 params.url, params.referrer, params.transition, params.extra_headers);
40 Observe(source);
41 return source;
44 virtual bool IsPopupOrPanel(
45 const content::WebContents* source) const OVERRIDE {
46 return true;
49 virtual void MoveContents(content::WebContents* source,
50 const gfx::Rect& pos) OVERRIDE {
51 aura::Window* keyboard = proxy_->GetKeyboardWindow();
52 // keyboard window must have been added to keyboard container window at this
53 // point. Otherwise, wrong keyboard bounds is used and may cause problem as
54 // described in crbug.com/367788.
55 DCHECK(keyboard->parent());
56 gfx::Rect bounds = keyboard->bounds();
57 int new_height = pos.height();
58 bounds.set_y(bounds.y() + bounds.height() - new_height);
59 bounds.set_height(new_height);
60 keyboard->SetBounds(bounds);
63 // Overridden from content::WebContentsDelegate:
64 virtual void RequestMediaAccessPermission(content::WebContents* web_contents,
65 const content::MediaStreamRequest& request,
66 const content::MediaResponseCallback& callback) OVERRIDE {
67 proxy_->RequestAudioInput(web_contents, request, callback);
70 // Overridden from content::WebContentsObserver:
71 virtual void WebContentsDestroyed(content::WebContents* contents) OVERRIDE {
72 delete this;
75 keyboard::KeyboardControllerProxy* proxy_;
77 DISALLOW_COPY_AND_ASSIGN(KeyboardContentsDelegate);
80 } // namespace
82 namespace keyboard {
84 KeyboardControllerProxy::KeyboardControllerProxy()
85 : default_url_(kKeyboardURL) {
88 KeyboardControllerProxy::~KeyboardControllerProxy() {
91 const GURL& KeyboardControllerProxy::GetVirtualKeyboardUrl() {
92 if (keyboard::IsInputViewEnabled()) {
93 const GURL& override_url = GetOverrideContentUrl();
94 return override_url.is_valid() ? override_url : default_url_;
95 } else {
96 return default_url_;
100 void KeyboardControllerProxy::LoadContents(const GURL& url) {
101 if (keyboard_contents_) {
102 content::OpenURLParams params(
103 url,
104 content::Referrer(),
105 SINGLETON_TAB,
106 content::PAGE_TRANSITION_AUTO_TOPLEVEL,
107 false);
108 keyboard_contents_->OpenURL(params);
112 aura::Window* KeyboardControllerProxy::GetKeyboardWindow() {
113 if (!keyboard_contents_) {
114 content::BrowserContext* context = GetBrowserContext();
115 keyboard_contents_.reset(content::WebContents::Create(
116 content::WebContents::CreateParams(context,
117 content::SiteInstance::CreateForURL(context,
118 GetVirtualKeyboardUrl()))));
119 keyboard_contents_->SetDelegate(new KeyboardContentsDelegate(this));
120 SetupWebContents(keyboard_contents_.get());
121 LoadContents(GetVirtualKeyboardUrl());
124 return keyboard_contents_->GetNativeView();
127 bool KeyboardControllerProxy::HasKeyboardWindow() const {
128 return keyboard_contents_;
131 void KeyboardControllerProxy::ShowKeyboardContainer(aura::Window* container) {
132 GetKeyboardWindow()->Show();
133 container->Show();
136 void KeyboardControllerProxy::HideKeyboardContainer(aura::Window* container) {
137 container->Hide();
138 GetKeyboardWindow()->Hide();
141 void KeyboardControllerProxy::SetUpdateInputType(ui::TextInputType type) {
144 void KeyboardControllerProxy::EnsureCaretInWorkArea() {
147 void KeyboardControllerProxy::LoadSystemKeyboard() {
148 DCHECK(keyboard_contents_);
149 if (keyboard_contents_->GetURL() != default_url_) {
150 // TODO(bshe): The height of system virtual keyboard and IME virtual
151 // keyboard may different. The height needs to be restored too.
152 LoadContents(default_url_);
156 void KeyboardControllerProxy::ReloadKeyboardIfNeeded() {
157 DCHECK(keyboard_contents_);
158 if (keyboard_contents_->GetURL() != GetVirtualKeyboardUrl()) {
159 LoadContents(GetVirtualKeyboardUrl());
163 void KeyboardControllerProxy::SetupWebContents(content::WebContents* contents) {
166 } // namespace keyboard