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