npunix.c 16 KB

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