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 blink::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
64 // processes. Because on MacOS we lazy-initialize GLSurface in the
65 // renderer process here, ensure we're not also initializing GL somewhere
66 // else, and that we only do this once.
67 static gfx::GLImplementation implementation = gfx::kGLImplementationNone;
68 if (implementation == gfx::kGLImplementationNone) {
69 // Not initialized yet.
70 DCHECK_EQ(implementation, gfx::GetGLImplementation())
71 << "GL already initialized by someone else to: "
72 << gfx::GetGLImplementation();
73 if (!gfx::GLSurface::InitializeOneOff()) {
76 implementation = gfx::GetGLImplementation();
78 return (implementation == gfx::kGLImplementationDesktopGL);
82 PluginHost::PluginHost() {
83 InitializeHostFuncs();
86 PluginHost::~PluginHost() {
89 PluginHost *PluginHost::Singleton() {
90 CR_DEFINE_STATIC_LOCAL(scoped_refptr<PluginHost>, singleton, ());
91 if (singleton.get() == NULL) {
92 singleton = new PluginHost();
95 DCHECK(singleton.get() != NULL);
96 return singleton.get();
99 void PluginHost::InitializeHostFuncs() {
100 memset(&host_funcs_, 0, sizeof(host_funcs_));
101 host_funcs_.size = sizeof(host_funcs_);
102 host_funcs_.version = (NP_VERSION_MAJOR << 8) | (NP_VERSION_MINOR);
104 // The "basic" functions
105 host_funcs_.geturl = &NPN_GetURL;
106 host_funcs_.posturl = &NPN_PostURL;
107 host_funcs_.requestread = &NPN_RequestRead;
108 host_funcs_.newstream = &NPN_NewStream;
109 host_funcs_.write = &NPN_Write;
110 host_funcs_.destroystream = &NPN_DestroyStream;
111 host_funcs_.status = &NPN_Status;
112 host_funcs_.uagent = &NPN_UserAgent;
113 host_funcs_.memalloc = &NPN_MemAlloc;
114 host_funcs_.memfree = &NPN_MemFree;
115 host_funcs_.memflush = &NPN_MemFlush;
116 host_funcs_.reloadplugins = &NPN_ReloadPlugins;
118 // Stubs for deprecated Java functions
119 host_funcs_.getJavaEnv = &NPN_GetJavaEnv;
120 host_funcs_.getJavaPeer = &NPN_GetJavaPeer;
122 // Advanced functions we implement
123 host_funcs_.geturlnotify = &NPN_GetURLNotify;
124 host_funcs_.posturlnotify = &NPN_PostURLNotify;
125 host_funcs_.getvalue = &NPN_GetValue;
126 host_funcs_.setvalue = &NPN_SetValue;
127 host_funcs_.invalidaterect = &NPN_InvalidateRect;
128 host_funcs_.invalidateregion = &NPN_InvalidateRegion;
129 host_funcs_.forceredraw = &NPN_ForceRedraw;
131 // These come from the Javascript Engine
132 host_funcs_.getstringidentifier = WebBindings::getStringIdentifier;
133 host_funcs_.getstringidentifiers = WebBindings::getStringIdentifiers;
134 host_funcs_.getintidentifier = WebBindings::getIntIdentifier;
135 host_funcs_.identifierisstring = WebBindings::identifierIsString;
136 host_funcs_.utf8fromidentifier = WebBindings::utf8FromIdentifier;
137 host_funcs_.intfromidentifier = WebBindings::intFromIdentifier;
138 host_funcs_.createobject = WebBindings::createObject;
139 host_funcs_.retainobject = WebBindings::retainObject;
140 host_funcs_.releaseobject = WebBindings::releaseObject;
141 host_funcs_.invoke = WebBindings::invoke;
142 host_funcs_.invokeDefault = WebBindings::invokeDefault;
143 host_funcs_.evaluate = WebBindings::evaluate;
144 host_funcs_.getproperty = WebBindings::getProperty;
145 host_funcs_.setproperty = WebBindings::setProperty;
146 host_funcs_.removeproperty = WebBindings::removeProperty;
147 host_funcs_.hasproperty = WebBindings::hasProperty;
148 host_funcs_.hasmethod = WebBindings::hasMethod;
149 host_funcs_.releasevariantvalue = WebBindings::releaseVariantValue;
150 host_funcs_.setexception = WebBindings::setException;
151 host_funcs_.pushpopupsenabledstate = NPN_PushPopupsEnabledState;
152 host_funcs_.poppopupsenabledstate = NPN_PopPopupsEnabledState;
153 host_funcs_.enumerate = WebBindings::enumerate;
154 host_funcs_.pluginthreadasynccall = NPN_PluginThreadAsyncCall;
155 host_funcs_.construct = WebBindings::construct;
156 host_funcs_.getvalueforurl = NPN_GetValueForURL;
157 host_funcs_.setvalueforurl = NPN_SetValueForURL;
158 host_funcs_.getauthenticationinfo = NPN_GetAuthenticationInfo;
159 host_funcs_.scheduletimer = NPN_ScheduleTimer;
160 host_funcs_.unscheduletimer = NPN_UnscheduleTimer;
161 host_funcs_.popupcontextmenu = NPN_PopUpContextMenu;
162 host_funcs_.convertpoint = NPN_ConvertPoint;
163 host_funcs_.handleevent = NPN_HandleEvent;
164 host_funcs_.unfocusinstance = NPN_UnfocusInstance;
165 host_funcs_.urlredirectresponse = NPN_URLRedirectResponse;
168 void PluginHost::PatchNPNetscapeFuncs(NPNetscapeFuncs* overrides) {
169 // When running in the plugin process, we need to patch the NPN functions
170 // that the plugin calls to interact with NPObjects that we give. Otherwise
171 // the plugin will call the v8 NPN functions, which won't work since we have
172 // an NPObjectProxy and not a real v8 implementation.
173 if (overrides->invoke)
174 host_funcs_.invoke = overrides->invoke;
176 if (overrides->invokeDefault)
177 host_funcs_.invokeDefault = overrides->invokeDefault;
179 if (overrides->evaluate)
180 host_funcs_.evaluate = overrides->evaluate;
182 if (overrides->getproperty)
183 host_funcs_.getproperty = overrides->getproperty;
185 if (overrides->setproperty)
186 host_funcs_.setproperty = overrides->setproperty;
188 if (overrides->removeproperty)
189 host_funcs_.removeproperty = overrides->removeproperty;
191 if (overrides->hasproperty)
192 host_funcs_.hasproperty = overrides->hasproperty;
194 if (overrides->hasmethod)
195 host_funcs_.hasmethod = overrides->hasmethod;
197 if (overrides->setexception)
198 host_funcs_.setexception = overrides->setexception;
200 if (overrides->enumerate)
201 host_funcs_.enumerate = overrides->enumerate;
204 bool PluginHost::SetPostData(const char* buf,
206 std::vector<std::string>* names,
207 std::vector<std::string>* values,
208 std::vector<char>* body) {
209 // Use a state table to do the parsing. Whitespace must be
210 // trimmed after the fact if desired. In our case, we actually
211 // don't care about the whitespace, because we're just going to
212 // pass this back into another POST. This function strips out the
213 // "Content-length" header and does not append it to the request.
216 // This parser takes action only on state changes.
220 // 0 GetHeader 1 2 4 0
221 // 1 GetValue 1 0 3 1
226 enum { INPUT_COLON=0, INPUT_NEWLINE, INPUT_NULL, INPUT_OTHER };
227 enum { GETNAME, GETVALUE, GETDATA, DONE, ERR };
228 int statemachine[3][4] = { { GETVALUE, GETDATA, GETDATA, GETNAME },
229 { GETVALUE, GETNAME, DONE, GETVALUE },
230 { GETDATA, GETDATA, DONE, GETDATA } };
231 std::string name, value;
232 const char* ptr = static_cast<const char*>(buf);
233 const char* start = ptr;
234 int state = GETNAME; // initial state
240 // Translate the current character into an input
241 // for the state table.
247 input = INPUT_NEWLINE;
257 int newstate = statemachine[state][input];
259 // Take action based on the new state.
260 if (state != newstate) {
264 value = std::string(start, ptr - start);
265 TrimWhitespace(value, TRIM_ALL, &value);
266 // If the name field is empty, we'll skip this header
267 // but we won't error out.
268 if (!name.empty() && name != "content-length") {
269 names->push_back(name);
270 values->push_back(value);
276 name = StringToLowerASCII(std::string(start, ptr - start));
277 TrimWhitespace(name, TRIM_ALL, &name);
281 // Finished headers, now get body
284 size_t previous_size = body->size();
285 size_t new_body_size = length - static_cast<int>(start - buf);
286 body->resize(previous_size + new_body_size);
288 memcpy(&body->front() + previous_size, start, new_body_size);
306 } // namespace content
310 using content::FindInstance;
311 using content::PluginHost;
312 using content::PluginInstance;
313 using content::WebPlugin;
315 // Allocates memory from the host's memory space.
316 void* NPN_MemAlloc(uint32_t size) {
317 // Note: We must use the same allocator/deallocator
318 // that is used by the javascript library, as some of the
319 // JS APIs will pass memory to the plugin which the plugin
320 // will attempt to free.
324 // Deallocates memory from the host's memory space
325 void NPN_MemFree(void* ptr) {
326 if (ptr != NULL && ptr != reinterpret_cast<void*>(-1))
330 // Requests that the host free a specified amount of memory.
331 uint32_t NPN_MemFlush(uint32_t size) {
332 // This is not relevant on Windows; MAC specific
336 // This is for dynamic discovery of new plugins.
337 // Should force a re-scan of the plugins directory to load new ones.
338 void NPN_ReloadPlugins(NPBool reload_pages) {
339 blink::resetPluginCache(reload_pages ? true : false);
342 // Requests a range of bytes for a seekable stream.
343 NPError NPN_RequestRead(NPStream* stream, NPByteRange* range_list) {
344 if (!stream || !range_list)
345 return NPERR_GENERIC_ERROR;
347 scoped_refptr<PluginInstance> plugin(
348 reinterpret_cast<PluginInstance*>(stream->ndata));
350 return NPERR_GENERIC_ERROR;
352 plugin->RequestRead(stream, range_list);
353 return NPERR_NO_ERROR;
356 // Generic form of GetURL for common code between GetURL and GetURLNotify.
357 static NPError GetURLNotify(NPP id,
363 return NPERR_INVALID_URL;
365 scoped_refptr<PluginInstance> plugin(FindInstance(id));
367 return NPERR_GENERIC_ERROR;
370 plugin->RequestURL(url, "GET", target, NULL, 0, notify, notify_data);
371 return NPERR_NO_ERROR;
374 // Requests creation of a new stream with the contents of the
375 // specified URL; gets notification of the result.
376 NPError NPN_GetURLNotify(NPP id,
380 // This is identical to NPN_GetURL, but after finishing, the
381 // browser will call NPP_URLNotify to inform the plugin that
384 // According to the NPAPI documentation, if target == _self
385 // or a parent to _self, the browser should return NPERR_INVALID_PARAM,
386 // because it can't notify the plugin once deleted. This is
387 // absolutely false; firefox doesn't do this, and Flash relies on
388 // being able to use this.
390 // Also according to the NPAPI documentation, we should return
391 // NPERR_INVALID_URL if the url requested is not valid. However,
392 // this would require that we synchronously start fetching the
393 // URL. That just isn't practical. As such, there really is
394 // no way to return this error. From looking at the Firefox
395 // implementation, it doesn't look like Firefox does this either.
397 return GetURLNotify(id, url, target, true, notify_data);
400 NPError NPN_GetURL(NPP id, const char* url, const char* target) {
402 // Request from the Plugin to fetch content either for the plugin
403 // or to be placed into a browser window.
405 // If target == null, the browser fetches content and streams to plugin.
406 // otherwise, the browser loads content into an existing browser frame.
407 // If the target is the window/frame containing the plugin, the plugin
409 // If the target is _blank, a mailto: or news: url open content in a new
411 // If the target is _self, no other instance of the plugin is created. The
412 // plugin continues to operate in its own window
414 return GetURLNotify(id, url, target, false, 0);
417 // Generic form of PostURL for common code between PostURL and PostURLNotify.
418 static NPError PostURLNotify(NPP id,
427 return NPERR_INVALID_URL;
429 scoped_refptr<PluginInstance> plugin(FindInstance(id));
432 return NPERR_GENERIC_ERROR;
435 std::string post_file_contents;
438 // Post data to be uploaded from a file. This can be handled in two
440 // 1. Read entire file and send the contents as if it was a post data
441 // specified in the argument
442 // 2. Send just the file details and read them in the browser at the
443 // time of sending the request.
444 // Approach 2 is more efficient but complicated. Approach 1 has a major
445 // drawback of sending potentially large data over two IPC hops. In a way
446 // 'large data over IPC' problem exists as it is in case of plugin giving
447 // the data directly instead of in a file.
448 // Currently we are going with the approach 1 to get the feature working.
449 // We can optimize this later with approach 2.
451 // TODO(joshia): Design a scheme to send a file descriptor instead of
452 // entire file contents across.
456 // Here we are blindly uploading whatever file requested by a plugin.
457 // This is risky as someone could exploit a plugin to send private
458 // data in arbitrary locations.
459 // A malicious (non-sandboxed) plugin has unfeterred access to OS
460 // resources and can do this anyway without using browser's HTTP stack.
461 // FWIW, Firefox and Safari don't perform any security checks.
464 return NPERR_FILE_NOT_FOUND;
466 std::string file_path_ascii(buf);
467 base::FilePath file_path;
468 static const char kFileUrlPrefix[] = "file:";
469 if (StartsWithASCII(file_path_ascii, kFileUrlPrefix, false)) {
470 GURL file_url(file_path_ascii);
471 DCHECK(file_url.SchemeIsFile());
472 net::FileURLToFilePath(file_url, &file_path);
474 file_path = base::FilePath::FromUTF8Unsafe(file_path_ascii);
477 base::File::Info post_file_info;
478 if (!base::GetFileInfo(file_path, &post_file_info) ||
479 post_file_info.is_directory)
480 return NPERR_FILE_NOT_FOUND;
482 if (!base::ReadFileToString(file_path, &post_file_contents))
483 return NPERR_FILE_NOT_FOUND;
485 buf = post_file_contents.c_str();
486 len = post_file_contents.size();
489 // The post data sent by a plugin contains both headers
490 // and post data. Example:
491 // Content-type: text/html
492 // Content-length: 200
494 // <200 bytes of content here>
496 // Unfortunately, our stream needs these broken apart,
497 // so we need to parse the data and set headers and data
499 plugin->RequestURL(url, "POST", target, buf, len, notify, notify_data);
500 return NPERR_NO_ERROR;
503 NPError NPN_PostURLNotify(NPP id,
510 return PostURLNotify(id, url, target, len, buf, file, true, notify_data);
513 NPError NPN_PostURL(NPP id,
519 // POSTs data to an URL, either from a temp file or a buffer.
520 // If file is true, buf contains a temp file (which host will delete after
521 // completing), and len contains the length of the filename.
522 // If file is false, buf contains the data to send, and len contains the
523 // length of the buffer
525 // If target is null,
526 // server response is returned to the plugin
527 // If target is _current, _self, or _top,
528 // server response is written to the plugin window and plugin is unloaded.
529 // If target is _new or _blank,
530 // server response is written to a new browser window
531 // If target is an existing frame,
532 // server response goes to that frame.
534 // For protocols other than FTP
535 // file uploads must be line-end converted from \r\n to \n
537 // Note: you cannot specify headers (even a blank line) in a memory buffer,
538 // use NPN_PostURLNotify
540 return PostURLNotify(id, url, target, len, buf, file, false, 0);
543 NPError NPN_NewStream(NPP id,
547 // Requests creation of a new data stream produced by the plugin,
548 // consumed by the browser.
550 // Browser should put this stream into a window target.
552 // TODO: implement me
553 DVLOG(1) << "NPN_NewStream is not implemented yet.";
554 return NPERR_GENERIC_ERROR;
557 int32_t NPN_Write(NPP id, NPStream* stream, int32_t len, void* buffer) {
558 // Writes data to an existing Plugin-created stream.
560 // TODO: implement me
561 DVLOG(1) << "NPN_Write is not implemented yet.";
562 return NPERR_GENERIC_ERROR;
565 NPError NPN_DestroyStream(NPP id, NPStream* stream, NPReason reason) {
566 // Destroys a stream (could be created by plugin or browser).
569 // NPRES_DONE - normal completion
570 // NPRES_USER_BREAK - user terminated
571 // NPRES_NETWORK_ERROR - network error (all errors fit here?)
575 scoped_refptr<PluginInstance> plugin(FindInstance(id));
576 if (plugin.get() == NULL) {
578 return NPERR_GENERIC_ERROR;
581 return plugin->NPP_DestroyStream(stream, reason);
584 const char* NPN_UserAgent(NPP id) {
586 // Flash passes in a null id during the NP_initialize call. We need to
587 // default to the Mozilla user agent if we don't have an NPP instance or
588 // else Flash won't request windowless mode.
589 bool use_mozilla_user_agent = true;
591 scoped_refptr<PluginInstance> plugin = FindInstance(id);
592 if (plugin.get() && !plugin->use_mozilla_user_agent())
593 use_mozilla_user_agent = false;
596 if (use_mozilla_user_agent)
597 return "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) "
598 "Gecko/20061103 Firefox/2.0a1";
601 return webkit_glue::GetUserAgent(GURL()).c_str();
604 void NPN_Status(NPP id, const char* message) {
605 // Displays a message on the status line of the browser window.
607 // TODO: implement me
608 DVLOG(1) << "NPN_Status is not implemented yet.";
611 void NPN_InvalidateRect(NPP id, NPRect *invalidRect) {
612 // Invalidates specified drawing area prior to repainting or refreshing a
615 // Before a windowless plugin can refresh part of its drawing area, it must
616 // first invalidate it. This function causes the NPP_HandleEvent method to
617 // pass an update event or a paint message to the plug-in. After calling
618 // this method, the plug-in recieves a paint message asynchronously.
620 // The browser redraws invalid areas of the document and any windowless
621 // plug-ins at regularly timed intervals. To force a paint message, the
622 // plug-in can call NPN_ForceRedraw after calling this method.
624 scoped_refptr<PluginInstance> plugin(FindInstance(id));
625 if (plugin.get() && plugin->webplugin()) {
628 if (!plugin->windowless()) {
630 rect.left = invalidRect->left;
631 rect.right = invalidRect->right;
632 rect.top = invalidRect->top;
633 rect.bottom = invalidRect->bottom;
634 ::InvalidateRect(plugin->window_handle(), &rect, false);
638 gfx::Rect rect(invalidRect->left,
640 invalidRect->right - invalidRect->left,
641 invalidRect->bottom - invalidRect->top);
642 plugin->webplugin()->InvalidateRect(rect);
644 plugin->webplugin()->Invalidate();
649 void NPN_InvalidateRegion(NPP id, NPRegion invalidRegion) {
650 // Invalidates a specified drawing region prior to repainting
651 // or refreshing a window-less plugin.
653 // Similar to NPN_InvalidateRect.
655 // TODO: this is overkill--add platform-specific region handling (at the
656 // very least, fetch the region's bounding box and pass it to InvalidateRect).
657 scoped_refptr<PluginInstance> plugin(FindInstance(id));
658 DCHECK(plugin.get() != NULL);
659 if (plugin.get() && plugin->webplugin())
660 plugin->webplugin()->Invalidate();
663 void NPN_ForceRedraw(NPP id) {
664 // Forces repaint for a windowless plug-in.
666 // We deliberately do not implement this; we don't want plugins forcing
667 // synchronous paints.
670 NPError NPN_GetValue(NPP id, NPNVariable variable, void* value) {
671 // Allows the plugin to query the browser for information
674 // NPNVxDisplay (unix only)
675 // NPNVxtAppContext (unix only)
676 // NPNVnetscapeWindow (win only) - Gets the native window on which the
677 // plug-in drawing occurs, returns HWND
678 // NPNVjavascriptEnabledBool: tells whether Javascript is enabled
679 // NPNVasdEnabledBool: tells whether SmartUpdate is enabled
680 // NPNVOfflineBool: tells whether offline-mode is enabled
682 NPError rv = NPERR_GENERIC_ERROR;
684 switch (static_cast<int>(variable)) {
685 case NPNVWindowNPObject: {
686 scoped_refptr<PluginInstance> plugin(FindInstance(id));
689 return NPERR_INVALID_INSTANCE_ERROR;
691 NPObject *np_object = plugin->webplugin()->GetWindowScriptNPObject();
692 // Return value is expected to be retained, as
694 // <http://www.mozilla.org/projects/plugins/npruntime.html#browseraccess>
696 WebBindings::retainObject(np_object);
697 void **v = (void **)value;
705 case NPNVPluginElementNPObject: {
706 scoped_refptr<PluginInstance> plugin(FindInstance(id));
709 return NPERR_INVALID_INSTANCE_ERROR;
711 NPObject *np_object = plugin->webplugin()->GetPluginElement();
712 // Return value is expected to be retained, as
714 // <http://www.mozilla.org/projects/plugins/npruntime.html#browseraccess>
716 WebBindings::retainObject(np_object);
717 void** v = static_cast<void**>(value);
725 #if !defined(OS_MACOSX) // OS X doesn't have windowed plugins.
726 case NPNVnetscapeWindow: {
727 scoped_refptr<PluginInstance> plugin = FindInstance(id);
730 return NPERR_INVALID_INSTANCE_ERROR;
732 gfx::PluginWindowHandle handle = plugin->window_handle();
733 *((void**)value) = (void*)handle;
738 case NPNVjavascriptEnabledBool: {
739 // yes, JS is enabled.
740 *((void**)value) = (void*)1;
744 #if defined(TOOLKIT_GTK)
746 // Tell them we are GTK2. (The alternative is GTK 1.2.)
747 *reinterpret_cast<int*>(value) = NPNVGtk2;
751 case NPNVSupportsXEmbedBool:
752 *reinterpret_cast<NPBool*>(value) = true;
756 case NPNVSupportsWindowless: {
757 NPBool* supports_windowless = reinterpret_cast<NPBool*>(value);
758 *supports_windowless = true;
762 case NPNVprivateModeBool: {
763 NPBool* private_mode = reinterpret_cast<NPBool*>(value);
764 scoped_refptr<PluginInstance> plugin(FindInstance(id));
767 return NPERR_INVALID_INSTANCE_ERROR;
769 *private_mode = plugin->webplugin()->IsOffTheRecord();
773 #if defined(OS_MACOSX)
774 case NPNVpluginDrawingModel: {
775 // return the drawing model that was negotiated when we initialized.
776 scoped_refptr<PluginInstance> plugin(FindInstance(id));
779 return NPERR_INVALID_INSTANCE_ERROR;
781 *reinterpret_cast<int*>(value) = plugin->drawing_model();
785 case NPNVsupportsCoreGraphicsBool:
786 case NPNVsupportsCocoaBool: {
787 // These drawing and event models are always supported.
788 NPBool* supports_model = reinterpret_cast<NPBool*>(value);
789 *supports_model = true;
793 case NPNVsupportsInvalidatingCoreAnimationBool:
794 case NPNVsupportsCoreAnimationBool: {
795 NPBool* supports_model = reinterpret_cast<NPBool*>(value);
796 *supports_model = content::SupportsCoreAnimationPlugins();
801 case NPNVsupportsCarbonBool:
803 #ifndef NP_NO_QUICKDRAW
804 case NPNVsupportsQuickDrawBool:
806 case NPNVsupportsOpenGLBool: {
807 // These models are never supported. OpenGL was never widely supported,
808 // and QuickDraw and Carbon have been deprecated for quite some time.
809 NPBool* supports_model = reinterpret_cast<NPBool*>(value);
810 *supports_model = false;
814 case NPNVsupportsCompositingCoreAnimationPluginsBool: {
815 NPBool* supports_compositing = reinterpret_cast<NPBool*>(value);
816 *supports_compositing = content::SupportsCoreAnimationPlugins();
820 case NPNVsupportsUpdatedCocoaTextInputBool: {
821 // We support the clarifications to the Cocoa IME event spec.
822 NPBool* supports_update = reinterpret_cast<NPBool*>(value);
823 *supports_update = true;
829 DVLOG(1) << "NPN_GetValue(" << variable << ") is not implemented yet.";
835 NPError NPN_SetValue(NPP id, NPPVariable variable, void* value) {
836 // Allows the plugin to set various modes
838 scoped_refptr<PluginInstance> plugin(FindInstance(id));
841 return NPERR_INVALID_INSTANCE_ERROR;
844 case NPPVpluginWindowBool: {
845 // Sets windowless mode for display of the plugin
846 // Note: the documentation at
847 // http://developer.mozilla.org/en/docs/NPN_SetValue is wrong. When
848 // value is NULL, the mode is set to true. This is the same way Mozilla
850 plugin->set_windowless(value == 0);
851 return NPERR_NO_ERROR;
853 case NPPVpluginTransparentBool: {
854 // Sets transparent mode for display of the plugin
856 // Transparent plugins require the browser to paint the background
857 // before having the plugin paint. By default, windowless plugins
858 // are transparent. Making a windowless plugin opaque means that
859 // the plugin does not require the browser to paint the background.
860 bool mode = (value != 0);
861 plugin->set_transparent(mode);
862 return NPERR_NO_ERROR;
864 case NPPVjavascriptPushCallerBool:
865 // Specifies whether you are pushing or popping the JSContext off.
867 // TODO: implement me
868 DVLOG(1) << "NPN_SetValue(NPPVJavascriptPushCallerBool) is not "
870 return NPERR_GENERIC_ERROR;
871 case NPPVpluginKeepLibraryInMemory:
872 // Tells browser that plugin library should live longer than usual.
873 // TODO: implement me
874 DVLOG(1) << "NPN_SetValue(NPPVpluginKeepLibraryInMemory) is not "
876 return NPERR_GENERIC_ERROR;
877 #if defined(OS_MACOSX)
878 case NPPVpluginDrawingModel: {
879 intptr_t model = reinterpret_cast<intptr_t>(value);
880 if (model == NPDrawingModelCoreGraphics ||
881 ((model == NPDrawingModelInvalidatingCoreAnimation ||
882 model == NPDrawingModelCoreAnimation) &&
883 content::SupportsCoreAnimationPlugins())) {
884 plugin->set_drawing_model(static_cast<NPDrawingModel>(model));
885 return NPERR_NO_ERROR;
887 return NPERR_GENERIC_ERROR;
889 case NPPVpluginEventModel: {
890 // Only the Cocoa event model is supported.
891 intptr_t model = reinterpret_cast<intptr_t>(value);
892 if (model == NPEventModelCocoa) {
893 plugin->set_event_model(static_cast<NPEventModel>(model));
894 return NPERR_NO_ERROR;
896 return NPERR_GENERIC_ERROR;
900 // TODO: implement me
901 DVLOG(1) << "NPN_SetValue(" << variable << ") is not implemented.";
906 return NPERR_GENERIC_ERROR;
909 void* NPN_GetJavaEnv() {
910 // TODO: implement me
911 DVLOG(1) << "NPN_GetJavaEnv is not implemented.";
915 void* NPN_GetJavaPeer(NPP) {
916 // TODO: implement me
917 DVLOG(1) << "NPN_GetJavaPeer is not implemented.";
921 void NPN_PushPopupsEnabledState(NPP id, NPBool enabled) {
922 scoped_refptr<PluginInstance> plugin(FindInstance(id));
924 plugin->PushPopupsEnabledState(enabled ? true : false);
927 void NPN_PopPopupsEnabledState(NPP id) {
928 scoped_refptr<PluginInstance> plugin(FindInstance(id));
930 plugin->PopPopupsEnabledState();
933 void NPN_PluginThreadAsyncCall(NPP id,
936 scoped_refptr<PluginInstance> plugin(FindInstance(id));
938 plugin->PluginThreadAsyncCall(func, user_data);
941 NPError NPN_GetValueForURL(NPP id,
942 NPNURLVariable variable,
947 return NPERR_INVALID_PARAM;
949 if (!url || !*url || !len)
950 return NPERR_INVALID_URL;
958 scoped_refptr<PluginInstance> plugin(FindInstance(id));
960 return NPERR_GENERIC_ERROR;
962 WebPlugin* webplugin = plugin->webplugin();
964 return NPERR_GENERIC_ERROR;
966 if (!webplugin->FindProxyForUrl(GURL(std::string(url)), &result))
967 return NPERR_GENERIC_ERROR;
970 case NPNURLVCookie: {
971 scoped_refptr<PluginInstance> plugin(FindInstance(id));
973 return NPERR_GENERIC_ERROR;
975 WebPlugin* webplugin = plugin->webplugin();
977 return NPERR_GENERIC_ERROR;
979 // Bypass third-party cookie blocking by using the url as the
980 // first_party_for_cookies.
981 GURL cookies_url((std::string(url)));
982 result = webplugin->GetCookies(cookies_url, cookies_url);
986 return NPERR_GENERIC_ERROR;
989 // Allocate this using the NPAPI allocator. The plugin will call
990 // NPN_Free to free this.
991 *value = static_cast<char*>(NPN_MemAlloc(result.length() + 1));
992 base::strlcpy(*value, result.c_str(), result.length() + 1);
993 *len = result.length();
995 return NPERR_NO_ERROR;
998 NPError NPN_SetValueForURL(NPP id,
999 NPNURLVariable variable,
1004 return NPERR_INVALID_PARAM;
1007 return NPERR_INVALID_URL;
1010 case NPNURLVCookie: {
1011 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1013 return NPERR_GENERIC_ERROR;
1015 WebPlugin* webplugin = plugin->webplugin();
1017 return NPERR_GENERIC_ERROR;
1019 std::string cookie(value, len);
1020 GURL cookies_url((std::string(url)));
1021 webplugin->SetCookie(cookies_url, cookies_url, cookie);
1022 return NPERR_NO_ERROR;
1025 // We don't support setting proxy values, fall through...
1028 // Fall through and return an error...
1032 return NPERR_GENERIC_ERROR;
1035 NPError NPN_GetAuthenticationInfo(NPP id,
1036 const char* protocol,
1045 if (!id || !protocol || !host || !scheme || !realm || !username ||
1046 !ulen || !password || !plen)
1047 return NPERR_INVALID_PARAM;
1049 // TODO: implement me (bug 23928)
1050 return NPERR_GENERIC_ERROR;
1053 uint32_t NPN_ScheduleTimer(NPP id,
1056 void (*func)(NPP id, uint32_t timer_id)) {
1057 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1061 return plugin->ScheduleTimer(interval, repeat, func);
1064 void NPN_UnscheduleTimer(NPP id, uint32_t timer_id) {
1065 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1067 plugin->UnscheduleTimer(timer_id);
1070 NPError NPN_PopUpContextMenu(NPP id, NPMenu* menu) {
1072 return NPERR_INVALID_PARAM;
1074 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1076 return plugin->PopUpContextMenu(menu);
1079 return NPERR_GENERIC_ERROR;
1082 NPBool NPN_ConvertPoint(NPP id, double sourceX, double sourceY,
1083 NPCoordinateSpace sourceSpace,
1084 double *destX, double *destY,
1085 NPCoordinateSpace destSpace) {
1086 scoped_refptr<PluginInstance> plugin(FindInstance(id));
1088 return plugin->ConvertPoint(
1089 sourceX, sourceY, sourceSpace, destX, destY, destSpace);
1095 NPBool NPN_HandleEvent(NPP id, void *event, NPBool handled) {
1096 // TODO: Implement advanced key handling: http://crbug.com/46578
1101 NPBool NPN_UnfocusInstance(NPP id, NPFocusDirection direction) {
1102 // TODO: Implement advanced key handling: http://crbug.com/46578
1107 void NPN_URLRedirectResponse(NPP instance, void* notify_data, NPBool allow) {
1108 scoped_refptr<PluginInstance> plugin(FindInstance(instance));
1110 plugin->URLRedirectResponse(!!allow, notify_data);