Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / browser_plugin / browser_plugin_guest.h
blobd9e2f6d4ee2f1ab13b1166b9a8bd2bd32d45179d
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/web_contents_observer.h"
32 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
33 #include "third_party/WebKit/public/web/WebDragOperation.h"
34 #include "third_party/WebKit/public/web/WebDragStatus.h"
35 #include "third_party/WebKit/public/web/WebInputEvent.h"
36 #include "ui/base/ime/text_input_mode.h"
37 #include "ui/base/ime/text_input_type.h"
38 #include "ui/gfx/rect.h"
40 class SkBitmap;
41 struct BrowserPluginHostMsg_Attach_Params;
42 struct BrowserPluginHostMsg_ResizeGuest_Params;
43 struct FrameHostMsg_CompositorFrameSwappedACK_Params;
44 struct FrameHostMsg_ReclaimCompositorResources_Params;
45 #if defined(OS_MACOSX)
46 struct FrameHostMsg_ShowPopup_Params;
47 #endif
48 struct ViewHostMsg_TextInputState_Params;
49 struct ViewHostMsg_UpdateRect_Params;
51 namespace blink {
52 class WebInputEvent;
55 namespace gfx {
56 class Range;
59 namespace content {
61 class BrowserPluginGuestManager;
62 class RenderViewHostImpl;
63 class RenderWidgetHostView;
64 class SiteInstance;
65 class WebCursor;
66 struct DropData;
68 // A browser plugin guest provides functionality for WebContents to operate in
69 // the guest role and implements guest-specific overrides for ViewHostMsg_*
70 // messages.
72 // When a guest is initially created, it is in an unattached state. That is,
73 // it is not visible anywhere and has no embedder WebContents assigned.
74 // A BrowserPluginGuest is said to be "attached" if it has an embedder.
75 // A BrowserPluginGuest can also create a new unattached guest via
76 // CreateNewWindow. The newly created guest will live in the same partition,
77 // which means it can share storage and can script this guest.
78 class CONTENT_EXPORT BrowserPluginGuest : public WebContentsObserver {
79 public:
80 virtual ~BrowserPluginGuest();
82 // The WebContents passed into the factory method here has not been
83 // initialized yet and so it does not yet hold a SiteInstance.
84 // BrowserPluginGuest must be constructed and installed into a WebContents
85 // prior to its initialization because WebContents needs to determine what
86 // type of WebContentsView to construct on initialization. The content
87 // embedder needs to be aware of |guest_site_instance| on the guest's
88 // construction and so we pass it in here.
89 static BrowserPluginGuest* Create(WebContentsImpl* web_contents,
90 BrowserPluginGuestDelegate* delegate);
92 // Returns whether the given WebContents is a BrowserPlugin guest.
93 static bool IsGuest(WebContentsImpl* web_contents);
95 // Returns whether the given RenderviewHost is a BrowserPlugin guest.
96 static bool IsGuest(RenderViewHostImpl* render_view_host);
98 // Returns a WeakPtr to this BrowserPluginGuest.
99 base::WeakPtr<BrowserPluginGuest> AsWeakPtr();
101 // Sets the lock state of the pointer. Returns true if |allowed| is true and
102 // the mouse has been successfully locked.
103 bool LockMouse(bool allowed);
105 // Return true if the mouse is locked.
106 bool mouse_locked() const { return mouse_locked_; }
108 // Called when the embedder WebContents changes visibility.
109 void EmbedderVisibilityChanged(bool visible);
111 // Destroys the guest WebContents and all its associated state, including
112 // this BrowserPluginGuest, and its new unattached windows.
113 void Destroy();
115 // Creates a new guest WebContentsImpl with the provided |params| with |this|
116 // as the |opener|.
117 WebContentsImpl* CreateNewGuestWindow(
118 const WebContents::CreateParams& params);
120 // Returns the identifier that uniquely identifies a browser plugin guest
121 // within an embedder.
122 int browser_plugin_instance_id() const { return browser_plugin_instance_id_; }
124 bool OnMessageReceivedFromEmbedder(const IPC::Message& message);
126 WebContentsImpl* embedder_web_contents() const {
127 return embedder_web_contents_;
130 // Returns the embedder's RenderWidgetHostView if it is available.
131 // Returns NULL otherwise.
132 RenderWidgetHostView* GetEmbedderRenderWidgetHostView();
134 bool focused() const { return focused_; }
135 bool visible() const { return guest_visible_; }
136 bool is_in_destruction() { return is_in_destruction_; }
138 void UpdateVisibility();
140 void CopyFromCompositingSurface(
141 gfx::Rect src_subrect,
142 gfx::Size dst_size,
143 const base::Callback<void(bool, const SkBitmap&)>& callback);
145 BrowserPluginGuestManager* GetBrowserPluginGuestManager() const;
147 // WebContentsObserver implementation.
148 virtual void DidCommitProvisionalLoadForFrame(
149 RenderFrameHost* render_frame_host,
150 const GURL& url,
151 PageTransition transition_type) OVERRIDE;
153 virtual void RenderViewReady() OVERRIDE;
154 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
155 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
156 virtual bool OnMessageReceived(const IPC::Message& message,
157 RenderFrameHost* render_frame_host) OVERRIDE;
159 // Exposes the protected web_contents() from WebContentsObserver.
160 WebContentsImpl* GetWebContents() const;
162 gfx::Point GetScreenCoordinates(const gfx::Point& relative_position) const;
164 // Helper to send messages to embedder. This methods fills the message with
165 // the correct routing id.
166 void SendMessageToEmbedder(IPC::Message* msg);
168 // Returns whether the guest is attached to an embedder.
169 bool attached() const { return embedder_web_contents_ != NULL; }
171 // Attaches this BrowserPluginGuest to the provided |embedder_web_contents|
172 // and initializes the guest with the provided |params|. Attaching a guest
173 // to an embedder implies that this guest's lifetime is no longer managed
174 // by its opener, and it can begin loading resources.
175 void Attach(int browser_plugin_instance_id,
176 WebContentsImpl* embedder_web_contents,
177 const BrowserPluginHostMsg_Attach_Params& params);
179 // Returns whether BrowserPluginGuest is interested in receiving the given
180 // |message|.
181 static bool ShouldForwardToBrowserPluginGuest(const IPC::Message& message);
182 gfx::Rect ToGuestRect(const gfx::Rect& rect);
184 void DragSourceEndedAt(int client_x, int client_y, int screen_x,
185 int screen_y, blink::WebDragOperation operation);
187 // Called when the drag started by this guest ends at an OS-level.
188 void EndSystemDrag();
190 void RespondToPermissionRequest(int request_id,
191 bool should_allow,
192 const std::string& user_input);
194 void PointerLockPermissionResponse(bool allow);
196 private:
197 class EmbedderWebContentsObserver;
199 // BrowserPluginGuest is a WebContentsObserver of |web_contents| and
200 // |web_contents| has to stay valid for the lifetime of BrowserPluginGuest.
201 BrowserPluginGuest(bool has_render_view,
202 WebContentsImpl* web_contents,
203 BrowserPluginGuestDelegate* delegate);
205 void WillDestroy();
207 void Initialize(int browser_plugin_instance_id,
208 const BrowserPluginHostMsg_Attach_Params& params,
209 WebContentsImpl* embedder_web_contents);
211 bool InAutoSizeBounds(const gfx::Size& size) const;
213 // Message handlers for messages from embedder.
215 void OnCompositorFrameSwappedACK(
216 int instance_id,
217 const FrameHostMsg_CompositorFrameSwappedACK_Params& params);
218 void OnCopyFromCompositingSurfaceAck(int instance_id,
219 int request_id,
220 const SkBitmap& bitmap);
221 // Handles drag events from the embedder.
222 // When dragging, the drag events go to the embedder first, and if the drag
223 // happens on the browser plugin, then the plugin sends a corresponding
224 // drag-message to the guest. This routes the drag-message to the guest
225 // renderer.
226 void OnDragStatusUpdate(int instance_id,
227 blink::WebDragStatus drag_status,
228 const DropData& drop_data,
229 blink::WebDragOperationsMask drag_mask,
230 const gfx::Point& location);
231 // Instructs the guest to execute an edit command decoded in the embedder.
232 void OnExecuteEditCommand(int instance_id,
233 const std::string& command);
235 // Returns compositor resources reclaimed in the embedder to the guest.
236 void OnReclaimCompositorResources(
237 int instance_id,
238 const FrameHostMsg_ReclaimCompositorResources_Params& params);
240 void OnHandleInputEvent(int instance_id,
241 const gfx::Rect& guest_window_rect,
242 const blink::WebInputEvent* event);
243 void OnLockMouse(bool user_gesture,
244 bool last_unlocked_by_target,
245 bool privileged);
246 void OnLockMouseAck(int instance_id, bool succeeded);
247 void OnPluginDestroyed(int instance_id);
248 // Resizes the guest's web contents.
249 void OnResizeGuest(
250 int instance_id, const BrowserPluginHostMsg_ResizeGuest_Params& params);
251 void OnSetFocus(int instance_id, bool focused);
252 // Sets the name of the guest so that other guests in the same partition can
253 // access it.
254 void OnSetName(int instance_id, const std::string& name);
255 // Updates the size state of the guest.
256 void OnSetEditCommandsForNextKeyEvent(
257 int instance_id,
258 const std::vector<EditCommand>& edit_commands);
259 void OnSetContentsOpaque(int instance_id, bool opaque);
260 // The guest WebContents is visible if both its embedder is visible and
261 // the browser plugin element is visible. If either one is not then the
262 // WebContents is marked as hidden. A hidden WebContents will consume
263 // fewer GPU and CPU resources.
265 // When every WebContents in a RenderProcessHost is hidden, it will lower
266 // the priority of the process (see RenderProcessHostImpl::WidgetHidden).
268 // It will also send a message to the guest renderer process to cleanup
269 // resources such as dropping back buffers and adjusting memory limits (if in
270 // compositing mode, see CCLayerTreeHost::setVisible).
272 // Additionally, it will slow down Javascript execution and garbage
273 // collection. See RenderThreadImpl::IdleHandler (executed when hidden) and
274 // RenderThreadImpl::IdleHandlerInForegroundTab (executed when visible).
275 void OnSetVisibility(int instance_id, bool visible);
276 void OnUnlockMouse();
277 void OnUnlockMouseAck(int instance_id);
278 void OnUpdateGeometry(int instance_id, const gfx::Rect& view_rect);
280 void OnTextInputStateChanged(
281 const ViewHostMsg_TextInputState_Params& params);
283 void OnImeSetComposition(
284 int instance_id,
285 const std::string& text,
286 const std::vector<blink::WebCompositionUnderline>& underlines,
287 int selection_start,
288 int selection_end);
289 void OnImeConfirmComposition(
290 int instance_id,
291 const std::string& text,
292 bool keep_selection);
293 void OnExtendSelectionAndDelete(int instance_id, int before, int after);
294 void OnImeCancelComposition();
295 #if defined(OS_MACOSX) || defined(USE_AURA)
296 void OnImeCompositionRangeChanged(
297 const gfx::Range& range,
298 const std::vector<gfx::Rect>& character_bounds);
299 #endif
301 // Message handlers for messages from guest.
303 void OnDragStopped();
304 void OnHandleInputEventAck(
305 blink::WebInputEvent::Type event_type,
306 InputEventAckState ack_result);
307 void OnHasTouchEventHandlers(bool accept);
308 void OnSetCursor(const WebCursor& cursor);
309 #if defined(OS_MACOSX)
310 // On MacOS X popups are painted by the browser process. We handle them here
311 // so that they are positioned correctly.
312 void OnShowPopup(RenderFrameHost* render_frame_host,
313 const FrameHostMsg_ShowPopup_Params& params);
314 #endif
315 void OnShowWidget(int route_id, const gfx::Rect& initial_pos);
316 void OnTakeFocus(bool reverse);
317 void OnUpdateFrameName(int frame_id,
318 bool is_top_level,
319 const std::string& name);
320 void OnUpdateRect(const ViewHostMsg_UpdateRect_Params& params);
322 // Forwards all messages from the |pending_messages_| queue to the embedder.
323 void SendQueuedMessages();
325 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_;
326 WebContentsImpl* embedder_web_contents_;
328 // An identifier that uniquely identifies a browser plugin within an embedder.
329 int browser_plugin_instance_id_;
330 float guest_device_scale_factor_;
331 gfx::Rect guest_window_rect_;
332 gfx::Rect guest_screen_rect_;
333 bool focused_;
334 bool mouse_locked_;
335 bool pending_lock_request_;
336 bool guest_visible_;
337 bool guest_opaque_;
338 bool embedder_visible_;
340 // Each copy-request is identified by a unique number. The unique number is
341 // used to keep track of the right callback.
342 int copy_request_id_;
343 typedef base::Callback<void(bool, const SkBitmap&)> CopyRequestCallback;
344 typedef std::map<int, const CopyRequestCallback> CopyRequestMap;
345 CopyRequestMap copy_request_callbacks_;
347 // Indicates that this BrowserPluginGuest has associated renderer-side state.
348 // This is used to determine whether or not to create a new RenderView when
349 // this guest is attached. A BrowserPluginGuest would have renderer-side state
350 // prior to attachment if it is created via a call to window.open and
351 // maintains a JavaScript reference to its opener.
352 bool has_render_view_;
354 // Last seen size of guest contents (by OnUpdateRect).
355 gfx::Size last_seen_view_size_;
356 // Last seen size of BrowserPlugin (by OnResizeGuest).
357 gfx::Size last_seen_browser_plugin_size_;
359 bool is_in_destruction_;
361 // Text input type states.
362 ui::TextInputType last_text_input_type_;
363 ui::TextInputMode last_input_mode_;
364 bool last_can_compose_inline_;
366 // This is a queue of messages that are destined to be sent to the embedder
367 // once the guest is attached to a particular embedder.
368 std::deque<linked_ptr<IPC::Message> > pending_messages_;
370 BrowserPluginGuestDelegate* const delegate_;
372 // Weak pointer used to ask GeolocationPermissionContext about geolocation
373 // permission.
374 base::WeakPtrFactory<BrowserPluginGuest> weak_ptr_factory_;
376 DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuest);
379 } // namespace content
381 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_H_