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