Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / keyboard / keyboard_controller_proxy.cc
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 "ui/keyboard/keyboard_controller_proxy.h"
6
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
24 namespace {
25
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 {
30  public:
31   KeyboardContentsDelegate(keyboard::KeyboardControllerProxy* proxy)
32       : proxy_(proxy) {}
33   virtual ~KeyboardContentsDelegate() {}
34
35  private:
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);
42     Observe(source);
43     return source;
44   }
45
46   virtual bool IsPopupOrPanel(
47       const content::WebContents* source) const OVERRIDE {
48     return true;
49   }
50
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);
63   }
64
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);
70   }
71
72   // Overridden from content::WebContentsObserver:
73   virtual void WebContentsDestroyed() OVERRIDE {
74     delete this;
75   }
76
77   keyboard::KeyboardControllerProxy* proxy_;
78
79   DISALLOW_COPY_AND_ASSIGN(KeyboardContentsDelegate);
80 };
81
82 }  // namespace
83
84 namespace keyboard {
85
86 KeyboardControllerProxy::KeyboardControllerProxy()
87     : default_url_(kKeyboardURL) {
88 }
89
90 KeyboardControllerProxy::~KeyboardControllerProxy() {
91 }
92
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_;
97   } else {
98     return default_url_;
99   }
100 }
101
102 void KeyboardControllerProxy::LoadContents(const GURL& url) {
103   if (keyboard_contents_) {
104     content::OpenURLParams params(
105         url,
106         content::Referrer(),
107         SINGLETON_TAB,
108         content::PAGE_TRANSITION_AUTO_TOPLEVEL,
109         false);
110     keyboard_contents_->OpenURL(params);
111   }
112 }
113
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());
124   }
125
126   return keyboard_contents_->GetNativeView();
127 }
128
129 bool KeyboardControllerProxy::HasKeyboardWindow() const {
130   return keyboard_contents_;
131 }
132
133 void KeyboardControllerProxy::ShowKeyboardContainer(aura::Window* container) {
134   GetKeyboardWindow()->Show();
135   container->Show();
136 }
137
138 void KeyboardControllerProxy::HideKeyboardContainer(aura::Window* container) {
139   container->Hide();
140   GetKeyboardWindow()->Hide();
141 }
142
143 void KeyboardControllerProxy::SetUpdateInputType(ui::TextInputType type) {
144 }
145
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);
155   }
156 }
157
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_);
164   }
165 }
166
167 void KeyboardControllerProxy::ReloadKeyboardIfNeeded() {
168   DCHECK(keyboard_contents_);
169   if (keyboard_contents_->GetURL() != GetVirtualKeyboardUrl()) {
170     LoadContents(GetVirtualKeyboardUrl());
171   }
172 }
173
174 void KeyboardControllerProxy::SetupWebContents(content::WebContents* contents) {
175 }
176
177 }  // namespace keyboard