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_host.h"
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"
30 #if defined(OS_MACOSX)
31 #include "base/mac/mac_util.h"
34 using WebKit::WebBindings;
36 // Declarations for stub implementations of deprecated functions, which are no
37 // longer listed in npapi.h.
39 void* NPN_GetJavaEnv();
40 void* NPN_GetJavaPeer(NPP);
45 // Finds a PluginInstance from an NPP.
46 // The caller must take a reference if needed.
47 static PluginInstance* FindInstance(NPP id) {
51 return reinterpret_cast<PluginInstance*>(id->ndata);
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))
62 // We also need to be running with desktop GL and not the software
63 // OSMesa renderer in order to share accelerated surfaces between
65 gfx::GLImplementation implementation = gfx::GetGLImplementation();
66 if (implementation == gfx::kGLImplementationNone) {
67 // Not initialized yet.
68 if (!gfx::GLSurface::InitializeOneOff()) {
71 implementation = gfx::GetGLImplementation();
73 return (implementation == gfx::kGLImplementationDesktopGL);
77 PluginHost::PluginHost() {
78 InitializeHostFuncs();
81 PluginHost::~PluginHost() {
84 PluginHost *PluginHost::Singleton() {
85 CR_DEFINE_STATIC_LOCAL(scoped_refptr<PluginHost>, singleton, ());
86 if (singleton.get() == NULL) {
87 singleton = new PluginHost();
90 DCHECK(singleton.get() != NULL);
91 return singleton.get();
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);
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;
113 // Stubs for deprecated Java functions
114 host_funcs_.getJavaEnv = &NPN_GetJavaEnv;
115 host_funcs_.getJavaPeer = &NPN_GetJavaPeer;
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;
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;
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;
171 if (overrides->invokeDefault)
172 host_funcs_.invokeDefault = overrides->invokeDefault;
174 if (overrides->evaluate)
175 host_funcs_.evaluate = overrides->evaluate;
177 if (overrides->getproperty)
178 host_funcs_.getproperty = overrides->getproperty;
180 if (overrides->setproperty)
181 host_funcs_.setproperty = overrides->setproperty;
183 if (overrides->removeproperty)
184 host_funcs_.removeproperty = overrides->removeproperty;
186 if (overrides->hasproperty)
187 host_funcs_.hasproperty = overrides->hasproperty;
189 if (overrides->hasmethod)
190 host_funcs_.hasmethod = overrides->hasmethod;
192 if (overrides->setexception)
193 host_funcs_.setexception = overrides->setexception;
195 if (overrides->enumerate)
196 host_funcs_.enumerate = overrides->enumerate;
199 bool PluginHost::SetPostData(const char* buf,
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.
211 // This parser takes action only on state changes.
215 // 0 GetHeader 1 2 4 0
216 // 1 GetValue 1 0 3 1
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
235 // Translate the current character into an input
236 // for the state table.
242 input = INPUT_NEWLINE;
252 int newstate = statemachine[state][input];
254 // Take action based on the new state.
255 if (state != newstate) {
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);
271 name = StringToLowerASCII(std::string(start, ptr - start));
272 TrimWhitespace(name, TRIM_ALL, &name);
276 // Finished headers, now get body
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);
283 memcpy(&body->front() + previous_size, start, new_body_size);
301 } // namespace content
305 using content::FindInstance;
306 using content::PluginHost;
307 using content::PluginInstance;
308 using content::WebPlugin;
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.
319 // Deallocates memory from the host's memory space
320 void NPN_MemFree(void* ptr) {
321 if (ptr != NULL && ptr != reinterpret_cast<void*>(-1))
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
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);
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;
342 scoped_refptr<PluginInstance> plugin(
343 reinterpret_cast<PluginInstance*>(stream->ndata));
345 return NPERR_GENERIC_ERROR;
347 plugin->RequestRead(stream, range_list);
348 return NPERR_NO_ERROR;
351 // Generic form of GetURL for common code between GetURL and GetURLNotify.
352 static NPError GetURLNotify(NPP id,
358 return NPERR_INVALID_URL;
360 scoped_refptr<PluginInstance> plugin(FindInstance(id));
362 return NPERR_GENERIC_ERROR;
365 plugin->RequestURL(url, "GET", target, NULL, 0, notify, notify_data);
366 return NPERR_NO_ERROR;
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,
375 // This is identical to NPN_GetURL, but after finishing, the
376 // browser will call NPP_URLNotify to inform the plugin that
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.
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.
392 return GetURLNotify(id, url, target, true, notify_data);
395 NPError NPN_GetURL(NPP id, const char* url, const char* target) {
397 // Request from the Plugin to fetch content either for the plugin
398 // or to be placed into a browser window.
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
404 // If the target is _blank, a mailto: or news: url open content in a new
406 // If the target is _self, no other instance of the plugin is created. The
407 // plugin continues to operate in its own window
409 return GetURLNotify(id, url, target, false, 0);
412 // Generic form of PostURL for common code between PostURL and PostURLNotify.
413 static NPError PostURLNotify(NPP id,
422 return NPERR_INVALID_URL;
424 scoped_refptr<PluginInstance> plugin(FindInstance(id));
427 return NPERR_GENERIC_ERROR;
430 std::string post_file_contents;
433 // Post data to be uploaded from a file. This can be handled in two
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.
446 // TODO(joshia): Design a scheme to send a file descriptor instead of
447 // entire file contents across.
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.
459 return NPERR_FILE_NOT_FOUND;
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);
469 file_path = base::FilePath::FromUTF8Unsafe(file_path_ascii);
472 base::PlatformFileInfo post_file_info;
473 if (!file_util::GetFileInfo(file_path, &post_file_info) ||
474 post_file_info.is_directory)
475 return NPERR_FILE_NOT_FOUND;
477 if (!base::ReadFileToString(file_path, &post_file_contents))
478 return NPERR_FILE_NOT_FOUND;
480 buf = post_file_contents.c_str();
481 len = post_file_contents.size();
484 // The post data sent by a plugin contains both headers
485 // and post data. Example:
486 // Content-type: text/html
487 // Content-length: 200
489 // <200 bytes of content here>
491 // Unfortunately, our stream needs these broken apart,
492 // so we need to parse the data and set headers and data
494 plugin->RequestURL(url, "POST", target, buf, len, notify, notify_data);
495 return NPERR_NO_ERROR;
498 NPError NPN_PostURLNotify(NPP id,
505 return PostURLNotify(id, url, target, len, buf, file, true, notify_data);
508 NPError NPN_PostURL(NPP id,
514 // POSTs data to an URL, either from a temp file or a buffer.
515 // If file is true, buf contains a temp file (which host will delete after
516 // completing), and len contains the length of the filename.
517 // If file is false, buf contains the data to send, and len contains the
518 // length of the buffer
520 // If target is null,
521 // server response is returned to the plugin
522 // If target is _current, _self, or _top,
523 // server response is written to the plugin window and plugin is unloaded.
524 // If target is _new or _blank,
525 // server response is written to a new browser window
526 // If target is an existing frame,
527 // server response goes to that frame.
529 // For protocols other than FTP
530 // file uploads must be line-end converted from \r\n to \n
532 // Note: you cannot specify headers (even a blank line) in a memory buffer,
533 // use NPN_PostURLNotify
535 return PostURLNotify(id, url, target, len, buf, file, false, 0);
538 NPError NPN_NewStream(NPP id,
542 // Requests creation of a new data stream produced by the plugin,
543 // consumed by the browser.
545 // Browser should put this stream into a window target.
547 // TODO: implement me
548 DVLOG(1) << "NPN_NewStream is not implemented yet.";
549 return NPERR_GENERIC_ERROR;
552 int32_t NPN_Write(NPP id, NPStream* stream, int32_t len, void* buffer) {
553 // Writes data to an existing Plugin-created stream.
555 // TODO: implement me
556 DVLOG(1) << "NPN_Write is not implemented yet.";
557 return NPERR_GENERIC_ERROR;
560 NPError NPN_DestroyStream(NPP id, NPStream* stream, NPReason reason) {
561 // Destroys a stream (could be created by plugin or browser).
564 // NPRES_DONE - normal completion
565 // NPRES_USER_BREAK - user terminated
566 // NPRES_NETWORK_ERROR - network error (all errors fit here?)
570 scoped_refptr<PluginInstance> plugin(FindInstance(id));
571 if (plugin.get() == NULL) {
573 return NPERR_GENERIC_ERROR;
576 return plugin->NPP_DestroyStream(stream, reason);
579 const char* NPN_UserAgent(NPP id) {
581 // Flash passes in a null id during the NP_initialize call. We need to
582 // default to the Mozilla user agent if we don't have an NPP instance or
583 // else Flash won't request windowless mode.
584 bool use_mozilla_user_agent = true;
586 scoped_refptr<PluginInstance> plugin = FindInstance(id);
587 if (plugin.get() && !plugin->use_mozilla_user_agent())
588 use_mozilla_user_agent = false;
591 if (use_mozilla_user_agent)
592 return "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) "
593 "Gecko/20061103 Firefox/2.0a1";
596 return webkit_glue::GetUserAgent(GURL()).c_str();
599 void NPN_Status(NPP id, const char* message) {
600 // Displays a message on the status line of the browser window.
602 // TODO: implement me
603 DVLOG(1) << "NPN_Status is not implemented yet.";
606 void NPN_InvalidateRect(NPP id, NPRect *invalidRect) {
607 // Invalidates specified drawing area prior to repainting or refreshing a
610 // Before a windowless plugin can refresh part of its drawing area, it must
611 // first invalidate it. This function causes the NPP_HandleEvent method to
612 // pass an update event or a paint message to the plug-in. After calling
613 // this method, the plug-in recieves a paint message asynchronously.
615 // The browser redraws invalid areas of the document and any windowless
616 // plug-ins at regularly timed intervals. To force a paint message, the
617 // plug-in can call NPN_ForceRedraw after calling this method.
619 scoped_refptr<PluginInstance> plugin(FindInstance(id));
620 if (plugin.get() && plugin->webplugin()) {
623 if (!plugin->windowless()) {
625 rect.left = invalidRect->left;
626 rect.right = invalidRect->right;
627 rect.top = invalidRect->top;
628 rect.bottom = invalidRect->bottom;
629 ::InvalidateRect(plugin->window_handle(), &rect, false);
633 gfx::Rect rect(invalidRect->left,
635 invalidRect->right - invalidRect->left,
636 invalidRect->bottom - invalidRect->top);
637 plugin->webplugin()->InvalidateRect(rect);
639 plugin->webplugin()->Invalidate();
644 void NPN_InvalidateRegion(NPP id, NPRegion invalidRegion) {
645 // Invalidates a specified drawing region prior to repainting
646 // or refreshing a window-less plugin.
648 // Similar to NPN_InvalidateRect.
650 // TODO: this is overkill--add platform-specific region handling (at the
651 // very least, fetch the region's bounding box and pass it to InvalidateRect).
652 scoped_refptr<PluginInstance> plugin(FindInstance(id));
653 DCHECK(plugin.get() != NULL);
654 if (plugin.get() && plugin->webplugin())
655 plugin->webplugin()->Invalidate();
658 void NPN_ForceRedraw(NPP id) {
659 // Forces repaint for a windowless plug-in.
661 // We deliberately do not implement this; we don't want plugins forcing
662 // synchronous paints.
665 NPError NPN_GetValue(NPP id, NPNVariable variable, void* value) {
666 // Allows the plugin to query the browser for information
669 // NPNVxDisplay (unix only)
670 // NPNVxtAppContext (unix only)
671 // NPNVnetscapeWindow (win only) - Gets the native window on which the
672 // plug-in drawing occurs, returns HWND
673 // NPNVjavascriptEnabledBool: tells whether Javascript is enabled
674 // NPNVasdEnabledBool: tells whether SmartUpdate is enabled
675 // NPNVOfflineBool: tells whether offline-mode is enabled
677 NPError rv = NPERR_GENERIC_ERROR;
679 switch (static_cast<int>(variable)) {
680 case NPNVWindowNPObject: {
681 scoped_refptr<PluginInstance> plugin(FindInstance(id));
684 return NPERR_INVALID_INSTANCE_ERROR;
686 NPObject *np_object = plugin->webplugin()->GetWindowScriptNPObject();
687 // Return value is expected to be retained, as
689 // <http://www.mozilla.org/projects/plugins/npruntime.html#browseraccess>
691 WebBindings::retainObject(np_object);
692 void **v = (void **)value;
700 case NPNVPluginElementNPObject: {
701 scoped_refptr<PluginInstance> plugin(FindInstance(id));
704 return NPERR_INVALID_INSTANCE_ERROR;
706 NPObject *np_object = plugin->webplugin()->GetPluginElement();
707 // Return value is expected to be retained, as
709 // <http://www.mozilla.org/projects/plugins/npruntime.html#browseraccess>
711 WebBindings::retainObject(np_object);
712 void** v = static_cast<void**>(value);
720 #if !defined(OS_MACOSX) // OS X doesn't have windowed plugins.
721 case NPNVnetscapeWindow: {
722 scoped_refptr<PluginInstance> plugin = FindInstance(id);
725 return NPERR_INVALID_INSTANCE_ERROR;
727 gfx::PluginWindowHandle handle = plugin->window_handle();
728 *((void**)value) = (void*)handle;
733 case NPNVjavascriptEnabledBool: {
734 // yes, JS is enabled.
735 *((void**)value) = (void*)1;
739 #if defined(TOOLKIT_GTK)
741 // Tell them we are GTK2. (The alternative is GTK 1.2.)
742 *reinterpret_cast<int*>(value) = NPNVGtk2;
746 case NPNVSupportsXEmbedBool:
747 *reinterpret_cast<NPBool*>(value) = true;
751 case NPNVSupportsWindowless: {
752 NPBool* supports_windowless = reinterpret_cast<NPBool*>(value);
753 *supports_windowless = true;
757 case NPNVprivateModeBool: {
758 NPBool* private_mode = reinterpret_cast<NPBool*>(value);
759 scoped_refptr<PluginInstance> plugin(FindInstance(id));
762 return NPERR_INVALID_INSTANCE_ERROR;
764 *private_mode = plugin->webplugin()->IsOffTheRecord();
768 #if defined(OS_MACOSX)
769 case NPNVpluginDrawingModel: {
770 // return the drawing model that was negotiated when we initialized.
771 scoped_refptr<PluginInstance> plugin(FindInstance(id));
774 return NPERR_INVALID_INSTANCE_ERROR;
776 *reinterpret_cast<int*>(value) = plugin->drawing_model();
780 case NPNVsupportsCoreGraphicsBool:
781 case NPNVsupportsCocoaBool: {
782 // These drawing and event models are always supported.
783 NPBool* supports_model = reinterpret_cast<NPBool*>(value);
784 *supports_model = true;
788 case NPNVsupportsInvalidatingCoreAnimationBool:
789 case NPNVsupportsCoreAnimationBool: {
790 NPBool* supports_model = reinterpret_cast<NPBool*>(value);
791 *supports_model = content::SupportsCoreAnimationPlugins();
796 case NPNVsupportsCarbonBool:
798 #ifndef NP_NO_QUICKDRAW
799 case NPNVsupportsQuickDrawBool:
801 case NPNVsupportsOpenGLBool: {
802 // These models are never supported. OpenGL was never widely supported,
803 // and QuickDraw and Carbon have been deprecated for quite some time.
804 NPBool* supports_model = reinterpret_cast<NPBool*>(value);
805 *supports_model = false;
809 case NPNVsupportsCompositingCoreAnimationPluginsBool: {
810 NPBool* supports_compositing = reinterpret_cast<NPBool*>(value);
811 *supports_compositing = content::SupportsCoreAnimationPlugins();
815 case NPNVsupportsUpdatedCocoaTextInputBool: {
816 // We support the clarifications to the Cocoa IME event spec.
817 NPBool* supports_update = reinterpret_cast<NPBool*>(value);
818 *supports_update = true;
824 DVLOG(1) << "NPN_GetValue(" << variable << ") is not implemented yet.";
830 NPError NPN_SetValue(NPP id, NPPVariable variable, void* value) {
831 // Allows the plugin to set various modes
833 scoped_refptr<PluginInstance> plugin(FindInstance(id));
836 return NPERR_INVALID_INSTANCE_ERROR;
839 case NPPVpluginWindowBool: {
840 // Sets windowless mode for display of the plugin
841 // Note: the documentation at
842 // http://developer.mozilla.org/en/docs/NPN_SetValue is wrong. When
843 // value is NULL, the mode is set to true. This is the same way Mozilla
845 plugin->set_windowless(value == 0);
846 return NPERR_NO_ERROR;
848 case NPPVpluginTransparentBool: {
849 // Sets transparent mode for display of the plugin
851 // Transparent plugins require the browser to paint the background
852 // before having the plugin paint. By default, windowless plugins
853 // are transparent. Making a windowless plugin opaque means that
854 // the plugin does not require the browser to paint the background.
855 bool mode = (value != 0);
856 plugin->set_transparent(mode);
857 return NPERR_NO_ERROR;
859 case NPPVjavascriptPushCallerBool:
860 // Specifies whether you are pushing or popping the JSContext off.
862 // TODO: implement me
863 DVLOG(1) << "NPN_SetValue(NPPVJavascriptPushCallerBool) is not "
865 return NPERR_GENERIC_ERROR;
866 case NPPVpluginKeepLibraryInMemory:
867 // Tells browser that plugin library should live longer than usual.
868 // TODO: implement me
869 DVLOG(1) << "NPN_SetValue(NPPVpluginKeepLibraryInMemory) is not "
871 return NPERR_GENERIC_ERROR;
872 #if defined(OS_MACOSX)
873 case NPPVpluginDrawingModel: {
874 intptr_t model = reinterpret_cast<intptr_t>(value);
875 if (model == NPDrawingModelCoreGraphics ||
876 ((model == NPDrawingModelInvalidatingCoreAnimation ||
877 model == NPDrawingModelCoreAnimation) &&
878 content::SupportsCoreAnimationPlugins())) {
879 plugin->set_drawing_model(static_cast<NPDrawingModel>(model));
880 return NPERR_NO_ERROR;
882 return NPERR_GENERIC_ERROR;
884 case NPPVpluginEventModel: {
885 // Only the Cocoa event model is supported.
886 intptr_t model = reinterpret_cast<intptr_t>(value);
887 if (model == NPEventModelCocoa) {
888 plugin->set_event_model(static_cast<NPEventModel>(model));
889 return NPERR_NO_ERROR;
891 return NPERR_GENERIC_ERROR;
895 // TODO: implement me
896 DVLOG(1) << "NPN_SetValue(" << variable << ") is not implemented.";
901 return NPERR_GENERIC_ERROR;
904 void* NPN_GetJavaEnv() {
905 // TODO: implement me
906 DVLOG(1) << "NPN_GetJavaEnv is not implemented.";
910 void* NPN_GetJavaPeer(NPP) {
911 // TODO: implement me
912 DVLOG(1) << "NPN_GetJavaPeer is not implemented.";
916 void NPN_PushPopupsEnabledState(NPP id, NPBool enabled) {
917 scoped_refptr<PluginInstance> plugin(FindInstance(id));
919 plugin->PushPopupsEnabledState(enabled ? true : false);
922 void NPN_PopPopupsEnabledState(NPP id) {
923 scoped_refptr<PluginInstance> plugin(FindInstance(id));
925 plugin->PopPopupsEnabledState();
928 void NPN_PluginThreadAsyncCall(NPP id,
931 scoped_refptr<PluginInstance> plugin(FindInstance(id));
933 plugin->PluginThreadAsyncCall(func, user_data);
936 NPError NPN_GetValueForURL(NPP id,
937 NPNURLVariable variable,
942 return NPERR_INVALID_PARAM;
944 if (!url || !*url || !len)
945 return NPERR_INVALID_URL;
953 scoped_refptr<PluginInstance> plugin(FindInstance(id));
955 return NPERR_GENERIC_ERROR;
957 WebPlugin* webplugin = plugin->webplugin();
959 return NPERR_GENERIC_ERROR;
961 if (!webplugin->FindProxyForUrl(GURL(std::string(url)), &result))
962 return NPERR_GENERIC_ERROR;
965 case NPNURLVCookie: {
966 scoped_refptr<PluginInstance> plugin(FindInstance(id));
968 return NPERR_GENERIC_ERROR;
970 WebPlugin* webplugin = plugin->webplugin();
972 return NPERR_GENERIC_ERROR;
974 // Bypass third-party cookie blocking by using the url as the
975 // first_party_for_cookies.
976 GURL cookies_url((std::string(url)));
977 result = webplugin->GetCookies(cookies_url, cookies_url);
981 return NPERR_GENERIC_ERROR;
984 // Allocate this using the NPAPI allocator. The plugin will call
985 // NPN_Free to free this.
986 *value = static_cast<char*>(NPN_MemAlloc(result.length() + 1));
987 base::strlcpy(*value, result.c_str(), result.length() + 1);
988 *len = result.length();
990 return NPERR_NO_ERROR;
993 NPError NPN_SetValueForURL(NPP id,
994 NPNURLVariable variable,
999 return NPERR_INVALID_PARAM;
1002 return NPERR_INVALID_URL;
1005 case NPNURLVCookie: {
1006 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1008 return NPERR_GENERIC_ERROR;
1010 WebPlugin* webplugin = plugin->webplugin();
1012 return NPERR_GENERIC_ERROR;
1014 std::string cookie(value, len);
1015 GURL cookies_url((std::string(url)));
1016 webplugin->SetCookie(cookies_url, cookies_url, cookie);
1017 return NPERR_NO_ERROR;
1020 // We don't support setting proxy values, fall through...
1023 // Fall through and return an error...
1027 return NPERR_GENERIC_ERROR;
1030 NPError NPN_GetAuthenticationInfo(NPP id,
1031 const char* protocol,
1040 if (!id || !protocol || !host || !scheme || !realm || !username ||
1041 !ulen || !password || !plen)
1042 return NPERR_INVALID_PARAM;
1044 // TODO: implement me (bug 23928)
1045 return NPERR_GENERIC_ERROR;
1048 uint32_t NPN_ScheduleTimer(NPP id,
1051 void (*func)(NPP id, uint32_t timer_id)) {
1052 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1056 return plugin->ScheduleTimer(interval, repeat, func);
1059 void NPN_UnscheduleTimer(NPP id, uint32_t timer_id) {
1060 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1062 plugin->UnscheduleTimer(timer_id);
1065 NPError NPN_PopUpContextMenu(NPP id, NPMenu* menu) {
1067 return NPERR_INVALID_PARAM;
1069 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1071 return plugin->PopUpContextMenu(menu);
1074 return NPERR_GENERIC_ERROR;
1077 NPBool NPN_ConvertPoint(NPP id, double sourceX, double sourceY,
1078 NPCoordinateSpace sourceSpace,
1079 double *destX, double *destY,
1080 NPCoordinateSpace destSpace) {
1081 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1083 return plugin->ConvertPoint(
1084 sourceX, sourceY, sourceSpace, destX, destY, destSpace);
1090 NPBool NPN_HandleEvent(NPP id, void *event, NPBool handled) {
1091 // TODO: Implement advanced key handling: http://crbug.com/46578
1096 NPBool NPN_UnfocusInstance(NPP id, NPFocusDirection direction) {
1097 // TODO: Implement advanced key handling: http://crbug.com/46578
1102 void NPN_URLRedirectResponse(NPP instance, void* notify_data, NPBool allow) {
1103 scoped_refptr<PluginInstance> plugin(FindInstance(instance));
1105 plugin->URLRedirectResponse(!!allow, notify_data);