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 #ifndef CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
6 #define CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/process_util.h"
13 #include "base/sequenced_task_runner_helpers.h"
15 #include "base/shared_memory.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/render_view_impl.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDragStatus.h"
22 struct BrowserPluginHostMsg_AutoSize_Params
;
23 struct BrowserPluginHostMsg_ResizeGuest_Params
;
24 struct BrowserPluginMsg_LoadCommit_Params
;
25 struct BrowserPluginMsg_UpdateRect_Params
;
29 class BrowserPluginCompositingHelper
;
30 class BrowserPluginManager
;
31 class MockBrowserPlugin
;
33 class CONTENT_EXPORT BrowserPlugin
:
34 NON_EXPORTED_BASE(public WebKit::WebPlugin
) {
36 RenderViewImpl
* render_view() const { return render_view_
.get(); }
38 bool OnMessageReceived(const IPC::Message
& msg
);
40 // Update Browser Plugin's DOM Node attribute |attribute_name| with the value
42 void UpdateDOMAttribute(const std::string
& attribute_name
,
43 const std::string
& attribute_value
);
45 // Get the name attribute value.
46 std::string
name_attribute() const { return name_
; }
47 // Set the name attribute value.
48 void SetNameAttribute(const std::string
& name
);
49 // Get the src attribute value of the BrowserPlugin instance.
50 std::string
src_attribute() const { return src_
; }
51 // Set the src attribute value of the BrowserPlugin instance.
52 bool SetSrcAttribute(const std::string
& src
, std::string
* error_message
);
53 // Get the autosize attribute value.
54 bool auto_size_attribute() const { return auto_size_
; }
55 // Sets the autosize attribute value.
56 void SetAutoSizeAttribute(bool auto_size
);
57 // Get the maxheight attribute value.
58 int max_height_attribute() const { return max_height_
; }
59 // Set the maxheight attribute value.
60 void SetMaxHeightAttribute(int maxheight
);
61 // Get the maxwidth attribute value.
62 int max_width_attribute() const { return max_width_
; }
63 // Set the maxwidth attribute value.
64 void SetMaxWidthAttribute(int max_width
);
65 // Get the minheight attribute value.
66 int min_height_attribute() const { return min_height_
; }
67 // Set the minheight attribute value.
68 void SetMinHeightAttribute(int minheight
);
69 // Get the minwidth attribute value.
70 int min_width_attribute() const { return min_width_
; }
71 // Set the minwidth attribute value.
72 void SetMinWidthAttribute(int minwidth
);
73 bool InAutoSizeBounds(const gfx::Size
& size
) const;
75 // Get the guest's DOMWindow proxy.
76 NPObject
* GetContentWindow() const;
78 // Returns Chrome's process ID for the current guest.
79 int process_id() const { return process_id_
; }
80 // The partition identifier string is stored as UTF-8.
81 std::string
GetPartitionAttribute() const;
82 // Query whether the guest can navigate back to the previous entry.
83 bool CanGoBack() const;
84 // Query whether the guest can navigation forward to the next entry.
85 bool CanGoForward() const;
86 // This method can be successfully called only before the first navigation for
87 // this instance of BrowserPlugin. If an error occurs, the |error_message| is
88 // set appropriately to indicate the failure reason.
89 bool SetPartitionAttribute(const std::string
& partition_id
,
90 std::string
* error_message
);
92 // Inform the BrowserPlugin of the focus state of the embedder RenderView.
93 void SetEmbedderFocus(bool focused
);
94 // Informs the guest of an updated focus state.
95 void UpdateGuestFocusState();
96 // Indicates whether the guest should be focused.
97 bool ShouldGuestBeFocused() const;
99 // Tells the BrowserPlugin to tell the guest to navigate to the previous
100 // navigation entry in the navigation history.
102 // Tells the BrowserPlugin to tell the guest to navigate to the next
103 // navigation entry in the navigation history.
105 // Tells the BrowserPlugin to tell the guest to navigate to a position
106 // relative to the current index in its navigation history.
107 void Go(int relativeIndex
);
108 // Tells the BrowserPlugin to terminate the guest process.
109 void TerminateGuest();
111 // A request from Javascript has been made to stop the loading of the page.
113 // A request from Javascript has been made to reload the page.
115 // A request to enable hardware compositing.
116 void EnableCompositing(bool enable
);
118 // Returns true if |point| lies within the bounds of the plugin rectangle.
119 // Not OK to use this function for making security-sensitive decision since it
120 // can return false positives when the plugin has rotation transformation
122 bool InBounds(const gfx::Point
& point
) const;
124 gfx::Point
ToLocalCoordinates(const gfx::Point
& point
) const;
126 // WebKit::WebPlugin implementation.
127 virtual WebKit::WebPluginContainer
* container() const OVERRIDE
;
128 virtual bool initialize(WebKit::WebPluginContainer
* container
) OVERRIDE
;
129 virtual void destroy() OVERRIDE
;
130 virtual NPObject
* scriptableObject() OVERRIDE
;
131 virtual bool supportsKeyboardFocus() const OVERRIDE
;
132 virtual bool canProcessDrag() const OVERRIDE
;
134 WebKit::WebCanvas
* canvas
,
135 const WebKit::WebRect
& rect
) OVERRIDE
;
136 virtual void updateGeometry(
137 const WebKit::WebRect
& frame_rect
,
138 const WebKit::WebRect
& clip_rect
,
139 const WebKit::WebVector
<WebKit::WebRect
>& cut_outs_rects
,
140 bool is_visible
) OVERRIDE
;
141 virtual void updateFocus(bool focused
) OVERRIDE
;
142 virtual void updateVisibility(bool visible
) OVERRIDE
;
143 virtual bool acceptsInputEvents() OVERRIDE
;
144 virtual bool handleInputEvent(
145 const WebKit::WebInputEvent
& event
,
146 WebKit::WebCursorInfo
& cursor_info
) OVERRIDE
;
147 virtual bool handleDragStatusUpdate(WebKit::WebDragStatus drag_status
,
148 const WebKit::WebDragData
& drag_data
,
149 WebKit::WebDragOperationsMask mask
,
150 const WebKit::WebPoint
& position
,
151 const WebKit::WebPoint
& screen
) OVERRIDE
;
152 virtual void didReceiveResponse(
153 const WebKit::WebURLResponse
& response
) OVERRIDE
;
154 virtual void didReceiveData(const char* data
, int data_length
) OVERRIDE
;
155 virtual void didFinishLoading() OVERRIDE
;
156 virtual void didFailLoading(const WebKit::WebURLError
& error
) OVERRIDE
;
157 virtual void didFinishLoadingFrameRequest(
158 const WebKit::WebURL
& url
,
159 void* notify_data
) OVERRIDE
;
160 virtual void didFailLoadingFrameRequest(
161 const WebKit::WebURL
& url
,
163 const WebKit::WebURLError
& error
) OVERRIDE
;
165 friend class base::DeleteHelper
<BrowserPlugin
>;
166 // Only the manager is allowed to create a BrowserPlugin.
167 friend class BrowserPluginManagerImpl
;
168 friend class MockBrowserPluginManager
;
170 // For unit/integration tests.
171 friend class MockBrowserPlugin
;
173 // A BrowserPlugin object is a controller that represents an instance of a
174 // browser plugin within the embedder renderer process. Each BrowserPlugin
175 // within a process has a unique instance_id that is used to route messages
176 // to it. It takes in a RenderViewImpl that it's associated with along
177 // with the frame within which it lives and the initial attributes assigned
178 // to it on creation.
181 RenderViewImpl
* render_view
,
182 WebKit::WebFrame
* frame
,
183 const WebKit::WebPluginParams
& params
);
185 virtual ~BrowserPlugin();
187 int width() const { return plugin_rect_
.width(); }
188 int height() const { return plugin_rect_
.height(); }
189 int instance_id() const { return instance_id_
; }
190 int render_view_routing_id() const { return render_view_routing_id_
; }
191 BrowserPluginManager
* browser_plugin_manager() const {
192 return browser_plugin_manager_
;
195 // Virtual to allow for mocking in tests.
196 virtual float GetDeviceScaleFactor() const;
198 // Parses the attributes of the browser plugin from the element's attributes
199 // and sets them appropriately.
200 void ParseAttributes(const WebKit::WebPluginParams
& params
);
202 // Triggers the event-listeners for |event_name|. Note that the function
203 // frees all the values in |props|.
204 void TriggerEvent(const std::string
& event_name
,
205 std::map
<std::string
, base::Value
*>* props
);
207 // Creates and maps a shared damage buffer.
208 virtual base::SharedMemory
* CreateDamageBuffer(
210 base::SharedMemoryHandle
* shared_memory_handle
);
211 // Swaps out the |current_damage_buffer_| with the |pending_damage_buffer_|.
212 void SwapDamageBuffers();
214 // Populates BrowserPluginHostMsg_ResizeGuest_Params with resize state and
215 // allocates a new |pending_damage_buffer_| if in software rendering mode.
216 void PopulateResizeGuestParameters(
217 BrowserPluginHostMsg_ResizeGuest_Params
* params
,
218 const gfx::Size
& view_size
);
220 // Populates BrowserPluginHostMsg_AutoSize_Params object with autosize state.
221 void PopulateAutoSizeParameters(
222 BrowserPluginHostMsg_AutoSize_Params
* params
);
224 // Populates both AutoSize and ResizeGuest parameters based on the current
226 void GetDamageBufferWithSizeParams(
227 BrowserPluginHostMsg_AutoSize_Params
* auto_size_params
,
228 BrowserPluginHostMsg_ResizeGuest_Params
* resize_guest_params
);
230 // Informs the guest of an updated autosize state.
231 void UpdateGuestAutoSizeState();
233 // Informs the BrowserPlugin that guest has changed its size in autosize mode.
234 void SizeChangedDueToAutoSize(const gfx::Size
& old_view_size
);
236 // Indicates whether a damage buffer was used by the guest process for the
237 // provided |params|.
238 static bool UsesDamageBuffer(
239 const BrowserPluginMsg_UpdateRect_Params
& params
);
241 // Indicates whether the |pending_damage_buffer_| was used to copy over pixels
242 // given the provided |params|.
243 bool UsesPendingDamageBuffer(
244 const BrowserPluginMsg_UpdateRect_Params
& params
);
246 // IPC message handlers.
247 // Please keep in alphabetical order.
248 void OnAdvanceFocus(int instance_id
, bool reverse
);
249 void OnBuffersSwapped(int instance_id
,
250 const gfx::Size
& size
,
251 std::string mailbox_name
,
254 void OnGuestContentWindowReady(int instance_id
,
255 int content_window_routing_id
);
256 void OnGuestGone(int instance_id
, int process_id
, int status
);
257 void OnGuestResponsive(int instance_id
, int process_id
);
258 void OnGuestUnresponsive(int instance_id
, int process_id
);
259 void OnLoadAbort(int instance_id
,
262 const std::string
& type
);
263 void OnLoadCommit(int instance_id
,
264 const BrowserPluginMsg_LoadCommit_Params
& params
);
265 void OnLoadRedirect(int instance_id
,
269 void OnLoadStart(int instance_id
, const GURL
& url
, bool is_top_level
);
270 void OnLoadStop(int instance_id
);
271 void OnSetCursor(int instance_id
, const WebCursor
& cursor
);
272 void OnShouldAcceptTouchEvents(int instance_id
, bool accept
);
273 void OnUpdatedName(int instance_id
, const std::string
& name
);
274 void OnUpdateRect(int instance_id
,
275 const BrowserPluginMsg_UpdateRect_Params
& params
);
278 base::WeakPtr
<RenderViewImpl
> render_view_
;
279 // We cache the |render_view_|'s routing ID because we need it on destruction.
280 // If the |render_view_| is destroyed before the BrowserPlugin is destroyed
281 // then we will attempt to access a NULL pointer.
282 int render_view_routing_id_
;
283 WebKit::WebPluginContainer
* container_
;
284 scoped_ptr
<BrowserPluginBindings
> bindings_
;
285 scoped_ptr
<BrowserPluginBackingStore
> backing_store_
;
286 scoped_ptr
<base::SharedMemory
> current_damage_buffer_
;
287 scoped_ptr
<base::SharedMemory
> pending_damage_buffer_
;
288 uint32 damage_buffer_sequence_id_
;
289 bool resize_ack_received_
;
290 gfx::Rect plugin_rect_
;
291 // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
292 SkBitmap
* sad_guest_
;
294 scoped_ptr
<BrowserPluginHostMsg_ResizeGuest_Params
> pending_resize_params_
;
295 // True if we have ever sent a NavigateGuest message to the embedder.
296 bool navigate_src_sent_
;
304 std::string storage_partition_id_
;
305 bool persist_storage_
;
306 bool valid_partition_id_
;
307 int content_window_routing_id_
;
308 bool plugin_focused_
;
309 bool embedder_focused_
;
310 // Tracks the visibility of the browser plugin regardless of the whole
311 // embedder RenderView's visibility.
317 gfx::Size last_view_size_
;
318 bool size_changed_in_flight_
;
320 // BrowserPlugin outlives RenderViewImpl in Chrome Apps and so we need to
321 // store the BrowserPlugin's BrowserPluginManager in a member variable to
322 // avoid accessing the RenderViewImpl.
323 scoped_refptr
<BrowserPluginManager
> browser_plugin_manager_
;
325 // Important: Do not add more history state here.
326 // We strongly discourage storing additional history state (such as page IDs)
327 // in the embedder process, at the risk of having incorrect information that
328 // can lead to broken back/forward logic in apps.
329 // It's also important that this state does not get modified by any logic in
330 // the embedder process. It should only be updated in response to navigation
331 // events in the guest. No assumptions should be made about how the index
332 // will change after a navigation (e.g., for back, forward, or go), because
333 // the changes are not always obvious. For example, there is a maximum
334 // number of entries and earlier ones will automatically be pruned.
335 int current_nav_entry_index_
;
336 int nav_entry_count_
;
338 // Used for HW compositing.
339 bool compositing_enabled_
;
340 scoped_ptr
<BrowserPluginCompositingHelper
> compositing_helper_
;
342 DISALLOW_COPY_AND_ASSIGN(BrowserPlugin
);
345 } // namespace content
347 #endif // CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_