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 // A BrowserPluginGuest is the browser side of a browser <--> embedder
6 // renderer channel. A BrowserPlugin (a WebPlugin) is on the embedder
7 // renderer side of browser <--> embedder renderer communication.
9 // BrowserPluginGuest lives on the UI thread of the browser process. It has a
10 // helper, BrowserPluginGuestHelper, which is a RenderViewHostObserver. The
11 // helper object intercepts messages (ViewHostMsg_*) directed at the browser
12 // process and redirects them to this class. Any messages about the guest render
13 // process that the embedder might be interested in receiving should be listened
16 // BrowserPluginGuest is a WebContentsDelegate and WebContentsObserver for the
17 // guest WebContents. BrowserPluginGuest operates under the assumption that the
18 // guest will be accessible through only one RenderViewHost for the lifetime of
19 // the guest WebContents. Thus, cross-process navigation is not supported.
21 #ifndef CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_
22 #define CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_
26 #include "base/compiler_specific.h"
27 #include "base/id_map.h"
28 #include "base/shared_memory.h"
29 #include "base/time.h"
30 #include "content/port/common/input_event_ack_state.h"
31 #include "content/public/browser/notification_observer.h"
32 #include "content/public/browser/notification_registrar.h"
33 #include "content/public/browser/web_contents_delegate.h"
34 #include "content/public/browser/web_contents_observer.h"
35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDragOperation.h"
36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDragStatus.h"
37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
38 #include "ui/gfx/rect.h"
39 #include "ui/surface/transport_dib.h"
41 struct BrowserPluginHostMsg_AutoSize_Params
;
42 struct BrowserPluginHostMsg_CreateGuest_Params
;
43 struct BrowserPluginHostMsg_ResizeGuest_Params
;
44 struct ViewHostMsg_CreateWindow_Params
;
45 #if defined(OS_MACOSX)
46 struct ViewHostMsg_ShowPopup_Params
;
48 struct ViewHostMsg_UpdateRect_Params
;
58 class BrowserPluginHostFactory
;
59 class BrowserPluginEmbedder
;
60 class RenderProcessHost
;
62 // A browser plugin guest provides functionality for WebContents to operate in
63 // the guest role and implements guest specific overrides for ViewHostMsg_*
66 // BrowserPluginEmbedder is responsible for creating and destroying a guest.
67 class CONTENT_EXPORT BrowserPluginGuest
: public NotificationObserver
,
68 public WebContentsDelegate
,
69 public WebContentsObserver
{
71 virtual ~BrowserPluginGuest();
73 static BrowserPluginGuest
* Create(
75 WebContentsImpl
* web_contents
,
76 const BrowserPluginHostMsg_CreateGuest_Params
& params
);
78 // Overrides factory for testing. Default (NULL) value indicates regular
79 // (non-test) environment.
80 static void set_factory_for_testing(BrowserPluginHostFactory
* factory
) {
81 content::BrowserPluginGuest::factory_
= factory
;
84 bool OnMessageReceivedFromEmbedder(const IPC::Message
& message
);
86 void Initialize(const BrowserPluginHostMsg_CreateGuest_Params
& params
,
87 content::RenderViewHost
* render_view_host
);
89 void set_guest_hang_timeout_for_testing(const base::TimeDelta
& timeout
) {
90 guest_hang_timeout_
= timeout
;
93 void set_embedder_web_contents(WebContentsImpl
* web_contents
) {
94 embedder_web_contents_
= web_contents
;
96 WebContentsImpl
* embedder_web_contents() const {
97 return embedder_web_contents_
;
100 bool focused() const { return focused_
; }
101 bool visible() const { return visible_
; }
103 void UpdateVisibility();
105 // NotificationObserver implementation.
106 virtual void Observe(int type
,
107 const NotificationSource
& source
,
108 const NotificationDetails
& details
) OVERRIDE
;
110 // WebContentsObserver implementation.
111 virtual void DidStartProvisionalLoadForFrame(
113 int64 parent_frame_id
,
115 const GURL
& validated_url
,
117 bool is_iframe_srcdoc
,
118 RenderViewHost
* render_view_host
) OVERRIDE
;
119 virtual void DidFailProvisionalLoad(
122 const GURL
& validated_url
,
124 const string16
& error_description
,
125 RenderViewHost
* render_view_host
) OVERRIDE
;
126 virtual void DidCommitProvisionalLoadForFrame(
130 PageTransition transition_type
,
131 RenderViewHost
* render_view_host
) OVERRIDE
;
132 virtual void DidStopLoading(RenderViewHost
* render_view_host
) OVERRIDE
;
134 virtual void RenderViewReady() OVERRIDE
;
135 virtual void RenderViewGone(base::TerminationStatus status
) OVERRIDE
;
136 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
139 // WebContentsDelegate implementation.
140 virtual bool CanDownload(RenderViewHost
* render_view_host
,
142 const std::string
& request_method
) OVERRIDE
;
143 virtual bool HandleContextMenu(const ContextMenuParams
& params
) OVERRIDE
;
144 virtual void RendererUnresponsive(WebContents
* source
) OVERRIDE
;
145 virtual void RendererResponsive(WebContents
* source
) OVERRIDE
;
146 virtual void RunFileChooser(WebContents
* web_contents
,
147 const FileChooserParams
& params
) OVERRIDE
;
148 virtual bool ShouldFocusPageAfterCrash() OVERRIDE
;
150 // Exposes the protected web_contents() from WebContentsObserver.
151 WebContents
* GetWebContents();
153 // Kill the guest process.
156 // Overridden in tests.
157 virtual void SetDamageBuffer(
158 const BrowserPluginHostMsg_ResizeGuest_Params
& params
);
160 gfx::Point
GetScreenCoordinates(const gfx::Point
& relative_position
) const;
162 // Helper to send messages to embedder. Overridden in test implementation
163 // since we want to intercept certain messages for testing.
164 virtual void SendMessageToEmbedder(IPC::Message
* msg
);
166 // Returns the embedder's routing ID.
167 int embedder_routing_id() const;
168 // Returns the identifier that uniquely identifies a browser plugin guest
169 // within an embedder.
170 int instance_id() const { return instance_id_
; }
173 friend class TestBrowserPluginGuest
;
175 BrowserPluginGuest(int instance_id
,
176 WebContentsImpl
* web_contents
,
177 const BrowserPluginHostMsg_CreateGuest_Params
& params
);
179 base::SharedMemory
* damage_buffer() const { return damage_buffer_
.get(); }
180 const gfx::Size
& damage_view_size() const { return damage_view_size_
; }
181 float damage_buffer_scale_factor() const {
182 return damage_buffer_scale_factor_
;
184 // Returns the damage buffer corresponding to the handle in resize |params|.
185 base::SharedMemory
* GetDamageBufferFromEmbedder(
186 const BrowserPluginHostMsg_ResizeGuest_Params
& params
);
188 // Called when a redirect notification occurs.
189 void LoadRedirect(const GURL
& old_url
,
193 bool InAutoSizeBounds(const gfx::Size
& size
) const;
195 // Message handlers for messsages from embedder.
197 // Handles drag events from the embedder.
198 // When dragging, the drag events go to the embedder first, and if the drag
199 // happens on the browser plugin, then the plugin sends a corresponding
200 // drag-message to the guest. This routes the drag-message to the guest
202 void OnDragStatusUpdate(int instance_id
,
203 WebKit::WebDragStatus drag_status
,
204 const WebDropData
& drop_data
,
205 WebKit::WebDragOperationsMask drag_mask
,
206 const gfx::Point
& location
);
207 // If possible, navigate the guest to |relative_index| entries away from the
208 // current navigation entry.
209 virtual void OnGo(int instance_id
, int relative_index
);
210 // Overriden in tests.
211 virtual void OnHandleInputEvent(int instance_id
,
212 const gfx::Rect
& guest_window_rect
,
213 const WebKit::WebInputEvent
* event
);
214 void OnNavigateGuest(int instance_id
, const std::string
& src
);
215 // Reload the guest. Overriden in tests.
216 virtual void OnReload(int instance_id
);
217 // Grab the new damage buffer from the embedder, and resize the guest's
219 void OnResizeGuest(int instance_id
,
220 const BrowserPluginHostMsg_ResizeGuest_Params
& params
);
221 // Overriden in tests.
222 virtual void OnSetFocus(int instance_id
, bool focused
);
223 // Sets the name of the guest so that other guests in the same partition can
225 void OnSetName(int instance_id
, const std::string
& name
);
226 // Updates the size state of the guest.
229 const BrowserPluginHostMsg_AutoSize_Params
& auto_size_params
,
230 const BrowserPluginHostMsg_ResizeGuest_Params
& resize_guest_params
);
231 // The guest WebContents is visible if both its embedder is visible and
232 // the browser plugin element is visible. If either one is not then the
233 // WebContents is marked as hidden. A hidden WebContents will consume
234 // fewer GPU and CPU resources.
236 // When every WebContents in a RenderProcessHost is hidden, it will lower
237 // the priority of the process (see RenderProcessHostImpl::WidgetHidden).
239 // It will also send a message to the guest renderer process to cleanup
240 // resources such as dropping back buffers and adjusting memory limits (if in
241 // compositing mode, see CCLayerTreeHost::setVisible).
243 // Additionally, it will slow down Javascript execution and garbage
244 // collection. See RenderThreadImpl::IdleHandler (executed when hidden) and
245 // RenderThreadImpl::IdleHandlerInForegroundTab (executed when visible).
246 void OnSetVisibility(int instance_id
, bool visible
);
247 // Stop loading the guest. Overriden in tests.
248 virtual void OnStop(int instance_id
);
249 void OnTerminateGuest(int instance_id
);
250 void OnUpdateRectACK(
252 const BrowserPluginHostMsg_AutoSize_Params
& auto_size_params
,
253 const BrowserPluginHostMsg_ResizeGuest_Params
& resize_guest_params
);
256 // Message handlers for messages from guest.
258 void OnCreateWindow(const ViewHostMsg_CreateWindow_Params
& params
,
261 int64
* cloned_session_storage_namespace_id
);
262 void OnHandleInputEventAck(
263 WebKit::WebInputEvent::Type event_type
,
264 InputEventAckState ack_result
);
265 void OnHasTouchEventHandlers(bool accept
);
266 void OnSetCursor(const WebCursor
& cursor
);
267 // On MacOSX popups are painted by the browser process. We handle them here
268 // so that they are positioned correctly.
269 #if defined(OS_MACOSX)
270 void OnShowPopup(const ViewHostMsg_ShowPopup_Params
& params
);
272 void OnShowWidget(int route_id
, const gfx::Rect
& initial_pos
);
273 // Overriden in tests.
274 virtual void OnTakeFocus(bool reverse
);
275 void OnUpdateDragCursor(WebKit::WebDragOperation operation
);
276 void OnUpdateFrameName(int frame_id
,
278 const std::string
& name
);
279 void OnUpdateRect(const ViewHostMsg_UpdateRect_Params
& params
);
281 // Static factory instance (always NULL for non-test).
282 static content::BrowserPluginHostFactory
* factory_
;
284 NotificationRegistrar notification_registrar_
;
285 WebContentsImpl
* embedder_web_contents_
;
286 // An identifier that uniquely identifies a browser plugin guest within an
289 scoped_ptr
<base::SharedMemory
> damage_buffer_
;
290 // An identifier that uniquely identifies a damage buffer.
291 uint32 damage_buffer_sequence_id_
;
292 size_t damage_buffer_size_
;
293 gfx::Size damage_view_size_
;
294 float damage_buffer_scale_factor_
;
295 gfx::Rect guest_window_rect_
;
296 gfx::Rect guest_screen_rect_
;
297 base::TimeDelta guest_hang_timeout_
;
301 bool auto_size_enabled_
;
302 gfx::Size max_auto_size_
;
303 gfx::Size min_auto_size_
;
305 DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuest
);
308 } // namespace content
310 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_