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