Fixing bug 470993. Expose the remainder of the Java plugins XPCOM dependency through the NPAPI. r=joshmoz@gmail.com, sr=bzbarsky@mit.edu
authorJohnny Stenback <jst@mozilla.com>
Mon, 12 Jan 2009 16:55:52 -0800
changeset 23571 62e0965bc0e49bb9db5f82116c10a95ff8cdaba4
parent 23570 48b12c9bb824ce04b0d366ce85622e88afd74117
child 23572 caf24afe46eb914c5c85e83a5addcf92ae8699ec
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjoshmoz, bzbarsky
bugs470993
milestone1.9.2a1pre
Fixing bug 470993. Expose the remainder of the Java plugins XPCOM dependency through the NPAPI. r=joshmoz@gmail.com, sr=bzbarsky@mit.edu
modules/plugin/base/public/npapi.h
modules/plugin/base/public/npfunctions.h
modules/plugin/base/src/nsNPAPIPlugin.cpp
modules/plugin/base/src/nsNPAPIPlugin.h
--- a/modules/plugin/base/public/npapi.h
+++ b/modules/plugin/base/public/npapi.h
@@ -86,17 +86,17 @@
 # endif
 #endif
 
 /*----------------------------------------------------------------------*/
 /*                        Plugin Version Constants                      */
 /*----------------------------------------------------------------------*/
 
 #define NP_VERSION_MAJOR 0
-#define NP_VERSION_MINOR 20
+#define NP_VERSION_MINOR 21
 
 
 /* The OS/2 version of Netscape uses RC_DATA to define the
    mime types, file extensions, etc that are required.
    Use a vertical bar to separate types, end types with \0.
    FileVersion and ProductVersion are 32bit ints, all other
    entries are strings the MUST be terminated wwith a \0.
 
@@ -365,16 +365,21 @@ typedef enum {
   , NPNVpluginDrawingModel = 1000
 #ifndef NP_NO_QUICKDRAW
   , NPNVsupportsQuickDrawBool = 2000
 #endif
   , NPNVsupportsCoreGraphicsBool = 2001
 #endif
 } NPNVariable;
 
+typedef enum {
+  NPNURLVCookie = 501,
+  NPNURLVProxy
+} NPNURLVariable;
+
 /*
  * The type of Tookkit the widgets use
  */
 typedef enum {
   NPNVGtk12 = 1,
   NPNVGtk2
 } NPNToolkitType;
 
@@ -566,16 +571,17 @@ enum NPEventType {
 #define NPVERS_HAS_XPCONNECT_SCRIPTING      13
 #define NPVERS_HAS_NPRUNTIME_SCRIPTING      14
 #define NPVERS_HAS_FORM_VALUES              15
 #define NPVERS_HAS_POPUPS_ENABLED_STATE     16
 #define NPVERS_HAS_RESPONSE_HEADERS         17
 #define NPVERS_HAS_NPOBJECT_ENUM            18
 #define NPVERS_HAS_PLUGIN_THREAD_ASYNC_CALL 19
 #define NPVERS_HAS_ALL_NETWORK_STREAMS      20
+#define NPVERS_HAS_URL_AND_AUTH_INFO        21
 
 /*----------------------------------------------------------------------*/
 /*                        Function Prototypes                           */
 /*----------------------------------------------------------------------*/
 
 #if defined(__OS2__)
 #define NP_LOADDS _System
 #else
@@ -656,17 +662,32 @@ NPError     NP_LOADDS NPN_SetValue(NPP i
                                    void *value);
 void        NP_LOADDS NPN_InvalidateRect(NPP instance, NPRect *invalidRect);
 void        NP_LOADDS NPN_InvalidateRegion(NPP instance,
                                            NPRegion invalidRegion);
 void        NP_LOADDS NPN_ForceRedraw(NPP instance);
 void        NP_LOADDS NPN_PushPopupsEnabledState(NPP instance, NPBool enabled);
 void        NP_LOADDS NPN_PopPopupsEnabledState(NPP instance);
 void        NP_LOADDS NPN_PluginThreadAsyncCall(NPP instance,
-                                                void (*func) (void *), void *userData);
+                                                void (*func) (void *),
+                                                void *userData);
+NPError     NP_LOADDS NPN_GetValueForURL(NPP instance, NPNURLVariable variable,
+                                         const char *url, char **value,
+                                         uint32_t *len);
+NPError     NP_LOADDS NPN_SetValueForURL(NPP instance, NPNURLVariable variable,
+                                         const char *url, const char *value,
+                                         uint32_t len);
+NPError     NP_LOADDS NPN_GetAuthenticationInfo(NPP instance,
+                                                const char *protocol,
+                                                const char *host, int32_t port,
+                                                const char *scheme,
+                                                const char *realm,
+                                                char **username, uint32_t *ulen,
+                                                char **password,
+                                                uint32_t *plen);
 
 #ifdef __cplusplus
 }  /* end extern "C" */
 #endif
 
 #endif /* RC_INVOKED */
 #ifdef __OS2__
 #pragma pack()
--- a/modules/plugin/base/public/npfunctions.h
+++ b/modules/plugin/base/public/npfunctions.h
@@ -105,16 +105,19 @@ typedef bool         (*NPN_HasPropertyPr
 typedef bool         (*NPN_HasMethodProcPtr)(NPP npp, NPObject *obj, NPIdentifier propertyName);
 typedef void         (*NPN_ReleaseVariantValueProcPtr)(NPVariant *variant);
 typedef void         (*NPN_SetExceptionProcPtr)(NPObject *obj, const NPUTF8 *message);
 typedef bool         (*NPN_PushPopupsEnabledStateProcPtr)(NPP npp, NPBool enabled);
 typedef bool         (*NPN_PopPopupsEnabledStateProcPtr)(NPP npp);
 typedef bool         (*NPN_EnumerateProcPtr)(NPP npp, NPObject *obj, NPIdentifier **identifier, uint32_t *count);
 typedef void         (*NPN_PluginThreadAsyncCallProcPtr)(NPP instance, void (*func)(void *), void *userData);
 typedef bool         (*NPN_ConstructProcPtr)(NPP npp, NPObject* obj, const NPVariant *args, uint32_t argCount, NPVariant *result);
+typedef NPError      (*NPN_GetValueForURLPtr)(NPP npp, NPNURLVariable variable, const char *url, char **value, uint32_t *len);
+typedef NPError      (*NPN_SetValueForURLPtr)(NPP npp, NPNURLVariable variable, const char *url, const char *value, uint32_t len);
+typedef NPError      (*NPN_GetAuthenticationInfoPtr)(NPP npp, const char *protocol, const char *host, int32_t port, const char *scheme, const char *realm, char **username, uint32_t *ulen, char **password, uint32_t *plen);
 
 typedef struct _NPPluginFuncs {
   uint16_t size;
   uint16_t version;
   NPP_NewProcPtr newp;
   NPP_DestroyProcPtr destroy;
   NPP_SetWindowProcPtr setwindow;
   NPP_NewStreamProcPtr newstream;
@@ -173,16 +176,19 @@ typedef struct _NPNetscapeFuncs {
   NPN_HasMethodProcPtr hasmethod;
   NPN_ReleaseVariantValueProcPtr releasevariantvalue;
   NPN_SetExceptionProcPtr setexception;
   NPN_PushPopupsEnabledStateProcPtr pushpopupsenabledstate;
   NPN_PopPopupsEnabledStateProcPtr poppopupsenabledstate;
   NPN_EnumerateProcPtr enumerate;
   NPN_PluginThreadAsyncCallProcPtr pluginthreadasynccall;
   NPN_ConstructProcPtr construct;
+  NPN_GetValueForURLPtr getvalueforurl;
+  NPN_SetValueForURLPtr setvalueforurl;
+  NPN_GetAuthenticationInfoPtr getauthenticationinfo;
 } NPNetscapeFuncs;
 
 #ifdef XP_MACOSX
 /*
  * Mac OS X version(s) of NP_GetMIMEDescription(const char *)
  * These can be called to retreive MIME information from the plugin dynamically
  *
  * Note: For compatibility with Quicktime, BPSupportedMIMEtypes is another way
--- a/modules/plugin/base/src/nsNPAPIPlugin.cpp
+++ b/modules/plugin/base/src/nsNPAPIPlugin.cpp
@@ -80,16 +80,18 @@
 // needed for nppdf plugin
 #ifdef MOZ_WIDGET_GTK2
 #include <gdk/gdk.h>
 #include <gdk/gdkx.h>
 #include "gtk2xtbin.h"
 #endif
 
 #include "nsJSNPRuntime.h"
+#include "nsIHttpAuthManager.h"
+#include "nsICookieService.h"
 
 static PRLock *sPluginThreadAsyncCallLock = nsnull;
 static PRCList sPendingAsyncCalls = PR_INIT_STATIC_CLIST(&sPendingAsyncCalls);
 
 // POST/GET stream type
 enum eNPPStreamTypeInternal {
   eNPPStreamTypeInternal_Get,
   eNPPStreamTypeInternal_Post
@@ -152,27 +154,16 @@ PR_BEGIN_EXTERN_C
   _invalidaterect(NPP npp, NPRect *invalidRect);
 
   static void NP_CALLBACK
   _invalidateregion(NPP npp, NPRegion invalidRegion);
 
   static void NP_CALLBACK
   _forceredraw(NPP npp);
 
-  static void NP_CALLBACK
-  _pushpopupsenabledstate(NPP npp, NPBool enabled);
-
-  static void NP_CALLBACK
-  _poppopupsenabledstate(NPP npp);
-
-  typedef void(*PluginThreadCallback)(void *);
-  static void NP_CALLBACK
-  _pluginthreadasynccall(NPP instance, PluginThreadCallback func,
-                         void *userData);
-
   static const char* NP_CALLBACK
   _useragent(NPP npp);
 
   static void* NP_CALLBACK
   _memalloc (uint32_t size);
 
   // Deprecated entry points for the old Java plugin.
   static void* NP_CALLBACK /* OJI type: JRIEnv* */
@@ -254,16 +245,19 @@ nsNPAPIPlugin::CheckClassInitialized(voi
   CALLBACKS.hasmethod = ((NPN_HasMethodProcPtr)_hasmethod);
   CALLBACKS.enumerate = ((NPN_EnumerateProcPtr)_enumerate);
   CALLBACKS.construct = ((NPN_ConstructProcPtr)_construct);
   CALLBACKS.releasevariantvalue = ((NPN_ReleaseVariantValueProcPtr)_releasevariantvalue);
   CALLBACKS.setexception = ((NPN_SetExceptionProcPtr)_setexception);
   CALLBACKS.pushpopupsenabledstate = ((NPN_PushPopupsEnabledStateProcPtr)_pushpopupsenabledstate);
   CALLBACKS.poppopupsenabledstate = ((NPN_PopPopupsEnabledStateProcPtr)_poppopupsenabledstate);
   CALLBACKS.pluginthreadasynccall = ((NPN_PluginThreadAsyncCallProcPtr)_pluginthreadasynccall);
+  CALLBACKS.getvalueforurl = ((NPN_GetValueForURLPtr)_getvalueforurl);
+  CALLBACKS.setvalueforurl = ((NPN_SetValueForURLPtr)_setvalueforurl);
+  CALLBACKS.getauthenticationinfo = ((NPN_GetAuthenticationInfoPtr)_getauthenticationinfo);
 
   if (!sPluginThreadAsyncCallLock)
     sPluginThreadAsyncCallLock = nsAutoLock::NewLock("sPluginThreadAsyncCallLock");
 
   initialized = PR_TRUE;
 
   NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,("NPN callbacks initialized\n"));
 }
@@ -2394,16 +2388,160 @@ void NP_CALLBACK
   nsRefPtr<nsPluginThreadRunnable> evt =
     new nsPluginThreadRunnable(instance, func, userData);
 
   if (evt && evt->IsValid()) {
     NS_DispatchToMainThread(evt);
   }
 }
 
+NPError NP_CALLBACK
+_getvalueforurl(NPP instance, NPNURLVariable variable, const char *url,
+                char **value, uint32_t *len)
+{
+  if (!instance) {
+    return NPERR_INVALID_PARAM;
+  }
+
+  if (!url || !*url || !len) {
+    return NPERR_INVALID_URL;
+  }
+
+  *len = 0;
+
+  switch (variable) {
+  case NPNURLVProxy:
+    {
+      nsCOMPtr<nsIPluginManager2> pm(do_GetService(kPluginManagerCID));
+
+      if (pm && NS_SUCCEEDED(pm->FindProxyForURL(url, value))) {
+        *len = *value ? PL_strlen(*value) : 0;
+        return NPERR_NO_ERROR;
+      }
+      break;
+    }
+  case NPNURLVCookie:
+    {
+      nsCOMPtr<nsICookieService> cookieService =
+        do_GetService(NS_COOKIESERVICE_CONTRACTID);
+
+      if (!cookieService)
+        return NPERR_GENERIC_ERROR;
+
+      // Make an nsURI from the url argument
+      nsCOMPtr<nsIURI> uri;
+      if (NS_FAILED(NS_NewURI(getter_AddRefs(uri), nsDependentCString(url)))) {
+        return NPERR_GENERIC_ERROR;
+      }
+
+      nsXPIDLCString cookieStr;
+      if (NS_FAILED(cookieService->GetCookieString(uri, nsnull,
+                                                   getter_Copies(cookieStr))) ||
+          !cookieStr) {
+        return NPERR_GENERIC_ERROR;
+      }
+
+      *value = PL_strndup(cookieStr, cookieStr.Length());
+
+      if (*value) {
+        *len = cookieStr.Length();
+
+        return NPERR_NO_ERROR;
+      }
+    }
+
+    break;
+  default:
+    // Fall through and return an error...
+    ;
+  }
+
+  return NPERR_GENERIC_ERROR;
+}
+
+NPError NP_CALLBACK
+_setvalueforurl(NPP instance, NPNURLVariable variable, const char *url,
+                const char *value, uint32_t len)
+{
+  if (!instance) {
+    return NPERR_INVALID_PARAM;
+  }
+
+  if (!url || !*url) {
+    return NPERR_INVALID_URL;
+  }
+
+  switch (variable) {
+  case NPNURLVCookie:
+    {
+      nsCOMPtr<nsICookieStorage> cs = do_GetService(kPluginManagerCID);
+
+      if (cs && NS_SUCCEEDED(cs->SetCookie(url, value, len))) {
+        return NPERR_NO_ERROR;
+      }
+    }
+
+    break;
+  case NPNURLVProxy:
+    // We don't support setting proxy values, fall through...
+  default:
+    // Fall through and return an error...
+    ;
+  }
+
+  return NPERR_GENERIC_ERROR;
+}
+
+NPError NP_CALLBACK
+_getauthenticationinfo(NPP instance, const char *protocol, const char *host,
+                       int32_t port, const char *scheme, const char *realm,
+                       char **username, uint32_t *ulen, char **password,
+                       uint32_t *plen)
+{
+  if (!instance || !protocol || !host || !scheme || !realm || !username ||
+      !ulen || !password || !plen)
+    return NPERR_INVALID_PARAM;
+
+  *username = nsnull;
+  *password = nsnull;
+  *ulen = 0;
+  *plen = 0;
+
+  nsDependentCString proto(protocol);
+
+  if (!proto.LowerCaseEqualsLiteral("http") &&
+      !proto.LowerCaseEqualsLiteral("https"))
+    return NPERR_GENERIC_ERROR;
+
+  nsCOMPtr<nsIHttpAuthManager> authManager =
+    do_GetService("@mozilla.org/network/http-auth-manager;1");
+  if (!authManager)
+    return NPERR_GENERIC_ERROR;
+
+  nsAutoString unused, uname16, pwd16;
+  if (NS_FAILED(authManager->GetAuthIdentity(proto, nsDependentCString(host),
+                                             port, nsDependentCString(scheme),
+                                             nsDependentCString(realm),
+                                             EmptyCString(), unused, uname16,
+                                             pwd16))) {
+    return NPERR_GENERIC_ERROR;
+  }
+
+  NS_ConvertUTF16toUTF8 uname8(uname16);
+  NS_ConvertUTF16toUTF8 pwd8(pwd16);
+
+  *username = ToNewCString(uname8);
+  *ulen = *username ? uname8.Length() : 0;
+
+  *password = ToNewCString(pwd8);
+  *plen = *password ? pwd8.Length() : 0;
+
+  return NPERR_NO_ERROR;
+}
+
 void
 OnPluginDestroy(NPP instance)
 {
   if (!sPluginThreadAsyncCallLock) {
     return;
   }
 
   {
@@ -2451,8 +2589,9 @@ void
 ExitAsyncPluginThreadCallLock()
 {
   if (sPluginThreadAsyncCallLock) {
     PR_Unlock(sPluginThreadAsyncCallLock);
   }
 }
 
 NPP NPPStack::sCurrentNPP = nsnull;
+
--- a/modules/plugin/base/src/nsNPAPIPlugin.h
+++ b/modules/plugin/base/src/nsNPAPIPlugin.h
@@ -185,16 +185,40 @@ bool NP_CALLBACK
            uint32_t argCount, NPVariant *result);
 
 void NP_CALLBACK
 _releasevariantvalue(NPVariant *variant);
 
 void NP_CALLBACK
 _setexception(NPObject* npobj, const NPUTF8 *message);
 
+void NP_CALLBACK
+_pushpopupsenabledstate(NPP npp, NPBool enabled);
+
+void NP_CALLBACK
+_poppopupsenabledstate(NPP npp);
+
+typedef void(*PluginThreadCallback)(void *);
+void NP_CALLBACK
+_pluginthreadasynccall(NPP instance, PluginThreadCallback func,
+                       void *userData);
+
+NPError NP_CALLBACK
+_getvalueforurl(NPP instance, NPNURLVariable variable, const char *url,
+                char **value, uint32_t *len);
+NPError NP_CALLBACK
+_setvalueforurl(NPP instance, NPNURLVariable variable, const char *url,
+                const char *value, uint32_t len);
+
+NPError NP_CALLBACK
+_getauthenticationinfo(NPP instance, const char *protocol, const char *host,
+                       int32_t port, const char *scheme, const char *realm,
+                       char **username, uint32_t *ulen, char **password,
+                       uint32_t *plen);
+
 PR_END_EXTERN_C
 
 const char *
 PeekException();
 
 void
 PopException();