[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / plugin / webplugin_proxy.cc
blobdbfb39e7de59e229d73aa6637e9365781373dc5f
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 "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"
29 #endif
31 #if defined(OS_WIN)
32 #include "content/common/plugin_process_messages.h"
33 #include "content/public/common/sandbox_init.h"
34 #endif
36 using blink::WebBindings;
38 namespace content {
40 WebPluginProxy::SharedTransportDIB::SharedTransportDIB(TransportDIB* dib)
41 : dib_(dib) {
44 WebPluginProxy::SharedTransportDIB::~SharedTransportDIB() {
47 WebPluginProxy::WebPluginProxy(
48 PluginChannel* channel,
49 int route_id,
50 const GURL& page_url,
51 int host_render_view_routing_id)
52 : channel_(channel),
53 route_id_(route_id),
54 window_npobject_(NULL),
55 plugin_element_(NULL),
56 delegate_(NULL),
57 waiting_for_paint_(false),
58 page_url_(page_url),
59 windowless_buffer_index_(0),
60 host_render_view_routing_id_(host_render_view_routing_id),
61 weak_factory_(this) {
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();
69 #endif
71 if (plugin_element_)
72 WebBindings::releaseObject(plugin_element_);
73 if (window_npobject_)
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) {
86 NOTREACHED();
89 void WebPluginProxy::WillDestroyWindow(gfx::PluginWindowHandle window) {
90 #if defined(OS_WIN)
91 PluginThread::current()->Send(
92 new PluginProcessHostMsg_PluginWindowDestroyed(
93 window, ::GetParent(window)));
94 #else
95 NOTIMPLEMENTED();
96 #endif
99 #if defined(OS_WIN)
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));
110 #endif
112 void WebPluginProxy::CancelResource(unsigned long id) {
113 Send(new PluginHostMsg_CancelResource(route_id_, id));
114 resource_clients_.erase(id);
117 void WebPluginProxy::Invalidate() {
118 gfx::Rect rect(0, 0,
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
127 // delegate.
128 if (!windowless_context()) {
129 delegate_->PluginDidInvalidate();
130 return;
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);
139 #else
140 const gfx::Rect invalidate_rect(rect);
141 #endif
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_))
154 return;
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(
163 FROM_HERE,
164 base::Bind(&WebPluginProxy::OnPaint,
165 weak_factory_.GetWeakPtr(),
166 damaged_rect_));
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));
179 if (!success)
180 return NULL;
182 // PluginChannel creates a dummy owner identifier for unknown owners, so
183 // use that.
184 NPP owner = channel_->GetExistingNPObjectOwner(MSG_ROUTING_NONE);
186 window_npobject_ = NPObjectProxy::Create(channel_.get(),
187 npobject_route_id,
188 host_render_view_routing_id_,
189 page_url_,
190 owner);
192 return window_npobject_;
195 NPObject* WebPluginProxy::GetPluginElement() {
196 if (plugin_element_)
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,
202 &success));
203 if (!success)
204 return NULL;
206 // PluginChannel creates a dummy owner identifier for unknown owners, so
207 // use that.
208 NPP owner = channel_->GetExistingNPObjectOwner(MSG_ROUTING_NONE);
210 plugin_element_ = NPObjectProxy::Create(channel_.get(),
211 npobject_route_id,
212 host_render_view_routing_id_,
213 page_url_,
214 owner);
216 return plugin_element_;
219 bool WebPluginProxy::FindProxyForUrl(const GURL& url, std::string* proxy_list) {
220 bool result = false;
221 Send(new PluginHostMsg_ResolveProxy(route_id_, url, &result, proxy_list));
222 return result;
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) {
234 std::string cookies;
235 Send(new PluginHostMsg_GetCookies(route_id_, url,
236 first_party_for_cookies, &cookies));
238 return 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()) {
247 return NULL;
250 return iterator->second;
253 int WebPluginProxy::GetRendererId() {
254 if (channel_.get())
255 return channel_->renderer_id();
256 return -1;
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,
274 const char* method,
275 const char* target,
276 const char* buf,
277 unsigned int len,
278 int notify_id,
279 bool popups_allowed,
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)) {
290 return;
295 PluginHostMsg_URLRequest_Params params;
296 params.url = url;
297 params.method = method;
298 if (target)
299 params.target = std::string(target);
301 if (len) {
302 params.buffer.resize(len);
303 memcpy(&params.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())
316 return;
317 #else
318 if (!windowless_canvas() || !windowless_canvas()->getDevice())
319 return;
320 #endif
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]);
346 #else
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
361 // will work.
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
371 // bitmap.
372 delegate_->Paint(saved_canvas.get(), offset_rect);
374 saved_canvas->restore();
375 #endif
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,
393 windowless_buffer1,
394 window_rect);
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());
403 #else
404 delegate_->UpdateGeometry(window_rect, clip_rect);
405 #endif
407 // Send over any pending invalidates which occured when the plugin was
408 // off screen.
409 if (delegate_->IsWindowless() && !clip_rect.IsEmpty() &&
410 !damaged_rect_.IsEmpty()) {
411 InvalidateRect(damaged_rect_);
415 #if defined(OS_WIN)
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(),
424 true,
425 dib_handle,
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,
436 window_rect,
437 &windowless_canvases_[0]);
438 if (!windowless_canvases_[0]) {
439 windowless_canvases_[1].clear();
440 return;
442 CreateCanvasFromHandle(windowless_buffer1,
443 window_rect,
444 &windowless_canvases_[1]);
445 if (!windowless_canvases_[1]) {
446 windowless_canvases_[0].clear();
447 return;
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;
462 if (dib) {
463 cg_context = CGBitmapContextCreate(
464 dib->memory(),
465 window_rect.width(),
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);
474 dib_out->reset(dib);
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,
483 window_rect,
484 &windowless_dibs_[0],
485 &windowless_contexts_[0]);
486 CreateDIBAndCGContextFromHandle(windowless_buffer1,
487 window_rect,
488 &windowless_dibs_[1],
489 &windowless_contexts_[1]);
492 #else
494 void WebPluginProxy::SetWindowlessBuffers(
495 const TransportDIB::Handle& windowless_buffer0,
496 const TransportDIB::Handle& windowless_buffer1,
497 const gfx::Rect& window_rect) {
498 NOTIMPLEMENTED();
501 #endif
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,
522 bool defer) {
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);
529 Send(msg);
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);
537 Send(msg);
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,
553 int32 height,
554 uint32 surface_id) {
555 Send(new PluginHostMsg_AcceleratedPluginAllocatedIOSurface(
556 route_id_, width, height, surface_id));
559 void WebPluginProxy::AcceleratedPluginSwappedIOSurface() {
560 Send(new PluginHostMsg_AcceleratedPluginSwappedIOSurface(
561 route_id_));
563 #endif
565 void WebPluginProxy::OnPaint(const gfx::Rect& damaged_rect) {
566 GetContentClient()->SetActiveURL(page_url_);
568 Paint(damaged_rect);
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);
585 return;
586 } else {
587 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) {
597 bool result = true;
598 Send(new PluginHostMsg_CheckIfRunInsecureContent(
599 route_id_, url, &result));
600 return 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.
607 int input_type;
608 gfx::Rect caret_rect;
609 if (!delegate_->GetIMEStatus(&input_type, &caret_rect))
610 return;
612 Send(new PluginHostMsg_NotifyIMEStatus(route_id_, input_type, caret_rect));
614 #endif
616 } // namespace content