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/child/npapi/plugin_instance.h" 6 7 #include "base/bind.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/public/common/content_constants.h" 20 #include "net/base/escape.h" 21 22 #if defined(OS_MACOSX) 23 #include <ApplicationServices/ApplicationServices.h> 24 #endif 25 26 namespace content { 27 28 PluginInstance::PluginInstance(PluginLib* plugin, const std::string& mime_type) 29 : plugin_(plugin), 30 npp_(0), 31 host_(PluginHost::Singleton()), 32 npp_functions_(plugin->functions()), 33 window_handle_(0), 34 windowless_(false), 35 transparent_(true), 36 webplugin_(0), 37 mime_type_(mime_type), 38 get_notify_data_(0), 39 use_mozilla_user_agent_(false), 40 #if defined (OS_MACOSX) 41 #ifdef NP_NO_QUICKDRAW 42 drawing_model_(NPDrawingModelCoreGraphics), 43 #else 44 drawing_model_(NPDrawingModelQuickDraw), 45 #endif 46 #ifdef NP_NO_CARBON 47 event_model_(NPEventModelCocoa), 48 #else 49 event_model_(NPEventModelCarbon), 50 #endif 51 currently_handled_event_(NULL), 52 #endif 53 message_loop_(base::MessageLoop::current()), 54 load_manually_(false), 55 in_close_streams_(false), 56 next_timer_id_(1), 57 next_notify_id_(0), 58 next_range_request_id_(0), 59 handles_url_redirects_(false) { 60 npp_ = new NPP_t(); 61 npp_->ndata = 0; 62 npp_->pdata = 0; 63 64 if (mime_type_ == kFlashPluginSwfMimeType) 65 transparent_ = false; 66 67 memset(&zero_padding_, 0, sizeof(zero_padding_)); 68 DCHECK(message_loop_); 69 } 70 71 PluginInstance::~PluginInstance() { 72 CloseStreams(); 73 74 if (npp_ != 0) { 75 delete npp_; 76 npp_ = 0; 77 } 78 79 if (plugin_.get()) 80 plugin_->CloseInstance(); 81 } 82 83 PluginStreamUrl* PluginInstance::CreateStream(unsigned long resource_id, 84 const GURL& url, 85 const std::string& mime_type, 86 int notify_id) { 87 88 bool notify; 89 void* notify_data; 90 GetNotifyData(notify_id, ¬ify, ¬ify_data); 91 PluginStreamUrl* stream = new PluginStreamUrl( 92 resource_id, url, this, notify, notify_data); 93 94 AddStream(stream); 95 return stream; 96 } 97 98 void PluginInstance::AddStream(PluginStream* stream) { 99 open_streams_.push_back(make_scoped_refptr(stream)); 100 } 101 102 void PluginInstance::RemoveStream(PluginStream* stream) { 103 if (in_close_streams_) 104 return; 105 106 std::vector<scoped_refptr<PluginStream> >::iterator stream_index; 107 for (stream_index = open_streams_.begin(); 108 stream_index != open_streams_.end(); ++stream_index) { 109 if (stream_index->get() == stream) { 110 open_streams_.erase(stream_index); 111 break; 112 } 113 } 114 } 115 116 bool PluginInstance::IsValidStream(const NPStream* stream) { 117 std::vector<scoped_refptr<PluginStream> >::iterator stream_index; 118 for (stream_index = open_streams_.begin(); 119 stream_index != open_streams_.end(); ++stream_index) { 120 if ((*stream_index)->stream() == stream) 121 return true; 122 } 123 124 return false; 125 } 126 127 void PluginInstance::CloseStreams() { 128 in_close_streams_ = true; 129 for (unsigned int index = 0; index < open_streams_.size(); ++index) { 130 // Close all streams on the way down. 131 open_streams_[index]->Close(NPRES_USER_BREAK); 132 } 133 open_streams_.clear(); 134 in_close_streams_ = false; 135 } 136 137 WebPluginResourceClient* PluginInstance::GetRangeRequest( 138 int id) { 139 PendingRangeRequestMap::iterator iter = pending_range_requests_.find(id); 140 if (iter == pending_range_requests_.end()) { 141 NOTREACHED(); 142 return NULL; 143 } 144 145 WebPluginResourceClient* rv = iter->second->AsResourceClient(); 146 pending_range_requests_.erase(iter); 147 return rv; 148 } 149 150 bool PluginInstance::Start(const GURL& url, 151 char** const param_names, 152 char** const param_values, 153 int param_count, 154 bool load_manually) { 155 load_manually_ = load_manually; 156 unsigned short mode = load_manually_ ? NP_FULL : NP_EMBED; 157 npp_->ndata = this; 158 159 NPError err = NPP_New(mode, param_count, 160 const_cast<char **>(param_names), const_cast<char **>(param_values)); 161 162 if (err == NPERR_NO_ERROR) { 163 handles_url_redirects_ = 164 ((npp_functions_->version >= NPVERS_HAS_URL_REDIRECT_HANDLING) && 165 (npp_functions_->urlredirectnotify)); 166 } 167 return err == NPERR_NO_ERROR; 168 } 169 170 NPObject *PluginInstance::GetPluginScriptableObject() { 171 NPObject *value = NULL; 172 NPError error = NPP_GetValue(NPPVpluginScriptableNPObject, &value); 173 if (error != NPERR_NO_ERROR || value == NULL) 174 return NULL; 175 return value; 176 } 177 178 bool PluginInstance::GetFormValue(base::string16* value) { 179 // Plugins will allocate memory for the return value by using NPN_MemAlloc(). 180 char *plugin_value = NULL; 181 NPError error = NPP_GetValue(NPPVformValue, &plugin_value); 182 if (error != NPERR_NO_ERROR || !plugin_value) { 183 return false; 184 } 185 // Assumes the result is UTF8 text, as Firefox does. 186 *value = UTF8ToUTF16(plugin_value); 187 host_->host_functions()->memfree(plugin_value); 188 return true; 189 } 190 191 // WebPluginLoadDelegate methods 192 void PluginInstance::DidFinishLoadWithReason( 193 const GURL& url, NPReason reason, int notify_id) { 194 bool notify; 195 void* notify_data; 196 GetNotifyData(notify_id, ¬ify, ¬ify_data); 197 if (!notify) { 198 NOTREACHED(); 199 return; 200 } 201 202 NPP_URLNotify(url.spec().c_str(), reason, notify_data); 203 } 204 205 unsigned PluginInstance::GetBackingTextureId() { 206 // By default the plugin instance is not backed by an OpenGL texture. 207 return 0; 208 } 209 210 // NPAPI methods 211 NPError PluginInstance::NPP_New(unsigned short mode, 212 short argc, 213 char *argn[], 214 char *argv[]) { 215 DCHECK(npp_functions_ != 0); 216 DCHECK(npp_functions_->newp != 0); 217 DCHECK(argc >= 0); 218 219 if (npp_functions_->newp != 0) { 220 return npp_functions_->newp( 221 (NPMIMEType)mime_type_.c_str(), npp_, mode, argc, argn, argv, NULL); 222 } 223 return NPERR_INVALID_FUNCTABLE_ERROR; 224 } 225 226 void PluginInstance::NPP_Destroy() { 227 DCHECK(npp_functions_ != 0); 228 DCHECK(npp_functions_->destroy != 0); 229 230 if (npp_functions_->destroy != 0) { 231 NPSavedData *savedData = 0; 232 npp_functions_->destroy(npp_, &savedData); 233 234 // TODO: Support savedData. Technically, these need to be 235 // saved on a per-URL basis, and then only passed 236 // to new instances of the plugin at the same URL. 237 // Sounds like a huge security risk. When we do support 238 // these, we should pass them back to the PluginLib 239 // to be stored there. 240 DCHECK(savedData == 0); 241 } 242 243 for (unsigned int file_index = 0; file_index < files_created_.size(); 244 file_index++) { 245 base::DeleteFile(files_created_[file_index], false); 246 } 247 248 // Ensure that no timer callbacks are invoked after NPP_Destroy. 249 timers_.clear(); 250 } 251 252 NPError PluginInstance::NPP_SetWindow(NPWindow *window) { 253 DCHECK(npp_functions_ != 0); 254 DCHECK(npp_functions_->setwindow != 0); 255 256 if (npp_functions_->setwindow != 0) { 257 return npp_functions_->setwindow(npp_, window); 258 } 259 return NPERR_INVALID_FUNCTABLE_ERROR; 260 } 261 262 NPError PluginInstance::NPP_NewStream(NPMIMEType type, 263 NPStream *stream, 264 NPBool seekable, 265 unsigned short *stype) { 266 DCHECK(npp_functions_ != 0); 267 DCHECK(npp_functions_->newstream != 0); 268 if (npp_functions_->newstream != 0) { 269 return npp_functions_->newstream(npp_, type, stream, seekable, stype); 270 } 271 return NPERR_INVALID_FUNCTABLE_ERROR; 272 } 273 274 NPError PluginInstance::NPP_DestroyStream(NPStream *stream, NPReason reason) { 275 DCHECK(npp_functions_ != 0); 276 DCHECK(npp_functions_->destroystream != 0); 277 278 if (stream == NULL || !IsValidStream(stream) || (stream->ndata == NULL)) 279 return NPERR_INVALID_INSTANCE_ERROR; 280 281 if (npp_functions_->destroystream != 0) { 282 NPError result = npp_functions_->destroystream(npp_, stream, reason); 283 stream->ndata = NULL; 284 return result; 285 } 286 return NPERR_INVALID_FUNCTABLE_ERROR; 287 } 288 289 int PluginInstance::NPP_WriteReady(NPStream *stream) { 290 DCHECK(npp_functions_ != 0); 291 DCHECK(npp_functions_->writeready != 0); 292 if (npp_functions_->writeready != 0) { 293 return npp_functions_->writeready(npp_, stream); 294 } 295 return 0; 296 } 297 298 int PluginInstance::NPP_Write(NPStream *stream, 299 int offset, 300 int len, 301 void *buffer) { 302 DCHECK(npp_functions_ != 0); 303 DCHECK(npp_functions_->write != 0); 304 if (npp_functions_->write != 0) { 305 return npp_functions_->write(npp_, stream, offset, len, buffer); 306 } 307 return 0; 308 } 309 310 void PluginInstance::NPP_StreamAsFile(NPStream *stream, const char *fname) { 311 DCHECK(npp_functions_ != 0); 312 DCHECK(npp_functions_->asfile != 0); 313 if (npp_functions_->asfile != 0) { 314 npp_functions_->asfile(npp_, stream, fname); 315 } 316 317 // Creating a temporary FilePath instance on the stack as the explicit 318 // FilePath constructor with StringType as an argument causes a compiler 319 // error when invoked via vector push back. 320 base::FilePath file_name = base::FilePath::FromWStringHack(UTF8ToWide(fname)); 321 files_created_.push_back(file_name); 322 } 323 324 void PluginInstance::NPP_URLNotify(const char *url, 325 NPReason reason, 326 void *notifyData) { 327 DCHECK(npp_functions_ != 0); 328 DCHECK(npp_functions_->urlnotify != 0); 329 if (npp_functions_->urlnotify != 0) { 330 npp_functions_->urlnotify(npp_, url, reason, notifyData); 331 } 332 } 333 334 NPError PluginInstance::NPP_GetValue(NPPVariable variable, void *value) { 335 DCHECK(npp_functions_ != 0); 336 // getvalue is NULL for Shockwave 337 if (npp_functions_->getvalue != 0) { 338 return npp_functions_->getvalue(npp_, variable, value); 339 } 340 return NPERR_INVALID_FUNCTABLE_ERROR; 341 } 342 343 NPError PluginInstance::NPP_SetValue(NPNVariable variable, void *value) { 344 DCHECK(npp_functions_ != 0); 345 if (npp_functions_->setvalue != 0) { 346 return npp_functions_->setvalue(npp_, variable, value); 347 } 348 return NPERR_INVALID_FUNCTABLE_ERROR; 349 } 350 351 short PluginInstance::NPP_HandleEvent(void* event) { 352 DCHECK(npp_functions_ != 0); 353 DCHECK(npp_functions_->event != 0); 354 if (npp_functions_->event != 0) { 355 return npp_functions_->event(npp_, (void*)event); 356 } 357 return false; 358 } 359 360 bool PluginInstance::NPP_Print(NPPrint* platform_print) { 361 DCHECK(npp_functions_ != 0); 362 if (npp_functions_->print != 0) { 363 npp_functions_->print(npp_, platform_print); 364 return true; 365 } 366 return false; 367 } 368 369 void PluginInstance::NPP_URLRedirectNotify(const char* url, int32_t status, 370 void* notify_data) { 371 DCHECK(npp_functions_ != 0); 372 if (npp_functions_->urlredirectnotify != 0) { 373 npp_functions_->urlredirectnotify(npp_, url, status, notify_data); 374 } 375 } 376 377 void PluginInstance::SendJavaScriptStream(const GURL& url, 378 const std::string& result, 379 bool success, 380 int notify_id) { 381 bool notify; 382 void* notify_data; 383 GetNotifyData(notify_id, ¬ify, ¬ify_data); 384 385 if (success) { 386 PluginStringStream *stream = 387 new PluginStringStream(this, url, notify, notify_data); 388 AddStream(stream); 389 stream->SendToPlugin(result, "text/html"); 390 } else { 391 // NOTE: Sending an empty stream here will crash MacroMedia 392 // Flash 9. Just send the URL Notify. 393 if (notify) 394 NPP_URLNotify(url.spec().c_str(), NPRES_DONE, notify_data); 395 } 396 } 397 398 void PluginInstance::DidReceiveManualResponse(const GURL& url, 399 const std::string& mime_type, 400 const std::string& headers, 401 uint32 expected_length, 402 uint32 last_modified) { 403 DCHECK(load_manually_); 404 405 plugin_data_stream_ = CreateStream(-1, url, mime_type, 0); 406 plugin_data_stream_->DidReceiveResponse(mime_type, headers, expected_length, 407 last_modified, true); 408 } 409 410 void PluginInstance::DidReceiveManualData(const char* buffer, int length) { 411 DCHECK(load_manually_); 412 if (plugin_data_stream_.get() != NULL) { 413 plugin_data_stream_->DidReceiveData(buffer, length, 0); 414 } 415 } 416 417 void PluginInstance::DidFinishManualLoading() { 418 DCHECK(load_manually_); 419 if (plugin_data_stream_.get() != NULL) { 420 plugin_data_stream_->DidFinishLoading(plugin_data_stream_->ResourceId()); 421 plugin_data_stream_->Close(NPRES_DONE); 422 plugin_data_stream_ = NULL; 423 } 424 } 425 426 void PluginInstance::DidManualLoadFail() { 427 DCHECK(load_manually_); 428 if (plugin_data_stream_.get() != NULL) { 429 plugin_data_stream_->DidFail(plugin_data_stream_->ResourceId()); 430 plugin_data_stream_ = NULL; 431 } 432 } 433 434 void PluginInstance::PluginThreadAsyncCall(void (*func)(void *), 435 void *user_data) { 436 message_loop_->PostTask( 437 FROM_HERE, base::Bind(&PluginInstance::OnPluginThreadAsyncCall, this, 438 func, user_data)); 439 } 440 441 void PluginInstance::OnPluginThreadAsyncCall(void (*func)(void *), 442 void *user_data) { 443 // Do not invoke the callback if NPP_Destroy has already been invoked. 444 if (webplugin_) 445 func(user_data); 446 } 447 448 uint32 PluginInstance::ScheduleTimer(uint32 interval, 449 NPBool repeat, 450 void (*func)(NPP id, uint32 timer_id)) { 451 // Use next timer id. 452 uint32 timer_id; 453 timer_id = next_timer_id_; 454 ++next_timer_id_; 455 DCHECK(next_timer_id_ != 0); 456 457 // Record timer interval and repeat. 458 TimerInfo info; 459 info.interval = interval; 460 info.repeat = repeat ? true : false; 461 timers_[timer_id] = info; 462 463 // Schedule the callback. 464 base::MessageLoop::current()->PostDelayedTask( 465 FROM_HERE, 466 base::Bind(&PluginInstance::OnTimerCall, this, func, npp_, timer_id), 467 base::TimeDelta::FromMilliseconds(interval)); 468 return timer_id; 469 } 470 471 void PluginInstance::UnscheduleTimer(uint32 timer_id) { 472 // Remove info about the timer. 473 TimerMap::iterator it = timers_.find(timer_id); 474 if (it != timers_.end()) 475 timers_.erase(it); 476 } 477 478 #if !defined(OS_MACOSX) 479 NPError PluginInstance::PopUpContextMenu(NPMenu* menu) { 480 NOTIMPLEMENTED(); 481 return NPERR_GENERIC_ERROR; 482 } 483 #endif 484 485 void PluginInstance::OnTimerCall(void (*func)(NPP id, uint32 timer_id), 486 NPP id, 487 uint32 timer_id) { 488 // Do not invoke callback if the timer has been unscheduled. 489 TimerMap::iterator it = timers_.find(timer_id); 490 if (it == timers_.end()) 491 return; 492 493 // Get all information about the timer before invoking the callback. The 494 // callback might unschedule the timer. 495 TimerInfo info = it->second; 496 497 func(id, timer_id); 498 499 // If the timer was unscheduled by the callback, just free up the timer id. 500 if (timers_.find(timer_id) == timers_.end()) 501 return; 502 503 // Reschedule repeating timers after invoking the callback so callback is not 504 // re-entered if it pumps the message loop. 505 if (info.repeat) { 506 base::MessageLoop::current()->PostDelayedTask( 507 FROM_HERE, 508 base::Bind(&PluginInstance::OnTimerCall, this, func, npp_, timer_id), 509 base::TimeDelta::FromMilliseconds(info.interval)); 510 } else { 511 timers_.erase(it); 512 } 513 } 514 515 void PluginInstance::PushPopupsEnabledState(bool enabled) { 516 popups_enabled_stack_.push(enabled); 517 } 518 519 void PluginInstance::PopPopupsEnabledState() { 520 popups_enabled_stack_.pop(); 521 } 522 523 void PluginInstance::RequestRead(NPStream* stream, NPByteRange* range_list) { 524 std::string range_info = "bytes="; 525 526 while (range_list) { 527 range_info += base::IntToString(range_list->offset); 528 range_info.push_back('-'); 529 range_info += 530 base::IntToString(range_list->offset + range_list->length - 1); 531 range_list = range_list->next; 532 if (range_list) 533 range_info.push_back(','); 534 } 535 536 if (plugin_data_stream_.get()) { 537 if (plugin_data_stream_->stream() == stream) { 538 webplugin_->CancelDocumentLoad(); 539 plugin_data_stream_ = NULL; 540 } 541 } 542 543 // The lifetime of a NPStream instance depends on the PluginStream instance 544 // which owns it. When a plugin invokes NPN_RequestRead on a seekable stream, 545 // we don't want to create a new stream when the corresponding response is 546 // received. We send over a cookie which represents the PluginStream 547 // instance which is sent back from the renderer when the response is 548 // received. 549 std::vector<scoped_refptr<PluginStream> >::iterator stream_index; 550 for (stream_index = open_streams_.begin(); 551 stream_index != open_streams_.end(); ++stream_index) { 552 PluginStream* plugin_stream = stream_index->get(); 553 if (plugin_stream->stream() == stream) { 554 // A stream becomes seekable the first time NPN_RequestRead 555 // is called on it. 556 plugin_stream->set_seekable(true); 557 558 pending_range_requests_[++next_range_request_id_] = plugin_stream; 559 webplugin_->InitiateHTTPRangeRequest( 560 stream->url, range_info.c_str(), next_range_request_id_); 561 return; 562 } 563 } 564 NOTREACHED(); 565 } 566 567 void PluginInstance::RequestURL(const char* url, 568 const char* method, 569 const char* target, 570 const char* buf, 571 unsigned int len, 572 bool notify, 573 void* notify_data) { 574 int notify_id = 0; 575 if (notify) { 576 notify_id = ++next_notify_id_; 577 pending_requests_[notify_id] = notify_data; 578 } 579 580 webplugin_->HandleURLRequest( 581 url, method, target, buf, len, notify_id, popups_allowed(), 582 notify ? handles_url_redirects_ : false); 583 } 584 585 bool PluginInstance::ConvertPoint(double source_x, double source_y, 586 NPCoordinateSpace source_space, 587 double* dest_x, double* dest_y, 588 NPCoordinateSpace dest_space) { 589 #if defined(OS_MACOSX) 590 CGRect main_display_bounds = CGDisplayBounds(CGMainDisplayID()); 591 592 double flipped_screen_x = source_x; 593 double flipped_screen_y = source_y; 594 switch(source_space) { 595 case NPCoordinateSpacePlugin: 596 flipped_screen_x += plugin_origin_.x(); 597 flipped_screen_y += plugin_origin_.y(); 598 break; 599 case NPCoordinateSpaceWindow: 600 flipped_screen_x += containing_window_frame_.x(); 601 flipped_screen_y = containing_window_frame_.height() - source_y + 602 containing_window_frame_.y(); 603 break; 604 case NPCoordinateSpaceFlippedWindow: 605 flipped_screen_x += containing_window_frame_.x(); 606 flipped_screen_y += containing_window_frame_.y(); 607 break; 608 case NPCoordinateSpaceScreen: 609 flipped_screen_y = main_display_bounds.size.height - flipped_screen_y; 610 break; 611 case NPCoordinateSpaceFlippedScreen: 612 break; 613 default: 614 NOTREACHED(); 615 return false; 616 } 617 618 double target_x = flipped_screen_x; 619 double target_y = flipped_screen_y; 620 switch(dest_space) { 621 case NPCoordinateSpacePlugin: 622 target_x -= plugin_origin_.x(); 623 target_y -= plugin_origin_.y(); 624 break; 625 case NPCoordinateSpaceWindow: 626 target_x -= containing_window_frame_.x(); 627 target_y -= containing_window_frame_.y(); 628 target_y = containing_window_frame_.height() - target_y; 629 break; 630 case NPCoordinateSpaceFlippedWindow: 631 target_x -= containing_window_frame_.x(); 632 target_y -= containing_window_frame_.y(); 633 break; 634 case NPCoordinateSpaceScreen: 635 target_y = main_display_bounds.size.height - flipped_screen_y; 636 break; 637 case NPCoordinateSpaceFlippedScreen: 638 break; 639 default: 640 NOTREACHED(); 641 return false; 642 } 643 644 if (dest_x) 645 *dest_x = target_x; 646 if (dest_y) 647 *dest_y = target_y; 648 return true; 649 #else 650 NOTIMPLEMENTED(); 651 return false; 652 #endif 653 } 654 655 void PluginInstance::GetNotifyData( 656 int notify_id, bool* notify, void** notify_data) { 657 PendingRequestMap::iterator iter = pending_requests_.find(notify_id); 658 if (iter != pending_requests_.end()) { 659 *notify = true; 660 *notify_data = iter->second; 661 pending_requests_.erase(iter); 662 } else { 663 *notify = false; 664 *notify_data = NULL; 665 } 666 } 667 668 void PluginInstance::URLRedirectResponse(bool allow, void* notify_data) { 669 // The notify_data passed in allows us to identify the matching stream. 670 std::vector<scoped_refptr<PluginStream> >::iterator stream_index; 671 for (stream_index = open_streams_.begin(); 672 stream_index != open_streams_.end(); ++stream_index) { 673 PluginStream* plugin_stream = stream_index->get(); 674 if (plugin_stream->notify_data() == notify_data) { 675 WebPluginResourceClient* resource_client = 676 plugin_stream->AsResourceClient(); 677 webplugin_->URLRedirectResponse(allow, resource_client->ResourceId()); 678 if (allow) { 679 plugin_stream->UpdateUrl( 680 plugin_stream->pending_redirect_url().c_str()); 681 } 682 break; 683 } 684 } 685 } 686 687 } // namespace content 688