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/web_contents_delegate.h"
11 #include "content/public/browser/web_contents_observer.h"
12 #include "ui/views/accessibility/native_view_accessibility.h"
13 #include "ui/views/controls/webview/webview_export.h"
14 #include "ui/views/view.h"
20 // Provides a view of a WebContents instance. WebView can be used standalone,
21 // creating and displaying an internally-owned WebContents; or within a full
22 // browser where the browser swaps its own WebContents instances in/out (e.g.,
25 // WebView creates and owns a single child view, a NativeViewHost, which will
26 // hold and display the native view provided by a WebContents.
28 // EmbedFullscreenWidgetMode: When enabled, WebView will observe for WebContents
29 // fullscreen changes and automatically swap the normal native view with the
30 // fullscreen native view (if different). In addition, if the WebContents is
31 // being screen-captured, the view will be centered within WebView, sized to
32 // the aspect ratio of the capture video resolution, and scaling will be avoided
34 class WEBVIEW_EXPORT WebView
: public View
,
35 public content::WebContentsDelegate
,
36 public content::WebContentsObserver
{
38 static const char kViewClassName
[];
40 explicit WebView(content::BrowserContext
* browser_context
);
43 // This creates a WebContents if none is yet associated with this WebView. The
44 // WebView owns this implicitly created WebContents.
45 content::WebContents
* GetWebContents();
47 // WebView does not assume ownership of WebContents set via this method, only
48 // those it implicitly creates via GetWebContents() above.
49 void SetWebContents(content::WebContents
* web_contents
);
51 // If |mode| is true, WebView will register itself with WebContents as a
52 // WebContentsObserver, monitor for the showing/destruction of fullscreen
53 // render widgets, and alter its child view hierarchy to embed the fullscreen
54 // widget or restore the normal WebContentsView.
55 void SetEmbedFullscreenWidgetMode(bool mode
);
57 content::BrowserContext
* browser_context() { return browser_context_
; }
59 // Loads the initial URL to display in the attached WebContents. Creates the
60 // WebContents if none is attached yet. Note that this is intended as a
61 // convenience for loading the initial URL, and so URLs are navigated with
62 // PAGE_TRANSITION_AUTO_TOPLEVEL, so this is not intended as a general purpose
63 // navigation method - use WebContents' API directly.
64 void LoadInitialURL(const GURL
& url
);
66 // Controls how the attached WebContents is resized.
67 // false = WebContents' views' bounds are updated continuously as the
68 // WebView's bounds change (default).
69 // true = WebContents' views' position is updated continuously but its size
70 // is not (which may result in some clipping or under-painting) until
71 // a continuous size operation completes. This allows for smoother
72 // resizing performance during interactive resizes and animations.
73 void SetFastResize(bool fast_resize
);
75 // Called when the WebContents is focused.
76 // TODO(beng): This view should become a WebContentsViewObserver when a
77 // WebContents is attached, and not rely on the delegate to
78 // forward this notification.
79 void OnWebContentsFocused(content::WebContents
* web_contents
);
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
;
93 ui::TextInputClient
* GetTextInputClient() override
;
96 // Swaps the owned WebContents |wc_owner_| with |new_web_contents|. Returns
97 // the previously owned WebContents.
98 scoped_ptr
<content::WebContents
> SwapWebContents(
99 scoped_ptr
<content::WebContents
> new_web_contents
);
101 // Overridden from View:
102 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
103 void ViewHierarchyChanged(
104 const ViewHierarchyChangedDetails
& details
) override
;
105 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent
& 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::WebContentsDelegate:
113 void WebContentsFocused(content::WebContents
* web_contents
) override
;
114 bool EmbedsFullscreenWidget() const override
;
116 // Overridden from content::WebContentsObserver:
117 void RenderViewDeleted(content::RenderViewHost
* render_view_host
) override
;
118 void RenderProcessGone(base::TerminationStatus status
) override
;
119 void RenderViewHostChanged(content::RenderViewHost
* old_host
,
120 content::RenderViewHost
* new_host
) override
;
121 void DidShowFullscreenWidget(int routing_id
) override
;
122 void DidDestroyFullscreenWidget(int routing_id
) override
;
123 void DidToggleFullscreenModeForTab(bool entered_fullscreen
) override
;
124 void DidAttachInterstitialPage() override
;
125 void DidDetachInterstitialPage() override
;
126 // Workaround for MSVC++ linker bug/feature that requires
127 // instantiation of the inline IPC::Listener methods in all translation units.
128 void OnChannelConnected(int32 peer_id
) override
{}
129 void OnChannelError() override
{}
130 void OnBadMessageReceived(const IPC::Message
& message
) override
{}
133 void AttachWebContents();
134 void DetachWebContents();
135 void ReattachForFullscreenChange(bool enter_fullscreen
);
136 void NotifyMaybeTextInputClientChanged();
138 // Create a regular or test web contents (based on whether we're running
139 // in a unit test or not).
140 content::WebContents
* CreateWebContents(
141 content::BrowserContext
* browser_context
);
143 NativeViewHost
* const holder_
;
144 // Non-NULL if |web_contents()| was created and is owned by this WebView.
145 scoped_ptr
<content::WebContents
> wc_owner_
;
146 // When true, WebView auto-embeds fullscreen widgets as a child view.
147 bool embed_fullscreen_widget_mode_enabled_
;
148 // Set to true while WebView is embedding a fullscreen widget view as a child
149 // view instead of the normal WebContentsView render view. Note: This will be
150 // false in the case of non-Flash fullscreen.
151 bool is_embedding_fullscreen_widget_
;
152 content::BrowserContext
* browser_context_
;
153 bool allow_accelerators_
;
154 gfx::Size preferred_size_
;
156 DISALLOW_COPY_AND_ASSIGN(WebView
);
161 #endif // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_