1 // Copyright (c) 2012 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/views/controls/webview/webview.h"
7 #include "content/public/browser/browser_accessibility_state.h"
8 #include "content/public/browser/browser_context.h"
9 #include "content/public/browser/navigation_controller.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/render_widget_host_view.h"
12 #include "content/public/browser/web_contents.h"
13 #include "ipc/ipc_message.h"
14 #include "ui/accessibility/ax_enums.h"
15 #include "ui/accessibility/ax_view_state.h"
16 #include "ui/base/ui_base_switches_util.h"
17 #include "ui/events/event.h"
18 #include "ui/views/accessibility/native_view_accessibility.h"
19 #include "ui/views/controls/native/native_view_host.h"
20 #include "ui/views/focus/focus_manager.h"
21 #include "ui/views/views_delegate.h"
26 const char WebView::kViewClassName
[] = "WebView";
28 ////////////////////////////////////////////////////////////////////////////////
31 WebView::WebView(content::BrowserContext
* browser_context
)
32 : holder_(new NativeViewHost()),
33 embed_fullscreen_widget_mode_enabled_(false),
34 is_embedding_fullscreen_widget_(false),
35 browser_context_(browser_context
),
36 allow_accelerators_(false) {
37 AddChildView(holder_
); // Takes ownership of |holder_|.
38 NativeViewAccessibility::RegisterWebView(this);
42 SetWebContents(NULL
); // Make sure all necessary tear-down takes place.
43 NativeViewAccessibility::UnregisterWebView(this);
46 content::WebContents
* WebView::GetWebContents() {
47 if (!web_contents()) {
48 wc_owner_
.reset(CreateWebContents(browser_context_
));
49 wc_owner_
->SetDelegate(this);
50 SetWebContents(wc_owner_
.get());
52 return web_contents();
55 void WebView::SetWebContents(content::WebContents
* replacement
) {
56 if (replacement
== web_contents())
59 WebContentsObserver::Observe(replacement
);
60 // web_contents() now returns |replacement| from here onwards.
61 SetFocusable(!!web_contents());
62 if (wc_owner_
!= replacement
)
64 if (embed_fullscreen_widget_mode_enabled_
) {
65 is_embedding_fullscreen_widget_
=
66 web_contents() && web_contents()->GetFullscreenRenderWidgetHostView();
68 DCHECK(!is_embedding_fullscreen_widget_
);
71 NotifyMaybeTextInputClientChanged();
74 void WebView::SetEmbedFullscreenWidgetMode(bool enable
) {
75 DCHECK(!web_contents())
76 << "Cannot change mode while a WebContents is attached.";
77 embed_fullscreen_widget_mode_enabled_
= enable
;
80 void WebView::LoadInitialURL(const GURL
& url
) {
81 GetWebContents()->GetController().LoadURL(
82 url
, content::Referrer(), content::PAGE_TRANSITION_AUTO_TOPLEVEL
,
86 void WebView::SetFastResize(bool fast_resize
) {
87 holder_
->set_fast_resize(fast_resize
);
90 void WebView::OnWebContentsFocused(content::WebContents
* web_contents
) {
91 FocusManager
* focus_manager
= GetFocusManager();
93 focus_manager
->SetFocusedView(this);
96 void WebView::SetPreferredSize(const gfx::Size
& preferred_size
) {
97 preferred_size_
= preferred_size
;
98 PreferredSizeChanged();
101 ////////////////////////////////////////////////////////////////////////////////
102 // WebView, View overrides:
104 const char* WebView::GetClassName() const {
105 return kViewClassName
;
108 ui::TextInputClient
* WebView::GetTextInputClient() {
109 // This function delegates the text input handling to the underlying
110 // content::RenderWidgetHostView. So when the underlying RWHV is destroyed or
111 // replaced with another one, we have to notify the FocusManager through
112 // FocusManager::OnTextInputClientChanged() that the focused TextInputClient
113 // needs to be updated.
114 if (switches::IsTextInputFocusManagerEnabled() &&
115 web_contents() && !web_contents()->IsBeingDestroyed()) {
116 content::RenderWidgetHostView
* host_view
=
117 is_embedding_fullscreen_widget_
?
118 web_contents()->GetFullscreenRenderWidgetHostView() :
119 web_contents()->GetRenderWidgetHostView();
121 return host_view
->GetTextInputClient();
126 scoped_ptr
<content::WebContents
> WebView::SwapWebContents(
127 scoped_ptr
<content::WebContents
> new_web_contents
) {
129 wc_owner_
->SetDelegate(NULL
);
130 scoped_ptr
<content::WebContents
> old_web_contents(wc_owner_
.Pass());
131 wc_owner_
= new_web_contents
.Pass();
133 wc_owner_
->SetDelegate(this);
134 SetWebContents(wc_owner_
.get());
135 return old_web_contents
.Pass();
138 void WebView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
139 // In most cases, the holder is simply sized to fill this WebView's bounds.
140 // Only WebContentses that are in fullscreen mode and being screen-captured
141 // will engage the special layout/sizing behavior.
142 gfx::Rect
holder_bounds(bounds().size());
143 if (!embed_fullscreen_widget_mode_enabled_
||
145 web_contents()->GetCapturerCount() == 0 ||
146 web_contents()->GetPreferredSize().IsEmpty() ||
147 !(is_embedding_fullscreen_widget_
||
148 (web_contents()->GetDelegate() &&
149 web_contents()->GetDelegate()->
150 IsFullscreenForTabOrPending(web_contents())))) {
151 holder_
->SetBoundsRect(holder_bounds
);
155 // Size the holder to the capture video resolution and center it. If this
156 // WebView is not large enough to contain the holder at the preferred size,
157 // scale down to fit (preserving aspect ratio).
158 const gfx::Size capture_size
= web_contents()->GetPreferredSize();
159 if (capture_size
.width() <= holder_bounds
.width() &&
160 capture_size
.height() <= holder_bounds
.height()) {
161 // No scaling, just centering.
162 holder_bounds
.ClampToCenteredSize(capture_size
);
164 // Scale down, preserving aspect ratio, and center.
165 // TODO(miu): This is basically media::ComputeLetterboxRegion(), and it
166 // looks like others have written this code elsewhere. Let's considate
167 // into a shared function ui/gfx/geometry or around there.
168 const int64 x
= static_cast<int64
>(capture_size
.width()) *
169 holder_bounds
.height();
170 const int64 y
= static_cast<int64
>(capture_size
.height()) *
171 holder_bounds
.width();
173 holder_bounds
.ClampToCenteredSize(gfx::Size(
174 holder_bounds
.width(), static_cast<int>(y
/ capture_size
.width())));
176 holder_bounds
.ClampToCenteredSize(gfx::Size(
177 static_cast<int>(x
/ capture_size
.height()), holder_bounds
.height()));
181 holder_
->SetBoundsRect(holder_bounds
);
184 void WebView::ViewHierarchyChanged(
185 const ViewHierarchyChangedDetails
& details
) {
190 bool WebView::SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) {
191 if (allow_accelerators_
)
192 return FocusManager::IsTabTraversalKeyEvent(event
);
194 // Don't look-up accelerators or tab-traversal if we are showing a non-crashed
196 // We'll first give the page a chance to process the key events. If it does
197 // not process them, they'll be returned to us and we'll treat them as
198 // accelerators then.
199 return web_contents() && !web_contents()->IsCrashed();
202 void WebView::OnFocus() {
204 web_contents()->Focus();
207 void WebView::AboutToRequestFocusFromTabTraversal(bool reverse
) {
209 web_contents()->FocusThroughTabTraversal(reverse
);
212 void WebView::GetAccessibleState(ui::AXViewState
* state
) {
213 state
->role
= ui::AX_ROLE_GROUP
;
216 gfx::NativeViewAccessible
WebView::GetNativeViewAccessible() {
217 if (web_contents()) {
218 content::RenderWidgetHostView
* host_view
=
219 web_contents()->GetRenderWidgetHostView();
221 return host_view
->GetNativeViewAccessible();
223 return View::GetNativeViewAccessible();
226 gfx::Size
WebView::GetPreferredSize() const {
227 if (preferred_size_
== gfx::Size())
228 return View::GetPreferredSize();
230 return preferred_size_
;
233 ////////////////////////////////////////////////////////////////////////////////
234 // WebView, content::WebContentsDelegate implementation:
236 void WebView::WebContentsFocused(content::WebContents
* web_contents
) {
237 DCHECK(wc_owner_
.get());
238 // The WebView is only the delegate of WebContentses it creates itself.
239 OnWebContentsFocused(wc_owner_
.get());
242 bool WebView::EmbedsFullscreenWidget() const {
243 DCHECK(wc_owner_
.get());
244 return embed_fullscreen_widget_mode_enabled_
;
247 ////////////////////////////////////////////////////////////////////////////////
248 // WebView, content::WebContentsObserver implementation:
250 void WebView::RenderViewDeleted(content::RenderViewHost
* render_view_host
) {
251 NotifyMaybeTextInputClientChanged();
254 void WebView::RenderProcessGone(base::TerminationStatus status
) {
255 NotifyMaybeTextInputClientChanged();
258 void WebView::RenderViewHostChanged(content::RenderViewHost
* old_host
,
259 content::RenderViewHost
* new_host
) {
260 FocusManager
* const focus_manager
= GetFocusManager();
261 if (focus_manager
&& focus_manager
->GetFocusedView() == this)
263 NotifyMaybeTextInputClientChanged();
266 void WebView::DidShowFullscreenWidget(int routing_id
) {
267 if (embed_fullscreen_widget_mode_enabled_
)
268 ReattachForFullscreenChange(true);
271 void WebView::DidDestroyFullscreenWidget(int routing_id
) {
272 if (embed_fullscreen_widget_mode_enabled_
)
273 ReattachForFullscreenChange(false);
276 void WebView::DidToggleFullscreenModeForTab(bool entered_fullscreen
) {
277 if (embed_fullscreen_widget_mode_enabled_
)
278 ReattachForFullscreenChange(entered_fullscreen
);
281 void WebView::DidAttachInterstitialPage() {
282 NotifyMaybeTextInputClientChanged();
285 void WebView::DidDetachInterstitialPage() {
286 NotifyMaybeTextInputClientChanged();
289 ////////////////////////////////////////////////////////////////////////////////
292 void WebView::AttachWebContents() {
293 // Prevents attachment if the WebView isn't already in a Widget, or it's
295 if (!GetWidget() || !web_contents())
298 const gfx::NativeView view_to_attach
= is_embedding_fullscreen_widget_
?
299 web_contents()->GetFullscreenRenderWidgetHostView()->GetNativeView() :
300 web_contents()->GetNativeView();
301 OnBoundsChanged(bounds());
302 if (holder_
->native_view() == view_to_attach
)
304 holder_
->Attach(view_to_attach
);
306 // The view will not be focused automatically when it is attached, so we need
307 // to pass on focus to it if the FocusManager thinks the view is focused. Note
308 // that not every Widget has a focus manager.
309 FocusManager
* const focus_manager
= GetFocusManager();
310 if (focus_manager
&& focus_manager
->GetFocusedView() == this)
314 if (!is_embedding_fullscreen_widget_
) {
315 web_contents()->SetParentNativeViewAccessible(
316 parent()->GetNativeViewAccessible());
321 void WebView::DetachWebContents() {
322 if (web_contents()) {
325 if (!is_embedding_fullscreen_widget_
)
326 web_contents()->SetParentNativeViewAccessible(NULL
);
331 void WebView::ReattachForFullscreenChange(bool enter_fullscreen
) {
332 DCHECK(embed_fullscreen_widget_mode_enabled_
);
333 const bool web_contents_has_separate_fs_widget
=
334 web_contents() && web_contents()->GetFullscreenRenderWidgetHostView();
335 if (is_embedding_fullscreen_widget_
|| web_contents_has_separate_fs_widget
) {
336 // Shutting down or starting up the embedding of the separate fullscreen
337 // widget. Need to detach and re-attach to a different native view.
339 is_embedding_fullscreen_widget_
=
340 enter_fullscreen
&& web_contents_has_separate_fs_widget
;
343 // Entering or exiting "non-Flash" fullscreen mode, where the native view is
344 // the same. So, do not change attachment.
345 OnBoundsChanged(bounds());
347 NotifyMaybeTextInputClientChanged();
350 void WebView::NotifyMaybeTextInputClientChanged() {
351 // Update the TextInputClient as needed; see GetTextInputClient().
352 FocusManager
* const focus_manager
= GetFocusManager();
354 focus_manager
->OnTextInputClientChanged(this);
357 content::WebContents
* WebView::CreateWebContents(
358 content::BrowserContext
* browser_context
) {
359 content::WebContents
* contents
= NULL
;
360 if (ViewsDelegate::views_delegate
) {
361 contents
= ViewsDelegate::views_delegate
->CreateWebContents(
362 browser_context
, NULL
);
366 content::WebContents::CreateParams
create_params(
367 browser_context
, NULL
);
368 return content::WebContents::Create(create_params
);