Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: content/renderer/browser_plugin/browser_plugin.cc

Issue 10830072: Browser Plugin: New Implementation (Renderer Side) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed leaky Persistents in BrowserPlugin Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 kNavigation[] = "navigation";
Charlie Reis 2012/08/01 23:20:51 Please make it clear what this is used for. And w
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 // This is a virtual method call and so it's potentially unsafe
72 // but given its use in MockBrowserPlugin (it doesn't access this), this
73 // should work fine in practice.
Charlie Reis 2012/08/01 23:20:51 Could you have MockBrowserPlugin call Cleanup inst
Fady Samuel 2012/08/02 18:32:47 No longer doing a virtual method call after iterat
74 FreeTransportDIB(damage_buffer_);
75 damage_buffer_ = NULL;
76 }
77 RemoveEventListeners();
78 BrowserPluginManager::Get()->RemoveBrowserPlugin(instance_id_);
79 BrowserPluginManager::Get()->Send(
80 new BrowserPluginHostMsg_PluginDestroyed(
81 routing_id_,
82 instance_id_));
83 }
84
85 void BrowserPlugin::Cleanup() {
86 if (damage_buffer_) {
87 FreeTransportDIB(damage_buffer_);
88 damage_buffer_ = NULL;
89 }
90 }
91
92 std::string BrowserPlugin::GetSrcAttribute() const {
93 if (guest_crashed_)
94 return std::string();
Charlie Reis 2012/08/01 23:20:51 I don't see why we wouldn't return src_ after a cr
Fady Samuel 2012/08/02 18:32:47 Removed.
95 return src_;
96 }
97
98 void BrowserPlugin::SetSrcAttribute(const std::string& src) {
99 // TODO(fsamuel): What if the BrowserPlugin element's parent frame
100 // changes?
101 if (src == src_ && !guest_crashed_)
102 return;
Charlie Reis 2012/08/01 23:20:51 This prevents using SetSrcAttribute for doing a re
Fady Samuel 2012/08/02 18:32:47 Actually, it's the exact opposite. If guest_crashe
Charlie Reis 2012/08/03 18:06:02 Yes, but why would you want to prevent reload if t
103 if (!src.empty()) {
104 BrowserPluginManager::Get()->Send(
105 new BrowserPluginHostMsg_NavigateFromEmbedder(
106 routing_id_,
107 instance_id_,
108 parent_frame_,
109 src));
110 }
111 src_ = src;
112 guest_crashed_ = false;
113 }
114
115 void BrowserPlugin::ParseSrcAttribute(
Charlie Reis 2012/08/01 23:20:51 There's no way to tell if this succeeded or failed
Fady Samuel 2012/08/02 18:32:47 Done.
116 const WebKit::WebPluginParams& params,
117 std::string* src) {
118 // Get the src attribute from the attributes vector
119 for (unsigned i = 0; i < params.attributeNames.size(); ++i) {
120 std::string attributeName = params.attributeNames[i].utf8();
121 if (LowerCaseEqualsASCII(attributeName, kSrcAttribute)) {
122 *src = params.attributeValues[i].utf8();
123 break;
124 }
125 }
126 }
127
128 TransportDIB* BrowserPlugin::CreateTransportDIB(size_t size) {
129 return static_cast<RenderProcessImpl*>(RenderProcess::current())->
130 CreateTransportDIB(size);
131 }
132
133 void BrowserPlugin::FreeTransportDIB(TransportDIB* dib) {
134 static_cast<RenderProcessImpl*>(RenderProcess::current())->
135 FreeTransportDIB(dib);
136 }
137
138 void BrowserPlugin::RemoveEventListeners() {
139 EventListenerMap::iterator event_listener_map_iter =
140 event_listener_map_.begin();
141 for (; event_listener_map_iter != event_listener_map_.end();
142 ++event_listener_map_iter) {
143 EventListeners& listeners =
144 event_listener_map_[event_listener_map_iter->first];
145 EventListeners::iterator it = listeners.begin();
146 for (; it != listeners.end(); ++it) {
147 it->Dispose();
148 }
149 }
150 event_listener_map_.clear();
151 }
152
153 void BrowserPlugin::UpdateRect(
154 int message_id,
155 const BrowserPluginMsg_UpdateRect_Params& params) {
156 if (width() != params.view_size.width() ||
157 height() != params.view_size.height()) {
158 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_UpdateRect_ACK(
159 routing_id_,
160 instance_id_,
161 message_id,
162 gfx::Size(width(), height())));
163 return;
164 }
165 if (params.is_resize_ack) {
166 resize_pending_ = false;
167 backing_store_.reset(
168 new BrowserPluginBackingStore(gfx::Size(width(), height())));
169 }
170
171 // Update the backing store.
172 if (!params.scroll_rect.IsEmpty()) {
173 backing_store_->ScrollBackingStore(params.dx,
174 params.dy,
175 params.scroll_rect,
176 params.view_size);
177 }
178 for (unsigned i = 0; i < params.copy_rects.size(); i++) {
179 backing_store_->PaintToBackingStore(params.bitmap_rect,
180 params.copy_rects,
181 damage_buffer_);
182 }
183 // Invalidate the container.
184 container_->invalidate();
185 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_UpdateRect_ACK(
186 routing_id_,
187 instance_id_,
188 message_id,
189 gfx::Size()));
190 }
191
192 void BrowserPlugin::GuestCrashed() {
193 guest_crashed_ = true;
194 src_.clear();
Charlie Reis 2012/08/01 23:20:51 Again, I don't think we should clear this.
Fady Samuel 2012/08/02 18:32:47 Done.
195 container_->invalidate();
196 }
197
198 bool BrowserPlugin::HasListeners(const std::string& event_name) {
199 return event_listener_map_.find(event_name) != event_listener_map_.end();
200 }
201
202 void BrowserPlugin::UpdateURL(const GURL& url) {
203 src_ = url.spec();
204 if (!HasListeners(kNavigation))
205 return;
206
207 EventListeners& listeners = event_listener_map_[kNavigation];
208 EventListeners::iterator it = listeners.begin();
209 for (; it != listeners.end(); ++it) {
210 v8::Context::Scope context_scope(v8::Context::New());
211 v8::HandleScope handle_scope;
212 v8::Local<v8::Value> param =
213 v8::Local<v8::Value>::New(v8::String::New(src_.c_str()));
214 container()->element().document().frame()->
215 callFunctionEvenIfScriptDisabled(*it,
Charlie Reis 2012/08/01 23:20:51 Is this common practice? I'm surprised we would c
Fady Samuel 2012/08/02 18:32:47 That's what I've seen done elsewhere, but there mi
216 v8::Object::New(),
217 1,
218 &param);
219 }
220 }
221
222 void BrowserPlugin::AdvanceFocus(bool reverse) {
223 // We do not have a RenderView when we are testing.
224 if (render_view_)
225 render_view_->GetWebView()->advanceFocus(reverse);
226 }
227
228 void BrowserPlugin::PostMessage(const string16& message,
229 const string16& target_origin) {
230 BrowserPluginHostMsg_PostMessage_Params params;
231 params.data = message;
232 params.target_origin = target_origin;
233 params.source_routing_id = instance_id_;
234 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_RouteMessageEvent(
235 routing_id_, params));
236 }
237
238 bool BrowserPlugin::AddEventListener(const std::string& event_name,
239 v8::Local<v8::Function> function) {
240 EventListeners& listeners = event_listener_map_[event_name];
241 for (unsigned int i = 0; i < listeners.size(); ++i) {
242 if (listeners[i] == function)
243 return false;
244 }
245 v8::Persistent<v8::Function> persistent_function =
246 v8::Persistent<v8::Function>::New(function);
247 listeners.push_back(persistent_function);
248 return true;
249 }
250
251 bool BrowserPlugin::RemoveEventListener(const std::string& event_name,
252 v8::Local<v8::Function> function) {
253 if (event_listener_map_.find(event_name) == event_listener_map_.end())
254 return false;
255
256 EventListeners& listeners = event_listener_map_[event_name];
257 EventListeners::iterator it = listeners.begin();
258 for (; it != listeners.end(); ++it) {
259 if (*it == function) {
260 it->Dispose();
261 listeners.erase(it);
262 return true;
263 }
264 }
265 return false;
266 }
267
268 WebKit::WebPluginContainer* BrowserPlugin::container() const {
269 return container_;
270 }
271
272 bool BrowserPlugin::initialize(WebPluginContainer* container) {
273 container_ = container;
274 return true;
275 }
276
277 void BrowserPlugin::destroy() {
278 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
279 }
280
281 NPObject* BrowserPlugin::scriptableObject() {
282 NPObject* browser_plugin_np_object(bindings_->np_object());
283 // The object is expected to be retained before it is returned.
284 WebKit::WebBindings::retainObject(browser_plugin_np_object);
285 return browser_plugin_np_object;
286 }
287
288 bool BrowserPlugin::supportsKeyboardFocus() const {
289 return true;
290 }
291
292 void BrowserPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
293 if (guest_crashed_) {
294 if (!sad_guest_) // Lazily initialize bitmap.
295 sad_guest_ = content::GetContentClient()->renderer()->
296 GetSadPluginBitmap();
297 webkit::PaintSadPlugin(canvas, plugin_rect_, *sad_guest_);
Charlie Reis 2012/08/01 23:20:51 While this is technically implemented as a plugin,
Fady Samuel 2012/08/02 18:32:47 Done.
298 return;
299 }
300 SkAutoCanvasRestore auto_restore(canvas, true);
301 canvas->translate(plugin_rect_.x(), plugin_rect_.y());
302 SkRect image_data_rect = SkRect::MakeXYWH(
303 SkIntToScalar(0),
304 SkIntToScalar(0),
305 SkIntToScalar(plugin_rect_.width()),
306 SkIntToScalar(plugin_rect_.height()));
307 canvas->clipRect(image_data_rect);
308 // Paint white in case we have nothing in our backing store.
309 SkPaint paint;
310 paint.setStyle(SkPaint::kFill_Style);
311 paint.setColor(SK_ColorWHITE);
312 canvas->drawRect(image_data_rect, paint);
313 // Stay at white if we have no src set, or we don't yet have a backing store.
314 if (!backing_store_.get() || src_.empty())
315 return;
316 canvas->drawBitmap(backing_store_->GetBitmap(), 0, 0);
317 }
318
319 void BrowserPlugin::updateGeometry(
320 const WebRect& window_rect,
321 const WebRect& clip_rect,
322 const WebVector<WebRect>& cut_outs_rects,
323 bool is_visible) {
324 int old_width = width();
325 int old_height = height();
326 plugin_rect_ = window_rect;
327 if (old_width == window_rect.width &&
328 old_height == window_rect.height)
329 return;
330
331 const size_t stride = skia::PlatformCanvas::StrideForWidth(window_rect.width);
332 const size_t size = window_rect.height * stride;
333
334 // Don't drop the old damage buffer until after we've made sure that the
335 // browser process has dropped it.
336 TransportDIB* new_damage_buffer = CreateTransportDIB(size);
337 DCHECK(new_damage_buffer);
338
339 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_ResizeGuest(
340 routing_id_,
341 instance_id_,
342 new_damage_buffer->id(),
343 window_rect.width,
344 window_rect.height,
345 resize_pending_));
346 resize_pending_ = true;
347
348 if (damage_buffer_) {
349 FreeTransportDIB(damage_buffer_);
350 damage_buffer_ = NULL;
351 }
352 damage_buffer_ = new_damage_buffer;
353 }
354
355 void BrowserPlugin::updateFocus(bool focused) {
356 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_SetFocus(
357 routing_id_,
358 instance_id_,
359 focused));
360 }
361
362 void BrowserPlugin::updateVisibility(bool visible) {
363 }
364
365 bool BrowserPlugin::acceptsInputEvents() {
366 return true;
367 }
368
369 bool BrowserPlugin::handleInputEvent(const WebKit::WebInputEvent& event,
370 WebKit::WebCursorInfo& cursor_info) {
371 if (guest_crashed_ || src_.empty())
372 return false;
373 bool handled = false;
374 WebCursor cursor;
375 IPC::Message* message =
376 new BrowserPluginHostMsg_HandleInputEvent(
377 routing_id_,
378 &handled,
379 &cursor);
380 message->WriteInt(instance_id_);
381 message->WriteData(reinterpret_cast<const char*>(&plugin_rect_),
382 sizeof(gfx::Rect));
383 message->WriteData(reinterpret_cast<const char*>(&event), event.size);
384 BrowserPluginManager::Get()->Send(message);
385 cursor.GetCursorInfo(&cursor_info);
386 return handled;
387 }
388
389 void BrowserPlugin::didReceiveResponse(
390 const WebKit::WebURLResponse& response) {
391 }
392
393 void BrowserPlugin::didReceiveData(const char* data, int data_length) {
394 }
395
396 void BrowserPlugin::didFinishLoading() {
397 }
398
399 void BrowserPlugin::didFailLoading(const WebKit::WebURLError& error) {
400 }
401
402 void BrowserPlugin::didFinishLoadingFrameRequest(const WebKit::WebURL& url,
403 void* notify_data) {
404 }
405
406 void BrowserPlugin::didFailLoadingFrameRequest(
407 const WebKit::WebURL& url,
408 void* notify_data,
409 const WebKit::WebURLError& error) {
410 }
411
412 } // namespace browser_plugin
413 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698