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/child/npapi/plugin_instance.h"
8 #include "base/file_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h"
13 #include "content/child/npapi/plugin_host.h"
14 #include "content/child/npapi/plugin_lib.h"
15 #include "content/child/npapi/plugin_stream_url.h"
16 #include "content/child/npapi/plugin_string_stream.h"
17 #include "content/child/npapi/webplugin.h"
18 #include "content/child/npapi/webplugin_delegate.h"
19 #include "content/child/npapi/webplugin_resource_client.h"
20 #include "content/public/common/content_constants.h"
21 #include "net/base/escape.h"
23 #if defined(OS_MACOSX)
24 #include <ApplicationServices/ApplicationServices.h>
29 PluginInstance::PluginInstance(PluginLib* plugin, const std::string& mime_type)
32 host_(PluginHost::Singleton()),
33 npp_functions_(plugin->functions()),
38 mime_type_(mime_type),
40 use_mozilla_user_agent_(false),
41 #if defined (OS_MACOSX)
42 #ifdef NP_NO_QUICKDRAW
43 drawing_model_(NPDrawingModelCoreGraphics),
45 drawing_model_(NPDrawingModelQuickDraw),
48 event_model_(NPEventModelCocoa),
50 event_model_(NPEventModelCarbon),
52 currently_handled_event_(NULL),
54 message_loop_(base::MessageLoop::current()),
55 load_manually_(false),
56 in_close_streams_(false),
59 next_range_request_id_(0),
60 handles_url_redirects_(false) {
65 if (mime_type_ == kFlashPluginSwfMimeType)
68 memset(&zero_padding_, 0, sizeof(zero_padding_));
69 DCHECK(message_loop_);
72 PluginInstance::~PluginInstance() {
81 plugin_->CloseInstance();
84 PluginStreamUrl* PluginInstance::CreateStream(unsigned long resource_id,
86 const std::string& mime_type,
91 GetNotifyData(notify_id, ¬ify, ¬ify_data);
92 PluginStreamUrl* stream = new PluginStreamUrl(
93 resource_id, url, this, notify, notify_data);
99 void PluginInstance::AddStream(PluginStream* stream) {
100 open_streams_.push_back(make_scoped_refptr(stream));
103 void PluginInstance::RemoveStream(PluginStream* stream) {
104 if (in_close_streams_)
107 std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
108 for (stream_index = open_streams_.begin();
109 stream_index != open_streams_.end(); ++stream_index) {
110 if (stream_index->get() == stream) {
111 open_streams_.erase(stream_index);
117 bool PluginInstance::IsValidStream(const NPStream* stream) {
118 std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
119 for (stream_index = open_streams_.begin();
120 stream_index != open_streams_.end(); ++stream_index) {
121 if ((*stream_index)->stream() == stream)
128 void PluginInstance::CloseStreams() {
129 in_close_streams_ = true;
130 for (unsigned int index = 0; index < open_streams_.size(); ++index) {
131 // Close all streams on the way down.
132 open_streams_[index]->Close(NPRES_USER_BREAK);
134 open_streams_.clear();
135 in_close_streams_ = false;
138 WebPluginResourceClient* PluginInstance::GetRangeRequest(
140 PendingRangeRequestMap::iterator iter = pending_range_requests_.find(id);
141 if (iter == pending_range_requests_.end()) {
146 WebPluginResourceClient* rv = iter->second->AsResourceClient();
147 pending_range_requests_.erase(iter);
151 bool PluginInstance::Start(const GURL& url,
152 char** const param_names,
153 char** const param_values,
155 bool load_manually) {
156 load_manually_ = load_manually;
157 unsigned short mode = load_manually_ ? NP_FULL : NP_EMBED;
160 NPError err = NPP_New(mode, param_count,
161 const_cast<char **>(param_names), const_cast<char **>(param_values));
163 if (err == NPERR_NO_ERROR) {
164 handles_url_redirects_ =
165 ((npp_functions_->version >= NPVERS_HAS_URL_REDIRECT_HANDLING) &&
166 (npp_functions_->urlredirectnotify));
168 return err == NPERR_NO_ERROR;
171 NPObject *PluginInstance::GetPluginScriptableObject() {
172 NPObject *value = NULL;
173 NPError error = NPP_GetValue(NPPVpluginScriptableNPObject, &value);
174 if (error != NPERR_NO_ERROR || value == NULL)
179 bool PluginInstance::GetFormValue(base::string16* value) {
180 // Plugins will allocate memory for the return value by using NPN_MemAlloc().
181 char *plugin_value = NULL;
182 NPError error = NPP_GetValue(NPPVformValue, &plugin_value);
183 if (error != NPERR_NO_ERROR || !plugin_value) {
186 // Assumes the result is UTF8 text, as Firefox does.
187 *value = UTF8ToUTF16(plugin_value);
188 host_->host_functions()->memfree(plugin_value);
192 // WebPluginLoadDelegate methods
193 void PluginInstance::DidFinishLoadWithReason(const GURL& url,
198 GetNotifyData(notify_id, ¬ify, ¬ify_data);
204 NPP_URLNotify(url.spec().c_str(), reason, notify_data);
207 unsigned PluginInstance::GetBackingTextureId() {
208 // By default the plugin instance is not backed by an OpenGL texture.
213 NPError PluginInstance::NPP_New(unsigned short mode,
217 DCHECK(npp_functions_ != 0);
218 DCHECK(npp_functions_->newp != 0);
221 if (npp_functions_->newp != 0) {
222 return npp_functions_->newp(
223 (NPMIMEType)mime_type_.c_str(), npp_, mode, argc, argn, argv, NULL);
225 return NPERR_INVALID_FUNCTABLE_ERROR;
228 void PluginInstance::NPP_Destroy() {
229 DCHECK(npp_functions_ != 0);
230 DCHECK(npp_functions_->destroy != 0);
232 if (npp_functions_->destroy != 0) {
233 NPSavedData *savedData = 0;
234 npp_functions_->destroy(npp_, &savedData);
236 // TODO: Support savedData. Technically, these need to be
237 // saved on a per-URL basis, and then only passed
238 // to new instances of the plugin at the same URL.
239 // Sounds like a huge security risk. When we do support
240 // these, we should pass them back to the PluginLib
241 // to be stored there.
242 DCHECK(savedData == 0);
245 for (unsigned int file_index = 0; file_index < files_created_.size();
247 base::DeleteFile(files_created_[file_index], false);
250 // Ensure that no timer callbacks are invoked after NPP_Destroy.
254 NPError PluginInstance::NPP_SetWindow(NPWindow* window) {
255 DCHECK(npp_functions_ != 0);
256 DCHECK(npp_functions_->setwindow != 0);
258 if (npp_functions_->setwindow != 0) {
259 return npp_functions_->setwindow(npp_, window);
261 return NPERR_INVALID_FUNCTABLE_ERROR;
264 NPError PluginInstance::NPP_NewStream(NPMIMEType type,
267 unsigned short* stype) {
268 DCHECK(npp_functions_ != 0);
269 DCHECK(npp_functions_->newstream != 0);
270 if (npp_functions_->newstream != 0) {
271 return npp_functions_->newstream(npp_, type, stream, seekable, stype);
273 return NPERR_INVALID_FUNCTABLE_ERROR;
276 NPError PluginInstance::NPP_DestroyStream(NPStream* stream, NPReason reason) {
277 DCHECK(npp_functions_ != 0);
278 DCHECK(npp_functions_->destroystream != 0);
280 if (stream == NULL || !IsValidStream(stream) || (stream->ndata == NULL))
281 return NPERR_INVALID_INSTANCE_ERROR;
283 if (npp_functions_->destroystream != 0) {
284 NPError result = npp_functions_->destroystream(npp_, stream, reason);
285 stream->ndata = NULL;
288 return NPERR_INVALID_FUNCTABLE_ERROR;
291 int PluginInstance::NPP_WriteReady(NPStream* stream) {
292 DCHECK(npp_functions_ != 0);
293 DCHECK(npp_functions_->writeready != 0);
294 if (npp_functions_->writeready != 0) {
295 return npp_functions_->writeready(npp_, stream);
300 int PluginInstance::NPP_Write(NPStream* stream,
304 DCHECK(npp_functions_ != 0);
305 DCHECK(npp_functions_->write != 0);
306 if (npp_functions_->write != 0) {
307 return npp_functions_->write(npp_, stream, offset, len, buffer);
312 void PluginInstance::NPP_StreamAsFile(NPStream* stream, const char* fname) {
313 DCHECK(npp_functions_ != 0);
314 DCHECK(npp_functions_->asfile != 0);
315 if (npp_functions_->asfile != 0) {
316 npp_functions_->asfile(npp_, stream, fname);
319 // Creating a temporary FilePath instance on the stack as the explicit
320 // FilePath constructor with StringType as an argument causes a compiler
321 // error when invoked via vector push back.
322 base::FilePath file_name = base::FilePath::FromUTF8Unsafe(fname);
323 files_created_.push_back(file_name);
326 void PluginInstance::NPP_URLNotify(const char* url,
329 DCHECK(npp_functions_ != 0);
330 DCHECK(npp_functions_->urlnotify != 0);
331 if (npp_functions_->urlnotify != 0) {
332 npp_functions_->urlnotify(npp_, url, reason, notifyData);
336 NPError PluginInstance::NPP_GetValue(NPPVariable variable, void* value) {
337 DCHECK(npp_functions_ != 0);
338 // getvalue is NULL for Shockwave
339 if (npp_functions_->getvalue != 0) {
340 return npp_functions_->getvalue(npp_, variable, value);
342 return NPERR_INVALID_FUNCTABLE_ERROR;
345 NPError PluginInstance::NPP_SetValue(NPNVariable variable, void* value) {
346 DCHECK(npp_functions_ != 0);
347 if (npp_functions_->setvalue != 0) {
348 return npp_functions_->setvalue(npp_, variable, value);
350 return NPERR_INVALID_FUNCTABLE_ERROR;
353 short PluginInstance::NPP_HandleEvent(void* event) {
354 DCHECK(npp_functions_ != 0);
355 DCHECK(npp_functions_->event != 0);
356 if (npp_functions_->event != 0) {
357 return npp_functions_->event(npp_, (void*)event);
362 bool PluginInstance::NPP_Print(NPPrint* platform_print) {
363 DCHECK(npp_functions_ != 0);
364 if (npp_functions_->print != 0) {
365 npp_functions_->print(npp_, platform_print);
371 void PluginInstance::NPP_URLRedirectNotify(const char* url, int32_t status,
373 DCHECK(npp_functions_ != 0);
374 if (npp_functions_->urlredirectnotify != 0) {
375 npp_functions_->urlredirectnotify(npp_, url, status, notify_data);
379 void PluginInstance::SendJavaScriptStream(const GURL& url,
380 const std::string& result,
385 GetNotifyData(notify_id, ¬ify, ¬ify_data);
388 PluginStringStream *stream =
389 new PluginStringStream(this, url, notify, notify_data);
391 stream->SendToPlugin(result, "text/html");
393 // NOTE: Sending an empty stream here will crash MacroMedia
394 // Flash 9. Just send the URL Notify.
396 NPP_URLNotify(url.spec().c_str(), NPRES_DONE, notify_data);
400 void PluginInstance::DidReceiveManualResponse(const GURL& url,
401 const std::string& mime_type,
402 const std::string& headers,
403 uint32 expected_length,
404 uint32 last_modified) {
405 DCHECK(load_manually_);
407 plugin_data_stream_ = CreateStream(-1, url, mime_type, 0);
408 plugin_data_stream_->DidReceiveResponse(mime_type, headers, expected_length,
409 last_modified, true);
412 void PluginInstance::DidReceiveManualData(const char* buffer, int length) {
413 DCHECK(load_manually_);
414 if (plugin_data_stream_.get() != NULL) {
415 plugin_data_stream_->DidReceiveData(buffer, length, 0);
419 void PluginInstance::DidFinishManualLoading() {
420 DCHECK(load_manually_);
421 if (plugin_data_stream_.get() != NULL) {
422 plugin_data_stream_->DidFinishLoading(plugin_data_stream_->ResourceId());
423 plugin_data_stream_->Close(NPRES_DONE);
424 plugin_data_stream_ = NULL;
428 void PluginInstance::DidManualLoadFail() {
429 DCHECK(load_manually_);
430 if (plugin_data_stream_.get() != NULL) {
431 plugin_data_stream_->DidFail(plugin_data_stream_->ResourceId());
432 plugin_data_stream_ = NULL;
436 void PluginInstance::PluginThreadAsyncCall(void (*func)(void*),
438 message_loop_->PostTask(
439 FROM_HERE, base::Bind(&PluginInstance::OnPluginThreadAsyncCall, this,
443 void PluginInstance::OnPluginThreadAsyncCall(void (*func)(void*),
445 // Do not invoke the callback if NPP_Destroy has already been invoked.
450 uint32 PluginInstance::ScheduleTimer(uint32 interval,
452 void (*func)(NPP id, uint32 timer_id)) {
453 // Use next timer id.
455 timer_id = next_timer_id_;
457 DCHECK(next_timer_id_ != 0);
459 // Record timer interval and repeat.
461 info.interval = interval;
462 info.repeat = repeat ? true : false;
463 timers_[timer_id] = info;
465 // Schedule the callback.
466 base::MessageLoop::current()->PostDelayedTask(
468 base::Bind(&PluginInstance::OnTimerCall, this, func, npp_, timer_id),
469 base::TimeDelta::FromMilliseconds(interval));
473 void PluginInstance::UnscheduleTimer(uint32 timer_id) {
474 // Remove info about the timer.
475 TimerMap::iterator it = timers_.find(timer_id);
476 if (it != timers_.end())
480 #if !defined(OS_MACOSX)
481 NPError PluginInstance::PopUpContextMenu(NPMenu* menu) {
483 return NPERR_GENERIC_ERROR;
487 void PluginInstance::OnTimerCall(void (*func)(NPP id, uint32 timer_id),
490 // Do not invoke callback if the timer has been unscheduled.
491 TimerMap::iterator it = timers_.find(timer_id);
492 if (it == timers_.end())
495 // Get all information about the timer before invoking the callback. The
496 // callback might unschedule the timer.
497 TimerInfo info = it->second;
501 // If the timer was unscheduled by the callback, just free up the timer id.
502 if (timers_.find(timer_id) == timers_.end())
505 // Reschedule repeating timers after invoking the callback so callback is not
506 // re-entered if it pumps the message loop.
508 base::MessageLoop::current()->PostDelayedTask(
510 base::Bind(&PluginInstance::OnTimerCall, this, func, npp_, timer_id),
511 base::TimeDelta::FromMilliseconds(info.interval));
517 void PluginInstance::PushPopupsEnabledState(bool enabled) {
518 popups_enabled_stack_.push(enabled);
521 void PluginInstance::PopPopupsEnabledState() {
522 popups_enabled_stack_.pop();
525 void PluginInstance::RequestRead(NPStream* stream, NPByteRange* range_list) {
526 std::string range_info = "bytes=";
529 range_info += base::IntToString(range_list->offset);
530 range_info.push_back('-');
532 base::IntToString(range_list->offset + range_list->length - 1);
533 range_list = range_list->next;
535 range_info.push_back(',');
538 if (plugin_data_stream_.get()) {
539 if (plugin_data_stream_->stream() == stream) {
540 webplugin_->CancelDocumentLoad();
541 plugin_data_stream_ = NULL;
545 // The lifetime of a NPStream instance depends on the PluginStream instance
546 // which owns it. When a plugin invokes NPN_RequestRead on a seekable stream,
547 // we don't want to create a new stream when the corresponding response is
548 // received. We send over a cookie which represents the PluginStream
549 // instance which is sent back from the renderer when the response is
551 std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
552 for (stream_index = open_streams_.begin();
553 stream_index != open_streams_.end(); ++stream_index) {
554 PluginStream* plugin_stream = stream_index->get();
555 if (plugin_stream->stream() == stream) {
556 // A stream becomes seekable the first time NPN_RequestRead
558 plugin_stream->set_seekable(true);
560 pending_range_requests_[++next_range_request_id_] = plugin_stream;
561 webplugin_->InitiateHTTPRangeRequest(
562 stream->url, range_info.c_str(), next_range_request_id_);
569 void PluginInstance::RequestURL(const char* url,
578 notify_id = ++next_notify_id_;
579 pending_requests_[notify_id] = notify_data;
582 webplugin_->HandleURLRequest(
583 url, method, target, buf, len, notify_id, popups_allowed(),
584 notify ? handles_url_redirects_ : false);
587 bool PluginInstance::ConvertPoint(double source_x, double source_y,
588 NPCoordinateSpace source_space,
589 double* dest_x, double* dest_y,
590 NPCoordinateSpace dest_space) {
591 #if defined(OS_MACOSX)
592 CGRect main_display_bounds = CGDisplayBounds(CGMainDisplayID());
594 double flipped_screen_x = source_x;
595 double flipped_screen_y = source_y;
596 switch(source_space) {
597 case NPCoordinateSpacePlugin:
598 flipped_screen_x += plugin_origin_.x();
599 flipped_screen_y += plugin_origin_.y();
601 case NPCoordinateSpaceWindow:
602 flipped_screen_x += containing_window_frame_.x();
603 flipped_screen_y = containing_window_frame_.height() - source_y +
604 containing_window_frame_.y();
606 case NPCoordinateSpaceFlippedWindow:
607 flipped_screen_x += containing_window_frame_.x();
608 flipped_screen_y += containing_window_frame_.y();
610 case NPCoordinateSpaceScreen:
611 flipped_screen_y = main_display_bounds.size.height - flipped_screen_y;
613 case NPCoordinateSpaceFlippedScreen:
620 double target_x = flipped_screen_x;
621 double target_y = flipped_screen_y;
623 case NPCoordinateSpacePlugin:
624 target_x -= plugin_origin_.x();
625 target_y -= plugin_origin_.y();
627 case NPCoordinateSpaceWindow:
628 target_x -= containing_window_frame_.x();
629 target_y -= containing_window_frame_.y();
630 target_y = containing_window_frame_.height() - target_y;
632 case NPCoordinateSpaceFlippedWindow:
633 target_x -= containing_window_frame_.x();
634 target_y -= containing_window_frame_.y();
636 case NPCoordinateSpaceScreen:
637 target_y = main_display_bounds.size.height - flipped_screen_y;
639 case NPCoordinateSpaceFlippedScreen:
657 void PluginInstance::GetNotifyData(int notify_id,
659 void** notify_data) {
660 PendingRequestMap::iterator iter = pending_requests_.find(notify_id);
661 if (iter != pending_requests_.end()) {
663 *notify_data = iter->second;
664 pending_requests_.erase(iter);
671 void PluginInstance::URLRedirectResponse(bool allow, void* notify_data) {
672 // The notify_data passed in allows us to identify the matching stream.
673 std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
674 for (stream_index = open_streams_.begin();
675 stream_index != open_streams_.end(); ++stream_index) {
676 PluginStream* plugin_stream = stream_index->get();
677 if (plugin_stream->notify_data() == notify_data) {
678 PluginStreamUrl* plugin_stream_url =
679 static_cast<PluginStreamUrl*>(plugin_stream);
680 plugin_stream_url->URLRedirectResponse(allow);
686 } // namespace content