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. Any
10 // messages about the guest render process that the embedder might be interested
11 // in receiving should be listened for here.
13 // BrowserPluginGuest is a WebContentsObserver for the guest WebContents.
14 // BrowserPluginGuest operates under the assumption that the guest will be
15 // accessible through only one RenderViewHost for the lifetime of
16 // the guest WebContents. Thus, cross-process navigation is not supported.
18 #ifndef CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_
19 #define CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_
24 #include "base/compiler_specific.h"
25 #include "base/memory/linked_ptr.h"
26 #include "base/memory/weak_ptr.h"
27 #include "base/values.h"
28 #include "content/common/edit_command.h"
29 #include "content/common/input/input_event_ack_state.h"
30 #include "content/public/browser/browser_plugin_guest_delegate.h"
31 #include "content/public/browser/readback_types.h"
32 #include "content/public/browser/web_contents_observer.h"
33 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
34 #include "third_party/WebKit/public/web/WebDragOperation.h"
35 #include "third_party/WebKit/public/web/WebDragStatus.h"
36 #include "third_party/WebKit/public/web/WebInputEvent.h"
37 #include "ui/base/ime/text_input_mode.h"
38 #include "ui/base/ime/text_input_type.h"
39 #include "ui/gfx/rect.h"
42 struct BrowserPluginHostMsg_Attach_Params
;
43 struct BrowserPluginHostMsg_ResizeGuest_Params
;
44 struct FrameHostMsg_CompositorFrameSwappedACK_Params
;
45 struct FrameHostMsg_ReclaimCompositorResources_Params
;
46 struct FrameMsg_CompositorFrameSwapped_Params
;
48 #if defined(OS_MACOSX)
49 struct FrameHostMsg_ShowPopup_Params
;
57 class CompositorFrame
;
66 class BrowserPluginGuestManager
;
67 class RenderViewHostImpl
;
68 class RenderWidgetHost
;
69 class RenderWidgetHostView
;
73 // A browser plugin guest provides functionality for WebContents to operate in
74 // the guest role and implements guest-specific overrides for ViewHostMsg_*
77 // When a guest is initially created, it is in an unattached state. That is,
78 // it is not visible anywhere and has no embedder WebContents assigned.
79 // A BrowserPluginGuest is said to be "attached" if it has an embedder.
80 // A BrowserPluginGuest can also create a new unattached guest via
81 // CreateNewWindow. The newly created guest will live in the same partition,
82 // which means it can share storage and can script this guest.
83 class CONTENT_EXPORT BrowserPluginGuest
: public WebContentsObserver
{
85 ~BrowserPluginGuest() override
;
87 // The WebContents passed into the factory method here has not been
88 // initialized yet and so it does not yet hold a SiteInstance.
89 // BrowserPluginGuest must be constructed and installed into a WebContents
90 // prior to its initialization because WebContents needs to determine what
91 // type of WebContentsView to construct on initialization. The content
92 // embedder needs to be aware of |guest_site_instance| on the guest's
93 // construction and so we pass it in here.
94 static BrowserPluginGuest
* Create(WebContentsImpl
* web_contents
,
95 BrowserPluginGuestDelegate
* delegate
);
97 // Returns whether the given WebContents is a BrowserPlugin guest.
98 static bool IsGuest(WebContentsImpl
* web_contents
);
100 // Returns whether the given RenderviewHost is a BrowserPlugin guest.
101 static bool IsGuest(RenderViewHostImpl
* render_view_host
);
103 // BrowserPluginGuest::Init is called after the associated guest WebContents
104 // initializes. If this guest cannot navigate without being attached to a
105 // container, then this call is a no-op. For guest types that can be
106 // navigated, this call adds the associated RenderWdigetHostViewGuest to the
107 // view hierachy and sets up the appropriate RendererPreferences so that this
108 // guest can navigate and resize offscreen.
111 // Returns a WeakPtr to this BrowserPluginGuest.
112 base::WeakPtr
<BrowserPluginGuest
> AsWeakPtr();
114 // Sets the focus state of the current RenderWidgetHostView.
115 void SetFocus(RenderWidgetHost
* rwh
, bool focused
);
117 // Sets the tooltip text.
118 void SetTooltipText(const base::string16
& tooltip_text
);
120 // Sets the lock state of the pointer. Returns true if |allowed| is true and
121 // the mouse has been successfully locked.
122 bool LockMouse(bool allowed
);
124 // Return true if the mouse is locked.
125 bool mouse_locked() const { return mouse_locked_
; }
127 // Called when the embedder WebContents changes visibility.
128 void EmbedderVisibilityChanged(bool visible
);
130 // Creates a new guest WebContentsImpl with the provided |params| with |this|
132 WebContentsImpl
* CreateNewGuestWindow(
133 const WebContents::CreateParams
& params
);
135 // Returns the identifier that uniquely identifies a browser plugin guest
136 // within an embedder.
137 int browser_plugin_instance_id() const { return browser_plugin_instance_id_
; }
139 bool OnMessageReceivedFromEmbedder(const IPC::Message
& message
);
141 WebContentsImpl
* embedder_web_contents() const {
142 return attached_
? owner_web_contents_
: NULL
;
145 // Returns the embedder's RenderWidgetHostView if it is available.
146 // Returns NULL otherwise.
147 RenderWidgetHostView
* GetOwnerRenderWidgetHostView();
149 bool focused() const { return focused_
; }
150 bool visible() const { return guest_visible_
; }
151 bool is_in_destruction() { return is_in_destruction_
; }
153 void UpdateVisibility();
155 BrowserPluginGuestManager
* GetBrowserPluginGuestManager() const;
157 // WebContentsObserver implementation.
158 void DidCommitProvisionalLoadForFrame(
159 RenderFrameHost
* render_frame_host
,
161 ui::PageTransition transition_type
) override
;
163 void RenderViewReady() override
;
164 void RenderProcessGone(base::TerminationStatus status
) override
;
165 bool OnMessageReceived(const IPC::Message
& message
) override
;
166 bool OnMessageReceived(const IPC::Message
& message
,
167 RenderFrameHost
* render_frame_host
) override
;
169 // Exposes the protected web_contents() from WebContentsObserver.
170 WebContentsImpl
* GetWebContents() const;
172 gfx::Point
GetScreenCoordinates(const gfx::Point
& relative_position
) const;
174 // Helper to send messages to embedder. This methods fills the message with
175 // the correct routing id.
176 void SendMessageToEmbedder(IPC::Message
* msg
);
178 // Returns whether the guest is attached to an embedder.
179 bool attached() const { return attached_
; }
181 // Attaches this BrowserPluginGuest to the provided |embedder_web_contents|
182 // and initializes the guest with the provided |params|. Attaching a guest
183 // to an embedder implies that this guest's lifetime is no longer managed
184 // by its opener, and it can begin loading resources.
185 void Attach(int browser_plugin_instance_id
,
186 WebContentsImpl
* embedder_web_contents
,
187 const BrowserPluginHostMsg_Attach_Params
& params
);
189 // Returns whether BrowserPluginGuest is interested in receiving the given
191 static bool ShouldForwardToBrowserPluginGuest(const IPC::Message
& message
);
193 void DragSourceEndedAt(int client_x
, int client_y
, int screen_x
,
194 int screen_y
, blink::WebDragOperation operation
);
196 // Called when the drag started by this guest ends at an OS-level.
197 void EndSystemDrag();
199 void RespondToPermissionRequest(int request_id
,
201 const std::string
& user_input
);
203 void PointerLockPermissionResponse(bool allow
);
205 void SwapCompositorFrame(uint32 output_surface_id
,
208 scoped_ptr
<cc::CompositorFrame
> frame
);
210 void SetContentsOpaque(bool opaque
);
212 // Find the given |search_text| in the page. Returns true if the find request
213 // is handled by this browser plugin guest.
214 bool Find(int request_id
,
215 const base::string16
& search_text
,
216 const blink::WebFindOptions
& options
);
219 class EmbedderVisibilityObserver
;
221 // BrowserPluginGuest is a WebContentsObserver of |web_contents| and
222 // |web_contents| has to stay valid for the lifetime of BrowserPluginGuest.
223 BrowserPluginGuest(bool has_render_view
,
224 WebContentsImpl
* web_contents
,
225 BrowserPluginGuestDelegate
* delegate
);
229 void InitInternal(const BrowserPluginHostMsg_Attach_Params
& params
,
230 WebContentsImpl
* owner_web_contents
);
232 bool InAutoSizeBounds(const gfx::Size
& size
) const;
234 // Message handlers for messages from embedder.
235 void OnCompositorFrameSwappedACK(
237 const FrameHostMsg_CompositorFrameSwappedACK_Params
& params
);
238 void OnDetach(int instance_id
);
239 // Handles drag events from the embedder.
240 // When dragging, the drag events go to the embedder first, and if the drag
241 // happens on the browser plugin, then the plugin sends a corresponding
242 // drag-message to the guest. This routes the drag-message to the guest
244 void OnDragStatusUpdate(int instance_id
,
245 blink::WebDragStatus drag_status
,
246 const DropData
& drop_data
,
247 blink::WebDragOperationsMask drag_mask
,
248 const gfx::Point
& location
);
249 // Instructs the guest to execute an edit command decoded in the embedder.
250 void OnExecuteEditCommand(int instance_id
,
251 const std::string
& command
);
253 // Returns compositor resources reclaimed in the embedder to the guest.
254 void OnReclaimCompositorResources(
256 const FrameHostMsg_ReclaimCompositorResources_Params
& params
);
258 void OnLockMouse(bool user_gesture
,
259 bool last_unlocked_by_target
,
261 void OnLockMouseAck(int instance_id
, bool succeeded
);
262 // Resizes the guest's web contents.
264 int browser_plugin_instance_id
,
265 const BrowserPluginHostMsg_ResizeGuest_Params
& params
);
266 void OnSetFocus(int instance_id
, bool focused
);
267 // Sets the name of the guest so that other guests in the same partition can
269 void OnSetName(int instance_id
, const std::string
& name
);
270 // Updates the size state of the guest.
271 void OnSetEditCommandsForNextKeyEvent(
273 const std::vector
<EditCommand
>& edit_commands
);
274 // The guest WebContents is visible if both its embedder is visible and
275 // the browser plugin element is visible. If either one is not then the
276 // WebContents is marked as hidden. A hidden WebContents will consume
277 // fewer GPU and CPU resources.
279 // When every WebContents in a RenderProcessHost is hidden, it will lower
280 // the priority of the process (see RenderProcessHostImpl::WidgetHidden).
282 // It will also send a message to the guest renderer process to cleanup
283 // resources such as dropping back buffers and adjusting memory limits (if in
284 // compositing mode, see CCLayerTreeHost::setVisible).
286 // Additionally, it will slow down Javascript execution and garbage
287 // collection. See RenderThreadImpl::IdleHandler (executed when hidden) and
288 // RenderThreadImpl::IdleHandlerInForegroundTab (executed when visible).
289 void OnSetVisibility(int instance_id
, bool visible
);
290 void OnUnlockMouse();
291 void OnUnlockMouseAck(int instance_id
);
292 void OnUpdateGeometry(int instance_id
, const gfx::Rect
& view_rect
);
294 void OnTextInputTypeChanged(ui::TextInputType type
,
295 ui::TextInputMode input_mode
,
296 bool can_compose_inline
,
298 void OnImeSetComposition(
300 const std::string
& text
,
301 const std::vector
<blink::WebCompositionUnderline
>& underlines
,
304 void OnImeConfirmComposition(
306 const std::string
& text
,
307 bool keep_selection
);
308 void OnExtendSelectionAndDelete(int instance_id
, int before
, int after
);
309 void OnImeCancelComposition();
310 #if defined(OS_MACOSX) || defined(USE_AURA)
311 void OnImeCompositionRangeChanged(
312 const gfx::Range
& range
,
313 const std::vector
<gfx::Rect
>& character_bounds
);
316 // Message handlers for messages from guest.
317 void OnHandleInputEventAck(
318 blink::WebInputEvent::Type event_type
,
319 InputEventAckState ack_result
);
320 void OnHasTouchEventHandlers(bool accept
);
321 #if defined(OS_MACOSX)
322 // On MacOS X popups are painted by the browser process. We handle them here
323 // so that they are positioned correctly.
324 void OnShowPopup(RenderFrameHost
* render_frame_host
,
325 const FrameHostMsg_ShowPopup_Params
& params
);
327 void OnShowWidget(int route_id
, const gfx::Rect
& initial_pos
);
328 void OnTakeFocus(bool reverse
);
329 void OnUpdateFrameName(int frame_id
,
331 const std::string
& name
);
333 // Forwards all messages from the |pending_messages_| queue to the embedder.
334 void SendQueuedMessages();
336 // The last tooltip that was set with SetTooltipText().
337 base::string16 current_tooltip_text_
;
339 scoped_ptr
<EmbedderVisibilityObserver
> embedder_visibility_observer_
;
340 WebContentsImpl
* owner_web_contents_
;
342 // Indicates whether this guest has been attached to a container.
345 // An identifier that uniquely identifies a browser plugin within an embedder.
346 int browser_plugin_instance_id_
;
347 float guest_device_scale_factor_
;
348 gfx::Rect guest_window_rect_
;
351 bool pending_lock_request_
;
353 bool embedder_visible_
;
354 // Whether the browser plugin is inside a plugin document.
355 bool is_full_page_plugin_
;
357 // Indicates that this BrowserPluginGuest has associated renderer-side state.
358 // This is used to determine whether or not to create a new RenderView when
359 // this guest is attached. A BrowserPluginGuest would have renderer-side state
360 // prior to attachment if it is created via a call to window.open and
361 // maintains a JavaScript reference to its opener.
362 bool has_render_view_
;
364 // Last seen size of guest contents (by SwapCompositorFrame).
365 gfx::Size last_seen_view_size_
;
366 // Last seen size of BrowserPlugin (by OnResizeGuest).
367 gfx::Size last_seen_browser_plugin_size_
;
369 bool is_in_destruction_
;
371 // BrowserPluginGuest::Init can only be called once. This flag allows it to
372 // exit early if it's already been called.
375 // Text input type states.
376 ui::TextInputType last_text_input_type_
;
377 ui::TextInputMode last_input_mode_
;
378 int last_input_flags_
;
379 bool last_can_compose_inline_
;
381 // The is the routing ID for a swapped out RenderView for the guest
382 // WebContents in the embedder's process.
383 int guest_proxy_routing_id_
;
385 // Guests generate frames and send a CompositorFrameSwapped (CFS) message
386 // indicating the next frame is ready to be positioned and composited.
387 // Subsequent frames are not generated until the IPC is ACKed. We would like
388 // to ensure that the guest generates frames on attachment so we directly ACK
389 // an unACKed CFS. ACKs could get lost between the time a guest is detached
390 // from a container and the time it is attached elsewhere. This mitigates this
391 // race by ensuring the guest is ACKed on attachment.
392 scoped_ptr
<FrameMsg_CompositorFrameSwapped_Params
> last_pending_frame_
;
394 // This is a queue of messages that are destined to be sent to the embedder
395 // once the guest is attached to a particular embedder.
396 std::deque
<linked_ptr
<IPC::Message
> > pending_messages_
;
398 BrowserPluginGuestDelegate
* const delegate_
;
400 // Weak pointer used to ask GeolocationPermissionContext about geolocation
402 base::WeakPtrFactory
<BrowserPluginGuest
> weak_ptr_factory_
;
404 DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuest
);
407 } // namespace content
409 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_