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