Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / browser_plugin / browser_plugin_guest.h
blob946d8d97a02fc5d71280f3740624e24bf8ba51c8
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.
8 //
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_
21 #include <map>
22 #include <queue>
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;
50 #endif
52 namespace cc {
53 class CompositorFrame;
54 } // namespace cc
56 namespace gfx {
57 class Range;
58 } // namespace gfx
60 namespace content {
62 class BrowserPluginGuestManager;
63 class RenderViewHostImpl;
64 class RenderWidgetHost;
65 class RenderWidgetHostView;
66 class RenderWidgetHostViewBase;
67 class SiteInstance;
68 struct DropData;
70 // A browser plugin guest provides functionality for WebContents to operate in
71 // the guest role and implements guest-specific overrides for ViewHostMsg_*
72 // messages.
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 GuestHost,
81 public WebContentsObserver {
82 public:
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.
107 void Init();
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,
114 bool focused,
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|
131 // as the |opener|.
132 WebContentsImpl* CreateNewGuestWindow(
133 const WebContents::CreateParams& params);
135 // Creates, if necessary, and returns the routing ID of a proxy for the guest
136 // in the owner's renderer process.
137 int GetGuestProxyRoutingID();
139 // Returns the identifier that uniquely identifies a browser plugin guest
140 // within an embedder.
141 int browser_plugin_instance_id() const { return browser_plugin_instance_id_; }
143 bool OnMessageReceivedFromEmbedder(const IPC::Message& message);
145 WebContentsImpl* embedder_web_contents() const {
146 return attached_ ? owner_web_contents_ : nullptr;
149 // Returns the embedder's RenderWidgetHostView if it is available.
150 // Returns nullptr otherwise.
151 RenderWidgetHostView* GetOwnerRenderWidgetHostView();
153 bool focused() const { return focused_; }
154 bool visible() const { return guest_visible_; }
155 bool is_in_destruction() { return is_in_destruction_; }
157 void UpdateVisibility();
159 BrowserPluginGuestManager* GetBrowserPluginGuestManager() const;
161 // WebContentsObserver implementation.
162 void DidCommitProvisionalLoadForFrame(
163 RenderFrameHost* render_frame_host,
164 const GURL& url,
165 ui::PageTransition transition_type) override;
167 void RenderViewReady() override;
168 void RenderProcessGone(base::TerminationStatus status) override;
169 bool OnMessageReceived(const IPC::Message& message) override;
170 bool OnMessageReceived(const IPC::Message& message,
171 RenderFrameHost* render_frame_host) override;
173 // GuestHost implementation.
174 int LoadURLWithParams(
175 const NavigationController::LoadURLParams& load_params) override;
176 void SizeContents(const gfx::Size& new_size) override;
177 void WillDestroy() override;
179 // Exposes the protected web_contents() from WebContentsObserver.
180 WebContentsImpl* GetWebContents() const;
182 gfx::Point GetScreenCoordinates(const gfx::Point& relative_position) const;
184 // Helper to send messages to embedder. If this guest is not yet attached,
185 // then IPCs will be queued until attachment.
186 void SendMessageToEmbedder(IPC::Message* msg);
188 // Returns whether the guest is attached to an embedder.
189 bool attached() const { return attached_; }
191 // Attaches this BrowserPluginGuest to the provided |embedder_web_contents|
192 // and initializes the guest with the provided |params|. Attaching a guest
193 // to an embedder implies that this guest's lifetime is no longer managed
194 // by its opener, and it can begin loading resources.
195 void Attach(int browser_plugin_instance_id,
196 WebContentsImpl* embedder_web_contents,
197 const BrowserPluginHostMsg_Attach_Params& params);
199 // Returns whether BrowserPluginGuest is interested in receiving the given
200 // |message|.
201 static bool ShouldForwardToBrowserPluginGuest(const IPC::Message& message);
203 void DragSourceEndedAt(int client_x, int client_y, int screen_x,
204 int screen_y, blink::WebDragOperation operation);
206 // Called when the drag started by this guest ends at an OS-level.
207 void EmbedderSystemDragEnded();
208 void EndSystemDragIfApplicable();
210 void RespondToPermissionRequest(int request_id,
211 bool should_allow,
212 const std::string& user_input);
214 void PointerLockPermissionResponse(bool allow);
216 void SwapCompositorFrame(uint32 output_surface_id,
217 int host_process_id,
218 int host_routing_id,
219 scoped_ptr<cc::CompositorFrame> frame);
221 void SetContentsOpaque(bool opaque);
223 // Find the given |search_text| in the page. Returns true if the find request
224 // is handled by this browser plugin guest.
225 bool Find(int request_id,
226 const base::string16& search_text,
227 const blink::WebFindOptions& options);
228 bool StopFinding(StopFindAction action);
230 private:
231 class EmbedderVisibilityObserver;
233 // BrowserPluginGuest is a WebContentsObserver of |web_contents| and
234 // |web_contents| has to stay valid for the lifetime of BrowserPluginGuest.
235 BrowserPluginGuest(bool has_render_view,
236 WebContentsImpl* web_contents,
237 BrowserPluginGuestDelegate* delegate);
239 void InitInternal(const BrowserPluginHostMsg_Attach_Params& params,
240 WebContentsImpl* owner_web_contents);
242 bool InAutoSizeBounds(const gfx::Size& size) const;
244 // Message handlers for messages from embedder.
245 void OnCompositorFrameSwappedACK(
246 int instance_id,
247 const FrameHostMsg_CompositorFrameSwappedACK_Params& params);
248 void OnDetach(int instance_id);
249 // Handles drag events from the embedder.
250 // When dragging, the drag events go to the embedder first, and if the drag
251 // happens on the browser plugin, then the plugin sends a corresponding
252 // drag-message to the guest. This routes the drag-message to the guest
253 // renderer.
254 void OnDragStatusUpdate(int instance_id,
255 blink::WebDragStatus drag_status,
256 const DropData& drop_data,
257 blink::WebDragOperationsMask drag_mask,
258 const gfx::Point& location);
259 // Instructs the guest to execute an edit command decoded in the embedder.
260 void OnExecuteEditCommand(int instance_id,
261 const std::string& command);
263 // Returns compositor resources reclaimed in the embedder to the guest.
264 void OnReclaimCompositorResources(
265 int instance_id,
266 const FrameHostMsg_ReclaimCompositorResources_Params& params);
268 void OnLockMouse(bool user_gesture,
269 bool last_unlocked_by_target,
270 bool privileged);
271 void OnLockMouseAck(int instance_id, bool succeeded);
272 // Resizes the guest's web contents.
273 void OnSetFocus(int instance_id,
274 bool focused,
275 blink::WebFocusType focus_type);
276 // Sets the name of the guest so that other guests in the same partition can
277 // access it.
278 void OnSetName(int instance_id, const std::string& name);
279 // Updates the size state of the guest.
280 void OnSetEditCommandsForNextKeyEvent(
281 int instance_id,
282 const std::vector<EditCommand>& edit_commands);
283 // The guest WebContents is visible if both its embedder is visible and
284 // the browser plugin element is visible. If either one is not then the
285 // WebContents is marked as hidden. A hidden WebContents will consume
286 // fewer GPU and CPU resources.
288 // When every WebContents in a RenderProcessHost is hidden, it will lower
289 // the priority of the process (see RenderProcessHostImpl::WidgetHidden).
291 // It will also send a message to the guest renderer process to cleanup
292 // resources such as dropping back buffers and adjusting memory limits (if in
293 // compositing mode, see CCLayerTreeHost::setVisible).
295 // Additionally, it will slow down Javascript execution and garbage
296 // collection. See RenderThreadImpl::IdleHandler (executed when hidden) and
297 // RenderThreadImpl::IdleHandlerInForegroundTab (executed when visible).
298 void OnSetVisibility(int instance_id, bool visible);
299 void OnUnlockMouse();
300 void OnUnlockMouseAck(int instance_id);
301 void OnUpdateGeometry(int instance_id, const gfx::Rect& view_rect);
303 void OnTextInputTypeChanged(ui::TextInputType type,
304 ui::TextInputMode input_mode,
305 bool can_compose_inline,
306 int flags);
307 void OnImeSetComposition(
308 int instance_id,
309 const std::string& text,
310 const std::vector<blink::WebCompositionUnderline>& underlines,
311 int selection_start,
312 int selection_end);
313 void OnImeConfirmComposition(
314 int instance_id,
315 const std::string& text,
316 bool keep_selection);
317 void OnExtendSelectionAndDelete(int instance_id, int before, int after);
318 void OnImeCancelComposition();
319 #if defined(OS_MACOSX) || defined(USE_AURA)
320 void OnImeCompositionRangeChanged(
321 const gfx::Range& range,
322 const std::vector<gfx::Rect>& character_bounds);
323 #endif
325 // Message handlers for messages from guest.
326 void OnHandleInputEventAck(
327 blink::WebInputEvent::Type event_type,
328 InputEventAckState ack_result);
329 void OnHasTouchEventHandlers(bool accept);
330 #if defined(OS_MACOSX)
331 // On MacOS X popups are painted by the browser process. We handle them here
332 // so that they are positioned correctly.
333 void OnShowPopup(RenderFrameHost* render_frame_host,
334 const FrameHostMsg_ShowPopup_Params& params);
335 #endif
336 void OnShowWidget(int route_id, const gfx::Rect& initial_rect);
337 void OnTakeFocus(bool reverse);
338 void OnUpdateFrameName(int frame_id,
339 bool is_top_level,
340 const std::string& name);
342 // Forwards all messages from the |pending_messages_| queue to the embedder.
343 void SendQueuedMessages();
345 void SendTextInputTypeChangedToView(RenderWidgetHostViewBase* guest_rwhv);
347 // The last tooltip that was set with SetTooltipText().
348 base::string16 current_tooltip_text_;
350 scoped_ptr<EmbedderVisibilityObserver> embedder_visibility_observer_;
351 WebContentsImpl* owner_web_contents_;
353 // Indicates whether this guest has been attached to a container.
354 bool attached_;
356 // An identifier that uniquely identifies a browser plugin within an embedder.
357 int browser_plugin_instance_id_;
358 gfx::Rect guest_window_rect_;
359 bool focused_;
360 bool mouse_locked_;
361 bool pending_lock_request_;
362 bool guest_visible_;
363 bool embedder_visible_;
364 // Whether the browser plugin is inside a plugin document.
365 bool is_full_page_plugin_;
367 // Indicates that this BrowserPluginGuest has associated renderer-side state.
368 // This is used to determine whether or not to create a new RenderView when
369 // this guest is attached. A BrowserPluginGuest would have renderer-side state
370 // prior to attachment if it is created via a call to window.open and
371 // maintains a JavaScript reference to its opener.
372 bool has_render_view_;
374 // Last seen size of guest contents (by SwapCompositorFrame).
375 gfx::Size last_seen_view_size_;
377 bool is_in_destruction_;
379 // BrowserPluginGuest::Init can only be called once. This flag allows it to
380 // exit early if it's already been called.
381 bool initialized_;
383 // Text input type states.
384 ui::TextInputType last_text_input_type_;
385 ui::TextInputMode last_input_mode_;
386 int last_input_flags_;
387 bool last_can_compose_inline_;
389 // The is the routing ID for a swapped out RenderView for the guest
390 // WebContents in the embedder's process.
391 int guest_proxy_routing_id_;
392 // Last seen state of drag status update.
393 blink::WebDragStatus last_drag_status_;
394 // Whether or not our embedder has seen a SystemDragEnded() call.
395 bool seen_embedder_system_drag_ended_;
396 // Whether or not our embedder has seen a DragSourceEndedAt() call.
397 bool seen_embedder_drag_source_ended_at_;
399 // Indicates the URL dragged into the guest if any.
400 GURL dragged_url_;
402 // Guests generate frames and send a CompositorFrameSwapped (CFS) message
403 // indicating the next frame is ready to be positioned and composited.
404 // Subsequent frames are not generated until the IPC is ACKed. We would like
405 // to ensure that the guest generates frames on attachment so we directly ACK
406 // an unACKed CFS. ACKs could get lost between the time a guest is detached
407 // from a container and the time it is attached elsewhere. This mitigates this
408 // race by ensuring the guest is ACKed on attachment.
409 scoped_ptr<FrameMsg_CompositorFrameSwapped_Params> last_pending_frame_;
411 // This is a queue of messages that are destined to be sent to the embedder
412 // once the guest is attached to a particular embedder.
413 std::deque<linked_ptr<IPC::Message> > pending_messages_;
415 BrowserPluginGuestDelegate* const delegate_;
417 // Weak pointer used to ask GeolocationPermissionContext about geolocation
418 // permission.
419 base::WeakPtrFactory<BrowserPluginGuest> weak_ptr_factory_;
421 DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuest);
424 } // namespace content
426 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_