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.
82 class CONTENT_EXPORT BrowserPluginGuest
: public GuestHost
,
83 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
,
117 blink::WebFocusType focus_type
);
119 // Sets the tooltip text.
120 void SetTooltipText(const base::string16
& tooltip_text
);
122 // Sets the lock state of the pointer. Returns true if |allowed| is true and
123 // the mouse has been successfully locked.
124 bool LockMouse(bool allowed
);
126 // Return true if the mouse is locked.
127 bool mouse_locked() const { return mouse_locked_
; }
129 // Called when the embedder WebContents changes visibility.
130 void EmbedderVisibilityChanged(bool visible
);
132 // Creates a new guest WebContentsImpl with the provided |params| with |this|
134 WebContentsImpl
* CreateNewGuestWindow(
135 const WebContents::CreateParams
& params
);
137 // Creates, if necessary, and returns the routing ID of a proxy for the guest
138 // in the owner's renderer process.
139 int GetGuestProxyRoutingID();
141 // Returns the identifier that uniquely identifies a browser plugin guest
142 // within an embedder.
143 int browser_plugin_instance_id() const { return browser_plugin_instance_id_
; }
145 bool OnMessageReceivedFromEmbedder(const IPC::Message
& message
);
147 WebContentsImpl
* embedder_web_contents() const {
148 return attached_
? owner_web_contents_
: nullptr;
151 // Returns the embedder's RenderWidgetHostView if it is available.
152 // Returns nullptr otherwise.
153 RenderWidgetHostView
* GetOwnerRenderWidgetHostView();
155 bool focused() const { return focused_
; }
156 bool visible() const { return guest_visible_
; }
157 bool is_in_destruction() { return is_in_destruction_
; }
159 void UpdateVisibility();
161 BrowserPluginGuestManager
* GetBrowserPluginGuestManager() const;
163 // WebContentsObserver implementation.
164 void DidCommitProvisionalLoadForFrame(
165 RenderFrameHost
* render_frame_host
,
167 ui::PageTransition transition_type
) override
;
169 void RenderViewReady() override
;
170 void RenderProcessGone(base::TerminationStatus status
) override
;
171 bool OnMessageReceived(const IPC::Message
& message
) override
;
172 bool OnMessageReceived(const IPC::Message
& message
,
173 RenderFrameHost
* render_frame_host
) override
;
175 // GuestHost implementation.
176 int LoadURLWithParams(
177 const NavigationController::LoadURLParams
& load_params
) override
;
178 void SizeContents(const gfx::Size
& new_size
) override
;
179 void WillDestroy() override
;
181 // Exposes the protected web_contents() from WebContentsObserver.
182 WebContentsImpl
* GetWebContents() const;
184 gfx::Point
GetScreenCoordinates(const gfx::Point
& relative_position
) const;
186 // Helper to send messages to embedder. If this guest is not yet attached,
187 // then IPCs will be queued until attachment.
188 void SendMessageToEmbedder(IPC::Message
* msg
);
190 // Returns whether the guest is attached to an embedder.
191 bool attached() const { return attached_
; }
193 // Attaches this BrowserPluginGuest to the provided |embedder_web_contents|
194 // and initializes the guest with the provided |params|. Attaching a guest
195 // to an embedder implies that this guest's lifetime is no longer managed
196 // by its opener, and it can begin loading resources.
197 void Attach(int browser_plugin_instance_id
,
198 WebContentsImpl
* embedder_web_contents
,
199 const BrowserPluginHostMsg_Attach_Params
& params
);
201 // Returns whether BrowserPluginGuest is interested in receiving the given
203 static bool ShouldForwardToBrowserPluginGuest(const IPC::Message
& message
);
205 void DragSourceEndedAt(int client_x
, int client_y
, int screen_x
,
206 int screen_y
, blink::WebDragOperation operation
);
208 // Called when the drag started by this guest ends at an OS-level.
209 void EmbedderSystemDragEnded();
210 void EndSystemDragIfApplicable();
212 void RespondToPermissionRequest(int request_id
,
214 const std::string
& user_input
);
216 void PointerLockPermissionResponse(bool allow
);
218 // The next three functions are virtual for test purposes.
219 virtual void UpdateGuestSizeIfNecessary(const gfx::Size
& frame_size
,
221 virtual void SwapCompositorFrame(uint32 output_surface_id
,
224 scoped_ptr
<cc::CompositorFrame
> frame
);
225 virtual void SetChildFrameSurface(const cc::SurfaceId
& surface_id
,
226 const gfx::Size
& frame_size
,
228 const cc::SurfaceSequence
& sequence
);
230 void SetContentsOpaque(bool opaque
);
232 // Find the given |search_text| in the page. Returns true if the find request
233 // is handled by this browser plugin guest.
234 bool Find(int request_id
,
235 const base::string16
& search_text
,
236 const blink::WebFindOptions
& options
);
237 bool StopFinding(StopFindAction action
);
241 // BrowserPluginGuest is a WebContentsObserver of |web_contents| and
242 // |web_contents| has to stay valid for the lifetime of BrowserPluginGuest.
243 // Constructor protected for testing.
244 BrowserPluginGuest(bool has_render_view
,
245 WebContentsImpl
* web_contents
,
246 BrowserPluginGuestDelegate
* delegate
);
249 class EmbedderVisibilityObserver
;
251 void InitInternal(const BrowserPluginHostMsg_Attach_Params
& params
,
252 WebContentsImpl
* owner_web_contents
);
254 bool InAutoSizeBounds(const gfx::Size
& size
) const;
256 void OnSatisfySequence(int instance_id
, const cc::SurfaceSequence
& sequence
);
257 void OnRequireSequence(int instance_id
,
258 const cc::SurfaceId
& id
,
259 const cc::SurfaceSequence
& sequence
);
260 // Message handlers for messages from embedder.
261 void OnCompositorFrameSwappedACK(
263 const FrameHostMsg_CompositorFrameSwappedACK_Params
& params
);
264 void OnDetach(int instance_id
);
265 // Handles drag events from the embedder.
266 // When dragging, the drag events go to the embedder first, and if the drag
267 // happens on the browser plugin, then the plugin sends a corresponding
268 // drag-message to the guest. This routes the drag-message to the guest
270 void OnDragStatusUpdate(int instance_id
,
271 blink::WebDragStatus drag_status
,
272 const DropData
& drop_data
,
273 blink::WebDragOperationsMask drag_mask
,
274 const gfx::Point
& location
);
275 // Instructs the guest to execute an edit command decoded in the embedder.
276 void OnExecuteEditCommand(int instance_id
,
277 const std::string
& command
);
279 // Returns compositor resources reclaimed in the embedder to the guest.
280 void OnReclaimCompositorResources(
282 const FrameHostMsg_ReclaimCompositorResources_Params
& params
);
284 void OnLockMouse(bool user_gesture
,
285 bool last_unlocked_by_target
,
287 void OnLockMouseAck(int instance_id
, bool succeeded
);
288 // Resizes the guest's web contents.
289 void OnSetFocus(int instance_id
,
291 blink::WebFocusType focus_type
);
292 // Sets the name of the guest so that other guests in the same partition can
294 void OnSetName(int instance_id
, const std::string
& name
);
295 // Updates the size state of the guest.
296 void OnSetEditCommandsForNextKeyEvent(
298 const std::vector
<EditCommand
>& edit_commands
);
299 // The guest WebContents is visible if both its embedder is visible and
300 // the browser plugin element is visible. If either one is not then the
301 // WebContents is marked as hidden. A hidden WebContents will consume
302 // fewer GPU and CPU resources.
304 // When every WebContents in a RenderProcessHost is hidden, it will lower
305 // the priority of the process (see RenderProcessHostImpl::WidgetHidden).
307 // It will also send a message to the guest renderer process to cleanup
308 // resources such as dropping back buffers and adjusting memory limits (if in
309 // compositing mode, see CCLayerTreeHost::setVisible).
311 // Additionally, it will slow down Javascript execution and garbage
312 // collection. See RenderThreadImpl::IdleHandler (executed when hidden) and
313 // RenderThreadImpl::IdleHandlerInForegroundTab (executed when visible).
314 void OnSetVisibility(int instance_id
, bool visible
);
315 void OnUnlockMouse();
316 void OnUnlockMouseAck(int instance_id
);
317 void OnUpdateGeometry(int instance_id
, const gfx::Rect
& view_rect
);
319 void OnTextInputTypeChanged(ui::TextInputType type
,
320 ui::TextInputMode input_mode
,
321 bool can_compose_inline
,
323 void OnImeSetComposition(
325 const std::string
& text
,
326 const std::vector
<blink::WebCompositionUnderline
>& underlines
,
329 void OnImeConfirmComposition(
331 const std::string
& text
,
332 bool keep_selection
);
333 void OnExtendSelectionAndDelete(int instance_id
, int before
, int after
);
334 void OnImeCancelComposition();
335 #if defined(OS_MACOSX) || defined(USE_AURA)
336 void OnImeCompositionRangeChanged(
337 const gfx::Range
& range
,
338 const std::vector
<gfx::Rect
>& character_bounds
);
341 // Message handlers for messages from guest.
342 void OnHandleInputEventAck(
343 blink::WebInputEvent::Type event_type
,
344 InputEventAckState ack_result
);
345 void OnHasTouchEventHandlers(bool accept
);
346 #if defined(OS_MACOSX)
347 // On MacOS X popups are painted by the browser process. We handle them here
348 // so that they are positioned correctly.
349 void OnShowPopup(RenderFrameHost
* render_frame_host
,
350 const FrameHostMsg_ShowPopup_Params
& params
);
352 void OnShowWidget(int route_id
, const gfx::Rect
& initial_rect
);
353 void OnTakeFocus(bool reverse
);
354 void OnUpdateFrameName(int frame_id
,
356 const std::string
& name
);
358 // Called when WillAttach is complete.
359 void OnWillAttachComplete(WebContentsImpl
* embedder_web_contents
,
360 const BrowserPluginHostMsg_Attach_Params
& params
);
362 // Forwards all messages from the |pending_messages_| queue to the embedder.
363 void SendQueuedMessages();
365 void SendTextInputTypeChangedToView(RenderWidgetHostViewBase
* guest_rwhv
);
367 // The last tooltip that was set with SetTooltipText().
368 base::string16 current_tooltip_text_
;
370 scoped_ptr
<EmbedderVisibilityObserver
> embedder_visibility_observer_
;
371 WebContentsImpl
* owner_web_contents_
;
373 // Indicates whether this guest has been attached to a container.
376 // An identifier that uniquely identifies a browser plugin within an embedder.
377 int browser_plugin_instance_id_
;
378 gfx::Rect guest_window_rect_
;
381 bool pending_lock_request_
;
383 bool embedder_visible_
;
384 // Whether the browser plugin is inside a plugin document.
385 bool is_full_page_plugin_
;
387 // Indicates that this BrowserPluginGuest has associated renderer-side state.
388 // This is used to determine whether or not to create a new RenderView when
389 // this guest is attached. A BrowserPluginGuest would have renderer-side state
390 // prior to attachment if it is created via a call to window.open and
391 // maintains a JavaScript reference to its opener.
392 bool has_render_view_
;
394 // Last seen size of guest contents (by SwapCompositorFrame).
395 gfx::Size last_seen_view_size_
;
397 bool is_in_destruction_
;
399 // BrowserPluginGuest::Init can only be called once. This flag allows it to
400 // exit early if it's already been called.
403 // Text input type states.
404 ui::TextInputType last_text_input_type_
;
405 ui::TextInputMode last_input_mode_
;
406 int last_input_flags_
;
407 bool last_can_compose_inline_
;
409 // The is the routing ID for a swapped out RenderView for the guest
410 // WebContents in the embedder's process.
411 int guest_proxy_routing_id_
;
412 // Last seen state of drag status update.
413 blink::WebDragStatus last_drag_status_
;
414 // Whether or not our embedder has seen a SystemDragEnded() call.
415 bool seen_embedder_system_drag_ended_
;
416 // Whether or not our embedder has seen a DragSourceEndedAt() call.
417 bool seen_embedder_drag_source_ended_at_
;
419 // Indicates the URL dragged into the guest if any.
422 // Guests generate frames and send a CompositorFrameSwapped (CFS) message
423 // indicating the next frame is ready to be positioned and composited.
424 // Subsequent frames are not generated until the IPC is ACKed. We would like
425 // to ensure that the guest generates frames on attachment so we directly ACK
426 // an unACKed CFS. ACKs could get lost between the time a guest is detached
427 // from a container and the time it is attached elsewhere. This mitigates this
428 // race by ensuring the guest is ACKed on attachment.
429 scoped_ptr
<FrameMsg_CompositorFrameSwapped_Params
> last_pending_frame_
;
431 // This is a queue of messages that are destined to be sent to the embedder
432 // once the guest is attached to a particular embedder.
433 std::deque
<linked_ptr
<IPC::Message
> > pending_messages_
;
435 BrowserPluginGuestDelegate
* const delegate_
;
437 // Weak pointer used to ask GeolocationPermissionContext about geolocation
439 base::WeakPtrFactory
<BrowserPluginGuest
> weak_ptr_factory_
;
441 DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuest
);
444 } // namespace content
446 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_