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 // Called when the WebContents is focused.
82 // TODO(beng): This view should become a WebContentsViewObserver when a
83 // WebContents is attached, and not rely on the delegate to
84 // forward this notification.
85 void OnWebContentsFocused(content::WebContents
* web_contents
);
87 // When used to host UI, we need to explicitly allow accelerators to be
88 // processed. Default is false.
89 void set_allow_accelerators(bool allow_accelerators
) {
90 allow_accelerators_
= allow_accelerators
;
93 // Sets the preferred size. If empty, View's implementation of
94 // GetPreferredSize() is used.
95 void SetPreferredSize(const gfx::Size
& preferred_size
);
97 // Overridden from View:
98 const char* GetClassName() const override
;
99 ui::TextInputClient
* GetTextInputClient() override
;
102 // Swaps the owned WebContents |wc_owner_| with |new_web_contents|. Returns
103 // the previously owned WebContents.
104 scoped_ptr
<content::WebContents
> SwapWebContents(
105 scoped_ptr
<content::WebContents
> new_web_contents
);
107 // Overridden from View:
108 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
109 void ViewHierarchyChanged(
110 const ViewHierarchyChangedDetails
& details
) override
;
111 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) override
;
112 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
113 void OnFocus() override
;
114 void AboutToRequestFocusFromTabTraversal(bool reverse
) override
;
115 void GetAccessibleState(ui::AXViewState
* state
) override
;
116 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
117 gfx::Size
GetPreferredSize() const override
;
119 // Overridden from content::RenderProcessHostObserver:
120 void RenderProcessExited(content::RenderProcessHost
* host
,
121 base::TerminationStatus status
,
122 int exit_code
) override
;
123 void RenderProcessHostDestroyed(content::RenderProcessHost
* host
) override
;
125 // Overridden from content::WebContentsDelegate:
126 void WebContentsFocused(content::WebContents
* web_contents
) override
;
127 bool EmbedsFullscreenWidget() const override
;
129 // Overridden from content::WebContentsObserver:
130 void RenderViewReady() override
;
131 void RenderViewDeleted(content::RenderViewHost
* render_view_host
) override
;
132 void RenderViewHostChanged(content::RenderViewHost
* old_host
,
133 content::RenderViewHost
* new_host
) override
;
134 void WebContentsDestroyed() override
;
135 void DidShowFullscreenWidget(int routing_id
) override
;
136 void DidDestroyFullscreenWidget(int routing_id
) override
;
137 void DidToggleFullscreenModeForTab(bool entered_fullscreen
) override
;
138 void DidAttachInterstitialPage() override
;
139 void DidDetachInterstitialPage() override
;
140 // Workaround for MSVC++ linker bug/feature that requires
141 // instantiation of the inline IPC::Listener methods in all translation units.
142 void OnChannelConnected(int32 peer_id
) override
{}
143 void OnChannelError() override
{}
144 void OnBadMessageReceived(const IPC::Message
& message
) override
{}
147 friend class WebViewUnitTest
;
149 void AttachWebContents();
150 void DetachWebContents();
151 void ReattachForFullscreenChange(bool enter_fullscreen
);
152 void NotifyMaybeTextInputClientChanged();
154 // Create a regular or test web contents (based on whether we're running
155 // in a unit test or not).
156 content::WebContents
* CreateWebContents(
157 content::BrowserContext
* browser_context
);
159 NativeViewHost
* const holder_
;
160 // Non-NULL if |web_contents()| was created and is owned by this WebView.
161 scoped_ptr
<content::WebContents
> wc_owner_
;
162 // The RenderProcessHost to which this RenderProcessHostObserver is added.
163 // Since WebView::GetTextInputClient is relying on RWHV::GetTextInputClient,
164 // we have to observe the lifecycle of the underlying RWHV through
165 // RenderProcessHostObserver.
166 content::RenderProcessHost
* observing_render_process_host_
;
167 // When true, WebView auto-embeds fullscreen widgets as a child view.
168 bool embed_fullscreen_widget_mode_enabled_
;
169 // Set to true while WebView is embedding a fullscreen widget view as a child
170 // view instead of the normal WebContentsView render view. Note: This will be
171 // false in the case of non-Flash fullscreen.
172 bool is_embedding_fullscreen_widget_
;
173 content::BrowserContext
* browser_context_
;
174 bool allow_accelerators_
;
175 gfx::Size preferred_size_
;
177 DISALLOW_COPY_AND_ASSIGN(WebView
);
182 #endif // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_