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/guest_host.h"
32 #include "content/public/browser/readback_types.h"
33 #include "content/public/browser/web_contents_observer.h"
34 #include "third_party/WebKit/public/platform/WebFocusType.h"
35 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
36 #include "third_party/WebKit/public/web/WebDragOperation.h"
37 #include "third_party/WebKit/public/web/WebDragStatus.h"
38 #include "third_party/WebKit/public/web/WebInputEvent.h"
39 #include "ui/base/ime/text_input_mode.h"
40 #include "ui/base/ime/text_input_type.h"
41 #include "ui/gfx/geometry/rect.h"
43 struct BrowserPluginHostMsg_Attach_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
;
53 class CompositorFrame
;
55 struct SurfaceSequence
;
64 class BrowserPluginGuestManager
;
65 class RenderViewHostImpl
;
66 class RenderWidgetHost
;
67 class RenderWidgetHostView
;
68 class RenderWidgetHostViewBase
;
72 // A browser plugin guest provides functionality for WebContents to operate in
73 // the guest role and implements guest-specific overrides for ViewHostMsg_*
76 // When a guest is initially created, it is in an unattached state. That is,
77 // it is not visible anywhere and has no embedder WebContents assigned.
78 // A BrowserPluginGuest is said to be "attached" if it has an embedder.
79 // A BrowserPluginGuest can also create a new unattached guest via
80 // CreateNewWindow. The newly created guest will live in the same partition,
81 // which means it can share storage and can script this guest.
83 // Note: in --site-per-process, all IPCs sent out from this class will be
84 // dropped on the floor since we don't have a BrowserPlugin.
85 class CONTENT_EXPORT BrowserPluginGuest
: public GuestHost
,
86 public WebContentsObserver
{
88 ~BrowserPluginGuest() override
;
90 // The WebContents passed into the factory method here has not been
91 // initialized yet and so it does not yet hold a SiteInstance.
92 // BrowserPluginGuest must be constructed and installed into a WebContents
93 // prior to its initialization because WebContents needs to determine what
94 // type of WebContentsView to construct on initialization. The content
95 // embedder needs to be aware of |guest_site_instance| on the guest's
96 // construction and so we pass it in here.
97 static BrowserPluginGuest
* Create(WebContentsImpl
* web_contents
,
98 BrowserPluginGuestDelegate
* delegate
);
100 // Returns whether the given WebContents is a BrowserPlugin guest.
101 static bool IsGuest(WebContentsImpl
* web_contents
);
103 // Returns whether the given RenderviewHost is a BrowserPlugin guest.
104 static bool IsGuest(RenderViewHostImpl
* render_view_host
);
106 // BrowserPluginGuest::Init is called after the associated guest WebContents
107 // initializes. If this guest cannot navigate without being attached to a
108 // container, then this call is a no-op. For guest types that can be
109 // navigated, this call adds the associated RenderWdigetHostViewGuest to the
110 // view hierachy and sets up the appropriate RendererPreferences so that this
111 // guest can navigate and resize offscreen.
114 // Returns a WeakPtr to this BrowserPluginGuest.
115 base::WeakPtr
<BrowserPluginGuest
> AsWeakPtr();
117 // Sets the focus state of the current RenderWidgetHostView.
118 void SetFocus(RenderWidgetHost
* rwh
,
120 blink::WebFocusType focus_type
);
122 // Sets the tooltip text.
123 void SetTooltipText(const base::string16
& tooltip_text
);
125 // Sets the lock state of the pointer. Returns true if |allowed| is true and
126 // the mouse has been successfully locked.
127 bool LockMouse(bool allowed
);
129 // Return true if the mouse is locked.
130 bool mouse_locked() const { return mouse_locked_
; }
132 // Called when the embedder WebContents changes visibility.
133 void EmbedderVisibilityChanged(bool visible
);
135 // Creates a new guest WebContentsImpl with the provided |params| with |this|
137 WebContentsImpl
* CreateNewGuestWindow(
138 const WebContents::CreateParams
& params
);
140 // Creates, if necessary, and returns the routing ID of a proxy for the guest
141 // in the owner's renderer process.
142 int GetGuestProxyRoutingID();
144 // Returns the identifier that uniquely identifies a browser plugin guest
145 // within an embedder.
146 int browser_plugin_instance_id() const { return browser_plugin_instance_id_
; }
148 bool OnMessageReceivedFromEmbedder(const IPC::Message
& message
);
150 WebContentsImpl
* embedder_web_contents() const {
151 return attached_
? owner_web_contents_
: nullptr;
154 // Returns the embedder's RenderWidgetHostView if it is available.
155 // Returns nullptr otherwise.
156 RenderWidgetHostView
* GetOwnerRenderWidgetHostView();
158 bool focused() const { return focused_
; }
159 bool visible() const { return guest_visible_
; }
160 bool is_in_destruction() { return is_in_destruction_
; }
162 void UpdateVisibility();
164 BrowserPluginGuestManager
* GetBrowserPluginGuestManager() const;
166 // WebContentsObserver implementation.
167 void DidCommitProvisionalLoadForFrame(
168 RenderFrameHost
* render_frame_host
,
170 ui::PageTransition transition_type
) override
;
172 void RenderViewReady() override
;
173 void RenderProcessGone(base::TerminationStatus status
) override
;
174 bool OnMessageReceived(const IPC::Message
& message
) override
;
175 bool OnMessageReceived(const IPC::Message
& message
,
176 RenderFrameHost
* render_frame_host
) override
;
178 // GuestHost implementation.
179 int LoadURLWithParams(
180 const NavigationController::LoadURLParams
& load_params
) override
;
181 void SizeContents(const gfx::Size
& new_size
) override
;
182 void WillDestroy() override
;
184 // Exposes the protected web_contents() from WebContentsObserver.
185 WebContentsImpl
* GetWebContents() const;
187 gfx::Point
GetScreenCoordinates(const gfx::Point
& relative_position
) const;
189 // Helper to send messages to embedder. If this guest is not yet attached,
190 // then IPCs will be queued until attachment.
191 void SendMessageToEmbedder(IPC::Message
* msg
);
193 // Returns whether the guest is attached to an embedder.
194 bool attached() const { return attached_
; }
196 // Returns true when an attachment has taken place since the last time the
197 // compositor surface was set.
198 bool has_attached_since_surface_set() const {
199 return has_attached_since_surface_set_
;
202 // Attaches this BrowserPluginGuest to the provided |embedder_web_contents|
203 // and initializes the guest with the provided |params|. Attaching a guest
204 // to an embedder implies that this guest's lifetime is no longer managed
205 // by its opener, and it can begin loading resources.
206 void Attach(int browser_plugin_instance_id
,
207 WebContentsImpl
* embedder_web_contents
,
208 const BrowserPluginHostMsg_Attach_Params
& params
);
210 // Returns whether BrowserPluginGuest is interested in receiving the given
212 static bool ShouldForwardToBrowserPluginGuest(const IPC::Message
& message
);
214 void DragSourceEndedAt(int client_x
, int client_y
, int screen_x
,
215 int screen_y
, blink::WebDragOperation operation
);
217 // Called when the drag started by this guest ends at an OS-level.
218 void EmbedderSystemDragEnded();
219 void EndSystemDragIfApplicable();
221 void RespondToPermissionRequest(int request_id
,
223 const std::string
& user_input
);
225 void PointerLockPermissionResponse(bool allow
);
227 // The next three functions are virtual for test purposes.
228 virtual void UpdateGuestSizeIfNecessary(const gfx::Size
& frame_size
,
230 virtual void SwapCompositorFrame(uint32 output_surface_id
,
233 scoped_ptr
<cc::CompositorFrame
> frame
);
234 virtual void SetChildFrameSurface(const cc::SurfaceId
& surface_id
,
235 const gfx::Size
& frame_size
,
237 const cc::SurfaceSequence
& sequence
);
239 void SetContentsOpaque(bool opaque
);
241 // Find the given |search_text| in the page. Returns true if the find request
242 // is handled by this browser plugin guest.
243 bool Find(int request_id
,
244 const base::string16
& search_text
,
245 const blink::WebFindOptions
& options
);
246 bool StopFinding(StopFindAction action
);
250 // BrowserPluginGuest is a WebContentsObserver of |web_contents| and
251 // |web_contents| has to stay valid for the lifetime of BrowserPluginGuest.
252 // Constructor protected for testing.
253 BrowserPluginGuest(bool has_render_view
,
254 WebContentsImpl
* web_contents
,
255 BrowserPluginGuestDelegate
* delegate
);
257 // Protected for testing.
258 void set_has_attached_since_surface_set_for_test(bool has_attached
) {
259 has_attached_since_surface_set_
= has_attached
;
262 void set_attached_for_test(bool attached
) {
263 attached_
= attached
;
267 class EmbedderVisibilityObserver
;
269 void InitInternal(const BrowserPluginHostMsg_Attach_Params
& params
,
270 WebContentsImpl
* owner_web_contents
);
272 bool InAutoSizeBounds(const gfx::Size
& size
) const;
274 void OnSatisfySequence(int instance_id
, const cc::SurfaceSequence
& sequence
);
275 void OnRequireSequence(int instance_id
,
276 const cc::SurfaceId
& id
,
277 const cc::SurfaceSequence
& sequence
);
278 // Message handlers for messages from embedder.
279 void OnCompositorFrameSwappedACK(
281 const FrameHostMsg_CompositorFrameSwappedACK_Params
& params
);
282 void OnDetach(int instance_id
);
283 // Handles drag events from the embedder.
284 // When dragging, the drag events go to the embedder first, and if the drag
285 // happens on the browser plugin, then the plugin sends a corresponding
286 // drag-message to the guest. This routes the drag-message to the guest
288 void OnDragStatusUpdate(int instance_id
,
289 blink::WebDragStatus drag_status
,
290 const DropData
& drop_data
,
291 blink::WebDragOperationsMask drag_mask
,
292 const gfx::Point
& location
);
293 // Instructs the guest to execute an edit command decoded in the embedder.
294 void OnExecuteEditCommand(int instance_id
,
295 const std::string
& command
);
297 // Returns compositor resources reclaimed in the embedder to the guest.
298 void OnReclaimCompositorResources(
300 const FrameHostMsg_ReclaimCompositorResources_Params
& params
);
302 void OnLockMouse(bool user_gesture
,
303 bool last_unlocked_by_target
,
305 void OnLockMouseAck(int instance_id
, bool succeeded
);
306 // Resizes the guest's web contents.
307 void OnSetFocus(int instance_id
,
309 blink::WebFocusType focus_type
);
310 // Sets the name of the guest so that other guests in the same partition can
312 void OnSetName(int instance_id
, const std::string
& name
);
313 // Updates the size state of the guest.
314 void OnSetEditCommandsForNextKeyEvent(
316 const std::vector
<EditCommand
>& edit_commands
);
317 // The guest WebContents is visible if both its embedder is visible and
318 // the browser plugin element is visible. If either one is not then the
319 // WebContents is marked as hidden. A hidden WebContents will consume
320 // fewer GPU and CPU resources.
322 // When every WebContents in a RenderProcessHost is hidden, it will lower
323 // the priority of the process (see RenderProcessHostImpl::WidgetHidden).
325 // It will also send a message to the guest renderer process to cleanup
326 // resources such as dropping back buffers and adjusting memory limits (if in
327 // compositing mode, see CCLayerTreeHost::setVisible).
329 // Additionally, it will slow down Javascript execution and garbage
330 // collection. See RenderThreadImpl::IdleHandler (executed when hidden) and
331 // RenderThreadImpl::IdleHandlerInForegroundTab (executed when visible).
332 void OnSetVisibility(int instance_id
, bool visible
);
333 void OnUnlockMouse();
334 void OnUnlockMouseAck(int instance_id
);
335 void OnUpdateGeometry(int instance_id
, const gfx::Rect
& view_rect
);
337 void OnTextInputTypeChanged(ui::TextInputType type
,
338 ui::TextInputMode input_mode
,
339 bool can_compose_inline
,
341 void OnImeSetComposition(
343 const std::string
& text
,
344 const std::vector
<blink::WebCompositionUnderline
>& underlines
,
347 void OnImeConfirmComposition(
349 const std::string
& text
,
350 bool keep_selection
);
351 void OnExtendSelectionAndDelete(int instance_id
, int before
, int after
);
352 void OnImeCancelComposition();
353 #if defined(OS_MACOSX) || defined(USE_AURA)
354 void OnImeCompositionRangeChanged(
355 const gfx::Range
& range
,
356 const std::vector
<gfx::Rect
>& character_bounds
);
359 // Message handlers for messages from guest.
360 void OnHandleInputEventAck(
361 blink::WebInputEvent::Type event_type
,
362 InputEventAckState ack_result
);
363 void OnHasTouchEventHandlers(bool accept
);
364 #if defined(OS_MACOSX)
365 // On MacOS X popups are painted by the browser process. We handle them here
366 // so that they are positioned correctly.
367 void OnShowPopup(RenderFrameHost
* render_frame_host
,
368 const FrameHostMsg_ShowPopup_Params
& params
);
370 void OnShowWidget(int route_id
, const gfx::Rect
& initial_rect
);
371 void OnTakeFocus(bool reverse
);
372 void OnUpdateFrameName(int frame_id
,
374 const std::string
& name
);
376 // Called when WillAttach is complete.
377 void OnWillAttachComplete(WebContentsImpl
* embedder_web_contents
,
378 const BrowserPluginHostMsg_Attach_Params
& params
);
380 // Forwards all messages from the |pending_messages_| queue to the embedder.
381 void SendQueuedMessages();
383 void SendTextInputTypeChangedToView(RenderWidgetHostViewBase
* guest_rwhv
);
385 // The last tooltip that was set with SetTooltipText().
386 base::string16 current_tooltip_text_
;
388 scoped_ptr
<EmbedderVisibilityObserver
> embedder_visibility_observer_
;
389 WebContentsImpl
* owner_web_contents_
;
391 // Indicates whether this guest has been attached to a container.
394 // Used to signal if a browser plugin has been attached since the last time
395 // the compositing surface was set.
396 bool has_attached_since_surface_set_
;
398 // An identifier that uniquely identifies a browser plugin within an embedder.
399 int browser_plugin_instance_id_
;
400 gfx::Rect guest_window_rect_
;
403 bool pending_lock_request_
;
405 bool embedder_visible_
;
406 // Whether the browser plugin is inside a plugin document.
407 bool is_full_page_plugin_
;
409 // Indicates that this BrowserPluginGuest has associated renderer-side state.
410 // This is used to determine whether or not to create a new RenderView when
411 // this guest is attached. A BrowserPluginGuest would have renderer-side state
412 // prior to attachment if it is created via a call to window.open and
413 // maintains a JavaScript reference to its opener.
414 bool has_render_view_
;
416 // Last seen size of guest contents (by SwapCompositorFrame).
417 gfx::Size last_seen_view_size_
;
419 bool is_in_destruction_
;
421 // BrowserPluginGuest::Init can only be called once. This flag allows it to
422 // exit early if it's already been called.
425 // Text input type states.
426 ui::TextInputType last_text_input_type_
;
427 ui::TextInputMode last_input_mode_
;
428 int last_input_flags_
;
429 bool last_can_compose_inline_
;
431 // The is the routing ID for a swapped out RenderView for the guest
432 // WebContents in the embedder's process.
433 int guest_proxy_routing_id_
;
434 // Last seen state of drag status update.
435 blink::WebDragStatus last_drag_status_
;
436 // Whether or not our embedder has seen a SystemDragEnded() call.
437 bool seen_embedder_system_drag_ended_
;
438 // Whether or not our embedder has seen a DragSourceEndedAt() call.
439 bool seen_embedder_drag_source_ended_at_
;
441 // Indicates the URL dragged into the guest if any.
444 // Guests generate frames and send a CompositorFrameSwapped (CFS) message
445 // indicating the next frame is ready to be positioned and composited.
446 // Subsequent frames are not generated until the IPC is ACKed. We would like
447 // to ensure that the guest generates frames on attachment so we directly ACK
448 // an unACKed CFS. ACKs could get lost between the time a guest is detached
449 // from a container and the time it is attached elsewhere. This mitigates this
450 // race by ensuring the guest is ACKed on attachment.
451 scoped_ptr
<FrameMsg_CompositorFrameSwapped_Params
> last_pending_frame_
;
453 // This is a queue of messages that are destined to be sent to the embedder
454 // once the guest is attached to a particular embedder.
455 std::deque
<linked_ptr
<IPC::Message
> > pending_messages_
;
457 BrowserPluginGuestDelegate
* const delegate_
;
459 // Weak pointer used to ask GeolocationPermissionContext about geolocation
461 base::WeakPtrFactory
<BrowserPluginGuest
> weak_ptr_factory_
;
463 DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuest
);
466 } // namespace content
468 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_