npruntime.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * Copyright © 2004, Apple Computer, Inc. and The Mozilla Foundation.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of Apple Computer, Inc. ("Apple") or The Mozilla
  16. * Foundation ("Mozilla") nor the names of their contributors may be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY APPLE, MOZILLA AND THEIR CONTRIBUTORS "AS
  21. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  23. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE, MOZILLA OR
  24. * THEIR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  26. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * Revision 1 (March 4, 2004):
  33. * Initial proposal.
  34. *
  35. * Revision 2 (March 10, 2004):
  36. * All calls into script were made asynchronous. Results are
  37. * provided via the NPScriptResultFunctionPtr callback.
  38. *
  39. * Revision 3 (March 10, 2004):
  40. * Corrected comments to not refer to class retain/release FunctionPtrs.
  41. *
  42. * Revision 4 (March 11, 2004):
  43. * Added additional convenience NPN_SetExceptionWithUTF8().
  44. * Changed NPHasPropertyFunctionPtr and NPHasMethodFunctionPtr to take NPClass
  45. * pointers instead of NPObject pointers.
  46. * Added NPIsValidIdentifier().
  47. *
  48. * Revision 5 (March 17, 2004):
  49. * Added context parameter to result callbacks from ScriptObject functions.
  50. *
  51. * Revision 6 (March 29, 2004):
  52. * Renamed functions implemented by user agent to NPN_*. Removed _ from
  53. * type names.
  54. * Renamed "JavaScript" types to "Script".
  55. *
  56. * Revision 7 (April 21, 2004):
  57. * NPIdentifier becomes a void*, was int32_t
  58. * Remove NP_IsValidIdentifier, renamed NP_IdentifierFromUTF8 to NP_GetIdentifier
  59. * Added NPVariant and modified functions to use this new type.
  60. *
  61. * Revision 8 (July 9, 2004):
  62. * Updated to joint Apple-Mozilla license.
  63. *
  64. */
  65. #ifndef _NP_RUNTIME_H_
  66. #define _NP_RUNTIME_H_
  67. #ifdef __cplusplus
  68. extern "C" {
  69. #endif
  70. #include "nptypes.h"
  71. /*
  72. This API is used to facilitate binding code written in C to script
  73. objects. The API in this header does not assume the presence of a
  74. user agent. That is, it can be used to bind C code to scripting
  75. environments outside of the context of a user agent.
  76. However, the normal use of the this API is in the context of a
  77. scripting environment running in a browser or other user agent.
  78. In particular it is used to support the extended Netscape
  79. script-ability API for plugins (NP-SAP). NP-SAP is an extension
  80. of the Netscape plugin API. As such we have adopted the use of
  81. the "NP" prefix for this API.
  82. The following NP{N|P}Variables were added to the Netscape plugin
  83. API (in npapi.h):
  84. NPNVWindowNPObject
  85. NPNVPluginElementNPObject
  86. NPPVpluginScriptableNPObject
  87. These variables are exposed through NPN_GetValue() and
  88. NPP_GetValue() (respectively) and are used to establish the
  89. initial binding between the user agent and native code. The DOM
  90. objects in the user agent can be examined and manipulated using
  91. the NPN_ functions that operate on NPObjects described in this
  92. header.
  93. To the extent possible the assumptions about the scripting
  94. language used by the scripting environment have been minimized.
  95. */
  96. #define NP_BEGIN_MACRO do {
  97. #define NP_END_MACRO } while (0)
  98. /*
  99. Objects (non-primitive data) passed between 'C' and script is
  100. always wrapped in an NPObject. The 'interface' of an NPObject is
  101. described by an NPClass.
  102. */
  103. typedef struct NPObject NPObject;
  104. typedef struct NPClass NPClass;
  105. typedef char NPUTF8;
  106. typedef struct _NPString {
  107. const NPUTF8 *utf8characters;
  108. uint32_t utf8length;
  109. } NPString;
  110. typedef enum {
  111. NPVariantType_Void,
  112. NPVariantType_Null,
  113. NPVariantType_Bool,
  114. NPVariantType_Int32,
  115. NPVariantType_Double,
  116. NPVariantType_String,
  117. NPVariantType_Object
  118. } NPVariantType;
  119. typedef struct _NPVariant {
  120. NPVariantType type;
  121. union {
  122. bool boolValue;
  123. uint32_t intValue;
  124. double doubleValue;
  125. NPString stringValue;
  126. NPObject *objectValue;
  127. } value;
  128. } NPVariant;
  129. /*
  130. NPN_ReleaseVariantValue is called on all 'out' parameters
  131. references. Specifically it is to be called on variants that own
  132. their value, as is the case with all non-const NPVariant*
  133. arguments after a successful call to any methods (except this one)
  134. in this API.
  135. After calling NPN_ReleaseVariantValue, the type of the variant
  136. will be NPVariantType_Void.
  137. */
  138. void NPN_ReleaseVariantValue(NPVariant *variant);
  139. #define NPVARIANT_IS_VOID(_v) ((_v).type == NPVariantType_Void)
  140. #define NPVARIANT_IS_NULL(_v) ((_v).type == NPVariantType_Null)
  141. #define NPVARIANT_IS_BOOLEAN(_v) ((_v).type == NPVariantType_Bool)
  142. #define NPVARIANT_IS_INT32(_v) ((_v).type == NPVariantType_Int32)
  143. #define NPVARIANT_IS_DOUBLE(_v) ((_v).type == NPVariantType_Double)
  144. #define NPVARIANT_IS_STRING(_v) ((_v).type == NPVariantType_String)
  145. #define NPVARIANT_IS_OBJECT(_v) ((_v).type == NPVariantType_Object)
  146. #define NPVARIANT_TO_BOOLEAN(_v) ((_v).value.boolValue)
  147. #define NPVARIANT_TO_INT32(_v) ((_v).value.intValue)
  148. #define NPVARIANT_TO_DOUBLE(_v) ((_v).value.doubleValue)
  149. #define NPVARIANT_TO_STRING(_v) ((_v).value.stringValue)
  150. #define NPVARIANT_TO_OBJECT(_v) ((_v).value.objectValue)
  151. #define VOID_TO_NPVARIANT(_v) \
  152. NP_BEGIN_MACRO \
  153. (_v).type = NPVariantType_Void; \
  154. (_v).value.objectValue = NULL; \
  155. NP_END_MACRO
  156. #define NULL_TO_NPVARIANT(_v) \
  157. NP_BEGIN_MACRO \
  158. (_v).type = NPVariantType_Null; \
  159. (_v).value.objectValue = NULL; \
  160. NP_END_MACRO
  161. #define BOOLEAN_TO_NPVARIANT(_val, _v) \
  162. NP_BEGIN_MACRO \
  163. (_v).type = NPVariantType_Bool; \
  164. (_v).value.boolValue = !!(_val); \
  165. NP_END_MACRO
  166. #define INT32_TO_NPVARIANT(_val, _v) \
  167. NP_BEGIN_MACRO \
  168. (_v).type = NPVariantType_Int32; \
  169. (_v).value.intValue = _val; \
  170. NP_END_MACRO
  171. #define DOUBLE_TO_NPVARIANT(_val, _v) \
  172. NP_BEGIN_MACRO \
  173. (_v).type = NPVariantType_Double; \
  174. (_v).value.doubleValue = _val; \
  175. NP_END_MACRO
  176. #define STRINGZ_TO_NPVARIANT(_val, _v) \
  177. NP_BEGIN_MACRO \
  178. (_v).type = NPVariantType_String; \
  179. NPString str = { _val, strlen(_val) }; \
  180. (_v).value.stringValue = str; \
  181. NP_END_MACRO
  182. #define STRINGN_TO_NPVARIANT(_val, _len, _v) \
  183. NP_BEGIN_MACRO \
  184. (_v).type = NPVariantType_String; \
  185. NPString str = { _val, _len }; \
  186. (_v).value.stringValue = str; \
  187. NP_END_MACRO
  188. #define OBJECT_TO_NPVARIANT(_val, _v) \
  189. NP_BEGIN_MACRO \
  190. (_v).type = NPVariantType_Object; \
  191. (_v).value.objectValue = _val; \
  192. NP_END_MACRO
  193. /*
  194. Type mappings (JavaScript types have been used for illustration
  195. purposes):
  196. JavaScript to C (NPVariant with type:)
  197. undefined NPVariantType_Void
  198. null NPVariantType_Null
  199. Boolean NPVariantType_Bool
  200. Number NPVariantType_Double or NPVariantType_Int32
  201. String NPVariantType_String
  202. Object NPVariantType_Object
  203. C (NPVariant with type:) to JavaScript
  204. NPVariantType_Void undefined
  205. NPVariantType_Null null
  206. NPVariantType_Bool Boolean
  207. NPVariantType_Int32 Number
  208. NPVariantType_Double Number
  209. NPVariantType_String String
  210. NPVariantType_Object Object
  211. */
  212. typedef void *NPIdentifier;
  213. /*
  214. NPObjects have methods and properties. Methods and properties are
  215. identified with NPIdentifiers. These identifiers may be reflected
  216. in script. NPIdentifiers can be either strings or integers, IOW,
  217. methods and properties can be identified by either strings or
  218. integers (i.e. foo["bar"] vs foo[1]). NPIdentifiers can be
  219. compared using ==. In case of any errors, the requested
  220. NPIdentifier(s) will be NULL.
  221. */
  222. NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name);
  223. void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount,
  224. NPIdentifier *identifiers);
  225. NPIdentifier NPN_GetIntIdentifier(int32_t intid);
  226. bool NPN_IdentifierIsString(NPIdentifier identifier);
  227. /*
  228. The NPUTF8 returned from NPN_UTF8FromIdentifier SHOULD be freed.
  229. */
  230. NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier);
  231. /*
  232. Get the integer represented by identifier. If identifier is not an
  233. integer identifier, the behaviour is undefined.
  234. */
  235. int32_t NPN_IntFromIdentifier(NPIdentifier identifier);
  236. /*
  237. NPObject behavior is implemented using the following set of
  238. callback functions.
  239. The NPVariant *result argument of these functions (where
  240. applicable) should be released using NPN_ReleaseVariantValue().
  241. */
  242. typedef NPObject *(*NPAllocateFunctionPtr)(NPP npp, NPClass *aClass);
  243. typedef void (*NPDeallocateFunctionPtr)(NPObject *npobj);
  244. typedef void (*NPInvalidateFunctionPtr)(NPObject *npobj);
  245. typedef bool (*NPHasMethodFunctionPtr)(NPObject *npobj, NPIdentifier name);
  246. typedef bool (*NPInvokeFunctionPtr)(NPObject *npobj, NPIdentifier name,
  247. const NPVariant *args, uint32_t argCount,
  248. NPVariant *result);
  249. typedef bool (*NPInvokeDefaultFunctionPtr)(NPObject *npobj,
  250. const NPVariant *args,
  251. uint32_t argCount,
  252. NPVariant *result);
  253. typedef bool (*NPHasPropertyFunctionPtr)(NPObject *npobj, NPIdentifier name);
  254. typedef bool (*NPGetPropertyFunctionPtr)(NPObject *npobj, NPIdentifier name,
  255. NPVariant *result);
  256. typedef bool (*NPSetPropertyFunctionPtr)(NPObject *npobj, NPIdentifier name,
  257. const NPVariant *value);
  258. typedef bool (*NPRemovePropertyFunctionPtr)(NPObject *npobj,
  259. NPIdentifier name);
  260. /*
  261. NPObjects returned by create, retain, invoke, and getProperty pass
  262. a reference count to the caller. That is, the callee adds a
  263. reference count which passes to the caller. It is the caller's
  264. responsibility to release the returned object.
  265. NPInvokeFunctionPtr function may return 0 to indicate a void
  266. result.
  267. NPInvalidateFunctionPtr is called by the scripting environment
  268. when the native code is shutdown. Any attempt to message a
  269. NPObject instance after the invalidate callback has been
  270. called will result in undefined behavior, even if the native code
  271. is still retaining those NPObject instances. (The runtime
  272. will typically return immediately, with 0 or NULL, from an attempt
  273. to dispatch to a NPObject, but this behavior should not be
  274. depended upon.)
  275. */
  276. struct NPClass
  277. {
  278. uint32_t structVersion;
  279. NPAllocateFunctionPtr allocate;
  280. NPDeallocateFunctionPtr deallocate;
  281. NPInvalidateFunctionPtr invalidate;
  282. NPHasMethodFunctionPtr hasMethod;
  283. NPInvokeFunctionPtr invoke;
  284. NPInvokeDefaultFunctionPtr invokeDefault;
  285. NPHasPropertyFunctionPtr hasProperty;
  286. NPGetPropertyFunctionPtr getProperty;
  287. NPSetPropertyFunctionPtr setProperty;
  288. NPRemovePropertyFunctionPtr removeProperty;
  289. };
  290. #define NP_CLASS_STRUCT_VERSION 1
  291. struct NPObject {
  292. NPClass *_class;
  293. uint32_t referenceCount;
  294. /*
  295. * Additional space may be allocated here by types of NPObjects
  296. */
  297. };
  298. /*
  299. If the class has an allocate function, NPN_CreateObject invokes
  300. that function, otherwise a NPObject is allocated and
  301. returned. This method will initialize the referenceCount member of
  302. the NPObject to 1.
  303. */
  304. NPObject *NPN_CreateObject(NPP npp, NPClass *aClass);
  305. /*
  306. Increment the NPObject's reference count.
  307. */
  308. NPObject *NPN_RetainObject(NPObject *npobj);
  309. /*
  310. Decremented the NPObject's reference count. If the reference
  311. count goes to zero, the class's destroy function is invoke if
  312. specified, otherwise the object is freed directly.
  313. */
  314. void NPN_ReleaseObject(NPObject *npobj);
  315. /*
  316. Functions to access script objects represented by NPObject.
  317. Calls to script objects are synchronous. If a function returns a
  318. value, it will be supplied via the result NPVariant
  319. argument. Successful calls will return true, false will be
  320. returned in case of an error.
  321. Calls made from plugin code to script must be made from the thread
  322. on which the plugin was initialized.
  323. */
  324. bool NPN_Invoke(NPP npp, NPObject *npobj, NPIdentifier methodName,
  325. const NPVariant *args, uint32_t argCount, NPVariant *result);
  326. bool NPN_InvokeDefault(NPP npp, NPObject *npobj, const NPVariant *args,
  327. uint32_t argCount, NPVariant *result);
  328. bool NPN_Evaluate(NPP npp, NPObject *npobj, NPString *script,
  329. NPVariant *result);
  330. bool NPN_GetProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName,
  331. NPVariant *result);
  332. bool NPN_SetProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName,
  333. const NPVariant *value);
  334. bool NPN_RemoveProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName);
  335. bool NPN_HasProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName);
  336. bool NPN_HasMethod(NPP npp, NPObject *npobj, NPIdentifier methodName);
  337. /*
  338. NPN_SetException may be called to trigger a script exception upon
  339. return from entry points into NPObjects. Typical usage:
  340. NPN_SetException (npobj, message);
  341. */
  342. void NPN_SetException(NPObject *npobj, const NPUTF8 *message);
  343. #ifdef __cplusplus
  344. }
  345. #endif
  346. #endif