Upstream version 5.34.104.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/values.h"
8 #include "content/public/browser/site_instance.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents_delegate.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/browser/web_contents_view.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
20 namespace {
21
22 // Converts ui::TextInputType to string.
23 std::string TextInputTypeToString(ui::TextInputType type) {
24   switch (type) {
25     case ui::TEXT_INPUT_TYPE_NONE:
26       return "none";
27     case ui::TEXT_INPUT_TYPE_PASSWORD:
28       return "password";
29     case ui::TEXT_INPUT_TYPE_EMAIL:
30       return "email";
31     case ui::TEXT_INPUT_TYPE_NUMBER:
32       return "number";
33     case ui::TEXT_INPUT_TYPE_TELEPHONE:
34       return "tel";
35     case ui::TEXT_INPUT_TYPE_URL:
36       return "url";
37     case ui::TEXT_INPUT_TYPE_DATE:
38       return "date";
39     case ui::TEXT_INPUT_TYPE_TEXT:
40     case ui::TEXT_INPUT_TYPE_SEARCH:
41     case ui::TEXT_INPUT_TYPE_DATE_TIME:
42     case ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL:
43     case ui::TEXT_INPUT_TYPE_MONTH:
44     case ui::TEXT_INPUT_TYPE_TIME:
45     case ui::TEXT_INPUT_TYPE_WEEK:
46     case ui::TEXT_INPUT_TYPE_TEXT_AREA:
47     case ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE:
48     case ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD:
49       return "text";
50   }
51   NOTREACHED();
52   return "";
53 }
54
55 // The WebContentsDelegate for the keyboard.
56 // The delegate deletes itself when the keyboard is destroyed.
57 class KeyboardContentsDelegate : public content::WebContentsDelegate,
58                                  public content::WebContentsObserver {
59  public:
60   KeyboardContentsDelegate(keyboard::KeyboardControllerProxy* proxy)
61       : proxy_(proxy) {}
62   virtual ~KeyboardContentsDelegate() {}
63
64  private:
65   // Overridden from content::WebContentsDelegate:
66   virtual content::WebContents* OpenURLFromTab(
67       content::WebContents* source,
68       const content::OpenURLParams& params) OVERRIDE {
69     source->GetController().LoadURL(
70         params.url, params.referrer, params.transition, params.extra_headers);
71     Observe(source);
72     return source;
73   }
74
75   virtual bool IsPopupOrPanel(
76       const content::WebContents* source) const OVERRIDE {
77     return true;
78   }
79
80   virtual void MoveContents(content::WebContents* source,
81                             const gfx::Rect& pos) OVERRIDE {
82     aura::Window* keyboard = proxy_->GetKeyboardWindow();
83     gfx::Rect bounds = keyboard->bounds();
84     int new_height = pos.height();
85     bounds.set_y(bounds.y() + bounds.height() - new_height);
86     bounds.set_height(new_height);
87     proxy_->set_resizing_from_contents(true);
88     keyboard->SetBounds(bounds);
89     proxy_->set_resizing_from_contents(false);
90   }
91
92   // Overridden from content::WebContentsDelegate:
93   virtual void RequestMediaAccessPermission(content::WebContents* web_contents,
94       const content::MediaStreamRequest& request,
95       const content::MediaResponseCallback& callback) OVERRIDE {
96     proxy_->RequestAudioInput(web_contents, request, callback);
97   }
98
99   // Overridden from content::WebContentsObserver:
100   virtual void WebContentsDestroyed(content::WebContents* contents) OVERRIDE {
101     delete this;
102   }
103
104   keyboard::KeyboardControllerProxy* proxy_;
105
106   DISALLOW_COPY_AND_ASSIGN(KeyboardContentsDelegate);
107 };
108
109 }  // namespace
110
111 namespace keyboard {
112
113 KeyboardControllerProxy::KeyboardControllerProxy()
114     : default_url_(kKeyboardWebUIURL), resizing_from_contents_(false) {
115 }
116
117 KeyboardControllerProxy::~KeyboardControllerProxy() {
118 }
119
120 const GURL& KeyboardControllerProxy::GetValidUrl() {
121   return override_url_.is_valid() ? override_url_ : default_url_;
122 }
123
124 void KeyboardControllerProxy::SetOverrideContentUrl(const GURL& url) {
125   if (override_url_ == url)
126     return;
127
128   override_url_ = url;
129   // Restores the keyboard window size to default.
130   aura::Window* container = GetKeyboardWindow()->parent();
131   if (container) {
132     container->layout_manager()->OnWindowResized();
133     ReloadContents();
134   }
135 }
136
137 void KeyboardControllerProxy::ReloadContents() {
138   if (keyboard_contents_) {
139     content::OpenURLParams params(
140         GetValidUrl(),
141         content::Referrer(),
142         SINGLETON_TAB,
143         content::PAGE_TRANSITION_AUTO_TOPLEVEL,
144         false);
145     keyboard_contents_->OpenURL(params);
146   }
147 }
148
149 aura::Window* KeyboardControllerProxy::GetKeyboardWindow() {
150   if (!keyboard_contents_) {
151     content::BrowserContext* context = GetBrowserContext();
152     keyboard_contents_.reset(content::WebContents::Create(
153         content::WebContents::CreateParams(context,
154             content::SiteInstance::CreateForURL(context, GetValidUrl()))));
155     keyboard_contents_->SetDelegate(new KeyboardContentsDelegate(this));
156     SetupWebContents(keyboard_contents_.get());
157     ReloadContents();
158   }
159
160   return keyboard_contents_->GetView()->GetNativeView();
161 }
162
163 bool KeyboardControllerProxy::HasKeyboardWindow() const {
164   return keyboard_contents_;
165 }
166
167 void KeyboardControllerProxy::ShowKeyboardContainer(aura::Window* container) {
168   GetKeyboardWindow()->Show();
169   container->Show();
170 }
171
172 void KeyboardControllerProxy::HideKeyboardContainer(aura::Window* container) {
173   container->Hide();
174   GetKeyboardWindow()->Hide();
175 }
176
177 void KeyboardControllerProxy::SetUpdateInputType(ui::TextInputType type) {
178   content::WebUI* webui = keyboard_contents_ ?
179       keyboard_contents_->GetCommittedWebUI() : NULL;
180
181   if (webui &&
182       (0 != (webui->GetBindings() & content::BINDINGS_POLICY_WEB_UI))) {
183     // Only call OnTextInputBoxFocused function if it is a web ui keyboard,
184     // not an extension based keyboard.
185     base::DictionaryValue input_context;
186     input_context.SetString("type", TextInputTypeToString(type));
187     webui->CallJavascriptFunction("OnTextInputBoxFocused", input_context);
188   }
189 }
190
191 void KeyboardControllerProxy::EnsureCaretInWorkArea() {
192 }
193
194 void KeyboardControllerProxy::SetupWebContents(content::WebContents* contents) {
195 }
196
197 }  // namespace keyboard