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 BrowserPluginManager
;
30 class MockBrowserPlugin
;
32 class CONTENT_EXPORT BrowserPlugin
:
33 NON_EXPORTED_BASE(public WebKit::WebPlugin
) {
35 RenderViewImpl
* render_view() const { return render_view_
.get(); }
36 // Called only by tests to clean up before we blow away the MockRenderProcess.
39 virtual bool OnMessageReceived(const IPC::Message
& msg
);
41 // Update Browser Plugin's DOM Node attribute |attribute_name| with the value
43 void UpdateDOMAttribute(const std::string
& attribute_name
,
44 const std::string
& attribute_value
);
46 // Get the src attribute value of the BrowserPlugin instance.
47 std::string
src_attribute() const { return src_
; }
48 // Set the src attribute value of the BrowserPlugin instance.
49 bool SetSrcAttribute(const std::string
& src
, std::string
* error_message
);
50 // Get the autosize attribute value.
51 bool auto_size_attribute() const { return auto_size_
; }
52 // Sets the autosize attribute value.
53 void SetAutoSizeAttribute(bool auto_size
);
54 // Get the maxheight attribute value.
55 int max_height_attribute() const { return max_height_
; }
56 // Set the maxheight attribute value.
57 void SetMaxHeightAttribute(int maxheight
);
58 // Get the maxwidth attribute value.
59 int max_width_attribute() const { return max_width_
; }
60 // Set the maxwidth attribute value.
61 void SetMaxWidthAttribute(int max_width
);
62 // Get the minheight attribute value.
63 int min_height_attribute() const { return min_height_
; }
64 // Set the minheight attribute value.
65 void SetMinHeightAttribute(int minheight
);
66 // Get the minwidth attribute value.
67 int min_width_attribute() const { return min_width_
; }
68 // Set the minwidth attribute value.
69 void SetMinWidthAttribute(int minwidth
);
70 bool InAutoSizeBounds(const gfx::Size
& size
) const;
72 // Get the guest's DOMWindow proxy.
73 NPObject
* GetContentWindow() const;
75 // Returns Chrome's process ID for the current guest.
76 int process_id() const { return process_id_
; }
77 // The partition identifier string is stored as UTF-8.
78 std::string
GetPartitionAttribute() const;
79 // Query whether the guest can navigate back to the previous entry.
80 bool CanGoBack() const;
81 // Query whether the guest can navigation forward to the next entry.
82 bool CanGoForward() 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 SetPartitionAttribute(const std::string
& partition_id
,
87 std::string
* error_message
);
89 // Inform the BrowserPlugin of the focus state of the embedder RenderView.
90 void SetEmbedderFocus(bool focused
);
91 // Informs the guest of an updated focus state.
92 void UpdateGuestFocusState();
93 // Indicates whether the guest should be focused.
94 bool ShouldGuestBeFocused() const;
96 // Tells the BrowserPlugin to tell the guest to navigate to the previous
97 // navigation entry in the navigation history.
99 // Tells the BrowserPlugin to tell the guest to navigate to the next
100 // navigation entry in the navigation history.
102 // Tells the BrowserPlugin to tell the guest to navigate to a position
103 // relative to the current index in its navigation history.
104 void Go(int relativeIndex
);
105 // Tells the BrowserPlugin to terminate the guest process.
106 void TerminateGuest();
108 // A request from Javascript has been made to stop the loading of the page.
110 // A request from Javascript has been made to reload the page.
112 // A request to enable hardware compositing.
113 void EnableCompositing(bool enable
);
115 // Returns true if |point| lies within the bounds of the plugin rectangle.
116 // Not OK to use this function for making security-sensitive decision since it
117 // can return false positives when the plugin has rotation transformation
119 bool InBounds(const gfx::Point
& point
) const;
121 gfx::Point
ToLocalCoordinates(const gfx::Point
& point
) const;
123 // WebKit::WebPlugin implementation.
124 virtual WebKit::WebPluginContainer
* container() const OVERRIDE
;
125 virtual bool initialize(WebKit::WebPluginContainer
* container
) OVERRIDE
;
126 virtual void destroy() OVERRIDE
;
127 virtual NPObject
* scriptableObject() OVERRIDE
;
128 virtual bool supportsKeyboardFocus() const OVERRIDE
;
129 virtual bool canProcessDrag() const OVERRIDE
;
131 WebKit::WebCanvas
* canvas
,
132 const WebKit::WebRect
& rect
) OVERRIDE
;
133 virtual void updateGeometry(
134 const WebKit::WebRect
& frame_rect
,
135 const WebKit::WebRect
& clip_rect
,
136 const WebKit::WebVector
<WebKit::WebRect
>& cut_outs_rects
,
137 bool is_visible
) OVERRIDE
;
138 virtual void updateFocus(bool focused
) OVERRIDE
;
139 virtual void updateVisibility(bool visible
) OVERRIDE
;
140 virtual bool acceptsInputEvents() OVERRIDE
;
141 virtual bool handleInputEvent(
142 const WebKit::WebInputEvent
& event
,
143 WebKit::WebCursorInfo
& cursor_info
) OVERRIDE
;
144 virtual bool handleDragStatusUpdate(WebKit::WebDragStatus drag_status
,
145 const WebKit::WebDragData
& drag_data
,
146 WebKit::WebDragOperationsMask mask
,
147 const WebKit::WebPoint
& position
,
148 const WebKit::WebPoint
& screen
) OVERRIDE
;
149 virtual void didReceiveResponse(
150 const WebKit::WebURLResponse
& response
) OVERRIDE
;
151 virtual void didReceiveData(const char* data
, int data_length
) OVERRIDE
;
152 virtual void didFinishLoading() OVERRIDE
;
153 virtual void didFailLoading(const WebKit::WebURLError
& error
) OVERRIDE
;
154 virtual void didFinishLoadingFrameRequest(
155 const WebKit::WebURL
& url
,
156 void* notify_data
) OVERRIDE
;
157 virtual void didFailLoadingFrameRequest(
158 const WebKit::WebURL
& url
,
160 const WebKit::WebURLError
& error
) OVERRIDE
;
162 friend class base::DeleteHelper
<BrowserPlugin
>;
163 // Only the manager is allowed to create a BrowserPlugin.
164 friend class BrowserPluginManagerImpl
;
165 friend class MockBrowserPluginManager
;
167 // For unit/integration tests.
168 friend class MockBrowserPlugin
;
170 // A BrowserPlugin object is a controller that represents an instance of a
171 // browser plugin within the embedder renderer process. Each BrowserPlugin
172 // within a process has a unique instance_id that is used to route messages
173 // to it. It takes in a RenderViewImpl that it's associated with along
174 // with the frame within which it lives and the initial attributes assigned
175 // to it on creation.
178 RenderViewImpl
* render_view
,
179 WebKit::WebFrame
* frame
,
180 const WebKit::WebPluginParams
& params
);
182 virtual ~BrowserPlugin();
184 int width() const { return plugin_rect_
.width(); }
185 int height() const { return plugin_rect_
.height(); }
186 int instance_id() const { return instance_id_
; }
187 int render_view_routing_id() const { return render_view_routing_id_
; }
188 BrowserPluginManager
* browser_plugin_manager() const {
189 return browser_plugin_manager_
;
192 // Virtual to allow for mocking in tests.
193 virtual float GetDeviceScaleFactor() const;
195 // Parses the attributes of the browser plugin from the element's attributes
196 // and sets them appropriately.
197 void ParseAttributes(const WebKit::WebPluginParams
& params
);
199 // Triggers the event-listeners for |event_name|. Note that the function
200 // frees all the values in |props|.
201 void TriggerEvent(const std::string
& event_name
,
202 std::map
<std::string
, base::Value
*>* props
);
204 // Creates and maps transport dib. Overridden in tests.
205 virtual TransportDIB
* CreateTransportDIB(const size_t size
);
206 // Frees up the damage buffer. Overridden in tests.
207 virtual void FreeDamageBuffer(TransportDIB
** damage_buffer
);
208 // Swaps out the |current_damage_buffer_| with the |pending_damage_buffer_|.
209 void SwapDamageBuffers();
211 // Populates BrowserPluginHostMsg_ResizeGuest_Params with resize state and
212 // returns the newly allocated TransportDIB.
213 TransportDIB
* PopulateResizeGuestParameters(
214 BrowserPluginHostMsg_ResizeGuest_Params
* params
,
215 const gfx::Size
& view_size
);
217 // Populates BrowserPluginHostMsg_AutoSize_Params object with autosize state.
218 void PopulateAutoSizeParameters(
219 BrowserPluginHostMsg_AutoSize_Params
* params
);
221 // Populates both AutoSize and ResizeGuest parameters based on the current
223 TransportDIB
* GetDamageBufferWithSizeParams(
224 BrowserPluginHostMsg_AutoSize_Params
* auto_size_params
,
225 BrowserPluginHostMsg_ResizeGuest_Params
* resize_guest_params
);
227 // Informs the guest of an updated autosize state.
228 void UpdateGuestAutoSizeState();
230 // Informs the BrowserPlugin that guest has changed its size in autosize mode.
231 void SizeChangedDueToAutoSize(const gfx::Size
& old_view_size
);
233 #if defined(OS_MACOSX)
234 bool DamageBufferValid(const TransportDIB::Id
& damage_buffer_id
);
235 bool DamageBufferMatches(const TransportDIB
* damage_buffer
,
236 const TransportDIB::Id
& other_damage_buffer_id
);
238 bool DamageBufferValid(const TransportDIB::Handle
& damage_buffer_handle
);
239 bool DamageBufferMatches(
240 const TransportDIB
* damage_buffer
,
241 const TransportDIB::Handle
& other_damage_buffer_handle
);
244 // IPC message handlers.
245 // Please keep in alphabetical order.
246 void OnAdvanceFocus(int instance_id
, bool reverse
);
247 void OnGuestContentWindowReady(int instance_id
,
248 int content_window_routing_id
);
249 void OnGuestGone(int instance_id
, int process_id
, int status
);
250 void OnGuestResponsive(int instance_id
, int process_id
);
251 void OnGuestUnresponsive(int instance_id
, int process_id
);
252 void OnLoadAbort(int instance_id
,
255 const std::string
& type
);
256 void OnLoadCommit(int instance_id
,
257 const BrowserPluginMsg_LoadCommit_Params
& params
);
258 void OnLoadRedirect(int instance_id
,
262 void OnLoadStart(int instance_id
, const GURL
& url
, bool is_top_level
);
263 void OnLoadStop(int instance_id
);
264 void OnSetCursor(int instance_id
, const WebCursor
& cursor
);
265 void OnShouldAcceptTouchEvents(int instance_id
, bool accept
);
266 void OnUpdateRect(int instance_id
,
268 const BrowserPluginMsg_UpdateRect_Params
& params
);
271 base::WeakPtr
<RenderViewImpl
> render_view_
;
272 // We cache the |render_view_|'s routing ID because we need it on destruction.
273 // If the |render_view_| is destroyed before the BrowserPlugin is destroyed
274 // then we will attempt to access a NULL pointer.
275 int render_view_routing_id_
;
276 WebKit::WebPluginContainer
* container_
;
277 scoped_ptr
<BrowserPluginBindings
> bindings_
;
278 scoped_ptr
<BrowserPluginBackingStore
> backing_store_
;
279 TransportDIB
* current_damage_buffer_
;
280 TransportDIB
* pending_damage_buffer_
;
281 bool resize_ack_received_
;
282 gfx::Rect plugin_rect_
;
283 // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
284 SkBitmap
* sad_guest_
;
286 scoped_ptr
<BrowserPluginHostMsg_ResizeGuest_Params
> pending_resize_params_
;
287 // True if we have ever sent a NavigateGuest message to the embedder.
288 bool navigate_src_sent_
;
296 std::string storage_partition_id_
;
297 bool persist_storage_
;
298 bool valid_partition_id_
;
299 int content_window_routing_id_
;
300 bool plugin_focused_
;
301 bool embedder_focused_
;
302 // Tracks the visibility of the browser plugin regardless of the whole
303 // embedder RenderView's visibility.
308 gfx::Size last_view_size_
;
309 bool size_changed_in_flight_
;
311 // BrowserPlugin outlives RenderViewImpl in Chrome Apps and so we need to
312 // store the BrowserPlugin's BrowserPluginManager in a member variable to
313 // avoid accessing the RenderViewImpl.
314 scoped_refptr
<BrowserPluginManager
> browser_plugin_manager_
;
316 // Important: Do not add more history state here.
317 // We strongly discourage storing additional history state (such as page IDs)
318 // in the embedder process, at the risk of having incorrect information that
319 // can lead to broken back/forward logic in apps.
320 // It's also important that this state does not get modified by any logic in
321 // the embedder process. It should only be updated in response to navigation
322 // events in the guest. No assumptions should be made about how the index
323 // will change after a navigation (e.g., for back, forward, or go), because
324 // the changes are not always obvious. For example, there is a maximum
325 // number of entries and earlier ones will automatically be pruned.
326 int current_nav_entry_index_
;
327 int nav_entry_count_
;
329 // Used for HW compositing.
330 bool compositing_enabled_
;
332 DISALLOW_COPY_AND_ASSIGN(BrowserPlugin
);
335 } // namespace content
337 #endif // CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_