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 #ifndef UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_
6 #define UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "content/public/browser/render_process_host_observer.h"
11 #include "content/public/browser/web_contents_delegate.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "ui/views/accessibility/native_view_accessibility.h"
14 #include "ui/views/controls/webview/webview_export.h"
15 #include "ui/views/view.h"
21 // Provides a view of a WebContents instance. WebView can be used standalone,
22 // creating and displaying an internally-owned WebContents; or within a full
23 // browser where the browser swaps its own WebContents instances in/out (e.g.,
26 // WebView creates and owns a single child view, a NativeViewHost, which will
27 // hold and display the native view provided by a WebContents.
29 // EmbedFullscreenWidgetMode: When enabled, WebView will observe for WebContents
30 // fullscreen changes and automatically swap the normal native view with the
31 // fullscreen native view (if different). In addition, if the WebContents is
32 // being screen-captured, the view will be centered within WebView, sized to
33 // the aspect ratio of the capture video resolution, and scaling will be avoided
35 class WEBVIEW_EXPORT WebView
: public View
,
36 public content::RenderProcessHostObserver
,
37 public content::WebContentsDelegate
,
38 public content::WebContentsObserver
{
40 static const char kViewClassName
[];
42 explicit WebView(content::BrowserContext
* browser_context
);
45 // This creates a WebContents if none is yet associated with this WebView. The
46 // WebView owns this implicitly created WebContents.
47 content::WebContents
* GetWebContents();
49 // WebView does not assume ownership of WebContents set via this method, only
50 // those it implicitly creates via GetWebContents() above.
51 void SetWebContents(content::WebContents
* web_contents
);
53 // If |mode| is true, WebView will register itself with WebContents as a
54 // WebContentsObserver, monitor for the showing/destruction of fullscreen
55 // render widgets, and alter its child view hierarchy to embed the fullscreen
56 // widget or restore the normal WebContentsView.
57 void SetEmbedFullscreenWidgetMode(bool mode
);
59 content::BrowserContext
* browser_context() { return browser_context_
; }
61 // Loads the initial URL to display in the attached WebContents. Creates the
62 // WebContents if none is attached yet. Note that this is intended as a
63 // convenience for loading the initial URL, and so URLs are navigated with
64 // PAGE_TRANSITION_AUTO_TOPLEVEL, so this is not intended as a general purpose
65 // navigation method - use WebContents' API directly.
66 void LoadInitialURL(const GURL
& url
);
68 // Controls how the attached WebContents is resized.
69 // false = WebContents' views' bounds are updated continuously as the
70 // WebView's bounds change (default).
71 // true = WebContents' views' position is updated continuously but its size
72 // is not (which may result in some clipping or under-painting) until
73 // a continuous size operation completes. This allows for smoother
74 // resizing performance during interactive resizes and animations.
75 void SetFastResize(bool fast_resize
);
77 // Set the background color to use while resizing with a clip. This is white
79 void SetResizeBackgroundColor(SkColor resize_background_color
);
81 // When used to host UI, we need to explicitly allow accelerators to be
82 // processed. Default is false.
83 void set_allow_accelerators(bool allow_accelerators
) {
84 allow_accelerators_
= allow_accelerators
;
87 // Sets the preferred size. If empty, View's implementation of
88 // GetPreferredSize() is used.
89 void SetPreferredSize(const gfx::Size
& preferred_size
);
91 // Overridden from View:
92 const char* GetClassName() const override
;
94 NativeViewHost
* holder() { return holder_
; }
97 // Swaps the owned WebContents |wc_owner_| with |new_web_contents|. Returns
98 // the previously owned WebContents.
99 scoped_ptr
<content::WebContents
> SwapWebContents(
100 scoped_ptr
<content::WebContents
> new_web_contents
);
102 // Called when the web contents is successfully attached.
103 virtual void OnWebContentsAttached() {}
105 // Overridden from View:
106 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
107 void ViewHierarchyChanged(
108 const ViewHierarchyChangedDetails
& details
) override
;
109 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) override
;
110 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
111 void OnFocus() override
;
112 void AboutToRequestFocusFromTabTraversal(bool reverse
) override
;
113 void GetAccessibleState(ui::AXViewState
* state
) override
;
114 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
115 gfx::Size
GetPreferredSize() const override
;
117 // Overridden from content::RenderProcessHostObserver:
118 void RenderProcessExited(content::RenderProcessHost
* host
,
119 base::TerminationStatus status
,
120 int exit_code
) override
;
121 void RenderProcessHostDestroyed(content::RenderProcessHost
* host
) override
;
123 // Overridden from content::WebContentsDelegate:
124 bool EmbedsFullscreenWidget() const override
;
126 // Overridden from content::WebContentsObserver:
127 void RenderViewReady() override
;
128 void RenderViewDeleted(content::RenderViewHost
* render_view_host
) override
;
129 void RenderViewHostChanged(content::RenderViewHost
* old_host
,
130 content::RenderViewHost
* new_host
) override
;
131 void WebContentsDestroyed() override
;
132 void DidShowFullscreenWidget(int routing_id
) override
;
133 void DidDestroyFullscreenWidget(int routing_id
) override
;
134 void DidToggleFullscreenModeForTab(bool entered_fullscreen
) override
;
135 void DidAttachInterstitialPage() override
;
136 void DidDetachInterstitialPage() override
;
137 // Workaround for MSVC++ linker bug/feature that requires
138 // instantiation of the inline IPC::Listener methods in all translation units.
139 void OnChannelConnected(int32 peer_id
) override
{}
140 void OnChannelError() override
{}
141 void OnBadMessageReceived(const IPC::Message
& message
) override
{}
142 void OnWebContentsFocused() override
;
145 friend class WebViewUnitTest
;
147 void AttachWebContents();
148 void DetachWebContents();
149 void ReattachForFullscreenChange(bool enter_fullscreen
);
150 void NotifyMaybeTextInputClientAndAccessibilityChanged();
152 // Create a regular or test web contents (based on whether we're running
153 // in a unit test or not).
154 content::WebContents
* CreateWebContents(
155 content::BrowserContext
* browser_context
);
157 NativeViewHost
* const holder_
;
158 // Non-NULL if |web_contents()| was created and is owned by this WebView.
159 scoped_ptr
<content::WebContents
> wc_owner_
;
160 // The RenderProcessHost to which this RenderProcessHostObserver is added.
161 // Since WebView::GetTextInputClient is relying on RWHV::GetTextInputClient,
162 // we have to observe the lifecycle of the underlying RWHV through
163 // RenderProcessHostObserver.
164 content::RenderProcessHost
* observing_render_process_host_
;
165 // When true, WebView auto-embeds fullscreen widgets as a child view.
166 bool embed_fullscreen_widget_mode_enabled_
;
167 // Set to true while WebView is embedding a fullscreen widget view as a child
168 // view instead of the normal WebContentsView render view. Note: This will be
169 // false in the case of non-Flash fullscreen.
170 bool is_embedding_fullscreen_widget_
;
171 content::BrowserContext
* browser_context_
;
172 bool allow_accelerators_
;
173 gfx::Size preferred_size_
;
175 DISALLOW_COPY_AND_ASSIGN(WebView
);
180 #endif // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_