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/platform/WebFocusType.h"
34 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
35 #include "third_party/WebKit/public/web/WebDragOperation.h"
36 #include "third_party/WebKit/public/web/WebDragStatus.h"
37 #include "third_party/WebKit/public/web/WebInputEvent.h"
38 #include "ui/base/ime/text_input_mode.h"
39 #include "ui/base/ime/text_input_type.h"
40 #include "ui/gfx/geometry/rect.h"
44 struct BrowserPluginHostMsg_Attach_Params
;
45 struct FrameHostMsg_CompositorFrameSwappedACK_Params
;
46 struct FrameHostMsg_ReclaimCompositorResources_Params
;
47 struct FrameMsg_CompositorFrameSwapped_Params
;
49 #if defined(OS_MACOSX)
50 struct FrameHostMsg_ShowPopup_Params
;
54 class CompositorFrame
;
63 class BrowserPluginGuestManager
;
64 class RenderViewHostImpl
;
65 class RenderWidgetHost
;
66 class RenderWidgetHostView
;
70 // A browser plugin guest provides functionality for WebContents to operate in
71 // the guest role and implements guest-specific overrides for ViewHostMsg_*
74 // When a guest is initially created, it is in an unattached state. That is,
75 // it is not visible anywhere and has no embedder WebContents assigned.
76 // A BrowserPluginGuest is said to be "attached" if it has an embedder.
77 // A BrowserPluginGuest can also create a new unattached guest via
78 // CreateNewWindow. The newly created guest will live in the same partition,
79 // which means it can share storage and can script this guest.
80 class CONTENT_EXPORT BrowserPluginGuest
: public GuestSizer
,
81 public WebContentsObserver
{
83 ~BrowserPluginGuest() override
;
85 // The WebContents passed into the factory method here has not been
86 // initialized yet and so it does not yet hold a SiteInstance.
87 // BrowserPluginGuest must be constructed and installed into a WebContents
88 // prior to its initialization because WebContents needs to determine what
89 // type of WebContentsView to construct on initialization. The content
90 // embedder needs to be aware of |guest_site_instance| on the guest's
91 // construction and so we pass it in here.
92 static BrowserPluginGuest
* Create(WebContentsImpl
* web_contents
,
93 BrowserPluginGuestDelegate
* delegate
);
95 // Returns whether the given WebContents is a BrowserPlugin guest.
96 static bool IsGuest(WebContentsImpl
* web_contents
);
98 // Returns whether the given RenderviewHost is a BrowserPlugin guest.
99 static bool IsGuest(RenderViewHostImpl
* render_view_host
);
101 // BrowserPluginGuest::Init is called after the associated guest WebContents
102 // initializes. If this guest cannot navigate without being attached to a
103 // container, then this call is a no-op. For guest types that can be
104 // navigated, this call adds the associated RenderWdigetHostViewGuest to the
105 // view hierachy and sets up the appropriate RendererPreferences so that this
106 // guest can navigate and resize offscreen.
109 // Returns a WeakPtr to this BrowserPluginGuest.
110 base::WeakPtr
<BrowserPluginGuest
> AsWeakPtr();
112 // Sets the focus state of the current RenderWidgetHostView.
113 void SetFocus(RenderWidgetHost
* rwh
,
115 blink::WebFocusType focus_type
);
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_
: nullptr;
145 // Returns the embedder's RenderWidgetHostView if it is available.
146 // Returns nullptr 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 // GuestSizer implementation.
170 void SizeContents(const gfx::Size
& new_size
) override
;
172 // Exposes the protected web_contents() from WebContentsObserver.
173 WebContentsImpl
* GetWebContents() const;
175 gfx::Point
GetScreenCoordinates(const gfx::Point
& relative_position
) const;
177 // Helper to send messages to embedder. If this guest is not yet attached,
178 // then IPCs will be queued until attachment.
179 void SendMessageToEmbedder(IPC::Message
* msg
);
181 // Returns whether the guest is attached to an embedder.
182 bool attached() const { return attached_
; }
184 // Attaches this BrowserPluginGuest to the provided |embedder_web_contents|
185 // and initializes the guest with the provided |params|. Attaching a guest
186 // to an embedder implies that this guest's lifetime is no longer managed
187 // by its opener, and it can begin loading resources.
188 void Attach(int browser_plugin_instance_id
,
189 WebContentsImpl
* embedder_web_contents
,
190 const BrowserPluginHostMsg_Attach_Params
& params
);
192 // Returns whether BrowserPluginGuest is interested in receiving the given
194 static bool ShouldForwardToBrowserPluginGuest(const IPC::Message
& message
);
196 void DragSourceEndedAt(int client_x
, int client_y
, int screen_x
,
197 int screen_y
, blink::WebDragOperation operation
);
199 // Called when the drag started by this guest ends at an OS-level.
200 void EmbedderSystemDragEnded();
201 void EndSystemDragIfApplicable();
203 void RespondToPermissionRequest(int request_id
,
205 const std::string
& user_input
);
207 void PointerLockPermissionResponse(bool allow
);
209 void SwapCompositorFrame(uint32 output_surface_id
,
212 scoped_ptr
<cc::CompositorFrame
> frame
);
214 void SetContentsOpaque(bool opaque
);
216 // Find the given |search_text| in the page. Returns true if the find request
217 // is handled by this browser plugin guest.
218 bool Find(int request_id
,
219 const base::string16
& search_text
,
220 const blink::WebFindOptions
& options
);
221 bool StopFinding(StopFindAction action
);
224 class EmbedderVisibilityObserver
;
226 // BrowserPluginGuest is a WebContentsObserver of |web_contents| and
227 // |web_contents| has to stay valid for the lifetime of BrowserPluginGuest.
228 BrowserPluginGuest(bool has_render_view
,
229 WebContentsImpl
* web_contents
,
230 BrowserPluginGuestDelegate
* delegate
);
234 void InitInternal(const BrowserPluginHostMsg_Attach_Params
& params
,
235 WebContentsImpl
* owner_web_contents
);
237 bool InAutoSizeBounds(const gfx::Size
& size
) const;
239 // Message handlers for messages from embedder.
240 void OnCompositorFrameSwappedACK(
242 const FrameHostMsg_CompositorFrameSwappedACK_Params
& params
);
243 void OnDetach(int instance_id
);
244 // Handles drag events from the embedder.
245 // When dragging, the drag events go to the embedder first, and if the drag
246 // happens on the browser plugin, then the plugin sends a corresponding
247 // drag-message to the guest. This routes the drag-message to the guest
249 void OnDragStatusUpdate(int instance_id
,
250 blink::WebDragStatus drag_status
,
251 const DropData
& drop_data
,
252 blink::WebDragOperationsMask drag_mask
,
253 const gfx::Point
& location
);
254 // Instructs the guest to execute an edit command decoded in the embedder.
255 void OnExecuteEditCommand(int instance_id
,
256 const std::string
& command
);
258 // Returns compositor resources reclaimed in the embedder to the guest.
259 void OnReclaimCompositorResources(
261 const FrameHostMsg_ReclaimCompositorResources_Params
& params
);
263 void OnLockMouse(bool user_gesture
,
264 bool last_unlocked_by_target
,
266 void OnLockMouseAck(int instance_id
, bool succeeded
);
267 // Resizes the guest's web contents.
268 void OnSetFocus(int instance_id
,
270 blink::WebFocusType focus_type
);
271 // Sets the name of the guest so that other guests in the same partition can
273 void OnSetName(int instance_id
, const std::string
& name
);
274 // Updates the size state of the guest.
275 void OnSetEditCommandsForNextKeyEvent(
277 const std::vector
<EditCommand
>& edit_commands
);
278 // The guest WebContents is visible if both its embedder is visible and
279 // the browser plugin element is visible. If either one is not then the
280 // WebContents is marked as hidden. A hidden WebContents will consume
281 // fewer GPU and CPU resources.
283 // When every WebContents in a RenderProcessHost is hidden, it will lower
284 // the priority of the process (see RenderProcessHostImpl::WidgetHidden).
286 // It will also send a message to the guest renderer process to cleanup
287 // resources such as dropping back buffers and adjusting memory limits (if in
288 // compositing mode, see CCLayerTreeHost::setVisible).
290 // Additionally, it will slow down Javascript execution and garbage
291 // collection. See RenderThreadImpl::IdleHandler (executed when hidden) and
292 // RenderThreadImpl::IdleHandlerInForegroundTab (executed when visible).
293 void OnSetVisibility(int instance_id
, bool visible
);
294 void OnUnlockMouse();
295 void OnUnlockMouseAck(int instance_id
);
296 void OnUpdateGeometry(int instance_id
, const gfx::Rect
& view_rect
);
298 void OnTextInputTypeChanged(ui::TextInputType type
,
299 ui::TextInputMode input_mode
,
300 bool can_compose_inline
,
302 void OnImeSetComposition(
304 const std::string
& text
,
305 const std::vector
<blink::WebCompositionUnderline
>& underlines
,
308 void OnImeConfirmComposition(
310 const std::string
& text
,
311 bool keep_selection
);
312 void OnExtendSelectionAndDelete(int instance_id
, int before
, int after
);
313 void OnImeCancelComposition();
314 #if defined(OS_MACOSX) || defined(USE_AURA)
315 void OnImeCompositionRangeChanged(
316 const gfx::Range
& range
,
317 const std::vector
<gfx::Rect
>& character_bounds
);
320 // Message handlers for messages from guest.
321 void OnHandleInputEventAck(
322 blink::WebInputEvent::Type event_type
,
323 InputEventAckState ack_result
);
324 void OnHasTouchEventHandlers(bool accept
);
325 #if defined(OS_MACOSX)
326 // On MacOS X popups are painted by the browser process. We handle them here
327 // so that they are positioned correctly.
328 void OnShowPopup(RenderFrameHost
* render_frame_host
,
329 const FrameHostMsg_ShowPopup_Params
& params
);
331 void OnShowWidget(int route_id
, const gfx::Rect
& initial_rect
);
332 void OnTakeFocus(bool reverse
);
333 void OnUpdateFrameName(int frame_id
,
335 const std::string
& name
);
337 // Forwards all messages from the |pending_messages_| queue to the embedder.
338 void SendQueuedMessages();
340 // The last tooltip that was set with SetTooltipText().
341 base::string16 current_tooltip_text_
;
343 scoped_ptr
<EmbedderVisibilityObserver
> embedder_visibility_observer_
;
344 WebContentsImpl
* owner_web_contents_
;
346 // Indicates whether this guest has been attached to a container.
349 // An identifier that uniquely identifies a browser plugin within an embedder.
350 int browser_plugin_instance_id_
;
351 gfx::Rect guest_window_rect_
;
354 bool pending_lock_request_
;
356 bool embedder_visible_
;
357 // Whether the browser plugin is inside a plugin document.
358 bool is_full_page_plugin_
;
360 // Indicates that this BrowserPluginGuest has associated renderer-side state.
361 // This is used to determine whether or not to create a new RenderView when
362 // this guest is attached. A BrowserPluginGuest would have renderer-side state
363 // prior to attachment if it is created via a call to window.open and
364 // maintains a JavaScript reference to its opener.
365 bool has_render_view_
;
367 // Last seen size of guest contents (by SwapCompositorFrame).
368 gfx::Size last_seen_view_size_
;
370 bool is_in_destruction_
;
372 // BrowserPluginGuest::Init can only be called once. This flag allows it to
373 // exit early if it's already been called.
376 // Text input type states.
377 ui::TextInputType last_text_input_type_
;
378 ui::TextInputMode last_input_mode_
;
379 int last_input_flags_
;
380 bool last_can_compose_inline_
;
382 // The is the routing ID for a swapped out RenderView for the guest
383 // WebContents in the embedder's process.
384 int guest_proxy_routing_id_
;
385 // Last seen state of drag status update.
386 blink::WebDragStatus last_drag_status_
;
387 // Whether or not our embedder has seen a SystemDragEnded() call.
388 bool seen_embedder_system_drag_ended_
;
389 // Whether or not our embedder has seen a DragSourceEndedAt() call.
390 bool seen_embedder_drag_source_ended_at_
;
392 // Indicates the URL dragged into the guest if any.
395 // Guests generate frames and send a CompositorFrameSwapped (CFS) message
396 // indicating the next frame is ready to be positioned and composited.
397 // Subsequent frames are not generated until the IPC is ACKed. We would like
398 // to ensure that the guest generates frames on attachment so we directly ACK
399 // an unACKed CFS. ACKs could get lost between the time a guest is detached
400 // from a container and the time it is attached elsewhere. This mitigates this
401 // race by ensuring the guest is ACKed on attachment.
402 scoped_ptr
<FrameMsg_CompositorFrameSwapped_Params
> last_pending_frame_
;
404 // This is a queue of messages that are destined to be sent to the embedder
405 // once the guest is attached to a particular embedder.
406 std::deque
<linked_ptr
<IPC::Message
> > pending_messages_
;
408 BrowserPluginGuestDelegate
* const delegate_
;
410 // Weak pointer used to ask GeolocationPermissionContext about geolocation
412 base::WeakPtrFactory
<BrowserPluginGuest
> weak_ptr_factory_
;
414 DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuest
);
417 } // namespace content
419 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_