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 #include "content/renderer/browser_plugin/browser_plugin.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "base/string_util.h" | |
9 #include "content/common/browser_plugin_messages.h" | |
10 #include "content/public/common/content_client.h" | |
11 #include "content/public/renderer/content_renderer_client.h" | |
12 #include "content/renderer/browser_plugin/browser_plugin_bindings.h" | |
13 #include "content/renderer/browser_plugin/browser_plugin_manager.h" | |
14 #include "content/renderer/render_process.h" | |
15 #include "content/renderer/render_thread_impl.h" | |
16 #include "skia/ext/platform_canvas.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginParams.h" | |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" | |
25 #include "webkit/plugins/sad_plugin.h" | |
26 | |
27 using WebKit::WebCanvas; | |
28 using WebKit::WebPlugin; | |
29 using WebKit::WebPluginContainer; | |
30 using WebKit::WebPluginParams; | |
31 using WebKit::WebPoint; | |
32 using WebKit::WebString; | |
33 using WebKit::WebRect; | |
34 using WebKit::WebURL; | |
35 using WebKit::WebVector; | |
36 | |
37 namespace content { | |
38 namespace browser_plugin { | |
39 | |
40 namespace { | |
41 const char kNavigation[] = "navigation"; | |
42 } | |
43 | |
44 static const char* kSrcAttribute = "src"; | |
45 | |
46 BrowserPlugin::BrowserPlugin( | |
47 int instance_id, | |
48 RenderViewImpl* render_view, | |
49 int routing_id, | |
50 WebKit::WebFrame* frame, | |
51 const WebPluginParams& params) | |
52 : instance_id_(instance_id), | |
53 render_view_(render_view), | |
54 routing_id_(routing_id), | |
55 container_(NULL), | |
56 damage_buffer_(NULL), | |
57 sad_guest_(NULL), | |
58 guest_crashed_(false), | |
59 resize_pending_(false), | |
60 parent_frame_(frame->identifier()) { | |
61 BrowserPluginManager::Get()->AddBrowserPlugin(instance_id, this); | |
62 bindings_.reset(new BrowserPluginBindings(this)); | |
63 | |
64 std::string src; | |
65 ParseSrcAttribute(params, &src); | |
66 SetSrcAttribute(src); | |
67 } | |
68 | |
69 BrowserPlugin::~BrowserPlugin() { | |
70 if (damage_buffer_) { | |
71 RenderProcess::current()->FreeTransportDIB(damage_buffer_); | |
72 damage_buffer_ = NULL; | |
73 } | |
74 BrowserPluginManager::Get()->RemoveBrowserPlugin(instance_id_); | |
75 BrowserPluginManager::Get()->Send( | |
76 new BrowserPluginHostMsg_PluginDestroyed( | |
77 routing_id_, | |
78 instance_id_)); | |
79 } | |
80 | |
81 void BrowserPlugin::Cleanup() { | |
82 if (damage_buffer_) { | |
83 RenderProcess::current()->FreeTransportDIB(damage_buffer_); | |
84 damage_buffer_ = NULL; | |
85 } | |
86 } | |
87 | |
88 std::string BrowserPlugin::GetSrcAttribute() const { | |
89 if (guest_crashed_) | |
90 return std::string(); | |
91 return src_; | |
92 } | |
93 | |
94 void BrowserPlugin::SetSrcAttribute(const std::string& src) { | |
95 // TODO(fsamuel): What if the BrowserPlugin element's parent frame | |
96 // changes? | |
97 if (src == src_ && !guest_crashed_) | |
98 return; | |
99 if (!src.empty()) { | |
100 BrowserPluginManager::Get()->Send( | |
101 new BrowserPluginHostMsg_NavigateFromEmbedder( | |
102 routing_id_, | |
103 instance_id_, | |
104 parent_frame_, | |
105 src)); | |
106 } | |
107 src_ = src; | |
108 guest_crashed_ = false; | |
109 } | |
110 | |
111 void BrowserPlugin::ParseSrcAttribute( | |
112 const WebKit::WebPluginParams& params, | |
113 std::string* src) { | |
114 // Get the src attribute from the attributes vector | |
115 for (unsigned i = 0; i < params.attributeNames.size(); ++i) { | |
116 std::string attributeName = params.attributeNames[i].utf8(); | |
117 if (LowerCaseEqualsASCII(attributeName, kSrcAttribute)) { | |
118 *src = params.attributeValues[i].utf8(); | |
119 break; | |
120 } | |
121 } | |
122 } | |
123 | |
124 void BrowserPlugin::UpdateRect( | |
125 int message_id, | |
126 const BrowserPluginMsg_UpdateRect_Params& params) { | |
127 if (width() != params.view_size.width() || | |
128 height() != params.view_size.height()) { | |
129 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_UpdateRect_ACK( | |
130 routing_id_, | |
131 instance_id_, | |
132 message_id, | |
133 gfx::Size(width(), height()))); | |
134 return; | |
135 } | |
136 if (params.is_resize_ack) { | |
137 resize_pending_ = false; | |
138 backing_store_.reset( | |
139 new BrowserPluginBackingStore(gfx::Size(width(), height()))); | |
140 } | |
141 | |
142 // Update the backing store. | |
143 if (!params.scroll_rect.IsEmpty()) { | |
144 backing_store_->ScrollBackingStore(params.dx, | |
145 params.dy, | |
146 params.scroll_rect, | |
147 params.view_size); | |
148 } | |
149 for (unsigned i = 0; i < params.copy_rects.size(); i++) { | |
150 backing_store_->PaintToBackingStore(params.bitmap_rect, | |
151 params.copy_rects, | |
152 damage_buffer_); | |
153 } | |
154 // Invalidate the container. | |
155 container_->invalidate(); | |
156 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_UpdateRect_ACK( | |
157 routing_id_, | |
158 instance_id_, | |
159 message_id, | |
160 gfx::Size())); | |
161 } | |
162 | |
163 void BrowserPlugin::GuestCrashed() { | |
164 guest_crashed_ = true; | |
165 src_.clear(); | |
166 container_->invalidate(); | |
167 } | |
168 | |
169 bool BrowserPlugin::HasListeners(const std::string& event_name) { | |
170 return event_listener_map_.find(event_name) != event_listener_map_.end(); | |
171 } | |
172 | |
173 void BrowserPlugin::UpdateURL(const GURL& url) { | |
174 src_ = url.spec(); | |
175 if (!HasListeners(kNavigation)) | |
176 return; | |
177 | |
178 EventListeners& listeners = event_listener_map_[kNavigation]; | |
179 EventListeners::iterator it = listeners.begin(); | |
180 for (; it != listeners.end(); ++it) { | |
181 v8::Context::Scope context_scope(v8::Context::New()); | |
182 v8::HandleScope handle_scope; | |
183 v8::Local<v8::Value> param = | |
184 v8::Local<v8::Value>::New(v8::String::New(src_.c_str())); | |
185 container()->element().document().frame()-> | |
186 callFunctionEvenIfScriptDisabled(*it, | |
187 v8::Object::New(), | |
188 1, | |
189 ¶m); | |
190 } | |
191 } | |
192 | |
193 void BrowserPlugin::AdvanceFocus(bool reverse) { | |
194 // We do not have a RenderView when we are testing. | |
195 if (render_view_) | |
196 render_view_->GetWebView()->advanceFocus(reverse); | |
197 } | |
198 | |
199 void BrowserPlugin::PostMessage(const string16& message, | |
200 const string16& target_origin) { | |
201 BrowserPluginHostMsg_PostMessage_Params params; | |
202 params.data = message; | |
203 params.target_origin = target_origin; | |
204 params.source_routing_id = instance_id_; | |
205 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_RouteMessageEvent( | |
206 routing_id_, params)); | |
207 } | |
208 | |
209 bool BrowserPlugin::AddEventListener(const std::string& event_name, | |
210 v8::Local<v8::Function> function) { | |
211 EventListeners& listeners = event_listener_map_[event_name]; | |
212 for (unsigned int i = 0; i < listeners.size(); ++i) { | |
213 if (listeners[i] == function) | |
214 return false; | |
215 } | |
216 v8::Persistent<v8::Function> persistent_function = | |
217 v8::Persistent<v8::Function>::New(function); | |
218 listeners.push_back(persistent_function); | |
219 return true; | |
220 } | |
221 | |
222 bool BrowserPlugin::RemoveEventListener(const std::string& event_name, | |
223 v8::Local<v8::Function> function) { | |
224 if (event_listener_map_.find(event_name) == event_listener_map_.end()) | |
225 return false; | |
226 | |
227 EventListeners& listeners = event_listener_map_[event_name]; | |
228 EventListeners::iterator it = listeners.begin(); | |
229 for (; it != listeners.end(); ++it) { | |
230 if (*it == function) { | |
231 listeners.erase(it); | |
232 return true; | |
Nate Chapin
2012/08/01 17:36:17
I believe you need to call Dispose() on the v8::Pe
Fady Samuel
2012/08/01 20:32:08
Done.
| |
233 } | |
234 } | |
235 return false; | |
236 } | |
237 | |
238 WebKit::WebPluginContainer* BrowserPlugin::container() const { | |
239 return container_; | |
240 } | |
241 | |
242 bool BrowserPlugin::initialize(WebPluginContainer* container) { | |
243 container_ = container; | |
244 return true; | |
245 } | |
246 | |
247 void BrowserPlugin::destroy() { | |
248 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
249 } | |
250 | |
251 NPObject* BrowserPlugin::scriptableObject() { | |
252 NPObject* browser_plugin_np_object(bindings_->np_object()); | |
253 // The object is expected to be retained before it is returned. | |
254 WebKit::WebBindings::retainObject(browser_plugin_np_object); | |
255 return browser_plugin_np_object; | |
256 } | |
257 | |
258 bool BrowserPlugin::supportsKeyboardFocus() const { | |
259 return true; | |
260 } | |
261 | |
262 void BrowserPlugin::paint(WebCanvas* canvas, const WebRect& rect) { | |
263 if (guest_crashed_) { | |
264 if (!sad_guest_) // Lazily initialize bitmap. | |
265 sad_guest_ = content::GetContentClient()->renderer()-> | |
266 GetSadPluginBitmap(); | |
267 webkit::PaintSadPlugin(canvas, plugin_rect_, *sad_guest_); | |
268 return; | |
269 } | |
270 SkAutoCanvasRestore auto_restore(canvas, true); | |
271 canvas->translate(plugin_rect_.x(), plugin_rect_.y()); | |
272 SkRect image_data_rect = SkRect::MakeXYWH( | |
273 SkIntToScalar(0), | |
274 SkIntToScalar(0), | |
275 SkIntToScalar(plugin_rect_.width()), | |
276 SkIntToScalar(plugin_rect_.height())); | |
277 canvas->clipRect(image_data_rect); | |
278 // Paint white in case we have nothing in our backing store. | |
279 SkPaint paint; | |
280 paint.setStyle(SkPaint::kFill_Style); | |
281 paint.setColor(SK_ColorWHITE); | |
282 canvas->drawRect(image_data_rect, paint); | |
283 // Stay at white if we have no src set, or we don't yet have a backing store. | |
284 if (!backing_store_.get() || src_.empty()) | |
285 return; | |
286 canvas->drawBitmap(backing_store_->GetBitmap(), 0, 0); | |
287 } | |
288 | |
289 void BrowserPlugin::updateGeometry( | |
290 const WebRect& window_rect, | |
291 const WebRect& clip_rect, | |
292 const WebVector<WebRect>& cut_outs_rects, | |
293 bool is_visible) { | |
294 int old_width = width(); | |
295 int old_height = height(); | |
296 plugin_rect_ = window_rect; | |
297 if (old_width == window_rect.width && | |
298 old_height == window_rect.height) | |
299 return; | |
300 | |
301 const size_t stride = skia::PlatformCanvas::StrideForWidth(window_rect.width); | |
302 const size_t size = window_rect.height * stride; | |
303 | |
304 // Don't drop the old damage buffer until after we've made sure that the | |
305 // browser process has dropped it. | |
306 TransportDIB* new_damage_buffer = | |
307 RenderProcess::current()->CreateTransportDIB(size); | |
308 DCHECK(new_damage_buffer); | |
309 | |
310 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_ResizeGuest( | |
311 routing_id_, | |
312 instance_id_, | |
313 new_damage_buffer->id(), | |
314 window_rect.width, | |
315 window_rect.height, | |
316 resize_pending_)); | |
317 resize_pending_ = true; | |
318 | |
319 if (damage_buffer_) { | |
320 RenderProcess::current()->FreeTransportDIB(damage_buffer_); | |
321 damage_buffer_ = NULL; | |
322 } | |
323 damage_buffer_ = new_damage_buffer; | |
324 } | |
325 | |
326 void BrowserPlugin::updateFocus(bool focused) { | |
327 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_SetFocus( | |
328 routing_id_, | |
329 instance_id_, | |
330 focused)); | |
331 } | |
332 | |
333 void BrowserPlugin::updateVisibility(bool visible) { | |
334 } | |
335 | |
336 bool BrowserPlugin::acceptsInputEvents() { | |
337 return true; | |
338 } | |
339 | |
340 bool BrowserPlugin::handleInputEvent(const WebKit::WebInputEvent& event, | |
341 WebKit::WebCursorInfo& cursor_info) { | |
342 if (guest_crashed_ || src_.empty()) | |
343 return false; | |
344 bool handled = false; | |
345 WebCursor cursor; | |
346 IPC::Message* message = | |
347 new BrowserPluginHostMsg_HandleInputEvent( | |
348 routing_id_, | |
349 &handled, | |
350 &cursor); | |
351 message->WriteInt(instance_id_); | |
352 message->WriteData(reinterpret_cast<const char*>(&plugin_rect_), | |
353 sizeof(gfx::Rect)); | |
354 message->WriteData(reinterpret_cast<const char*>(&event), event.size); | |
355 BrowserPluginManager::Get()->Send(message); | |
356 cursor.GetCursorInfo(&cursor_info); | |
357 return handled; | |
358 } | |
359 | |
360 void BrowserPlugin::didReceiveResponse( | |
361 const WebKit::WebURLResponse& response) { | |
362 } | |
363 | |
364 void BrowserPlugin::didReceiveData(const char* data, int data_length) { | |
365 } | |
366 | |
367 void BrowserPlugin::didFinishLoading() { | |
368 } | |
369 | |
370 void BrowserPlugin::didFailLoading(const WebKit::WebURLError& error) { | |
371 } | |
372 | |
373 void BrowserPlugin::didFinishLoadingFrameRequest(const WebKit::WebURL& url, | |
374 void* notify_data) { | |
375 } | |
376 | |
377 void BrowserPlugin::didFailLoadingFrameRequest( | |
378 const WebKit::WebURL& url, | |
379 void* notify_data, | |
380 const WebKit::WebURLError& error) { | |
381 } | |
382 | |
383 } // namespace browser_plugin | |
384 } // namespace content | |
OLD | NEW |