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