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/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"
22 // Converts ui::TextInputType to string.
23 std::string TextInputTypeToString(ui::TextInputType type) {
25 case ui::TEXT_INPUT_TYPE_NONE:
27 case ui::TEXT_INPUT_TYPE_PASSWORD:
29 case ui::TEXT_INPUT_TYPE_EMAIL:
31 case ui::TEXT_INPUT_TYPE_NUMBER:
33 case ui::TEXT_INPUT_TYPE_TELEPHONE:
35 case ui::TEXT_INPUT_TYPE_URL:
37 case ui::TEXT_INPUT_TYPE_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:
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 {
60 KeyboardContentsDelegate(keyboard::KeyboardControllerProxy* proxy)
62 virtual ~KeyboardContentsDelegate() {}
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);
75 virtual bool IsPopupOrPanel(
76 const content::WebContents* source) const OVERRIDE {
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);
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);
99 // Overridden from content::WebContentsObserver:
100 virtual void WebContentsDestroyed(content::WebContents* contents) OVERRIDE {
104 keyboard::KeyboardControllerProxy* proxy_;
106 DISALLOW_COPY_AND_ASSIGN(KeyboardContentsDelegate);
113 KeyboardControllerProxy::KeyboardControllerProxy()
114 : default_url_(kKeyboardWebUIURL), resizing_from_contents_(false) {
117 KeyboardControllerProxy::~KeyboardControllerProxy() {
120 const GURL& KeyboardControllerProxy::GetValidUrl() {
121 return override_url_.is_valid() ? override_url_ : default_url_;
124 void KeyboardControllerProxy::SetOverrideContentUrl(const GURL& url) {
125 if (override_url_ == url)
129 // Restores the keyboard window size to default.
130 aura::Window* container = GetKeyboardWindow()->parent();
132 container->layout_manager()->OnWindowResized();
137 void KeyboardControllerProxy::ReloadContents() {
138 if (keyboard_contents_) {
139 content::OpenURLParams params(
143 content::PAGE_TRANSITION_AUTO_TOPLEVEL,
145 keyboard_contents_->OpenURL(params);
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());
160 return keyboard_contents_->GetView()->GetNativeView();
163 bool KeyboardControllerProxy::HasKeyboardWindow() const {
164 return keyboard_contents_;
167 void KeyboardControllerProxy::ShowKeyboardContainer(aura::Window* container) {
168 GetKeyboardWindow()->Show();
172 void KeyboardControllerProxy::HideKeyboardContainer(aura::Window* container) {
174 GetKeyboardWindow()->Hide();
177 void KeyboardControllerProxy::SetUpdateInputType(ui::TextInputType type) {
178 content::WebUI* webui = keyboard_contents_ ?
179 keyboard_contents_->GetCommittedWebUI() : NULL;
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);
191 void KeyboardControllerProxy::EnsureCaretInWorkArea() {
194 void KeyboardControllerProxy::SetupWebContents(content::WebContents* contents) {
197 } // namespace keyboard