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"
10 #include "base/lazy_instance.h"
11 #include "content/child/npapi/npobject_proxy.h"
12 #include "content/child/npapi/npobject_util.h"
13 #include "content/child/npapi/webplugin_delegate_impl.h"
14 #include "content/child/npapi/webplugin_resource_client.h"
15 #include "content/child/plugin_messages.h"
16 #include "content/plugin/plugin_channel.h"
17 #include "content/plugin/plugin_thread.h"
18 #include "content/public/common/content_client.h"
19 #include "skia/ext/platform_canvas.h"
20 #include "skia/ext/platform_device.h"
21 #include "third_party/WebKit/public/web/WebBindings.h"
22 #include "ui/gfx/canvas.h"
23 #include "url/url_constants.h"
25 #if defined(OS_MACOSX)
26 #include "base/mac/mac_util.h"
27 #include "base/mac/scoped_cftyperef.h"
28 #include "content/plugin/webplugin_accelerated_surface_proxy_mac.h"
32 #include "content/common/plugin_process_messages.h"
33 #include "content/public/common/sandbox_init.h"
36 using blink::WebBindings
;
40 WebPluginProxy::SharedTransportDIB::SharedTransportDIB(TransportDIB
* dib
)
44 WebPluginProxy::SharedTransportDIB::~SharedTransportDIB() {
47 WebPluginProxy::WebPluginProxy(
48 PluginChannel
* channel
,
51 int host_render_view_routing_id
)
54 window_npobject_(NULL
),
55 plugin_element_(NULL
),
57 waiting_for_paint_(false),
59 windowless_buffer_index_(0),
60 host_render_view_routing_id_(host_render_view_routing_id
),
64 WebPluginProxy::~WebPluginProxy() {
65 #if defined(OS_MACOSX)
66 // Destroy the surface early, since it may send messages during cleanup.
67 if (accelerated_surface_
)
68 accelerated_surface_
.reset();
72 WebBindings::releaseObject(plugin_element_
);
74 WebBindings::releaseObject(window_npobject_
);
77 bool WebPluginProxy::Send(IPC::Message
* msg
) {
78 return channel_
->Send(msg
);
81 void WebPluginProxy::SetWindow(gfx::PluginWindowHandle window
) {
82 Send(new PluginHostMsg_SetWindow(route_id_
, window
));
85 void WebPluginProxy::SetAcceptsInputEvents(bool accepts
) {
89 void WebPluginProxy::WillDestroyWindow(gfx::PluginWindowHandle window
) {
91 PluginThread::current()->Send(
92 new PluginProcessHostMsg_PluginWindowDestroyed(
93 window
, ::GetParent(window
)));
100 void WebPluginProxy::SetWindowlessData(
101 HANDLE pump_messages_event
, gfx::NativeViewId dummy_activation_window
) {
102 HANDLE pump_messages_event_for_renderer
= NULL
;
103 BrokerDuplicateHandle(pump_messages_event
, channel_
->peer_pid(),
104 &pump_messages_event_for_renderer
,
105 SYNCHRONIZE
| EVENT_MODIFY_STATE
, 0);
106 DCHECK(pump_messages_event_for_renderer
);
107 Send(new PluginHostMsg_SetWindowlessData(
108 route_id_
, pump_messages_event_for_renderer
, dummy_activation_window
));
112 void WebPluginProxy::CancelResource(unsigned long id
) {
113 Send(new PluginHostMsg_CancelResource(route_id_
, id
));
114 resource_clients_
.erase(id
);
117 void WebPluginProxy::Invalidate() {
119 delegate_
->GetRect().width(),
120 delegate_
->GetRect().height());
121 InvalidateRect(rect
);
124 void WebPluginProxy::InvalidateRect(const gfx::Rect
& rect
) {
125 #if defined(OS_MACOSX)
126 // If this is a Core Animation plugin, all we need to do is inform the
128 if (!windowless_context()) {
129 delegate_
->PluginDidInvalidate();
133 // Some plugins will send invalidates larger than their own rect when
134 // offscreen, so constrain invalidates to the plugin rect.
135 gfx::Rect plugin_rect
= delegate_
->GetRect();
136 plugin_rect
.set_origin(gfx::Point(0, 0));
137 plugin_rect
.Intersect(rect
);
138 const gfx::Rect
invalidate_rect(plugin_rect
);
140 const gfx::Rect
invalidate_rect(rect
);
142 damaged_rect_
.Union(invalidate_rect
);
143 // Ignore NPN_InvalidateRect calls with empty rects. Also don't send an
144 // invalidate if it's outside the clipping region, since if we did it won't
145 // lead to a paint and we'll be stuck waiting forever for a DidPaint response.
147 // TODO(piman): There is a race condition here, because this test assumes
148 // that when the paint actually occurs, the clip rect will not have changed.
149 // This is not true because scrolling (or window resize) could occur and be
150 // handled by the renderer before it receives the InvalidateRect message,
151 // changing the clip rect and then not painting.
152 if (damaged_rect_
.IsEmpty() ||
153 !delegate_
->GetClipRect().Intersects(damaged_rect_
))
156 // Only send a single InvalidateRect message at a time. From DidPaint we
157 // will dispatch an additional InvalidateRect message if necessary.
158 if (!waiting_for_paint_
) {
159 waiting_for_paint_
= true;
160 // Invalidates caused by calls to NPN_InvalidateRect/NPN_InvalidateRgn
161 // need to be painted asynchronously as per the NPAPI spec.
162 base::MessageLoop::current()->PostTask(
164 base::Bind(&WebPluginProxy::OnPaint
,
165 weak_factory_
.GetWeakPtr(),
167 damaged_rect_
= gfx::Rect();
171 NPObject
* WebPluginProxy::GetWindowScriptNPObject() {
172 if (window_npobject_
)
173 return window_npobject_
;
175 int npobject_route_id
= channel_
->GenerateRouteID();
176 bool success
= false;
177 Send(new PluginHostMsg_GetWindowScriptNPObject(
178 route_id_
, npobject_route_id
, &success
));
182 // PluginChannel creates a dummy owner identifier for unknown owners, so
184 NPP owner
= channel_
->GetExistingNPObjectOwner(MSG_ROUTING_NONE
);
186 window_npobject_
= NPObjectProxy::Create(channel_
.get(),
188 host_render_view_routing_id_
,
192 return window_npobject_
;
195 NPObject
* WebPluginProxy::GetPluginElement() {
197 return plugin_element_
;
199 int npobject_route_id
= channel_
->GenerateRouteID();
200 bool success
= false;
201 Send(new PluginHostMsg_GetPluginElement(route_id_
, npobject_route_id
,
206 // PluginChannel creates a dummy owner identifier for unknown owners, so
208 NPP owner
= channel_
->GetExistingNPObjectOwner(MSG_ROUTING_NONE
);
210 plugin_element_
= NPObjectProxy::Create(channel_
.get(),
212 host_render_view_routing_id_
,
216 return plugin_element_
;
219 bool WebPluginProxy::FindProxyForUrl(const GURL
& url
, std::string
* proxy_list
) {
221 Send(new PluginHostMsg_ResolveProxy(route_id_
, url
, &result
, proxy_list
));
225 void WebPluginProxy::SetCookie(const GURL
& url
,
226 const GURL
& first_party_for_cookies
,
227 const std::string
& cookie
) {
228 Send(new PluginHostMsg_SetCookie(route_id_
, url
,
229 first_party_for_cookies
, cookie
));
232 std::string
WebPluginProxy::GetCookies(const GURL
& url
,
233 const GURL
& first_party_for_cookies
) {
235 Send(new PluginHostMsg_GetCookies(route_id_
, url
,
236 first_party_for_cookies
, &cookies
));
241 WebPluginResourceClient
* WebPluginProxy::GetResourceClient(int id
) {
242 ResourceClientMap::iterator iterator
= resource_clients_
.find(id
);
243 // The IPC messages which deal with streams are now asynchronous. It is
244 // now possible to receive stream messages from the renderer for streams
245 // which may have been cancelled by the plugin.
246 if (iterator
== resource_clients_
.end()) {
250 return iterator
->second
;
253 int WebPluginProxy::GetRendererId() {
255 return channel_
->renderer_id();
259 void WebPluginProxy::DidPaint() {
260 // If we have an accumulated damaged rect, then check to see if we need to
261 // send out another InvalidateRect message.
262 waiting_for_paint_
= false;
263 if (!damaged_rect_
.IsEmpty())
264 InvalidateRect(damaged_rect_
);
267 void WebPluginProxy::OnResourceCreated(int resource_id
,
268 WebPluginResourceClient
* client
) {
269 DCHECK(resource_clients_
.find(resource_id
) == resource_clients_
.end());
270 resource_clients_
[resource_id
] = client
;
273 void WebPluginProxy::HandleURLRequest(const char* url
,
280 bool notify_redirects
) {
281 if (!target
&& base::EqualsCaseInsensitiveASCII(method
, "GET")) {
282 // Please refer to https://bugzilla.mozilla.org/show_bug.cgi?id=366082
283 // for more details on this.
284 if (delegate_
->GetQuirks() &
285 WebPluginDelegateImpl::PLUGIN_QUIRK_BLOCK_NONSTANDARD_GETURL_REQUESTS
) {
286 GURL
request_url(url
);
287 if (!request_url
.SchemeIs(url::kHttpScheme
) &&
288 !request_url
.SchemeIs(url::kHttpsScheme
) &&
289 !request_url
.SchemeIs(url::kFtpScheme
)) {
295 PluginHostMsg_URLRequest_Params params
;
297 params
.method
= method
;
299 params
.target
= std::string(target
);
302 params
.buffer
.resize(len
);
303 memcpy(¶ms
.buffer
.front(), buf
, len
);
306 params
.notify_id
= notify_id
;
307 params
.popups_allowed
= popups_allowed
;
308 params
.notify_redirects
= notify_redirects
;
310 Send(new PluginHostMsg_URLRequest(route_id_
, params
));
313 void WebPluginProxy::Paint(const gfx::Rect
& rect
) {
314 #if defined(OS_MACOSX)
315 if (!windowless_context())
318 if (!windowless_canvas() || !windowless_canvas()->getDevice())
322 // Clear the damaged area so that if the plugin doesn't paint there we won't
323 // end up with the old values.
324 gfx::Rect offset_rect
= rect
;
325 offset_rect
.Offset(delegate_
->GetRect().OffsetFromOrigin());
326 #if defined(OS_MACOSX)
327 CGContextSaveGState(windowless_context());
328 // It is possible for windowless_contexts_ to change during plugin painting
329 // (since the plugin can make a synchronous call during paint event handling),
330 // in which case we don't want to try to restore later. Not an owning ref
331 // since owning the ref without owning the shared backing memory doesn't make
332 // sense, so this should only be used for pointer comparisons.
333 CGContextRef saved_context_weak
= windowless_context();
334 // We also save the buffer index for the comparison because if we flip buffers
335 // but haven't reallocated them then we do need to restore the context because
336 // it is going to continue to be used.
337 int saved_index
= windowless_buffer_index_
;
339 CGContextClipToRect(windowless_context(), rect
.ToCGRect());
340 // TODO(caryclark): This is a temporary workaround to allow the Darwin / Skia
341 // port to share code with the Darwin / CG port. All ports will eventually use
342 // the common code below.
343 delegate_
->CGPaint(windowless_context(), rect
);
344 if (windowless_contexts_
[saved_index
].get() == saved_context_weak
)
345 CGContextRestoreGState(windowless_contexts_
[saved_index
]);
347 // See above comment about windowless_context_ changing.
348 // http::/crbug.com/139462
349 skia::RefPtr
<skia::PlatformCanvas
> saved_canvas
= windowless_canvas();
351 saved_canvas
->save();
353 // The given clip rect is relative to the plugin coordinate system.
354 SkRect sk_rect
= { SkIntToScalar(rect
.x()),
355 SkIntToScalar(rect
.y()),
356 SkIntToScalar(rect
.right()),
357 SkIntToScalar(rect
.bottom()) };
358 saved_canvas
->clipRect(sk_rect
);
360 // Fill a transparent value so that if the plugin supports transparency that
362 saved_canvas
->drawColor(SkColorSetARGB(0, 0, 0, 0), SkXfermode::kSrc_Mode
);
364 // Bring the windowless canvas into the window coordinate system, which is
365 // how the plugin expects to draw (since the windowless API was originally
366 // designed just for scribbling over the web page).
367 saved_canvas
->translate(SkIntToScalar(-delegate_
->GetRect().x()),
368 SkIntToScalar(-delegate_
->GetRect().y()));
370 // Before we send the invalidate, paint so that renderer uses the updated
372 delegate_
->Paint(saved_canvas
.get(), offset_rect
);
374 saved_canvas
->restore();
378 void WebPluginProxy::UpdateGeometry(
379 const gfx::Rect
& window_rect
,
380 const gfx::Rect
& clip_rect
,
381 const TransportDIB::Handle
& windowless_buffer0
,
382 const TransportDIB::Handle
& windowless_buffer1
,
383 int windowless_buffer_index
) {
384 gfx::Rect old
= delegate_
->GetRect();
385 gfx::Rect old_clip_rect
= delegate_
->GetClipRect();
387 // Update the buffers before doing anything that could call into plugin code,
388 // so that we don't process buffer changes out of order if plugins make
389 // synchronous calls that lead to nested UpdateGeometry calls.
390 if (TransportDIB::is_valid_handle(windowless_buffer0
)) {
391 // The plugin's rect changed, so now we have new buffers to draw into.
392 SetWindowlessBuffers(windowless_buffer0
,
397 DCHECK(0 <= windowless_buffer_index
&& windowless_buffer_index
<= 1);
398 windowless_buffer_index_
= windowless_buffer_index
;
400 #if defined(OS_MACOSX)
401 delegate_
->UpdateGeometryAndContext(
402 window_rect
, clip_rect
, windowless_context());
404 delegate_
->UpdateGeometry(window_rect
, clip_rect
);
407 // Send over any pending invalidates which occured when the plugin was
409 if (delegate_
->IsWindowless() && !clip_rect
.IsEmpty() &&
410 !damaged_rect_
.IsEmpty()) {
411 InvalidateRect(damaged_rect_
);
417 void WebPluginProxy::CreateCanvasFromHandle(
418 const TransportDIB::Handle
& dib_handle
,
419 const gfx::Rect
& window_rect
,
420 skia::RefPtr
<skia::PlatformCanvas
>* canvas
) {
421 *canvas
= skia::AdoptRef(
422 skia::CreatePlatformCanvas(window_rect
.width(),
423 window_rect
.height(),
426 skia::RETURN_NULL_ON_FAILURE
));
427 // The canvas does not own the section so we need to close it now.
428 CloseHandle(dib_handle
);
431 void WebPluginProxy::SetWindowlessBuffers(
432 const TransportDIB::Handle
& windowless_buffer0
,
433 const TransportDIB::Handle
& windowless_buffer1
,
434 const gfx::Rect
& window_rect
) {
435 CreateCanvasFromHandle(windowless_buffer0
,
437 &windowless_canvases_
[0]);
438 if (!windowless_canvases_
[0]) {
439 windowless_canvases_
[1].clear();
442 CreateCanvasFromHandle(windowless_buffer1
,
444 &windowless_canvases_
[1]);
445 if (!windowless_canvases_
[1]) {
446 windowless_canvases_
[0].clear();
451 #elif defined(OS_MACOSX)
453 void WebPluginProxy::CreateDIBAndCGContextFromHandle(
454 const TransportDIB::Handle
& dib_handle
,
455 const gfx::Rect
& window_rect
,
456 scoped_ptr
<TransportDIB
>* dib_out
,
457 base::ScopedCFTypeRef
<CGContextRef
>* cg_context_out
) {
458 // Convert the shared memory handle to a handle that works in our process,
459 // and then use that to create a CGContextRef.
460 TransportDIB
* dib
= TransportDIB::Map(dib_handle
);
461 CGContextRef cg_context
= NULL
;
463 cg_context
= CGBitmapContextCreate(
466 window_rect
.height(),
468 4 * window_rect
.width(),
469 base::mac::GetSystemColorSpace(),
470 kCGImageAlphaPremultipliedFirst
| kCGBitmapByteOrder32Host
);
471 CGContextTranslateCTM(cg_context
, 0, window_rect
.height());
472 CGContextScaleCTM(cg_context
, 1, -1);
475 cg_context_out
->reset(cg_context
);
478 void WebPluginProxy::SetWindowlessBuffers(
479 const TransportDIB::Handle
& windowless_buffer0
,
480 const TransportDIB::Handle
& windowless_buffer1
,
481 const gfx::Rect
& window_rect
) {
482 CreateDIBAndCGContextFromHandle(windowless_buffer0
,
484 &windowless_dibs_
[0],
485 &windowless_contexts_
[0]);
486 CreateDIBAndCGContextFromHandle(windowless_buffer1
,
488 &windowless_dibs_
[1],
489 &windowless_contexts_
[1]);
494 void WebPluginProxy::SetWindowlessBuffers(
495 const TransportDIB::Handle
& windowless_buffer0
,
496 const TransportDIB::Handle
& windowless_buffer1
,
497 const gfx::Rect
& window_rect
) {
503 void WebPluginProxy::CancelDocumentLoad() {
504 Send(new PluginHostMsg_CancelDocumentLoad(route_id_
));
507 void WebPluginProxy::InitiateHTTPRangeRequest(
508 const char* url
, const char* range_info
, int range_request_id
) {
509 Send(new PluginHostMsg_InitiateHTTPRangeRequest(
510 route_id_
, url
, range_info
, range_request_id
));
513 void WebPluginProxy::DidStartLoading() {
514 Send(new PluginHostMsg_DidStartLoading(route_id_
));
517 void WebPluginProxy::DidStopLoading() {
518 Send(new PluginHostMsg_DidStopLoading(route_id_
));
521 void WebPluginProxy::SetDeferResourceLoading(unsigned long resource_id
,
523 Send(new PluginHostMsg_DeferResourceLoading(route_id_
, resource_id
, defer
));
526 #if defined(OS_MACOSX)
527 void WebPluginProxy::FocusChanged(bool focused
) {
528 IPC::Message
* msg
= new PluginHostMsg_FocusChanged(route_id_
, focused
);
532 void WebPluginProxy::StartIme() {
533 IPC::Message
* msg
= new PluginHostMsg_StartIme(route_id_
);
534 // This message can be sent during event-handling, and needs to be delivered
535 // within that context.
536 msg
->set_unblock(true);
540 WebPluginAcceleratedSurface
* WebPluginProxy::GetAcceleratedSurface(
541 gfx::GpuPreference gpu_preference
) {
542 if (!accelerated_surface_
)
543 accelerated_surface_
.reset(
544 WebPluginAcceleratedSurfaceProxy::Create(this, gpu_preference
));
545 return accelerated_surface_
.get();
548 void WebPluginProxy::AcceleratedPluginEnabledRendering() {
549 Send(new PluginHostMsg_AcceleratedPluginEnabledRendering(route_id_
));
552 void WebPluginProxy::AcceleratedPluginAllocatedIOSurface(int32 width
,
555 Send(new PluginHostMsg_AcceleratedPluginAllocatedIOSurface(
556 route_id_
, width
, height
, surface_id
));
559 void WebPluginProxy::AcceleratedPluginSwappedIOSurface() {
560 Send(new PluginHostMsg_AcceleratedPluginSwappedIOSurface(
565 void WebPluginProxy::OnPaint(const gfx::Rect
& damaged_rect
) {
566 GetContentClient()->SetActiveURL(page_url_
);
569 Send(new PluginHostMsg_InvalidateRect(route_id_
, damaged_rect
));
572 bool WebPluginProxy::IsOffTheRecord() {
573 return channel_
->incognito();
576 void WebPluginProxy::ResourceClientDeleted(
577 WebPluginResourceClient
* resource_client
) {
578 // resource_client->ResourceId() is 0 at this point, so can't use it as an
579 // index into the map.
580 ResourceClientMap::iterator index
= resource_clients_
.begin();
581 while (index
!= resource_clients_
.end()) {
582 WebPluginResourceClient
* client
= (*index
).second
;
583 if (client
== resource_client
) {
584 resource_clients_
.erase(index
);
592 void WebPluginProxy::URLRedirectResponse(bool allow
, int resource_id
) {
593 Send(new PluginHostMsg_URLRedirectResponse(route_id_
, allow
, resource_id
));
596 bool WebPluginProxy::CheckIfRunInsecureContent(const GURL
& url
) {
598 Send(new PluginHostMsg_CheckIfRunInsecureContent(
599 route_id_
, url
, &result
));
603 #if defined(OS_WIN) && !defined(USE_AURA)
604 void WebPluginProxy::UpdateIMEStatus() {
605 // Retrieve the IME status from a plugin and send it to a renderer process
606 // when the plugin has updated it.
608 gfx::Rect caret_rect
;
609 if (!delegate_
->GetIMEStatus(&input_type
, &caret_rect
))
612 Send(new PluginHostMsg_NotifyIMEStatus(route_id_
, input_type
, caret_rect
));
616 } // namespace content