Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / content / renderer / browser_plugin / browser_plugin.h
1 // Copyright 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 #ifndef  CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
6 #define  CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
7
8 #include "third_party/WebKit/public/web/WebPlugin.h"
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequenced_task_runner_helpers.h"
13 #if defined(OS_WIN)
14 #include "base/memory/shared_memory.h"
15 #endif
16 #include "base/values.h"
17 #include "content/renderer/browser_plugin/browser_plugin_backing_store.h"
18 #include "content/renderer/browser_plugin/browser_plugin_bindings.h"
19 #include "content/renderer/mouse_lock_dispatcher.h"
20 #include "content/renderer/render_view_impl.h"
21 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
22 #include "third_party/WebKit/public/web/WebDragStatus.h"
23 #include "third_party/WebKit/public/web/WebWidget.h"
24
25 struct BrowserPluginHostMsg_AutoSize_Params;
26 struct BrowserPluginHostMsg_ResizeGuest_Params;
27 struct BrowserPluginMsg_Attach_ACK_Params;
28 struct BrowserPluginMsg_UpdateRect_Params;
29 struct FrameMsg_BuffersSwapped_Params;
30
31 namespace content {
32
33 class ChildFrameCompositingHelper;
34 class BrowserPluginManager;
35 class MockBrowserPlugin;
36
37 class CONTENT_EXPORT BrowserPlugin :
38     NON_EXPORTED_BASE(public blink::WebPlugin),
39     public MouseLockDispatcher::LockTarget {
40  public:
41   RenderViewImpl* render_view() const { return render_view_.get(); }
42   int render_view_routing_id() const { return render_view_routing_id_; }
43   int guest_instance_id() const { return guest_instance_id_; }
44   bool attached() const { return attached_; }
45   BrowserPluginManager* browser_plugin_manager() const {
46     return browser_plugin_manager_.get();
47   }
48
49   static BrowserPlugin* FromContainer(blink::WebPluginContainer* container);
50
51   bool OnMessageReceived(const IPC::Message& msg);
52
53   // Update Browser Plugin's DOM Node attribute |attribute_name| with the value
54   // |attribute_value|.
55   void UpdateDOMAttribute(const std::string& attribute_name,
56                           const std::string& attribute_value);
57   // Remove the DOM Node attribute with the name |attribute_name|.
58   void RemoveDOMAttribute(const std::string& attribute_name);
59   // Get Browser Plugin's DOM Node attribute |attribute_name|'s value.
60   std::string GetDOMAttributeValue(const std::string& attribute_name) const;
61   // Checks if the attribute |attribute_name| exists in the DOM.
62   bool HasDOMAttribute(const std::string& attribute_name) const;
63
64   // Get the name attribute value.
65   std::string GetNameAttribute() const;
66   // Parse the name attribute value.
67   void ParseNameAttribute();
68   // Get the allowtransparency attribute value.
69   bool GetAllowTransparencyAttribute() const;
70   // Parse the allowtransparency attribute and adjust transparency of
71   // BrowserPlugin accordingly.
72   void ParseAllowTransparencyAttribute();
73   // Get the src attribute value of the BrowserPlugin instance.
74   std::string GetSrcAttribute() const;
75   // Parse the src attribute value of the BrowserPlugin instance.
76   bool ParseSrcAttribute(std::string* error_message);
77   // Get the autosize attribute value.
78   bool GetAutoSizeAttribute() const;
79   // Parses the autosize attribute value.
80   void ParseAutoSizeAttribute();
81   // Get the maxheight attribute value.
82   int GetMaxHeightAttribute() const;
83   // Get the maxwidth attribute value.
84   int GetMaxWidthAttribute() const;
85   // Get the minheight attribute value.
86   int GetMinHeightAttribute() const;
87   // Get the minwidth attribute value.
88   int GetMinWidthAttribute() const;
89   // Parse the minwidth, maxwidth, minheight, and maxheight attribute values.
90   void ParseSizeContraintsChanged();
91   // The partition identifier string is stored as UTF-8.
92   std::string GetPartitionAttribute() const;
93   // This method can be successfully called only before the first navigation for
94   // this instance of BrowserPlugin. If an error occurs, the |error_message| is
95   // set appropriately to indicate the failure reason.
96   bool ParsePartitionAttribute(std::string* error_message);
97   // True if the partition attribute can be removed.
98   bool CanRemovePartitionAttribute(std::string* error_message);
99
100   bool InAutoSizeBounds(const gfx::Size& size) const;
101
102   // Get the guest's DOMWindow proxy.
103   NPObject* GetContentWindow() const;
104
105   // Returns whether the guest process has crashed.
106   bool guest_crashed() const { return guest_crashed_; }
107   // Returns whether this BrowserPlugin has requested an instance ID.
108   bool HasNavigated() const;
109   // Returns whether this BrowserPlugin has allocated an instance ID.
110   bool HasGuestInstanceID() const;
111
112   // Attaches the window identified by |window_id| to the the given node
113   // encapsulating a BrowserPlugin.
114   static bool AttachWindowTo(const blink::WebNode& node,
115                              int window_id);
116
117   // Informs the guest of an updated focus state.
118   void UpdateGuestFocusState();
119   // Indicates whether the guest should be focused.
120   bool ShouldGuestBeFocused() const;
121
122   // Embedder's device scale factor changed, we need to update the guest
123   // renderer.
124   void UpdateDeviceScaleFactor(float device_scale_factor);
125
126   // A request to enable hardware compositing.
127   void EnableCompositing(bool enable);
128
129   // Called when a guest instance ID has been allocated by the browser process.
130   void OnInstanceIDAllocated(int guest_instance_id);
131   // Provided that a guest instance ID has been allocated, this method attaches
132   // this BrowserPlugin instance to that guest. |extra_params| are parameters
133   // passed in by the content embedder to the browser process.
134   void Attach(scoped_ptr<base::DictionaryValue> extra_params);
135
136   // Notify the plugin about a compositor commit so that frame ACKs could be
137   // sent, if needed.
138   void DidCommitCompositorFrame();
139
140   // Returns whether a message should be forwarded to BrowserPlugin.
141   static bool ShouldForwardToBrowserPlugin(const IPC::Message& message);
142
143   // blink::WebPlugin implementation.
144   virtual blink::WebPluginContainer* container() const OVERRIDE;
145   virtual bool initialize(blink::WebPluginContainer* container) OVERRIDE;
146   virtual void destroy() OVERRIDE;
147   virtual NPObject* scriptableObject() OVERRIDE;
148   virtual struct _NPP* pluginNPP() OVERRIDE;
149   virtual bool supportsKeyboardFocus() const OVERRIDE;
150   virtual bool supportsEditCommands() const OVERRIDE;
151   virtual bool supportsInputMethod() const OVERRIDE;
152   virtual bool canProcessDrag() const OVERRIDE;
153   virtual void paint(
154       blink::WebCanvas* canvas,
155       const blink::WebRect& rect) OVERRIDE;
156   virtual void updateGeometry(
157       const blink::WebRect& frame_rect,
158       const blink::WebRect& clip_rect,
159       const blink::WebVector<blink::WebRect>& cut_outs_rects,
160       bool is_visible) OVERRIDE;
161   virtual void updateFocus(bool focused) OVERRIDE;
162   virtual void updateVisibility(bool visible) OVERRIDE;
163   virtual bool acceptsInputEvents() OVERRIDE;
164   virtual bool handleInputEvent(
165       const blink::WebInputEvent& event,
166       blink::WebCursorInfo& cursor_info) OVERRIDE;
167   virtual bool handleDragStatusUpdate(blink::WebDragStatus drag_status,
168                                       const blink::WebDragData& drag_data,
169                                       blink::WebDragOperationsMask mask,
170                                       const blink::WebPoint& position,
171                                       const blink::WebPoint& screen) OVERRIDE;
172   virtual void didReceiveResponse(
173       const blink::WebURLResponse& response) OVERRIDE;
174   virtual void didReceiveData(const char* data, int data_length) OVERRIDE;
175   virtual void didFinishLoading() OVERRIDE;
176   virtual void didFailLoading(const blink::WebURLError& error) OVERRIDE;
177   virtual void didFinishLoadingFrameRequest(
178       const blink::WebURL& url,
179       void* notify_data) OVERRIDE;
180   virtual void didFailLoadingFrameRequest(
181       const blink::WebURL& url,
182       void* notify_data,
183       const blink::WebURLError& error) OVERRIDE;
184   virtual bool executeEditCommand(const blink::WebString& name) OVERRIDE;
185   virtual bool executeEditCommand(const blink::WebString& name,
186                                   const blink::WebString& value) OVERRIDE;
187   virtual bool setComposition(
188       const blink::WebString& text,
189       const blink::WebVector<blink::WebCompositionUnderline>& underlines,
190       int selectionStart,
191       int selectionEnd) OVERRIDE;
192   virtual bool confirmComposition(
193       const blink::WebString& text,
194       blink::WebWidget::ConfirmCompositionBehavior selectionBehavior) OVERRIDE;
195   virtual void extendSelectionAndDelete(int before, int after) OVERRIDE;
196
197   // MouseLockDispatcher::LockTarget implementation.
198   virtual void OnLockMouseACK(bool succeeded) OVERRIDE;
199   virtual void OnMouseLockLost() OVERRIDE;
200   virtual bool HandleMouseLockedInputEvent(
201           const blink::WebMouseEvent& event) OVERRIDE;
202
203  private:
204   friend class base::DeleteHelper<BrowserPlugin>;
205   // Only the manager is allowed to create a BrowserPlugin.
206   friend class BrowserPluginManagerImpl;
207   friend class MockBrowserPluginManager;
208
209   // For unit/integration tests.
210   friend class MockBrowserPlugin;
211
212   // A BrowserPlugin object is a controller that represents an instance of a
213   // browser plugin within the embedder renderer process. Once a BrowserPlugin
214   // does an initial navigation or is attached to a newly created guest, it
215   // acquires a guest_instance_id as well. The guest instance ID uniquely
216   // identifies a guest WebContents that's hosted by this BrowserPlugin.
217   BrowserPlugin(RenderViewImpl* render_view, blink::WebFrame* frame);
218
219   virtual ~BrowserPlugin();
220
221   int width() const { return plugin_rect_.width(); }
222   int height() const { return plugin_rect_.height(); }
223   gfx::Rect plugin_rect() { return plugin_rect_; }
224   // Gets the Max Height value used for auto size.
225   int GetAdjustedMaxHeight() const;
226   // Gets the Max Width value used for auto size.
227   int GetAdjustedMaxWidth() const;
228   // Gets the Min Height value used for auto size.
229   int GetAdjustedMinHeight() const;
230   // Gets the Min Width value used for auto size.
231   int GetAdjustedMinWidth() const;
232
233   // Virtual to allow for mocking in tests.
234   virtual float GetDeviceScaleFactor() const;
235
236   void ShowSadGraphic();
237
238   // Parses the attributes of the browser plugin from the element's attributes
239   // and sets them appropriately.
240   void ParseAttributes();
241
242   // Triggers the event-listeners for |event_name|. Note that the function
243   // frees all the values in |props|.
244   void TriggerEvent(const std::string& event_name,
245                     std::map<std::string, base::Value*>* props);
246
247   // Creates and maps a shared damage buffer.
248   virtual base::SharedMemory* CreateDamageBuffer(
249       const size_t size,
250       base::SharedMemoryHandle* shared_memory_handle);
251   // Swaps out the |current_damage_buffer_| with the |pending_damage_buffer_|.
252   void SwapDamageBuffers();
253
254   // Populates BrowserPluginHostMsg_ResizeGuest_Params with resize state and
255   // allocates a new |pending_damage_buffer_| if in software rendering mode.
256   void PopulateResizeGuestParameters(
257       BrowserPluginHostMsg_ResizeGuest_Params* params,
258       const gfx::Rect& view_size,
259       bool needs_repaint);
260
261   // Populates BrowserPluginHostMsg_AutoSize_Params object with autosize state.
262   void PopulateAutoSizeParameters(
263       BrowserPluginHostMsg_AutoSize_Params* params, bool auto_size_enabled);
264
265   // Populates both AutoSize and ResizeGuest parameters based on the current
266   // autosize state.
267   void GetDamageBufferWithSizeParams(
268       BrowserPluginHostMsg_AutoSize_Params* auto_size_params,
269       BrowserPluginHostMsg_ResizeGuest_Params* resize_guest_params,
270       bool needs_repaint);
271
272   // Informs the guest of an updated autosize state.
273   void UpdateGuestAutoSizeState(bool auto_size_enabled);
274
275   // Indicates whether a damage buffer was used by the guest process for the
276   // provided |params|.
277   static bool UsesDamageBuffer(
278       const BrowserPluginMsg_UpdateRect_Params& params);
279
280   // Indicates whether the |pending_damage_buffer_| was used to copy over pixels
281   // given the provided |params|.
282   bool UsesPendingDamageBuffer(
283       const BrowserPluginMsg_UpdateRect_Params& params);
284
285   // IPC message handlers.
286   // Please keep in alphabetical order.
287   void OnAdvanceFocus(int instance_id, bool reverse);
288   void OnAttachACK(int instance_id,
289                    const BrowserPluginMsg_Attach_ACK_Params& ack_params);
290   void OnBuffersSwapped(int instance_id,
291                         const FrameMsg_BuffersSwapped_Params& params);
292   void OnCompositorFrameSwapped(const IPC::Message& message);
293   void OnCopyFromCompositingSurface(int instance_id,
294                                     int request_id,
295                                     gfx::Rect source_rect,
296                                     gfx::Size dest_size);
297   void OnGuestContentWindowReady(int instance_id,
298                                  int content_window_routing_id);
299   void OnGuestGone(int instance_id);
300   void OnSetCursor(int instance_id, const WebCursor& cursor);
301   void OnSetMouseLock(int instance_id, bool enable);
302   void OnShouldAcceptTouchEvents(int instance_id, bool accept);
303   void OnUpdatedName(int instance_id, const std::string& name);
304   void OnUpdateRect(int instance_id,
305                     const BrowserPluginMsg_UpdateRect_Params& params);
306
307   // This is the browser-process-allocated instance ID that uniquely identifies
308   // a guest WebContents.
309   int guest_instance_id_;
310   // This indicates whether this BrowserPlugin has been attached to a
311   // WebContents.
312   bool attached_;
313   base::WeakPtr<RenderViewImpl> render_view_;
314   // We cache the |render_view_|'s routing ID because we need it on destruction.
315   // If the |render_view_| is destroyed before the BrowserPlugin is destroyed
316   // then we will attempt to access a NULL pointer.
317   int render_view_routing_id_;
318   blink::WebPluginContainer* container_;
319   scoped_ptr<BrowserPluginBindings> bindings_;
320   scoped_ptr<BrowserPluginBackingStore> backing_store_;
321   scoped_ptr<base::SharedMemory> current_damage_buffer_;
322   scoped_ptr<base::SharedMemory> pending_damage_buffer_;
323   uint32 damage_buffer_sequence_id_;
324   bool paint_ack_received_;
325   gfx::Rect plugin_rect_;
326   float last_device_scale_factor_;
327   // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
328   SkBitmap* sad_guest_;
329   bool guest_crashed_;
330   scoped_ptr<BrowserPluginHostMsg_ResizeGuest_Params> pending_resize_params_;
331   bool is_auto_size_state_dirty_;
332   // Maximum size constraint for autosize.
333   gfx::Size max_auto_size_;
334   std::string storage_partition_id_;
335   bool persist_storage_;
336   bool valid_partition_id_;
337   int content_window_routing_id_;
338   bool plugin_focused_;
339   // Tracks the visibility of the browser plugin regardless of the whole
340   // embedder RenderView's visibility.
341   bool visible_;
342
343   WebCursor cursor_;
344
345   gfx::Size last_view_size_;
346   bool before_first_navigation_;
347   bool mouse_locked_;
348
349   // BrowserPlugin outlives RenderViewImpl in Chrome Apps and so we need to
350   // store the BrowserPlugin's BrowserPluginManager in a member variable to
351   // avoid accessing the RenderViewImpl.
352   scoped_refptr<BrowserPluginManager> browser_plugin_manager_;
353
354   // Used for HW compositing.
355   bool compositing_enabled_;
356   scoped_refptr<ChildFrameCompositingHelper> compositing_helper_;
357
358   // Used to identify the plugin to WebBindings.
359   scoped_ptr<struct _NPP> npp_;
360
361   // URL for the embedder frame.
362   GURL embedder_frame_url_;
363
364   std::vector<EditCommand> edit_commands_;
365
366   // Weak factory used in v8 |MakeWeak| callback, since the v8 callback might
367   // get called after BrowserPlugin has been destroyed.
368   base::WeakPtrFactory<BrowserPlugin> weak_ptr_factory_;
369
370   DISALLOW_COPY_AND_ASSIGN(BrowserPlugin);
371 };
372
373 }  // namespace content
374
375 #endif  // CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_