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
;
95 // Swaps the owned WebContents |wc_owner_| with |new_web_contents|. Returns
96 // the previously owned WebContents.
97 scoped_ptr
<content::WebContents
> SwapWebContents(
98 scoped_ptr
<content::WebContents
> new_web_contents
);
100 // Overridden from View:
101 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
102 void ViewHierarchyChanged(
103 const ViewHierarchyChangedDetails
& details
) override
;
104 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) override
;
105 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
106 void OnFocus() override
;
107 void AboutToRequestFocusFromTabTraversal(bool reverse
) override
;
108 void GetAccessibleState(ui::AXViewState
* state
) override
;
109 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
110 gfx::Size
GetPreferredSize() const override
;
112 // Overridden from content::RenderProcessHostObserver:
113 void RenderProcessExited(content::RenderProcessHost
* host
,
114 base::TerminationStatus status
,
115 int exit_code
) override
;
116 void RenderProcessHostDestroyed(content::RenderProcessHost
* host
) override
;
118 // Overridden from content::WebContentsDelegate:
119 bool EmbedsFullscreenWidget() const override
;
121 // Overridden from content::WebContentsObserver:
122 void RenderViewReady() override
;
123 void RenderViewDeleted(content::RenderViewHost
* render_view_host
) override
;
124 void RenderViewHostChanged(content::RenderViewHost
* old_host
,
125 content::RenderViewHost
* new_host
) override
;
126 void WebContentsDestroyed() override
;
127 void DidShowFullscreenWidget(int routing_id
) override
;
128 void DidDestroyFullscreenWidget(int routing_id
) override
;
129 void DidToggleFullscreenModeForTab(bool entered_fullscreen
) override
;
130 void DidAttachInterstitialPage() override
;
131 void DidDetachInterstitialPage() override
;
132 // Workaround for MSVC++ linker bug/feature that requires
133 // instantiation of the inline IPC::Listener methods in all translation units.
134 void OnChannelConnected(int32 peer_id
) override
{}
135 void OnChannelError() override
{}
136 void OnBadMessageReceived(const IPC::Message
& message
) override
{}
137 void OnWebContentsFocused() override
;
140 friend class WebViewUnitTest
;
142 void AttachWebContents();
143 void DetachWebContents();
144 void ReattachForFullscreenChange(bool enter_fullscreen
);
145 void NotifyMaybeTextInputClientAndAccessibilityChanged();
147 // Create a regular or test web contents (based on whether we're running
148 // in a unit test or not).
149 content::WebContents
* CreateWebContents(
150 content::BrowserContext
* browser_context
);
152 NativeViewHost
* const holder_
;
153 // Non-NULL if |web_contents()| was created and is owned by this WebView.
154 scoped_ptr
<content::WebContents
> wc_owner_
;
155 // The RenderProcessHost to which this RenderProcessHostObserver is added.
156 // Since WebView::GetTextInputClient is relying on RWHV::GetTextInputClient,
157 // we have to observe the lifecycle of the underlying RWHV through
158 // RenderProcessHostObserver.
159 content::RenderProcessHost
* observing_render_process_host_
;
160 // When true, WebView auto-embeds fullscreen widgets as a child view.
161 bool embed_fullscreen_widget_mode_enabled_
;
162 // Set to true while WebView is embedding a fullscreen widget view as a child
163 // view instead of the normal WebContentsView render view. Note: This will be
164 // false in the case of non-Flash fullscreen.
165 bool is_embedding_fullscreen_widget_
;
166 content::BrowserContext
* browser_context_
;
167 bool allow_accelerators_
;
168 gfx::Size preferred_size_
;
170 DISALLOW_COPY_AND_ASSIGN(WebView
);
175 #endif // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_