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