Process Alt-Svc headers.
[chromium-blink-merge.git] / content / browser / browser_plugin / browser_plugin_guest.h
blob868fcc8a35ab07c5920b2e70f48a9c25ceca03b3
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;
47 struct ViewHostMsg_TextInputState_Params;
49 #if defined(OS_MACOSX)
50 struct FrameHostMsg_ShowPopup_Params;
51 #endif
53 namespace cc {
54 class CompositorFrame;
55 struct SurfaceId;
56 struct SurfaceSequence;
57 } // namespace cc
59 namespace gfx {
60 class Range;
61 } // namespace gfx
63 namespace content {
65 class BrowserPluginGuestManager;
66 class RenderViewHostImpl;
67 class RenderWidgetHost;
68 class RenderWidgetHostView;
69 class RenderWidgetHostViewBase;
70 class SiteInstance;
71 struct DropData;
73 // A browser plugin guest provides functionality for WebContents to operate in
74 // the guest role and implements guest-specific overrides for ViewHostMsg_*
75 // messages.
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.
84 // Note: in --site-per-process, all IPCs sent out from this class will be
85 // dropped on the floor since we don't have a BrowserPlugin.
86 class CONTENT_EXPORT BrowserPluginGuest : public GuestHost,
87 public WebContentsObserver {
88 public:
89 ~BrowserPluginGuest() override;
91 // The WebContents passed into the factory method here has not been
92 // initialized yet and so it does not yet hold a SiteInstance.
93 // BrowserPluginGuest must be constructed and installed into a WebContents
94 // prior to its initialization because WebContents needs to determine what
95 // type of WebContentsView to construct on initialization. The content
96 // embedder needs to be aware of |guest_site_instance| on the guest's
97 // construction and so we pass it in here.
98 static BrowserPluginGuest* Create(WebContentsImpl* web_contents,
99 BrowserPluginGuestDelegate* delegate);
101 // Returns whether the given WebContents is a BrowserPlugin guest.
102 static bool IsGuest(WebContentsImpl* web_contents);
104 // Returns whether the given RenderviewHost is a BrowserPlugin guest.
105 static bool IsGuest(RenderViewHostImpl* render_view_host);
107 // BrowserPluginGuest::Init is called after the associated guest WebContents
108 // initializes. If this guest cannot navigate without being attached to a
109 // container, then this call is a no-op. For guest types that can be
110 // navigated, this call adds the associated RenderWdigetHostViewGuest to the
111 // view hierachy and sets up the appropriate RendererPreferences so that this
112 // guest can navigate and resize offscreen.
113 void Init();
115 // Returns a WeakPtr to this BrowserPluginGuest.
116 base::WeakPtr<BrowserPluginGuest> AsWeakPtr();
118 // Sets the focus state of the current RenderWidgetHostView.
119 void SetFocus(RenderWidgetHost* rwh,
120 bool focused,
121 blink::WebFocusType focus_type);
123 // Sets the tooltip text.
124 void SetTooltipText(const base::string16& tooltip_text);
126 // Sets the lock state of the pointer. Returns true if |allowed| is true and
127 // the mouse has been successfully locked.
128 bool LockMouse(bool allowed);
130 // Return true if the mouse is locked.
131 bool mouse_locked() const { return mouse_locked_; }
133 // Called when the embedder WebContents changes visibility.
134 void EmbedderVisibilityChanged(bool visible);
136 // Creates a new guest WebContentsImpl with the provided |params| with |this|
137 // as the |opener|.
138 WebContentsImpl* CreateNewGuestWindow(
139 const WebContents::CreateParams& params);
141 // Creates, if necessary, and returns the routing ID of a proxy for the guest
142 // in the owner's renderer process.
143 int GetGuestProxyRoutingID();
145 // Returns the identifier that uniquely identifies a browser plugin guest
146 // within an embedder.
147 int browser_plugin_instance_id() const { return browser_plugin_instance_id_; }
149 bool OnMessageReceivedFromEmbedder(const IPC::Message& message);
151 WebContentsImpl* embedder_web_contents() const {
152 return attached_ ? owner_web_contents_ : nullptr;
155 // Returns the embedder's RenderWidgetHostView if it is available.
156 // Returns nullptr otherwise.
157 RenderWidgetHostView* GetOwnerRenderWidgetHostView();
159 bool focused() const { return focused_; }
160 bool visible() const { return guest_visible_; }
161 bool is_in_destruction() { return is_in_destruction_; }
163 void UpdateVisibility();
165 BrowserPluginGuestManager* GetBrowserPluginGuestManager() const;
167 // WebContentsObserver implementation.
168 void DidCommitProvisionalLoadForFrame(
169 RenderFrameHost* render_frame_host,
170 const GURL& url,
171 ui::PageTransition transition_type) override;
173 void RenderViewReady() override;
174 void RenderProcessGone(base::TerminationStatus status) override;
175 bool OnMessageReceived(const IPC::Message& message) override;
176 bool OnMessageReceived(const IPC::Message& message,
177 RenderFrameHost* render_frame_host) override;
179 // GuestHost implementation.
180 int LoadURLWithParams(
181 const NavigationController::LoadURLParams& load_params) override;
182 void SizeContents(const gfx::Size& new_size) override;
183 void WillDestroy() override;
185 // Exposes the protected web_contents() from WebContentsObserver.
186 WebContentsImpl* GetWebContents() const;
188 gfx::Point GetScreenCoordinates(const gfx::Point& relative_position) const;
190 // Helper to send messages to embedder. If this guest is not yet attached,
191 // then IPCs will be queued until attachment.
192 void SendMessageToEmbedder(IPC::Message* msg);
194 // Returns whether the guest is attached to an embedder.
195 bool attached() const { return attached_; }
197 // Returns true when an attachment has taken place since the last time the
198 // compositor surface was set.
199 bool has_attached_since_surface_set() const {
200 return has_attached_since_surface_set_;
203 // Attaches this BrowserPluginGuest to the provided |embedder_web_contents|
204 // and initializes the guest with the provided |params|. Attaching a guest
205 // to an embedder implies that this guest's lifetime is no longer managed
206 // by its opener, and it can begin loading resources.
207 void Attach(int browser_plugin_instance_id,
208 WebContentsImpl* embedder_web_contents,
209 const BrowserPluginHostMsg_Attach_Params& params);
211 // Returns whether BrowserPluginGuest is interested in receiving the given
212 // |message|.
213 static bool ShouldForwardToBrowserPluginGuest(const IPC::Message& message);
215 void DragSourceEndedAt(int client_x, int client_y, int screen_x,
216 int screen_y, blink::WebDragOperation operation);
218 // Called when the drag started by this guest ends at an OS-level.
219 void EmbedderSystemDragEnded();
220 void EndSystemDragIfApplicable();
222 void RespondToPermissionRequest(int request_id,
223 bool should_allow,
224 const std::string& user_input);
226 void PointerLockPermissionResponse(bool allow);
228 // The next three functions are virtual for test purposes.
229 virtual void UpdateGuestSizeIfNecessary(const gfx::Size& frame_size,
230 float scale_factor);
231 virtual void SwapCompositorFrame(uint32 output_surface_id,
232 int host_process_id,
233 int host_routing_id,
234 scoped_ptr<cc::CompositorFrame> frame);
235 virtual void SetChildFrameSurface(const cc::SurfaceId& surface_id,
236 const gfx::Size& frame_size,
237 float scale_factor,
238 const cc::SurfaceSequence& sequence);
240 void SetContentsOpaque(bool opaque);
242 // Find the given |search_text| in the page. Returns true if the find request
243 // is handled by this browser plugin guest.
244 bool Find(int request_id,
245 const base::string16& search_text,
246 const blink::WebFindOptions& options);
247 bool StopFinding(StopFindAction action);
249 protected:
251 // BrowserPluginGuest is a WebContentsObserver of |web_contents| and
252 // |web_contents| has to stay valid for the lifetime of BrowserPluginGuest.
253 // Constructor protected for testing.
254 BrowserPluginGuest(bool has_render_view,
255 WebContentsImpl* web_contents,
256 BrowserPluginGuestDelegate* delegate);
258 // Protected for testing.
259 void set_has_attached_since_surface_set_for_test(bool has_attached) {
260 has_attached_since_surface_set_ = has_attached;
263 void set_attached_for_test(bool attached) {
264 attached_ = attached;
267 private:
268 class EmbedderVisibilityObserver;
270 void InitInternal(const BrowserPluginHostMsg_Attach_Params& params,
271 WebContentsImpl* owner_web_contents);
273 bool InAutoSizeBounds(const gfx::Size& size) const;
275 void OnSatisfySequence(int instance_id, const cc::SurfaceSequence& sequence);
276 void OnRequireSequence(int instance_id,
277 const cc::SurfaceId& id,
278 const cc::SurfaceSequence& sequence);
279 // Message handlers for messages from embedder.
280 void OnCompositorFrameSwappedACK(
281 int instance_id,
282 const FrameHostMsg_CompositorFrameSwappedACK_Params& params);
283 void OnDetach(int instance_id);
284 // Handles drag events from the embedder.
285 // When dragging, the drag events go to the embedder first, and if the drag
286 // happens on the browser plugin, then the plugin sends a corresponding
287 // drag-message to the guest. This routes the drag-message to the guest
288 // renderer.
289 void OnDragStatusUpdate(int instance_id,
290 blink::WebDragStatus drag_status,
291 const DropData& drop_data,
292 blink::WebDragOperationsMask drag_mask,
293 const gfx::Point& location);
294 // Instructs the guest to execute an edit command decoded in the embedder.
295 void OnExecuteEditCommand(int instance_id,
296 const std::string& command);
298 // Returns compositor resources reclaimed in the embedder to the guest.
299 void OnReclaimCompositorResources(
300 int instance_id,
301 const FrameHostMsg_ReclaimCompositorResources_Params& params);
303 void OnLockMouse(bool user_gesture,
304 bool last_unlocked_by_target,
305 bool privileged);
306 void OnLockMouseAck(int instance_id, bool succeeded);
307 // Resizes the guest's web contents.
308 void OnSetFocus(int instance_id,
309 bool focused,
310 blink::WebFocusType focus_type);
311 // Sets the name of the guest so that other guests in the same partition can
312 // access it.
313 void OnSetName(int instance_id, const std::string& name);
314 // Updates the size state of the guest.
315 void OnSetEditCommandsForNextKeyEvent(
316 int instance_id,
317 const std::vector<EditCommand>& edit_commands);
318 // The guest WebContents is visible if both its embedder is visible and
319 // the browser plugin element is visible. If either one is not then the
320 // WebContents is marked as hidden. A hidden WebContents will consume
321 // fewer GPU and CPU resources.
323 // When every WebContents in a RenderProcessHost is hidden, it will lower
324 // the priority of the process (see RenderProcessHostImpl::WidgetHidden).
326 // It will also send a message to the guest renderer process to cleanup
327 // resources such as dropping back buffers and adjusting memory limits (if in
328 // compositing mode, see CCLayerTreeHost::setVisible).
330 // Additionally, it will slow down Javascript execution and garbage
331 // collection. See RenderThreadImpl::IdleHandler (executed when hidden) and
332 // RenderThreadImpl::IdleHandlerInForegroundTab (executed when visible).
333 void OnSetVisibility(int instance_id, bool visible);
334 void OnUnlockMouse();
335 void OnUnlockMouseAck(int instance_id);
336 void OnUpdateGeometry(int instance_id, const gfx::Rect& view_rect);
338 void OnTextInputStateChanged(
339 const ViewHostMsg_TextInputState_Params& params);
340 void OnImeSetComposition(
341 int instance_id,
342 const std::string& text,
343 const std::vector<blink::WebCompositionUnderline>& underlines,
344 int selection_start,
345 int selection_end);
346 void OnImeConfirmComposition(
347 int instance_id,
348 const std::string& text,
349 bool keep_selection);
350 void OnExtendSelectionAndDelete(int instance_id, int before, int after);
351 void OnImeCancelComposition();
352 #if defined(OS_MACOSX) || defined(USE_AURA)
353 void OnImeCompositionRangeChanged(
354 const gfx::Range& range,
355 const std::vector<gfx::Rect>& character_bounds);
356 #endif
358 // Message handlers for messages from guest.
359 void OnHandleInputEventAck(
360 blink::WebInputEvent::Type event_type,
361 InputEventAckState ack_result);
362 void OnHasTouchEventHandlers(bool accept);
363 #if defined(OS_MACOSX)
364 // On MacOS X popups are painted by the browser process. We handle them here
365 // so that they are positioned correctly.
366 void OnShowPopup(RenderFrameHost* render_frame_host,
367 const FrameHostMsg_ShowPopup_Params& params);
368 #endif
369 void OnShowWidget(int route_id, const gfx::Rect& initial_rect);
370 void OnTakeFocus(bool reverse);
371 void OnUpdateFrameName(int frame_id,
372 bool is_top_level,
373 const std::string& name);
375 // Called when WillAttach is complete.
376 void OnWillAttachComplete(WebContentsImpl* embedder_web_contents,
377 const BrowserPluginHostMsg_Attach_Params& params);
379 // Forwards all messages from the |pending_messages_| queue to the embedder.
380 void SendQueuedMessages();
382 void SendTextInputTypeChangedToView(RenderWidgetHostViewBase* guest_rwhv);
384 // The last tooltip that was set with SetTooltipText().
385 base::string16 current_tooltip_text_;
387 scoped_ptr<EmbedderVisibilityObserver> embedder_visibility_observer_;
388 WebContentsImpl* owner_web_contents_;
390 // Indicates whether this guest has been attached to a container.
391 bool attached_;
393 // Used to signal if a browser plugin has been attached since the last time
394 // the compositing surface was set.
395 bool has_attached_since_surface_set_;
397 // An identifier that uniquely identifies a browser plugin within an embedder.
398 int browser_plugin_instance_id_;
399 gfx::Rect guest_window_rect_;
400 bool focused_;
401 bool mouse_locked_;
402 bool pending_lock_request_;
403 bool guest_visible_;
404 bool embedder_visible_;
405 // Whether the browser plugin is inside a plugin document.
406 bool is_full_page_plugin_;
408 // Indicates that this BrowserPluginGuest has associated renderer-side state.
409 // This is used to determine whether or not to create a new RenderView when
410 // this guest is attached. A BrowserPluginGuest would have renderer-side state
411 // prior to attachment if it is created via a call to window.open and
412 // maintains a JavaScript reference to its opener.
413 bool has_render_view_;
415 // Last seen size of guest contents (by SwapCompositorFrame).
416 gfx::Size last_seen_view_size_;
418 bool is_in_destruction_;
420 // BrowserPluginGuest::Init can only be called once. This flag allows it to
421 // exit early if it's already been called.
422 bool initialized_;
424 // Text input type states.
425 ui::TextInputType last_text_input_type_;
426 ui::TextInputMode last_input_mode_;
427 int last_input_flags_;
428 bool last_can_compose_inline_;
430 // The is the routing ID for a swapped out RenderView for the guest
431 // WebContents in the embedder's process.
432 int guest_proxy_routing_id_;
433 // Last seen state of drag status update.
434 blink::WebDragStatus last_drag_status_;
435 // Whether or not our embedder has seen a SystemDragEnded() call.
436 bool seen_embedder_system_drag_ended_;
437 // Whether or not our embedder has seen a DragSourceEndedAt() call.
438 bool seen_embedder_drag_source_ended_at_;
440 // Indicates the URL dragged into the guest if any.
441 GURL dragged_url_;
443 // Guests generate frames and send a CompositorFrameSwapped (CFS) message
444 // indicating the next frame is ready to be positioned and composited.
445 // Subsequent frames are not generated until the IPC is ACKed. We would like
446 // to ensure that the guest generates frames on attachment so we directly ACK
447 // an unACKed CFS. ACKs could get lost between the time a guest is detached
448 // from a container and the time it is attached elsewhere. This mitigates this
449 // race by ensuring the guest is ACKed on attachment.
450 scoped_ptr<FrameMsg_CompositorFrameSwapped_Params> last_pending_frame_;
452 // This is a queue of messages that are destined to be sent to the embedder
453 // once the guest is attached to a particular embedder.
454 std::deque<linked_ptr<IPC::Message> > pending_messages_;
456 BrowserPluginGuestDelegate* const delegate_;
458 // Weak pointer used to ask GeolocationPermissionContext about geolocation
459 // permission.
460 base::WeakPtrFactory<BrowserPluginGuest> weak_ptr_factory_;
462 DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuest);
465 } // namespace content
467 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_