Add an extension override bubble and warning box for proxy extensions. (2nd attempt...
[chromium-blink-merge.git] / content / plugin / webplugin_proxy.cc
blobc6a627d58bdfbe153fa6fd6261c6105bb0699ece
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.
5 #include "content/plugin/webplugin_proxy.h"
7 #include "build/build_config.h"
9 #include "base/bind.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/scoped_handle.h"
12 #include "base/memory/shared_memory.h"
13 #include "build/build_config.h"
14 #include "content/child/npapi/npobject_proxy.h"
15 #include "content/child/npapi/npobject_util.h"
16 #include "content/child/npapi/webplugin_delegate_impl.h"
17 #include "content/child/npapi/webplugin_resource_client.h"
18 #include "content/child/plugin_messages.h"
19 #include "content/plugin/plugin_channel.h"
20 #include "content/plugin/plugin_thread.h"
21 #include "content/public/common/content_client.h"
22 #include "skia/ext/platform_canvas.h"
23 #include "skia/ext/platform_device.h"
24 #include "third_party/WebKit/public/web/WebBindings.h"
25 #include "ui/gfx/blit.h"
26 #include "ui/gfx/canvas.h"
27 #include "url/url_constants.h"
29 #if defined(OS_MACOSX)
30 #include "base/mac/mac_util.h"
31 #include "base/mac/scoped_cftyperef.h"
32 #include "content/plugin/webplugin_accelerated_surface_proxy_mac.h"
33 #endif
35 #if defined(OS_WIN)
36 #include "content/common/plugin_process_messages.h"
37 #include "content/public/common/sandbox_init.h"
38 #endif
40 using blink::WebBindings;
42 namespace content {
44 WebPluginProxy::SharedTransportDIB::SharedTransportDIB(TransportDIB* dib)
45 : dib_(dib) {
48 WebPluginProxy::SharedTransportDIB::~SharedTransportDIB() {
51 WebPluginProxy::WebPluginProxy(
52 PluginChannel* channel,
53 int route_id,
54 const GURL& page_url,
55 int host_render_view_routing_id)
56 : channel_(channel),
57 route_id_(route_id),
58 window_npobject_(NULL),
59 plugin_element_(NULL),
60 delegate_(NULL),
61 waiting_for_paint_(false),
62 page_url_(page_url),
63 windowless_buffer_index_(0),
64 host_render_view_routing_id_(host_render_view_routing_id),
65 weak_factory_(this) {
68 WebPluginProxy::~WebPluginProxy() {
69 #if defined(OS_MACOSX)
70 // Destroy the surface early, since it may send messages during cleanup.
71 if (accelerated_surface_)
72 accelerated_surface_.reset();
73 #endif
75 if (plugin_element_)
76 WebBindings::releaseObject(plugin_element_);
77 if (window_npobject_)
78 WebBindings::releaseObject(window_npobject_);
81 bool WebPluginProxy::Send(IPC::Message* msg) {
82 return channel_->Send(msg);
85 void WebPluginProxy::SetWindow(gfx::PluginWindowHandle window) {
86 Send(new PluginHostMsg_SetWindow(route_id_, window));
89 void WebPluginProxy::SetAcceptsInputEvents(bool accepts) {
90 NOTREACHED();
93 void WebPluginProxy::WillDestroyWindow(gfx::PluginWindowHandle window) {
94 #if defined(OS_WIN)
95 PluginThread::current()->Send(
96 new PluginProcessHostMsg_PluginWindowDestroyed(
97 window, ::GetParent(window)));
98 #else
99 NOTIMPLEMENTED();
100 #endif
103 #if defined(OS_WIN)
104 void WebPluginProxy::SetWindowlessData(
105 HANDLE pump_messages_event, gfx::NativeViewId dummy_activation_window) {
106 HANDLE pump_messages_event_for_renderer = NULL;
107 BrokerDuplicateHandle(pump_messages_event, channel_->peer_pid(),
108 &pump_messages_event_for_renderer,
109 SYNCHRONIZE | EVENT_MODIFY_STATE, 0);
110 DCHECK(pump_messages_event_for_renderer);
111 Send(new PluginHostMsg_SetWindowlessData(
112 route_id_, pump_messages_event_for_renderer, dummy_activation_window));
114 #endif
116 void WebPluginProxy::CancelResource(unsigned long id) {
117 Send(new PluginHostMsg_CancelResource(route_id_, id));
118 resource_clients_.erase(id);
121 void WebPluginProxy::Invalidate() {
122 gfx::Rect rect(0, 0,
123 delegate_->GetRect().width(),
124 delegate_->GetRect().height());
125 InvalidateRect(rect);
128 void WebPluginProxy::InvalidateRect(const gfx::Rect& rect) {
129 #if defined(OS_MACOSX)
130 // If this is a Core Animation plugin, all we need to do is inform the
131 // delegate.
132 if (!windowless_context()) {
133 delegate_->PluginDidInvalidate();
134 return;
137 // Some plugins will send invalidates larger than their own rect when
138 // offscreen, so constrain invalidates to the plugin rect.
139 gfx::Rect plugin_rect = delegate_->GetRect();
140 plugin_rect.set_origin(gfx::Point(0, 0));
141 plugin_rect.Intersect(rect);
142 const gfx::Rect invalidate_rect(plugin_rect);
143 #else
144 const gfx::Rect invalidate_rect(rect);
145 #endif
146 damaged_rect_.Union(invalidate_rect);
147 // Ignore NPN_InvalidateRect calls with empty rects. Also don't send an
148 // invalidate if it's outside the clipping region, since if we did it won't
149 // lead to a paint and we'll be stuck waiting forever for a DidPaint response.
151 // TODO(piman): There is a race condition here, because this test assumes
152 // that when the paint actually occurs, the clip rect will not have changed.
153 // This is not true because scrolling (or window resize) could occur and be
154 // handled by the renderer before it receives the InvalidateRect message,
155 // changing the clip rect and then not painting.
156 if (damaged_rect_.IsEmpty() ||
157 !delegate_->GetClipRect().Intersects(damaged_rect_))
158 return;
160 // Only send a single InvalidateRect message at a time. From DidPaint we
161 // will dispatch an additional InvalidateRect message if necessary.
162 if (!waiting_for_paint_) {
163 waiting_for_paint_ = true;
164 // Invalidates caused by calls to NPN_InvalidateRect/NPN_InvalidateRgn
165 // need to be painted asynchronously as per the NPAPI spec.
166 base::MessageLoop::current()->PostTask(
167 FROM_HERE,
168 base::Bind(&WebPluginProxy::OnPaint,
169 weak_factory_.GetWeakPtr(),
170 damaged_rect_));
171 damaged_rect_ = gfx::Rect();
175 NPObject* WebPluginProxy::GetWindowScriptNPObject() {
176 if (window_npobject_)
177 return window_npobject_;
179 int npobject_route_id = channel_->GenerateRouteID();
180 bool success = false;
181 Send(new PluginHostMsg_GetWindowScriptNPObject(
182 route_id_, npobject_route_id, &success));
183 if (!success)
184 return NULL;
186 // PluginChannel creates a dummy owner identifier for unknown owners, so
187 // use that.
188 NPP owner = channel_->GetExistingNPObjectOwner(MSG_ROUTING_NONE);
190 window_npobject_ = NPObjectProxy::Create(channel_.get(),
191 npobject_route_id,
192 host_render_view_routing_id_,
193 page_url_,
194 owner);
196 return window_npobject_;
199 NPObject* WebPluginProxy::GetPluginElement() {
200 if (plugin_element_)
201 return plugin_element_;
203 int npobject_route_id = channel_->GenerateRouteID();
204 bool success = false;
205 Send(new PluginHostMsg_GetPluginElement(route_id_, npobject_route_id,
206 &success));
207 if (!success)
208 return NULL;
210 // PluginChannel creates a dummy owner identifier for unknown owners, so
211 // use that.
212 NPP owner = channel_->GetExistingNPObjectOwner(MSG_ROUTING_NONE);
214 plugin_element_ = NPObjectProxy::Create(channel_.get(),
215 npobject_route_id,
216 host_render_view_routing_id_,
217 page_url_,
218 owner);
220 return plugin_element_;
223 bool WebPluginProxy::FindProxyForUrl(const GURL& url, std::string* proxy_list) {
224 bool result = false;
225 Send(new PluginHostMsg_ResolveProxy(route_id_, url, &result, proxy_list));
226 return result;
229 void WebPluginProxy::SetCookie(const GURL& url,
230 const GURL& first_party_for_cookies,
231 const std::string& cookie) {
232 Send(new PluginHostMsg_SetCookie(route_id_, url,
233 first_party_for_cookies, cookie));
236 std::string WebPluginProxy::GetCookies(const GURL& url,
237 const GURL& first_party_for_cookies) {
238 std::string cookies;
239 Send(new PluginHostMsg_GetCookies(route_id_, url,
240 first_party_for_cookies, &cookies));
242 return cookies;
245 WebPluginResourceClient* WebPluginProxy::GetResourceClient(int id) {
246 ResourceClientMap::iterator iterator = resource_clients_.find(id);
247 // The IPC messages which deal with streams are now asynchronous. It is
248 // now possible to receive stream messages from the renderer for streams
249 // which may have been cancelled by the plugin.
250 if (iterator == resource_clients_.end()) {
251 return NULL;
254 return iterator->second;
257 int WebPluginProxy::GetRendererId() {
258 if (channel_.get())
259 return channel_->renderer_id();
260 return -1;
263 void WebPluginProxy::DidPaint() {
264 // If we have an accumulated damaged rect, then check to see if we need to
265 // send out another InvalidateRect message.
266 waiting_for_paint_ = false;
267 if (!damaged_rect_.IsEmpty())
268 InvalidateRect(damaged_rect_);
271 void WebPluginProxy::OnResourceCreated(int resource_id,
272 WebPluginResourceClient* client) {
273 DCHECK(resource_clients_.find(resource_id) == resource_clients_.end());
274 resource_clients_[resource_id] = client;
277 void WebPluginProxy::HandleURLRequest(const char* url,
278 const char* method,
279 const char* target,
280 const char* buf,
281 unsigned int len,
282 int notify_id,
283 bool popups_allowed,
284 bool notify_redirects) {
285 if (!target && (0 == base::strcasecmp(method, "GET"))) {
286 // Please refer to https://bugzilla.mozilla.org/show_bug.cgi?id=366082
287 // for more details on this.
288 if (delegate_->GetQuirks() &
289 WebPluginDelegateImpl::PLUGIN_QUIRK_BLOCK_NONSTANDARD_GETURL_REQUESTS) {
290 GURL request_url(url);
291 if (!request_url.SchemeIs(url::kHttpScheme) &&
292 !request_url.SchemeIs(url::kHttpsScheme) &&
293 !request_url.SchemeIs(url::kFtpScheme)) {
294 return;
299 PluginHostMsg_URLRequest_Params params;
300 params.url = url;
301 params.method = method;
302 if (target)
303 params.target = std::string(target);
305 if (len) {
306 params.buffer.resize(len);
307 memcpy(&params.buffer.front(), buf, len);
310 params.notify_id = notify_id;
311 params.popups_allowed = popups_allowed;
312 params.notify_redirects = notify_redirects;
314 Send(new PluginHostMsg_URLRequest(route_id_, params));
317 void WebPluginProxy::Paint(const gfx::Rect& rect) {
318 #if defined(OS_MACOSX)
319 if (!windowless_context())
320 return;
321 #else
322 if (!windowless_canvas() || !windowless_canvas()->getDevice())
323 return;
324 #endif
326 // Clear the damaged area so that if the plugin doesn't paint there we won't
327 // end up with the old values.
328 gfx::Rect offset_rect = rect;
329 offset_rect.Offset(delegate_->GetRect().OffsetFromOrigin());
330 #if defined(OS_MACOSX)
331 CGContextSaveGState(windowless_context());
332 // It is possible for windowless_contexts_ to change during plugin painting
333 // (since the plugin can make a synchronous call during paint event handling),
334 // in which case we don't want to try to restore later. Not an owning ref
335 // since owning the ref without owning the shared backing memory doesn't make
336 // sense, so this should only be used for pointer comparisons.
337 CGContextRef saved_context_weak = windowless_context();
338 // We also save the buffer index for the comparison because if we flip buffers
339 // but haven't reallocated them then we do need to restore the context because
340 // it is going to continue to be used.
341 int saved_index = windowless_buffer_index_;
343 CGContextClipToRect(windowless_context(), rect.ToCGRect());
344 // TODO(caryclark): This is a temporary workaround to allow the Darwin / Skia
345 // port to share code with the Darwin / CG port. All ports will eventually use
346 // the common code below.
347 delegate_->CGPaint(windowless_context(), rect);
348 if (windowless_contexts_[saved_index].get() == saved_context_weak)
349 CGContextRestoreGState(windowless_contexts_[saved_index]);
350 #else
351 // See above comment about windowless_context_ changing.
352 // http::/crbug.com/139462
353 skia::RefPtr<skia::PlatformCanvas> saved_canvas = windowless_canvas();
355 saved_canvas->save();
357 // The given clip rect is relative to the plugin coordinate system.
358 SkRect sk_rect = { SkIntToScalar(rect.x()),
359 SkIntToScalar(rect.y()),
360 SkIntToScalar(rect.right()),
361 SkIntToScalar(rect.bottom()) };
362 saved_canvas->clipRect(sk_rect);
364 // Fill a transparent value so that if the plugin supports transparency that
365 // will work.
366 saved_canvas->drawColor(SkColorSetARGB(0, 0, 0, 0), SkXfermode::kSrc_Mode);
368 // Bring the windowless canvas into the window coordinate system, which is
369 // how the plugin expects to draw (since the windowless API was originally
370 // designed just for scribbling over the web page).
371 saved_canvas->translate(SkIntToScalar(-delegate_->GetRect().x()),
372 SkIntToScalar(-delegate_->GetRect().y()));
374 // Before we send the invalidate, paint so that renderer uses the updated
375 // bitmap.
376 delegate_->Paint(saved_canvas.get(), offset_rect);
378 saved_canvas->restore();
379 #endif
382 void WebPluginProxy::UpdateGeometry(
383 const gfx::Rect& window_rect,
384 const gfx::Rect& clip_rect,
385 const TransportDIB::Handle& windowless_buffer0,
386 const TransportDIB::Handle& windowless_buffer1,
387 int windowless_buffer_index) {
388 gfx::Rect old = delegate_->GetRect();
389 gfx::Rect old_clip_rect = delegate_->GetClipRect();
391 // Update the buffers before doing anything that could call into plugin code,
392 // so that we don't process buffer changes out of order if plugins make
393 // synchronous calls that lead to nested UpdateGeometry calls.
394 if (TransportDIB::is_valid_handle(windowless_buffer0)) {
395 // The plugin's rect changed, so now we have new buffers to draw into.
396 SetWindowlessBuffers(windowless_buffer0,
397 windowless_buffer1,
398 window_rect);
401 DCHECK(0 <= windowless_buffer_index && windowless_buffer_index <= 1);
402 windowless_buffer_index_ = windowless_buffer_index;
404 #if defined(OS_MACOSX)
405 delegate_->UpdateGeometryAndContext(
406 window_rect, clip_rect, windowless_context());
407 #else
408 delegate_->UpdateGeometry(window_rect, clip_rect);
409 #endif
411 // Send over any pending invalidates which occured when the plugin was
412 // off screen.
413 if (delegate_->IsWindowless() && !clip_rect.IsEmpty() &&
414 !damaged_rect_.IsEmpty()) {
415 InvalidateRect(damaged_rect_);
419 #if defined(OS_WIN)
421 void WebPluginProxy::CreateCanvasFromHandle(
422 const TransportDIB::Handle& dib_handle,
423 const gfx::Rect& window_rect,
424 skia::RefPtr<skia::PlatformCanvas>* canvas) {
425 *canvas = skia::AdoptRef(
426 skia::CreatePlatformCanvas(window_rect.width(),
427 window_rect.height(),
428 true,
429 dib_handle,
430 skia::RETURN_NULL_ON_FAILURE));
431 // The canvas does not own the section so we need to close it now.
432 CloseHandle(dib_handle);
435 void WebPluginProxy::SetWindowlessBuffers(
436 const TransportDIB::Handle& windowless_buffer0,
437 const TransportDIB::Handle& windowless_buffer1,
438 const gfx::Rect& window_rect) {
439 CreateCanvasFromHandle(windowless_buffer0,
440 window_rect,
441 &windowless_canvases_[0]);
442 if (!windowless_canvases_[0]) {
443 windowless_canvases_[1].clear();
444 return;
446 CreateCanvasFromHandle(windowless_buffer1,
447 window_rect,
448 &windowless_canvases_[1]);
449 if (!windowless_canvases_[1]) {
450 windowless_canvases_[0].clear();
451 return;
455 #elif defined(OS_MACOSX)
457 void WebPluginProxy::CreateDIBAndCGContextFromHandle(
458 const TransportDIB::Handle& dib_handle,
459 const gfx::Rect& window_rect,
460 scoped_ptr<TransportDIB>* dib_out,
461 base::ScopedCFTypeRef<CGContextRef>* cg_context_out) {
462 // Convert the shared memory handle to a handle that works in our process,
463 // and then use that to create a CGContextRef.
464 TransportDIB* dib = TransportDIB::Map(dib_handle);
465 CGContextRef cg_context = NULL;
466 if (dib) {
467 cg_context = CGBitmapContextCreate(
468 dib->memory(),
469 window_rect.width(),
470 window_rect.height(),
472 4 * window_rect.width(),
473 base::mac::GetSystemColorSpace(),
474 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
475 CGContextTranslateCTM(cg_context, 0, window_rect.height());
476 CGContextScaleCTM(cg_context, 1, -1);
478 dib_out->reset(dib);
479 cg_context_out->reset(cg_context);
482 void WebPluginProxy::SetWindowlessBuffers(
483 const TransportDIB::Handle& windowless_buffer0,
484 const TransportDIB::Handle& windowless_buffer1,
485 const gfx::Rect& window_rect) {
486 CreateDIBAndCGContextFromHandle(windowless_buffer0,
487 window_rect,
488 &windowless_dibs_[0],
489 &windowless_contexts_[0]);
490 CreateDIBAndCGContextFromHandle(windowless_buffer1,
491 window_rect,
492 &windowless_dibs_[1],
493 &windowless_contexts_[1]);
496 #else
498 void WebPluginProxy::SetWindowlessBuffers(
499 const TransportDIB::Handle& windowless_buffer0,
500 const TransportDIB::Handle& windowless_buffer1,
501 const gfx::Rect& window_rect) {
502 NOTIMPLEMENTED();
505 #endif
507 void WebPluginProxy::CancelDocumentLoad() {
508 Send(new PluginHostMsg_CancelDocumentLoad(route_id_));
511 void WebPluginProxy::InitiateHTTPRangeRequest(
512 const char* url, const char* range_info, int range_request_id) {
513 Send(new PluginHostMsg_InitiateHTTPRangeRequest(
514 route_id_, url, range_info, range_request_id));
517 void WebPluginProxy::DidStartLoading() {
518 Send(new PluginHostMsg_DidStartLoading(route_id_));
521 void WebPluginProxy::DidStopLoading() {
522 Send(new PluginHostMsg_DidStopLoading(route_id_));
525 void WebPluginProxy::SetDeferResourceLoading(unsigned long resource_id,
526 bool defer) {
527 Send(new PluginHostMsg_DeferResourceLoading(route_id_, resource_id, defer));
530 #if defined(OS_MACOSX)
531 void WebPluginProxy::FocusChanged(bool focused) {
532 IPC::Message* msg = new PluginHostMsg_FocusChanged(route_id_, focused);
533 Send(msg);
536 void WebPluginProxy::StartIme() {
537 IPC::Message* msg = new PluginHostMsg_StartIme(route_id_);
538 // This message can be sent during event-handling, and needs to be delivered
539 // within that context.
540 msg->set_unblock(true);
541 Send(msg);
544 WebPluginAcceleratedSurface* WebPluginProxy::GetAcceleratedSurface(
545 gfx::GpuPreference gpu_preference) {
546 if (!accelerated_surface_)
547 accelerated_surface_.reset(
548 WebPluginAcceleratedSurfaceProxy::Create(this, gpu_preference));
549 return accelerated_surface_.get();
552 void WebPluginProxy::AcceleratedPluginEnabledRendering() {
553 Send(new PluginHostMsg_AcceleratedPluginEnabledRendering(route_id_));
556 void WebPluginProxy::AcceleratedPluginAllocatedIOSurface(int32 width,
557 int32 height,
558 uint32 surface_id) {
559 Send(new PluginHostMsg_AcceleratedPluginAllocatedIOSurface(
560 route_id_, width, height, surface_id));
563 void WebPluginProxy::AcceleratedPluginSwappedIOSurface() {
564 Send(new PluginHostMsg_AcceleratedPluginSwappedIOSurface(
565 route_id_));
567 #endif
569 void WebPluginProxy::OnPaint(const gfx::Rect& damaged_rect) {
570 GetContentClient()->SetActiveURL(page_url_);
572 Paint(damaged_rect);
573 Send(new PluginHostMsg_InvalidateRect(route_id_, damaged_rect));
576 bool WebPluginProxy::IsOffTheRecord() {
577 return channel_->incognito();
580 void WebPluginProxy::ResourceClientDeleted(
581 WebPluginResourceClient* resource_client) {
582 // resource_client->ResourceId() is 0 at this point, so can't use it as an
583 // index into the map.
584 ResourceClientMap::iterator index = resource_clients_.begin();
585 while (index != resource_clients_.end()) {
586 WebPluginResourceClient* client = (*index).second;
587 if (client == resource_client) {
588 resource_clients_.erase(index);
589 return;
590 } else {
591 index++;
596 void WebPluginProxy::URLRedirectResponse(bool allow, int resource_id) {
597 Send(new PluginHostMsg_URLRedirectResponse(route_id_, allow, resource_id));
600 bool WebPluginProxy::CheckIfRunInsecureContent(const GURL& url) {
601 bool result = true;
602 Send(new PluginHostMsg_CheckIfRunInsecureContent(
603 route_id_, url, &result));
604 return result;
607 #if defined(OS_WIN) && !defined(USE_AURA)
608 void WebPluginProxy::UpdateIMEStatus() {
609 // Retrieve the IME status from a plug-in and send it to a renderer process
610 // when the plug-in has updated it.
611 int input_type;
612 gfx::Rect caret_rect;
613 if (!delegate_->GetIMEStatus(&input_type, &caret_rect))
614 return;
616 Send(new PluginHostMsg_NotifyIMEStatus(route_id_, input_type, caret_rect));
618 #endif
620 } // namespace content