1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/keyboard/content/keyboard_ui_content.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/values.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_ui.h"
#include "content/public/common/bindings_policy.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/window.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/keyboard/content/keyboard_constants.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_switches.h"
#include "ui/keyboard/keyboard_util.h"
#include "ui/wm/core/shadow.h"
namespace {
// The WebContentsDelegate for the keyboard.
// The delegate deletes itself when the keyboard is destroyed.
class KeyboardContentsDelegate : public content::WebContentsDelegate,
public content::WebContentsObserver {
public:
explicit KeyboardContentsDelegate(keyboard::KeyboardUIContent* ui)
: ui_(ui) {}
~KeyboardContentsDelegate() override {}
private:
// Overridden from content::WebContentsDelegate:
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) override {
source->GetController().LoadURL(
params.url, params.referrer, params.transition, params.extra_headers);
Observe(source);
return source;
}
bool CanDragEnter(content::WebContents* source,
const content::DropData& data,
blink::WebDragOperationsMask operations_allowed) override {
return false;
}
bool ShouldCreateWebContents(
content::WebContents* web_contents,
int route_id,
int main_frame_route_id,
int main_frame_widget_route_id,
WindowContainerType window_container_type,
const std::string& frame_name,
const GURL& target_url,
const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) override {
return false;
}
bool IsPopupOrPanel(const content::WebContents* source) const override {
return true;
}
void MoveContents(content::WebContents* source,
const gfx::Rect& pos) override {
aura::Window* keyboard = ui_->GetKeyboardWindow();
// keyboard window must have been added to keyboard container window at this
// point. Otherwise, wrong keyboard bounds is used and may cause problem as
// described in crbug.com/367788.
DCHECK(keyboard->parent());
// keyboard window bounds may not set to |pos| after this call. If keyboard
// is in FULL_WIDTH mode, only the height of keyboard window will be
// changed.
keyboard->SetBounds(pos);
}
// Overridden from content::WebContentsDelegate:
void RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
const content::MediaResponseCallback& callback) override {
ui_->RequestAudioInput(web_contents, request, callback);
}
// Overridden from content::WebContentsObserver:
void WebContentsDestroyed() override { delete this; }
keyboard::KeyboardUIContent* ui_;
DISALLOW_COPY_AND_ASSIGN(KeyboardContentsDelegate);
};
} // namespace
namespace keyboard {
class WindowBoundsChangeObserver : public aura::WindowObserver {
public:
explicit WindowBoundsChangeObserver(KeyboardUIContent* ui) : ui_(ui) {}
~WindowBoundsChangeObserver() override {}
void AddObservedWindow(aura::Window* window) {
if (!window->HasObserver(this)) {
window->AddObserver(this);
observed_windows_.insert(window);
}
}
void RemoveAllObservedWindows() {
for (std::set<aura::Window*>::iterator it = observed_windows_.begin();
it != observed_windows_.end(); ++it)
(*it)->RemoveObserver(this);
observed_windows_.clear();
}
private:
void OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds) override {
ui_->UpdateInsetsForWindow(window);
}
void OnWindowDestroyed(aura::Window* window) override {
if (window->HasObserver(this))
window->RemoveObserver(this);
observed_windows_.erase(window);
}
KeyboardUIContent* ui_;
std::set<aura::Window*> observed_windows_;
DISALLOW_COPY_AND_ASSIGN(WindowBoundsChangeObserver);
};
KeyboardUIContent::KeyboardUIContent(content::BrowserContext* context)
: browser_context_(context),
default_url_(kKeyboardURL),
window_bounds_observer_(new WindowBoundsChangeObserver(this)) {
}
KeyboardUIContent::~KeyboardUIContent() {
ResetInsets();
}
void KeyboardUIContent::LoadSystemKeyboard() {
DCHECK(keyboard_contents_);
if (keyboard_contents_->GetURL() != default_url_) {
// TODO(bshe): The height of system virtual keyboard and IME virtual
// keyboard may different. The height needs to be restored too.
LoadContents(default_url_);
}
}
void KeyboardUIContent::UpdateInsetsForWindow(aura::Window* window) {
aura::Window* keyboard_container =
keyboard_controller()->GetContainerWindow();
if (!ShouldWindowOverscroll(window))
return;
scoped_ptr<content::RenderWidgetHostIterator> widgets(
content::RenderWidgetHost::GetRenderWidgetHosts());
while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
content::RenderWidgetHostView* view = widget->GetView();
if (view && window->Contains(view->GetNativeView())) {
gfx::Rect window_bounds = view->GetNativeView()->GetBoundsInScreen();
gfx::Rect intersect =
gfx::IntersectRects(window_bounds, keyboard_container->bounds());
int overlap = ShouldEnableInsets(window) ? intersect.height() : 0;
if (overlap > 0 && overlap < window_bounds.height())
view->SetInsets(gfx::Insets(0, 0, overlap, 0));
else
view->SetInsets(gfx::Insets());
return;
}
}
}
aura::Window* KeyboardUIContent::GetKeyboardWindow() {
if (!keyboard_contents_) {
content::BrowserContext* context = browser_context();
keyboard_contents_.reset(content::WebContents::Create(
content::WebContents::CreateParams(context,
content::SiteInstance::CreateForURL(context,
GetVirtualKeyboardUrl()))));
keyboard_contents_->SetDelegate(new KeyboardContentsDelegate(this));
SetupWebContents(keyboard_contents_.get());
LoadContents(GetVirtualKeyboardUrl());
keyboard_contents_->GetNativeView()->AddObserver(this);
}
return keyboard_contents_->GetNativeView();
}
bool KeyboardUIContent::HasKeyboardWindow() const {
return !!keyboard_contents_;
}
bool KeyboardUIContent::ShouldWindowOverscroll(aura::Window* window) const {
return true;
}
void KeyboardUIContent::ReloadKeyboardIfNeeded() {
DCHECK(keyboard_contents_);
if (keyboard_contents_->GetURL() != GetVirtualKeyboardUrl()) {
if (keyboard_contents_->GetURL().GetOrigin() !=
GetVirtualKeyboardUrl().GetOrigin()) {
// Sets keyboard window rectangle to 0 and close current page before
// navigate to a keyboard in a different extension. This keeps the UX the
// same as Android. Note we need to explicitly close current page as it
// might try to resize keyboard window in javascript on a resize event.
GetKeyboardWindow()->SetBounds(gfx::Rect());
keyboard_contents_->ClosePage();
keyboard_controller()->SetKeyboardMode(FULL_WIDTH);
}
LoadContents(GetVirtualKeyboardUrl());
}
}
void KeyboardUIContent::InitInsets(const gfx::Rect& new_bounds) {
// Adjust the height of the viewport for visible windows on the primary
// display.
// TODO(kevers): Add EnvObserver to properly initialize insets if a
// window is created while the keyboard is visible.
scoped_ptr<content::RenderWidgetHostIterator> widgets(
content::RenderWidgetHost::GetRenderWidgetHosts());
while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
content::RenderWidgetHostView* view = widget->GetView();
// Can be NULL, e.g. if the RenderWidget is being destroyed or
// the render process crashed.
if (view) {
aura::Window* window = view->GetNativeView();
if (ShouldWindowOverscroll(window)) {
gfx::Rect window_bounds = window->GetBoundsInScreen();
gfx::Rect intersect = gfx::IntersectRects(window_bounds,
new_bounds);
int overlap = intersect.height();
if (overlap > 0 && overlap < window_bounds.height())
view->SetInsets(gfx::Insets(0, 0, overlap, 0));
else
view->SetInsets(gfx::Insets());
AddBoundsChangedObserver(window);
}
}
}
}
void KeyboardUIContent::ResetInsets() {
const gfx::Insets insets;
scoped_ptr<content::RenderWidgetHostIterator> widgets(
content::RenderWidgetHost::GetRenderWidgetHosts());
while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
content::RenderWidgetHostView* view = widget->GetView();
if (view)
view->SetInsets(insets);
}
window_bounds_observer_->RemoveAllObservedWindows();
}
void KeyboardUIContent::SetupWebContents(content::WebContents* contents) {
}
void KeyboardUIContent::OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds) {
if (!shadow_) {
shadow_.reset(new wm::Shadow());
shadow_->Init(wm::Shadow::STYLE_ACTIVE);
shadow_->layer()->SetVisible(true);
DCHECK(keyboard_contents_->GetNativeView()->parent());
keyboard_contents_->GetNativeView()->parent()->layer()->Add(
shadow_->layer());
}
shadow_->SetContentBounds(new_bounds);
}
void KeyboardUIContent::OnWindowDestroyed(aura::Window* window) {
window->RemoveObserver(this);
}
const aura::Window* KeyboardUIContent::GetKeyboardRootWindow() const {
if (!keyboard_contents_) {
return nullptr;
}
return keyboard_contents_->GetNativeView()->GetRootWindow();
}
void KeyboardUIContent::LoadContents(const GURL& url) {
if (keyboard_contents_) {
content::OpenURLParams params(
url,
content::Referrer(),
SINGLETON_TAB,
ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
false);
keyboard_contents_->OpenURL(params);
}
}
const GURL& KeyboardUIContent::GetVirtualKeyboardUrl() {
if (keyboard::IsInputViewEnabled()) {
const GURL& override_url = GetOverrideContentUrl();
return override_url.is_valid() ? override_url : default_url_;
} else {
return default_url_;
}
}
bool KeyboardUIContent::ShouldEnableInsets(aura::Window* window) {
aura::Window* keyboard_window = GetKeyboardWindow();
return (keyboard_window->GetRootWindow() == window->GetRootWindow() &&
keyboard::IsKeyboardOverscrollEnabled() &&
keyboard_window->IsVisible() &&
keyboard_controller()->keyboard_visible());
}
void KeyboardUIContent::AddBoundsChangedObserver(aura::Window* window) {
aura::Window* target_window = window ? window->GetToplevelWindow() : nullptr;
if (target_window)
window_bounds_observer_->AddObservedWindow(target_window);
}
} // namespace keyboard
|