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_host.h" 6 7 #include "base/command_line.h" 8 #include "base/file_util.h" 9 #include "base/logging.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/strings/string_piece.h" 12 #include "base/strings/string_util.h" 13 #include "base/strings/sys_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h" 15 #include "build/build_config.h" 16 #include "content/child/npapi/plugin_instance.h" 17 #include "content/child/npapi/plugin_lib.h" 18 #include "content/child/npapi/plugin_stream_url.h" 19 #include "content/child/npapi/webplugin_delegate.h" 20 #include "content/public/common/content_switches.h" 21 #include "content/public/common/webplugininfo.h" 22 #include "net/base/net_util.h" 23 #include "third_party/WebKit/public/web/WebBindings.h" 24 #include "third_party/WebKit/public/web/WebKit.h" 25 #include "third_party/npapi/bindings/npruntime.h" 26 #include "ui/gl/gl_implementation.h" 27 #include "ui/gl/gl_surface.h" 28 #include "webkit/common/user_agent/user_agent.h" 29 30 #if defined(OS_MACOSX) 31 #include "base/mac/mac_util.h" 32 #endif 33 34 using WebKit::WebBindings; 35 36 // Declarations for stub implementations of deprecated functions, which are no 37 // longer listed in npapi.h. 38 extern "C" { 39 void* NPN_GetJavaEnv(); 40 void* NPN_GetJavaPeer(NPP); 41 } 42 43 namespace content { 44 45 // Finds a PluginInstance from an NPP. 46 // The caller must take a reference if needed. 47 static PluginInstance* FindInstance(NPP id) { 48 if (id == NULL) { 49 return NULL; 50 } 51 return reinterpret_cast<PluginInstance*>(id->ndata); 52 } 53 54 #if defined(OS_MACOSX) 55 // Returns true if Core Animation plugins are supported. This requires that the 56 // OS supports shared accelerated surfaces via IOSurface. This is true on Snow 57 // Leopard and higher. 58 static bool SupportsCoreAnimationPlugins() { 59 if (CommandLine::ForCurrentProcess()->HasSwitch( 60 switches::kDisableCoreAnimationPlugins)) 61 return false; 62 // We also need to be running with desktop GL and not the software 63 // OSMesa renderer in order to share accelerated surfaces between 64 // processes. 65 gfx::GLImplementation implementation = gfx::GetGLImplementation(); 66 if (implementation == gfx::kGLImplementationNone) { 67 // Not initialized yet. 68 if (!gfx::GLSurface::InitializeOneOff()) { 69 return false; 70 } 71 implementation = gfx::GetGLImplementation(); 72 } 73 return (implementation == gfx::kGLImplementationDesktopGL); 74 } 75 #endif 76 77 PluginHost::PluginHost() { 78 InitializeHostFuncs(); 79 } 80 81 PluginHost::~PluginHost() { 82 } 83 84 PluginHost *PluginHost::Singleton() { 85 CR_DEFINE_STATIC_LOCAL(scoped_refptr<PluginHost>, singleton, ()); 86 if (singleton.get() == NULL) { 87 singleton = new PluginHost(); 88 } 89 90 DCHECK(singleton.get() != NULL); 91 return singleton.get(); 92 } 93 94 void PluginHost::InitializeHostFuncs() { 95 memset(&host_funcs_, 0, sizeof(host_funcs_)); 96 host_funcs_.size = sizeof(host_funcs_); 97 host_funcs_.version = (NP_VERSION_MAJOR << 8) | (NP_VERSION_MINOR); 98 99 // The "basic" functions 100 host_funcs_.geturl = &NPN_GetURL; 101 host_funcs_.posturl = &NPN_PostURL; 102 host_funcs_.requestread = &NPN_RequestRead; 103 host_funcs_.newstream = &NPN_NewStream; 104 host_funcs_.write = &NPN_Write; 105 host_funcs_.destroystream = &NPN_DestroyStream; 106 host_funcs_.status = &NPN_Status; 107 host_funcs_.uagent = &NPN_UserAgent; 108 host_funcs_.memalloc = &NPN_MemAlloc; 109 host_funcs_.memfree = &NPN_MemFree; 110 host_funcs_.memflush = &NPN_MemFlush; 111 host_funcs_.reloadplugins = &NPN_ReloadPlugins; 112 113 // Stubs for deprecated Java functions 114 host_funcs_.getJavaEnv = &NPN_GetJavaEnv; 115 host_funcs_.getJavaPeer = &NPN_GetJavaPeer; 116 117 // Advanced functions we implement 118 host_funcs_.geturlnotify = &NPN_GetURLNotify; 119 host_funcs_.posturlnotify = &NPN_PostURLNotify; 120 host_funcs_.getvalue = &NPN_GetValue; 121 host_funcs_.setvalue = &NPN_SetValue; 122 host_funcs_.invalidaterect = &NPN_InvalidateRect; 123 host_funcs_.invalidateregion = &NPN_InvalidateRegion; 124 host_funcs_.forceredraw = &NPN_ForceRedraw; 125 126 // These come from the Javascript Engine 127 host_funcs_.getstringidentifier = WebBindings::getStringIdentifier; 128 host_funcs_.getstringidentifiers = WebBindings::getStringIdentifiers; 129 host_funcs_.getintidentifier = WebBindings::getIntIdentifier; 130 host_funcs_.identifierisstring = WebBindings::identifierIsString; 131 host_funcs_.utf8fromidentifier = WebBindings::utf8FromIdentifier; 132 host_funcs_.intfromidentifier = WebBindings::intFromIdentifier; 133 host_funcs_.createobject = WebBindings::createObject; 134 host_funcs_.retainobject = WebBindings::retainObject; 135 host_funcs_.releaseobject = WebBindings::releaseObject; 136 host_funcs_.invoke = WebBindings::invoke; 137 host_funcs_.invokeDefault = WebBindings::invokeDefault; 138 host_funcs_.evaluate = WebBindings::evaluate; 139 host_funcs_.getproperty = WebBindings::getProperty; 140 host_funcs_.setproperty = WebBindings::setProperty; 141 host_funcs_.removeproperty = WebBindings::removeProperty; 142 host_funcs_.hasproperty = WebBindings::hasProperty; 143 host_funcs_.hasmethod = WebBindings::hasMethod; 144 host_funcs_.releasevariantvalue = WebBindings::releaseVariantValue; 145 host_funcs_.setexception = WebBindings::setException; 146 host_funcs_.pushpopupsenabledstate = NPN_PushPopupsEnabledState; 147 host_funcs_.poppopupsenabledstate = NPN_PopPopupsEnabledState; 148 host_funcs_.enumerate = WebBindings::enumerate; 149 host_funcs_.pluginthreadasynccall = NPN_PluginThreadAsyncCall; 150 host_funcs_.construct = WebBindings::construct; 151 host_funcs_.getvalueforurl = NPN_GetValueForURL; 152 host_funcs_.setvalueforurl = NPN_SetValueForURL; 153 host_funcs_.getauthenticationinfo = NPN_GetAuthenticationInfo; 154 host_funcs_.scheduletimer = NPN_ScheduleTimer; 155 host_funcs_.unscheduletimer = NPN_UnscheduleTimer; 156 host_funcs_.popupcontextmenu = NPN_PopUpContextMenu; 157 host_funcs_.convertpoint = NPN_ConvertPoint; 158 host_funcs_.handleevent = NPN_HandleEvent; 159 host_funcs_.unfocusinstance = NPN_UnfocusInstance; 160 host_funcs_.urlredirectresponse = NPN_URLRedirectResponse; 161 } 162 163 void PluginHost::PatchNPNetscapeFuncs(NPNetscapeFuncs* overrides) { 164 // When running in the plugin process, we need to patch the NPN functions 165 // that the plugin calls to interact with NPObjects that we give. Otherwise 166 // the plugin will call the v8 NPN functions, which won't work since we have 167 // an NPObjectProxy and not a real v8 implementation. 168 if (overrides->invoke) 169 host_funcs_.invoke = overrides->invoke; 170 171 if (overrides->invokeDefault) 172 host_funcs_.invokeDefault = overrides->invokeDefault; 173 174 if (overrides->evaluate) 175 host_funcs_.evaluate = overrides->evaluate; 176 177 if (overrides->getproperty) 178 host_funcs_.getproperty = overrides->getproperty; 179 180 if (overrides->setproperty) 181 host_funcs_.setproperty = overrides->setproperty; 182 183 if (overrides->removeproperty) 184 host_funcs_.removeproperty = overrides->removeproperty; 185 186 if (overrides->hasproperty) 187 host_funcs_.hasproperty = overrides->hasproperty; 188 189 if (overrides->hasmethod) 190 host_funcs_.hasmethod = overrides->hasmethod; 191 192 if (overrides->setexception) 193 host_funcs_.setexception = overrides->setexception; 194 195 if (overrides->enumerate) 196 host_funcs_.enumerate = overrides->enumerate; 197 } 198 199 bool PluginHost::SetPostData(const char* buf, 200 uint32 length, 201 std::vector<std::string>* names, 202 std::vector<std::string>* values, 203 std::vector<char>* body) { 204 // Use a state table to do the parsing. Whitespace must be 205 // trimmed after the fact if desired. In our case, we actually 206 // don't care about the whitespace, because we're just going to 207 // pass this back into another POST. This function strips out the 208 // "Content-length" header and does not append it to the request. 209 210 // 211 // This parser takes action only on state changes. 212 // 213 // Transition table: 214 // : \n NULL Other 215 // 0 GetHeader 1 2 4 0 216 // 1 GetValue 1 0 3 1 217 // 2 GetData 2 2 3 2 218 // 3 DONE 219 // 4 ERR 220 // 221 enum { INPUT_COLON=0, INPUT_NEWLINE, INPUT_NULL, INPUT_OTHER }; 222 enum { GETNAME, GETVALUE, GETDATA, DONE, ERR }; 223 int statemachine[3][4] = { { GETVALUE, GETDATA, GETDATA, GETNAME }, 224 { GETVALUE, GETNAME, DONE, GETVALUE }, 225 { GETDATA, GETDATA, DONE, GETDATA } }; 226 std::string name, value; 227 const char* ptr = static_cast<const char*>(buf); 228 const char* start = ptr; 229 int state = GETNAME; // initial state 230 bool done = false; 231 bool err = false; 232 do { 233 int input; 234 235 // Translate the current character into an input 236 // for the state table. 237 switch (*ptr) { 238 case ':' : 239 input = INPUT_COLON; 240 break; 241 case '\n': 242 input = INPUT_NEWLINE; 243 break; 244 case 0 : 245 input = INPUT_NULL; 246 break; 247 default : 248 input = INPUT_OTHER; 249 break; 250 } 251 252 int newstate = statemachine[state][input]; 253 254 // Take action based on the new state. 255 if (state != newstate) { 256 switch (newstate) { 257 case GETNAME: 258 // Got a value. 259 value = std::string(start, ptr - start); 260 TrimWhitespace(value, TRIM_ALL, &value); 261 // If the name field is empty, we'll skip this header 262 // but we won't error out. 263 if (!name.empty() && name != "content-length") { 264 names->push_back(name); 265 values->push_back(value); 266 } 267 start = ptr + 1; 268 break; 269 case GETVALUE: 270 // Got a header. 271 name = StringToLowerASCII(std::string(start, ptr - start)); 272 TrimWhitespace(name, TRIM_ALL, &name); 273 start = ptr + 1; 274 break; 275 case GETDATA: { 276 // Finished headers, now get body 277 if (*ptr) 278 start = ptr + 1; 279 size_t previous_size = body->size(); 280 size_t new_body_size = length - static_cast<int>(start - buf); 281 body->resize(previous_size + new_body_size); 282 if (!body->empty()) 283 memcpy(&body->front() + previous_size, start, new_body_size); 284 done = true; 285 break; 286 } 287 case ERR: 288 // error 289 err = true; 290 done = true; 291 break; 292 } 293 } 294 state = newstate; 295 ptr++; 296 } while (!done); 297 298 return !err; 299 } 300 301 } // namespace content 302 303 extern "C" { 304 305 using content::FindInstance; 306 using content::PluginHost; 307 using content::PluginInstance; 308 using content::WebPlugin; 309 310 // Allocates memory from the host's memory space. 311 void* NPN_MemAlloc(uint32_t size) { 312 // Note: We must use the same allocator/deallocator 313 // that is used by the javascript library, as some of the 314 // JS APIs will pass memory to the plugin which the plugin 315 // will attempt to free. 316 return malloc(size); 317 } 318 319 // Deallocates memory from the host's memory space 320 void NPN_MemFree(void* ptr) { 321 if (ptr != NULL && ptr != reinterpret_cast<void*>(-1)) 322 free(ptr); 323 } 324 325 // Requests that the host free a specified amount of memory. 326 uint32_t NPN_MemFlush(uint32_t size) { 327 // This is not relevant on Windows; MAC specific 328 return size; 329 } 330 331 // This is for dynamic discovery of new plugins. 332 // Should force a re-scan of the plugins directory to load new ones. 333 void NPN_ReloadPlugins(NPBool reload_pages) { 334 WebKit::resetPluginCache(reload_pages ? true : false); 335 } 336 337 // Requests a range of bytes for a seekable stream. 338 NPError NPN_RequestRead(NPStream* stream, NPByteRange* range_list) { 339 if (!stream || !range_list) 340 return NPERR_GENERIC_ERROR; 341 342 scoped_refptr<PluginInstance> plugin( 343 reinterpret_cast<PluginInstance*>(stream->ndata)); 344 if (!plugin.get()) 345 return NPERR_GENERIC_ERROR; 346 347 plugin->RequestRead(stream, range_list); 348 return NPERR_NO_ERROR; 349 } 350 351 // Generic form of GetURL for common code between GetURL and GetURLNotify. 352 static NPError GetURLNotify(NPP id, 353 const char* url, 354 const char* target, 355 bool notify, 356 void* notify_data) { 357 if (!url) 358 return NPERR_INVALID_URL; 359 360 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 361 if (!plugin.get()) { 362 return NPERR_GENERIC_ERROR; 363 } 364 365 plugin->RequestURL(url, "GET", target, NULL, 0, notify, notify_data); 366 return NPERR_NO_ERROR; 367 } 368 369 // Requests creation of a new stream with the contents of the 370 // specified URL; gets notification of the result. 371 NPError NPN_GetURLNotify(NPP id, 372 const char* url, 373 const char* target, 374 void* notify_data) { 375 // This is identical to NPN_GetURL, but after finishing, the 376 // browser will call NPP_URLNotify to inform the plugin that 377 // it has completed. 378 379 // According to the NPAPI documentation, if target == _self 380 // or a parent to _self, the browser should return NPERR_INVALID_PARAM, 381 // because it can't notify the plugin once deleted. This is 382 // absolutely false; firefox doesn't do this, and Flash relies on 383 // being able to use this. 384 385 // Also according to the NPAPI documentation, we should return 386 // NPERR_INVALID_URL if the url requested is not valid. However, 387 // this would require that we synchronously start fetching the 388 // URL. That just isn't practical. As such, there really is 389 // no way to return this error. From looking at the Firefox 390 // implementation, it doesn't look like Firefox does this either. 391 392 return GetURLNotify(id, url, target, true, notify_data); 393 } 394 395 NPError NPN_GetURL(NPP id, const char* url, const char* target) { 396 // Notes: 397 // Request from the Plugin to fetch content either for the plugin 398 // or to be placed into a browser window. 399 // 400 // If target == null, the browser fetches content and streams to plugin. 401 // otherwise, the browser loads content into an existing browser frame. 402 // If the target is the window/frame containing the plugin, the plugin 403 // may be destroyed. 404 // If the target is _blank, a mailto: or news: url open content in a new 405 // browser window 406 // If the target is _self, no other instance of the plugin is created. The 407 // plugin continues to operate in its own window 408 409 return GetURLNotify(id, url, target, false, 0); 410 } 411 412 // Generic form of PostURL for common code between PostURL and PostURLNotify. 413 static NPError PostURLNotify(NPP id, 414 const char* url, 415 const char* target, 416 uint32_t len, 417 const char* buf, 418 NPBool file, 419 bool notify, 420 void* notify_data) { 421 if (!url) 422 return NPERR_INVALID_URL; 423 424 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 425 if (!plugin.get()) { 426 NOTREACHED(); 427 return NPERR_GENERIC_ERROR; 428 } 429 430 std::string post_file_contents; 431 432 if (file) { 433 // Post data to be uploaded from a file. This can be handled in two 434 // ways. 435 // 1. Read entire file and send the contents as if it was a post data 436 // specified in the argument 437 // 2. Send just the file details and read them in the browser at the 438 // time of sending the request. 439 // Approach 2 is more efficient but complicated. Approach 1 has a major 440 // drawback of sending potentially large data over two IPC hops. In a way 441 // 'large data over IPC' problem exists as it is in case of plugin giving 442 // the data directly instead of in a file. 443 // Currently we are going with the approach 1 to get the feature working. 444 // We can optimize this later with approach 2. 445 446 // TODO(joshia): Design a scheme to send a file descriptor instead of 447 // entire file contents across. 448 449 // Security alert: 450 // --------------- 451 // Here we are blindly uploading whatever file requested by a plugin. 452 // This is risky as someone could exploit a plugin to send private 453 // data in arbitrary locations. 454 // A malicious (non-sandboxed) plugin has unfeterred access to OS 455 // resources and can do this anyway without using browser's HTTP stack. 456 // FWIW, Firefox and Safari don't perform any security checks. 457 458 if (!buf) 459 return NPERR_FILE_NOT_FOUND; 460 461 std::string file_path_ascii(buf); 462 base::FilePath file_path; 463 static const char kFileUrlPrefix[] = "file:"; 464 if (StartsWithASCII(file_path_ascii, kFileUrlPrefix, false)) { 465 GURL file_url(file_path_ascii); 466 DCHECK(file_url.SchemeIsFile()); 467 net::FileURLToFilePath(file_url, &file_path); 468 } else { 469 file_path = base::FilePath::FromWStringHack( 470 base::SysNativeMBToWide(file_path_ascii)); 471 } 472 473 base::PlatformFileInfo post_file_info; 474 if (!file_util::GetFileInfo(file_path, &post_file_info) || 475 post_file_info.is_directory) 476 return NPERR_FILE_NOT_FOUND; 477 478 if (!file_util::ReadFileToString(file_path, &post_file_contents)) 479 return NPERR_FILE_NOT_FOUND; 480 481 buf = post_file_contents.c_str(); 482 len = post_file_contents.size(); 483 } 484 485 // The post data sent by a plugin contains both headers 486 // and post data. Example: 487 // Content-type: text/html 488 // Content-length: 200 489 // 490 // <200 bytes of content here> 491 // 492 // Unfortunately, our stream needs these broken apart, 493 // so we need to parse the data and set headers and data 494 // separately. 495 plugin->RequestURL(url, "POST", target, buf, len, notify, notify_data); 496 return NPERR_NO_ERROR; 497 } 498 499 NPError NPN_PostURLNotify(NPP id, 500 const char* url, 501 const char* target, 502 uint32_t len, 503 const char* buf, 504 NPBool file, 505 void* notify_data) { 506 return PostURLNotify(id, url, target, len, buf, file, true, notify_data); 507 } 508 509 NPError NPN_PostURL(NPP id, 510 const char* url, 511 const char* target, 512 uint32_t len, 513 const char* buf, 514 NPBool file) { 515 // POSTs data to an URL, either from a temp file or a buffer. 516 // If file is true, buf contains a temp file (which host will delete after 517 // completing), and len contains the length of the filename. 518 // If file is false, buf contains the data to send, and len contains the 519 // length of the buffer 520 // 521 // If target is null, 522 // server response is returned to the plugin 523 // If target is _current, _self, or _top, 524 // server response is written to the plugin window and plugin is unloaded. 525 // If target is _new or _blank, 526 // server response is written to a new browser window 527 // If target is an existing frame, 528 // server response goes to that frame. 529 // 530 // For protocols other than FTP 531 // file uploads must be line-end converted from \r\n to \n 532 // 533 // Note: you cannot specify headers (even a blank line) in a memory buffer, 534 // use NPN_PostURLNotify 535 536 return PostURLNotify(id, url, target, len, buf, file, false, 0); 537 } 538 539 NPError NPN_NewStream(NPP id, 540 NPMIMEType type, 541 const char* target, 542 NPStream** stream) { 543 // Requests creation of a new data stream produced by the plugin, 544 // consumed by the browser. 545 // 546 // Browser should put this stream into a window target. 547 // 548 // TODO: implement me 549 DVLOG(1) << "NPN_NewStream is not implemented yet."; 550 return NPERR_GENERIC_ERROR; 551 } 552 553 int32_t NPN_Write(NPP id, NPStream* stream, int32_t len, void* buffer) { 554 // Writes data to an existing Plugin-created stream. 555 556 // TODO: implement me 557 DVLOG(1) << "NPN_Write is not implemented yet."; 558 return NPERR_GENERIC_ERROR; 559 } 560 561 NPError NPN_DestroyStream(NPP id, NPStream* stream, NPReason reason) { 562 // Destroys a stream (could be created by plugin or browser). 563 // 564 // Reasons: 565 // NPRES_DONE - normal completion 566 // NPRES_USER_BREAK - user terminated 567 // NPRES_NETWORK_ERROR - network error (all errors fit here?) 568 // 569 // 570 571 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 572 if (plugin.get() == NULL) { 573 NOTREACHED(); 574 return NPERR_GENERIC_ERROR; 575 } 576 577 return plugin->NPP_DestroyStream(stream, reason); 578 } 579 580 const char* NPN_UserAgent(NPP id) { 581 #if defined(OS_WIN) 582 // Flash passes in a null id during the NP_initialize call. We need to 583 // default to the Mozilla user agent if we don't have an NPP instance or 584 // else Flash won't request windowless mode. 585 bool use_mozilla_user_agent = true; 586 if (id) { 587 scoped_refptr<PluginInstance> plugin = FindInstance(id); 588 if (plugin.get() && !plugin->use_mozilla_user_agent()) 589 use_mozilla_user_agent = false; 590 } 591 592 if (use_mozilla_user_agent) 593 return "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) " 594 "Gecko/20061103 Firefox/2.0a1"; 595 #endif 596 597 return webkit_glue::GetUserAgent(GURL()).c_str(); 598 } 599 600 void NPN_Status(NPP id, const char* message) { 601 // Displays a message on the status line of the browser window. 602 603 // TODO: implement me 604 DVLOG(1) << "NPN_Status is not implemented yet."; 605 } 606 607 void NPN_InvalidateRect(NPP id, NPRect *invalidRect) { 608 // Invalidates specified drawing area prior to repainting or refreshing a 609 // windowless plugin 610 611 // Before a windowless plugin can refresh part of its drawing area, it must 612 // first invalidate it. This function causes the NPP_HandleEvent method to 613 // pass an update event or a paint message to the plug-in. After calling 614 // this method, the plug-in recieves a paint message asynchronously. 615 616 // The browser redraws invalid areas of the document and any windowless 617 // plug-ins at regularly timed intervals. To force a paint message, the 618 // plug-in can call NPN_ForceRedraw after calling this method. 619 620 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 621 if (plugin.get() && plugin->webplugin()) { 622 if (invalidRect) { 623 #if defined(OS_WIN) 624 if (!plugin->windowless()) { 625 RECT rect = {0}; 626 rect.left = invalidRect->left; 627 rect.right = invalidRect->right; 628 rect.top = invalidRect->top; 629 rect.bottom = invalidRect->bottom; 630 ::InvalidateRect(plugin->window_handle(), &rect, false); 631 return; 632 } 633 #endif 634 gfx::Rect rect(invalidRect->left, 635 invalidRect->top, 636 invalidRect->right - invalidRect->left, 637 invalidRect->bottom - invalidRect->top); 638 plugin->webplugin()->InvalidateRect(rect); 639 } else { 640 plugin->webplugin()->Invalidate(); 641 } 642 } 643 } 644 645 void NPN_InvalidateRegion(NPP id, NPRegion invalidRegion) { 646 // Invalidates a specified drawing region prior to repainting 647 // or refreshing a window-less plugin. 648 // 649 // Similar to NPN_InvalidateRect. 650 651 // TODO: this is overkill--add platform-specific region handling (at the 652 // very least, fetch the region's bounding box and pass it to InvalidateRect). 653 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 654 DCHECK(plugin.get() != NULL); 655 if (plugin.get() && plugin->webplugin()) 656 plugin->webplugin()->Invalidate(); 657 } 658 659 void NPN_ForceRedraw(NPP id) { 660 // Forces repaint for a windowless plug-in. 661 // 662 // We deliberately do not implement this; we don't want plugins forcing 663 // synchronous paints. 664 } 665 666 NPError NPN_GetValue(NPP id, NPNVariable variable, void* value) { 667 // Allows the plugin to query the browser for information 668 // 669 // Variables: 670 // NPNVxDisplay (unix only) 671 // NPNVxtAppContext (unix only) 672 // NPNVnetscapeWindow (win only) - Gets the native window on which the 673 // plug-in drawing occurs, returns HWND 674 // NPNVjavascriptEnabledBool: tells whether Javascript is enabled 675 // NPNVasdEnabledBool: tells whether SmartUpdate is enabled 676 // NPNVOfflineBool: tells whether offline-mode is enabled 677 678 NPError rv = NPERR_GENERIC_ERROR; 679 680 switch (static_cast<int>(variable)) { 681 case NPNVWindowNPObject: { 682 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 683 if (!plugin.get()) { 684 NOTREACHED(); 685 return NPERR_INVALID_INSTANCE_ERROR; 686 } 687 NPObject *np_object = plugin->webplugin()->GetWindowScriptNPObject(); 688 // Return value is expected to be retained, as 689 // described here: 690 // <http://www.mozilla.org/projects/plugins/npruntime.html#browseraccess> 691 if (np_object) { 692 WebBindings::retainObject(np_object); 693 void **v = (void **)value; 694 *v = np_object; 695 rv = NPERR_NO_ERROR; 696 } else { 697 NOTREACHED(); 698 } 699 break; 700 } 701 case NPNVPluginElementNPObject: { 702 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 703 if (!plugin.get()) { 704 NOTREACHED(); 705 return NPERR_INVALID_INSTANCE_ERROR; 706 } 707 NPObject *np_object = plugin->webplugin()->GetPluginElement(); 708 // Return value is expected to be retained, as 709 // described here: 710 // <http://www.mozilla.org/projects/plugins/npruntime.html#browseraccess> 711 if (np_object) { 712 WebBindings::retainObject(np_object); 713 void** v = static_cast<void**>(value); 714 *v = np_object; 715 rv = NPERR_NO_ERROR; 716 } else { 717 NOTREACHED(); 718 } 719 break; 720 } 721 #if !defined(OS_MACOSX) // OS X doesn't have windowed plugins. 722 case NPNVnetscapeWindow: { 723 scoped_refptr<PluginInstance> plugin = FindInstance(id); 724 if (!plugin.get()) { 725 NOTREACHED(); 726 return NPERR_INVALID_INSTANCE_ERROR; 727 } 728 gfx::PluginWindowHandle handle = plugin->window_handle(); 729 *((void**)value) = (void*)handle; 730 rv = NPERR_NO_ERROR; 731 break; 732 } 733 #endif 734 case NPNVjavascriptEnabledBool: { 735 // yes, JS is enabled. 736 *((void**)value) = (void*)1; 737 rv = NPERR_NO_ERROR; 738 break; 739 } 740 #if defined(TOOLKIT_GTK) 741 case NPNVToolkit: 742 // Tell them we are GTK2. (The alternative is GTK 1.2.) 743 *reinterpret_cast<int*>(value) = NPNVGtk2; 744 rv = NPERR_NO_ERROR; 745 break; 746 747 case NPNVSupportsXEmbedBool: 748 *reinterpret_cast<NPBool*>(value) = true; 749 rv = NPERR_NO_ERROR; 750 break; 751 #endif 752 case NPNVSupportsWindowless: { 753 NPBool* supports_windowless = reinterpret_cast<NPBool*>(value); 754 *supports_windowless = true; 755 rv = NPERR_NO_ERROR; 756 break; 757 } 758 case NPNVprivateModeBool: { 759 NPBool* private_mode = reinterpret_cast<NPBool*>(value); 760 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 761 if (!plugin.get()) { 762 NOTREACHED(); 763 return NPERR_INVALID_INSTANCE_ERROR; 764 } 765 *private_mode = plugin->webplugin()->IsOffTheRecord(); 766 rv = NPERR_NO_ERROR; 767 break; 768 } 769 #if defined(OS_MACOSX) 770 case NPNVpluginDrawingModel: { 771 // return the drawing model that was negotiated when we initialized. 772 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 773 if (!plugin.get()) { 774 NOTREACHED(); 775 return NPERR_INVALID_INSTANCE_ERROR; 776 } 777 *reinterpret_cast<int*>(value) = plugin->drawing_model(); 778 rv = NPERR_NO_ERROR; 779 break; 780 } 781 case NPNVsupportsCoreGraphicsBool: 782 case NPNVsupportsCocoaBool: { 783 // These drawing and event models are always supported. 784 NPBool* supports_model = reinterpret_cast<NPBool*>(value); 785 *supports_model = true; 786 rv = NPERR_NO_ERROR; 787 break; 788 } 789 case NPNVsupportsInvalidatingCoreAnimationBool: 790 case NPNVsupportsCoreAnimationBool: { 791 NPBool* supports_model = reinterpret_cast<NPBool*>(value); 792 *supports_model = content::SupportsCoreAnimationPlugins(); 793 rv = NPERR_NO_ERROR; 794 break; 795 } 796 #ifndef NP_NO_CARBON 797 case NPNVsupportsCarbonBool: 798 #endif 799 #ifndef NP_NO_QUICKDRAW 800 case NPNVsupportsQuickDrawBool: 801 #endif 802 case NPNVsupportsOpenGLBool: { 803 // These models are never supported. OpenGL was never widely supported, 804 // and QuickDraw and Carbon have been deprecated for quite some time. 805 NPBool* supports_model = reinterpret_cast<NPBool*>(value); 806 *supports_model = false; 807 rv = NPERR_NO_ERROR; 808 break; 809 } 810 case NPNVsupportsCompositingCoreAnimationPluginsBool: { 811 NPBool* supports_compositing = reinterpret_cast<NPBool*>(value); 812 *supports_compositing = content::SupportsCoreAnimationPlugins(); 813 rv = NPERR_NO_ERROR; 814 break; 815 } 816 case NPNVsupportsUpdatedCocoaTextInputBool: { 817 // We support the clarifications to the Cocoa IME event spec. 818 NPBool* supports_update = reinterpret_cast<NPBool*>(value); 819 *supports_update = true; 820 rv = NPERR_NO_ERROR; 821 break; 822 } 823 #endif // OS_MACOSX 824 default: 825 DVLOG(1) << "NPN_GetValue(" << variable << ") is not implemented yet."; 826 break; 827 } 828 return rv; 829 } 830 831 NPError NPN_SetValue(NPP id, NPPVariable variable, void* value) { 832 // Allows the plugin to set various modes 833 834 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 835 if (!plugin.get()) { 836 NOTREACHED(); 837 return NPERR_INVALID_INSTANCE_ERROR; 838 } 839 switch(variable) { 840 case NPPVpluginWindowBool: { 841 // Sets windowless mode for display of the plugin 842 // Note: the documentation at 843 // http://developer.mozilla.org/en/docs/NPN_SetValue is wrong. When 844 // value is NULL, the mode is set to true. This is the same way Mozilla 845 // works. 846 plugin->set_windowless(value == 0); 847 return NPERR_NO_ERROR; 848 } 849 case NPPVpluginTransparentBool: { 850 // Sets transparent mode for display of the plugin 851 // 852 // Transparent plugins require the browser to paint the background 853 // before having the plugin paint. By default, windowless plugins 854 // are transparent. Making a windowless plugin opaque means that 855 // the plugin does not require the browser to paint the background. 856 bool mode = (value != 0); 857 plugin->set_transparent(mode); 858 return NPERR_NO_ERROR; 859 } 860 case NPPVjavascriptPushCallerBool: 861 // Specifies whether you are pushing or popping the JSContext off. 862 // the stack 863 // TODO: implement me 864 DVLOG(1) << "NPN_SetValue(NPPVJavascriptPushCallerBool) is not " 865 "implemented."; 866 return NPERR_GENERIC_ERROR; 867 case NPPVpluginKeepLibraryInMemory: 868 // Tells browser that plugin library should live longer than usual. 869 // TODO: implement me 870 DVLOG(1) << "NPN_SetValue(NPPVpluginKeepLibraryInMemory) is not " 871 "implemented."; 872 return NPERR_GENERIC_ERROR; 873 #if defined(OS_MACOSX) 874 case NPPVpluginDrawingModel: { 875 intptr_t model = reinterpret_cast<intptr_t>(value); 876 if (model == NPDrawingModelCoreGraphics || 877 ((model == NPDrawingModelInvalidatingCoreAnimation || 878 model == NPDrawingModelCoreAnimation) && 879 content::SupportsCoreAnimationPlugins())) { 880 plugin->set_drawing_model(static_cast<NPDrawingModel>(model)); 881 return NPERR_NO_ERROR; 882 } 883 return NPERR_GENERIC_ERROR; 884 } 885 case NPPVpluginEventModel: { 886 // Only the Cocoa event model is supported. 887 intptr_t model = reinterpret_cast<intptr_t>(value); 888 if (model == NPEventModelCocoa) { 889 plugin->set_event_model(static_cast<NPEventModel>(model)); 890 return NPERR_NO_ERROR; 891 } 892 return NPERR_GENERIC_ERROR; 893 } 894 #endif 895 default: 896 // TODO: implement me 897 DVLOG(1) << "NPN_SetValue(" << variable << ") is not implemented."; 898 break; 899 } 900 901 NOTREACHED(); 902 return NPERR_GENERIC_ERROR; 903 } 904 905 void* NPN_GetJavaEnv() { 906 // TODO: implement me 907 DVLOG(1) << "NPN_GetJavaEnv is not implemented."; 908 return NULL; 909 } 910 911 void* NPN_GetJavaPeer(NPP) { 912 // TODO: implement me 913 DVLOG(1) << "NPN_GetJavaPeer is not implemented."; 914 return NULL; 915 } 916 917 void NPN_PushPopupsEnabledState(NPP id, NPBool enabled) { 918 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 919 if (plugin.get()) 920 plugin->PushPopupsEnabledState(enabled ? true : false); 921 } 922 923 void NPN_PopPopupsEnabledState(NPP id) { 924 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 925 if (plugin.get()) 926 plugin->PopPopupsEnabledState(); 927 } 928 929 void NPN_PluginThreadAsyncCall(NPP id, 930 void (*func)(void*), 931 void* user_data) { 932 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 933 if (plugin.get()) 934 plugin->PluginThreadAsyncCall(func, user_data); 935 } 936 937 NPError NPN_GetValueForURL(NPP id, 938 NPNURLVariable variable, 939 const char* url, 940 char** value, 941 uint32_t* len) { 942 if (!id) 943 return NPERR_INVALID_PARAM; 944 945 if (!url || !*url || !len) 946 return NPERR_INVALID_URL; 947 948 *len = 0; 949 std::string result; 950 951 switch (variable) { 952 case NPNURLVProxy: { 953 result = "DIRECT"; 954 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 955 if (!plugin.get()) 956 return NPERR_GENERIC_ERROR; 957 958 WebPlugin* webplugin = plugin->webplugin(); 959 if (!webplugin) 960 return NPERR_GENERIC_ERROR; 961 962 if (!webplugin->FindProxyForUrl(GURL(std::string(url)), &result)) 963 return NPERR_GENERIC_ERROR; 964 break; 965 } 966 case NPNURLVCookie: { 967 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 968 if (!plugin.get()) 969 return NPERR_GENERIC_ERROR; 970 971 WebPlugin* webplugin = plugin->webplugin(); 972 if (!webplugin) 973 return NPERR_GENERIC_ERROR; 974 975 // Bypass third-party cookie blocking by using the url as the 976 // first_party_for_cookies. 977 GURL cookies_url((std::string(url))); 978 result = webplugin->GetCookies(cookies_url, cookies_url); 979 break; 980 } 981 default: 982 return NPERR_GENERIC_ERROR; 983 } 984 985 // Allocate this using the NPAPI allocator. The plugin will call 986 // NPN_Free to free this. 987 *value = static_cast<char*>(NPN_MemAlloc(result.length() + 1)); 988 base::strlcpy(*value, result.c_str(), result.length() + 1); 989 *len = result.length(); 990 991 return NPERR_NO_ERROR; 992 } 993 994 NPError NPN_SetValueForURL(NPP id, 995 NPNURLVariable variable, 996 const char* url, 997 const char* value, 998 uint32_t len) { 999 if (!id) 1000 return NPERR_INVALID_PARAM; 1001 1002 if (!url || !*url) 1003 return NPERR_INVALID_URL; 1004 1005 switch (variable) { 1006 case NPNURLVCookie: { 1007 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 1008 if (!plugin.get()) 1009 return NPERR_GENERIC_ERROR; 1010 1011 WebPlugin* webplugin = plugin->webplugin(); 1012 if (!webplugin) 1013 return NPERR_GENERIC_ERROR; 1014 1015 std::string cookie(value, len); 1016 GURL cookies_url((std::string(url))); 1017 webplugin->SetCookie(cookies_url, cookies_url, cookie); 1018 return NPERR_NO_ERROR; 1019 } 1020 case NPNURLVProxy: 1021 // We don't support setting proxy values, fall through... 1022 break; 1023 default: 1024 // Fall through and return an error... 1025 break; 1026 } 1027 1028 return NPERR_GENERIC_ERROR; 1029 } 1030 1031 NPError NPN_GetAuthenticationInfo(NPP id, 1032 const char* protocol, 1033 const char* host, 1034 int32_t port, 1035 const char* scheme, 1036 const char* realm, 1037 char** username, 1038 uint32_t* ulen, 1039 char** password, 1040 uint32_t* plen) { 1041 if (!id || !protocol || !host || !scheme || !realm || !username || 1042 !ulen || !password || !plen) 1043 return NPERR_INVALID_PARAM; 1044 1045 // TODO: implement me (bug 23928) 1046 return NPERR_GENERIC_ERROR; 1047 } 1048 1049 uint32_t NPN_ScheduleTimer(NPP id, 1050 uint32_t interval, 1051 NPBool repeat, 1052 void (*func)(NPP id, uint32_t timer_id)) { 1053 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 1054 if (!plugin.get()) 1055 return 0; 1056 1057 return plugin->ScheduleTimer(interval, repeat, func); 1058 } 1059 1060 void NPN_UnscheduleTimer(NPP id, uint32_t timer_id) { 1061 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 1062 if (plugin.get()) 1063 plugin->UnscheduleTimer(timer_id); 1064 } 1065 1066 NPError NPN_PopUpContextMenu(NPP id, NPMenu* menu) { 1067 if (!menu) 1068 return NPERR_INVALID_PARAM; 1069 1070 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 1071 if (plugin.get()) { 1072 return plugin->PopUpContextMenu(menu); 1073 } 1074 NOTREACHED(); 1075 return NPERR_GENERIC_ERROR; 1076 } 1077 1078 NPBool NPN_ConvertPoint(NPP id, double sourceX, double sourceY, 1079 NPCoordinateSpace sourceSpace, 1080 double *destX, double *destY, 1081 NPCoordinateSpace destSpace) { 1082 scoped_refptr<PluginInstance> plugin(FindInstance(id)); 1083 if (plugin.get()) { 1084 return plugin->ConvertPoint( 1085 sourceX, sourceY, sourceSpace, destX, destY, destSpace); 1086 } 1087 NOTREACHED(); 1088 return false; 1089 } 1090 1091 NPBool NPN_HandleEvent(NPP id, void *event, NPBool handled) { 1092 // TODO: Implement advanced key handling: http://crbug.com/46578 1093 NOTIMPLEMENTED(); 1094 return false; 1095 } 1096 1097 NPBool NPN_UnfocusInstance(NPP id, NPFocusDirection direction) { 1098 // TODO: Implement advanced key handling: http://crbug.com/46578 1099 NOTIMPLEMENTED(); 1100 return false; 1101 } 1102 1103 void NPN_URLRedirectResponse(NPP instance, void* notify_data, NPBool allow) { 1104 scoped_refptr<PluginInstance> plugin(FindInstance(instance)); 1105 if (plugin.get()) { 1106 plugin->URLRedirectResponse(!!allow, notify_data); 1107 } 1108 } 1109 1110 } // extern "C" 1111