home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / plugins / common / npunix.c
Encoding:
C/C++ Source or Header  |  1998-04-08  |  11.8 KB  |  425 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * npunix.c
  21.  *
  22.  * Netscape Client Plugin API
  23.  * - Wrapper function to interface with the Netscape Navigator
  24.  *
  25.  * dp Suresh <dp@netscape.com>
  26.  *
  27.  *----------------------------------------------------------------------
  28.  * PLUGIN DEVELOPERS:
  29.  *    YOU WILL NOT NEED TO EDIT THIS FILE.
  30.  *----------------------------------------------------------------------
  31.  */
  32.  
  33. #define XP_UNIX 1
  34.  
  35. #include <stdio.h>
  36. #include "npapi.h"
  37. #include "npupp.h"
  38.  
  39. /*
  40.  * Define PLUGIN_TRACE to have the wrapper functions print
  41.  * messages to stderr whenever they are called.
  42.  */
  43.  
  44. #ifdef PLUGIN_TRACE
  45. #include <stdio.h>
  46. #define PLUGINDEBUGSTR(msg)    fprintf(stderr, "%s\n", msg)
  47. #else
  48. #define PLUGINDEBUGSTR(msg)
  49. #endif
  50.  
  51.  
  52. /***********************************************************************
  53.  *
  54.  * Globals
  55.  *
  56.  ***********************************************************************/
  57.  
  58. static NPNetscapeFuncs   gNetscapeFuncs;    /* Netscape Function table */
  59.  
  60.  
  61. /***********************************************************************
  62.  *
  63.  * Wrapper functions : plugin calling Netscape Navigator
  64.  *
  65.  * These functions let the plugin developer just call the APIs
  66.  * as documented and defined in npapi.h, without needing to know
  67.  * about the function table and call macros in npupp.h.
  68.  *
  69.  ***********************************************************************/
  70.  
  71. void
  72. NPN_Version(int* plugin_major, int* plugin_minor,
  73.          int* netscape_major, int* netscape_minor)
  74. {
  75.     *plugin_major = NP_VERSION_MAJOR;
  76.     *plugin_minor = NP_VERSION_MINOR;
  77.  
  78.     /* Major version is in high byte */
  79.     *netscape_major = gNetscapeFuncs.version >> 8;
  80.     /* Minor version is in low byte */
  81.     *netscape_minor = gNetscapeFuncs.version & 0xFF;
  82. }
  83.  
  84. NPError
  85. NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
  86. {
  87.     return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
  88.                     instance, variable, r_value);
  89. }
  90.  
  91. NPError
  92. NPN_GetURL(NPP instance, const char* url, const char* window)
  93. {
  94.     return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
  95. }
  96.  
  97. NPError
  98. NPN_PostURL(NPP instance, const char* url, const char* window,
  99.          uint32 len, const char* buf, NPBool file)
  100. {
  101.     return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
  102.                     url, window, len, buf, file);
  103. }
  104.  
  105. NPError
  106. NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
  107. {
  108.     return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
  109.                     stream, rangeList);
  110. }
  111.  
  112. NPError
  113. NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
  114.           NPStream** stream_ptr)
  115. {
  116.     return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
  117.                     type, window, stream_ptr);
  118. }
  119.  
  120. int32
  121. NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
  122. {
  123.     return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
  124.                     stream, len, buffer);
  125. }
  126.  
  127. NPError
  128. NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
  129. {
  130.     return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
  131.                         instance, stream, reason);
  132. }
  133.  
  134. void
  135. NPN_Status(NPP instance, const char* message)
  136. {
  137.     CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
  138. }
  139.  
  140. const char*
  141. NPN_UserAgent(NPP instance)
  142. {
  143.     return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
  144. }
  145.  
  146. void*
  147. NPN_MemAlloc(uint32 size)
  148. {
  149.     return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
  150. }
  151.  
  152. void NPN_MemFree(void* ptr)
  153. {
  154.     CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
  155. }
  156.  
  157. uint32 NPN_MemFlush(uint32 size)
  158. {
  159.     return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
  160. }
  161.  
  162. void NPN_ReloadPlugins(NPBool reloadPages)
  163. {
  164.     CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
  165. }
  166.  
  167. JRIEnv* NPN_GetJavaEnv()
  168. {
  169.     return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
  170. }
  171.  
  172. jref NPN_GetJavaPeer(NPP instance)
  173. {
  174.     return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
  175.                        instance);
  176. }
  177.  
  178.  
  179. /***********************************************************************
  180.  *
  181.  * Wrapper functions : Netscape Navigator -> plugin
  182.  *
  183.  * These functions let the plugin developer just create the APIs
  184.  * as documented and defined in npapi.h, without needing to 
  185.  * install those functions in the function table or worry about
  186.  * setting up globals for 68K plugins.
  187.  *
  188.  ***********************************************************************/
  189.  
  190. NPError
  191. Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
  192.         int16 argc, char* argn[], char* argv[], NPSavedData* saved)
  193. {
  194.     NPError ret;
  195.     PLUGINDEBUGSTR("New");
  196.     ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
  197.     return ret;    
  198. }
  199.  
  200. NPError
  201. Private_Destroy(NPP instance, NPSavedData** save)
  202. {
  203.     PLUGINDEBUGSTR("Destroy");
  204.     return NPP_Destroy(instance, save);
  205. }
  206.  
  207. NPError
  208. Private_SetWindow(NPP instance, NPWindow* window)
  209. {
  210.     NPError err;
  211.     PLUGINDEBUGSTR("SetWindow");
  212.     err = NPP_SetWindow(instance, window);
  213.     return err;
  214. }
  215.  
  216. NPError
  217. Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
  218.             NPBool seekable, uint16* stype)
  219. {
  220.     NPError err;
  221.     PLUGINDEBUGSTR("NewStream");
  222.     err = NPP_NewStream(instance, type, stream, seekable, stype);
  223.     return err;
  224. }
  225.  
  226. int32
  227. Private_WriteReady(NPP instance, NPStream* stream)
  228. {
  229.     unsigned int result;
  230.     PLUGINDEBUGSTR("WriteReady");
  231.     result = NPP_WriteReady(instance, stream);
  232.     return result;
  233. }
  234.  
  235. int32
  236. Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
  237.         void* buffer)
  238. {
  239.     unsigned int result;
  240.     PLUGINDEBUGSTR("Write");
  241.     result = NPP_Write(instance, stream, offset, len, buffer);
  242.     return result;
  243. }
  244.  
  245. void
  246. Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
  247. {
  248.     PLUGINDEBUGSTR("StreamAsFile");
  249.     NPP_StreamAsFile(instance, stream, fname);
  250. }
  251.  
  252.  
  253. NPError
  254. Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
  255. {
  256.     NPError err;
  257.     PLUGINDEBUGSTR("DestroyStream");
  258.     err = NPP_DestroyStream(instance, stream, reason);
  259.     return err;
  260. }
  261.  
  262.  
  263. void
  264. Private_Print(NPP instance, NPPrint* platformPrint)
  265. {
  266.     PLUGINDEBUGSTR("Print");
  267.     NPP_Print(instance, platformPrint);
  268. }
  269.  
  270. JRIGlobalRef
  271. Private_GetJavaClass(void)
  272. {
  273.     jref clazz = NPP_GetJavaClass();
  274.     if (clazz) {
  275.     JRIEnv* env = NPN_GetJavaEnv();
  276.     return JRI_NewGlobalRef(env, clazz);
  277.     }
  278.     return NULL;
  279. }
  280.  
  281. /*********************************************************************** 
  282.  *
  283.  * These functions are located automagically by netscape.
  284.  *
  285.  ***********************************************************************/
  286.  
  287. /*
  288.  * NP_GetMIMEDescription
  289.  *    - Netscape needs to know about this symbol
  290.  *    - Netscape uses the return value to identify when an object instance
  291.  *      of this plugin should be created.
  292.  */
  293. char *
  294. NP_GetMIMEDescription(void)
  295. {
  296.     return NPP_GetMIMEDescription();
  297. }
  298.  
  299. /*
  300.  * NP_GetValue [optional]
  301.  *    - Netscape needs to know about this symbol.
  302.  *    - Interfaces with plugin to get values for predefined variables
  303.  *      that the navigator needs.
  304.  */
  305. NPError
  306. NP_GetValue(void *future, NPPVariable variable, void *value)
  307. {
  308.     return NPP_GetValue(future, variable, value);
  309. }
  310.  
  311. /*
  312.  * NP_Initialize
  313.  *    - Netscape needs to know about this symbol.
  314.  *    - It calls this function after looking up its symbol before it
  315.  *      is about to create the first ever object of this kind.
  316.  *
  317.  * PARAMETERS
  318.  *    nsTable    - The netscape function table. If developers just use these
  319.  *          wrappers, they dont need to worry about all these function
  320.  *          tables.
  321.  * RETURN
  322.  *    pluginFuncs
  323.  *        - This functions needs to fill the plugin function table
  324.  *          pluginFuncs and return it. Netscape Navigator plugin
  325.  *          library will use this function table to call the plugin.
  326.  *
  327.  */
  328. NPError
  329. NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
  330. {
  331.     NPError err = NPERR_NO_ERROR;
  332.  
  333.     PLUGINDEBUGSTR("NP_Initialize");
  334.     
  335.     /* validate input parameters */
  336.  
  337.     if ((nsTable == NULL) || (pluginFuncs == NULL))
  338.         err = NPERR_INVALID_FUNCTABLE_ERROR;
  339.     
  340.     /*
  341.      * Check the major version passed in Netscape's function table.
  342.      * We won't load if the major version is newer than what we expect.
  343.      * Also check that the function tables passed in are big enough for
  344.      * all the functions we need (they could be bigger, if Netscape added
  345.      * new APIs, but that's OK with us -- we'll just ignore them).
  346.      *
  347.      */
  348.  
  349.     if (err == NPERR_NO_ERROR) {
  350.         if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
  351.             err = NPERR_INCOMPATIBLE_VERSION_ERROR;
  352.         if (nsTable->size < sizeof(NPNetscapeFuncs))
  353.             err = NPERR_INVALID_FUNCTABLE_ERROR;
  354.         if (pluginFuncs->size < sizeof(NPPluginFuncs))        
  355.             err = NPERR_INVALID_FUNCTABLE_ERROR;
  356.     }
  357.         
  358.     
  359.     if (err == NPERR_NO_ERROR) {
  360.         /*
  361.          * Copy all the fields of Netscape function table into our
  362.          * copy so we can call back into Netscape later.  Note that
  363.          * we need to copy the fields one by one, rather than assigning
  364.          * the whole structure, because the Netscape function table
  365.          * could actually be bigger than what we expect.
  366.          */
  367.         gNetscapeFuncs.version       = nsTable->version;
  368.         gNetscapeFuncs.size          = nsTable->size;
  369.         gNetscapeFuncs.posturl       = nsTable->posturl;
  370.         gNetscapeFuncs.geturl        = nsTable->geturl;
  371.         gNetscapeFuncs.requestread   = nsTable->requestread;
  372.         gNetscapeFuncs.newstream     = nsTable->newstream;
  373.         gNetscapeFuncs.write         = nsTable->write;
  374.         gNetscapeFuncs.destroystream = nsTable->destroystream;
  375.         gNetscapeFuncs.status        = nsTable->status;
  376.         gNetscapeFuncs.uagent        = nsTable->uagent;
  377.         gNetscapeFuncs.memalloc      = nsTable->memalloc;
  378.         gNetscapeFuncs.memfree       = nsTable->memfree;
  379.         gNetscapeFuncs.memflush      = nsTable->memflush;
  380.         gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
  381.         gNetscapeFuncs.getJavaEnv    = nsTable->getJavaEnv;
  382.         gNetscapeFuncs.getJavaPeer   = nsTable->getJavaPeer;
  383.         gNetscapeFuncs.getvalue      = nsTable->getvalue;
  384.  
  385.         /*
  386.          * Set up the plugin function table that Netscape will use to
  387.          * call us.  Netscape needs to know about our version and size
  388.          * and have a UniversalProcPointer for every function we
  389.          * implement.
  390.          */
  391.         pluginFuncs->version    = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
  392.         pluginFuncs->size       = sizeof(NPPluginFuncs);
  393.         pluginFuncs->newp       = NewNPP_NewProc(Private_New);
  394.         pluginFuncs->destroy    = NewNPP_DestroyProc(Private_Destroy);
  395.         pluginFuncs->setwindow  = NewNPP_SetWindowProc(Private_SetWindow);
  396.         pluginFuncs->newstream  = NewNPP_NewStreamProc(Private_NewStream);
  397.         pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
  398.         pluginFuncs->asfile     = NewNPP_StreamAsFileProc(Private_StreamAsFile);
  399.         pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
  400.         pluginFuncs->write      = NewNPP_WriteProc(Private_Write);
  401.         pluginFuncs->print      = NewNPP_PrintProc(Private_Print);
  402.         pluginFuncs->event      = NULL;
  403.         pluginFuncs->javaClass    = Private_GetJavaClass();
  404.  
  405.         err = NPP_Initialize();
  406.     }
  407.     
  408.     return err;
  409. }
  410.  
  411. /*
  412.  * NP_Shutdown [optional]
  413.  *    - Netscape needs to know about this symbol.
  414.  *    - It calls this function after looking up its symbol after
  415.  *      the last object of this kind has been destroyed.
  416.  *
  417.  */
  418. NPError
  419. NP_Shutdown(void)
  420. {
  421.      PLUGINDEBUGSTR("NP_Shutdown");
  422.     NPP_Shutdown();
  423.     return NPERR_NO_ERROR;
  424. }
  425.