PageRenderTime 192ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/npapi/npruntime.h

http://firefox-mac-pdf.googlecode.com/
C Header | 393 lines | 173 code | 40 blank | 180 comment | 8 complexity | 0e23751e5314a31e46335d9122b84479 MD5 | raw file
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * Copyright (c) 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. */
  33. #ifndef _NP_RUNTIME_H_
  34. #define _NP_RUNTIME_H_
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #include "nptypes.h"
  39. /*
  40. This API is used to facilitate binding code written in C to script
  41. objects. The API in this header does not assume the presence of a
  42. user agent. That is, it can be used to bind C code to scripting
  43. environments outside of the context of a user agent.
  44. However, the normal use of the this API is in the context of a
  45. scripting environment running in a browser or other user agent.
  46. In particular it is used to support the extended Netscape
  47. script-ability API for plugins (NP-SAP). NP-SAP is an extension
  48. of the Netscape plugin API. As such we have adopted the use of
  49. the "NP" prefix for this API.
  50. The following NP{N|P}Variables were added to the Netscape plugin
  51. API (in npapi.h):
  52. NPNVWindowNPObject
  53. NPNVPluginElementNPObject
  54. NPPVpluginScriptableNPObject
  55. These variables are exposed through NPN_GetValue() and
  56. NPP_GetValue() (respectively) and are used to establish the
  57. initial binding between the user agent and native code. The DOM
  58. objects in the user agent can be examined and manipulated using
  59. the NPN_ functions that operate on NPObjects described in this
  60. header.
  61. To the extent possible the assumptions about the scripting
  62. language used by the scripting environment have been minimized.
  63. */
  64. #define NP_BEGIN_MACRO do {
  65. #define NP_END_MACRO } while (0)
  66. /*
  67. Objects (non-primitive data) passed between 'C' and script is
  68. always wrapped in an NPObject. The 'interface' of an NPObject is
  69. described by an NPClass.
  70. */
  71. typedef struct NPObject NPObject;
  72. typedef struct NPClass NPClass;
  73. typedef char NPUTF8;
  74. typedef struct _NPString {
  75. const NPUTF8 *UTF8Characters;
  76. uint32_t UTF8Length;
  77. } NPString;
  78. typedef enum {
  79. NPVariantType_Void,
  80. NPVariantType_Null,
  81. NPVariantType_Bool,
  82. NPVariantType_Int32,
  83. NPVariantType_Double,
  84. NPVariantType_String,
  85. NPVariantType_Object
  86. } NPVariantType;
  87. typedef struct _NPVariant {
  88. NPVariantType type;
  89. union {
  90. bool boolValue;
  91. int32_t intValue;
  92. double doubleValue;
  93. NPString stringValue;
  94. NPObject *objectValue;
  95. } value;
  96. } NPVariant;
  97. /*
  98. NPN_ReleaseVariantValue is called on all 'out' parameters
  99. references. Specifically it is to be called on variants that own
  100. their value, as is the case with all non-const NPVariant*
  101. arguments after a successful call to any methods (except this one)
  102. in this API.
  103. After calling NPN_ReleaseVariantValue, the type of the variant
  104. will be NPVariantType_Void.
  105. */
  106. void NPN_ReleaseVariantValue(NPVariant *variant);
  107. #define NPVARIANT_IS_VOID(_v) ((_v).type == NPVariantType_Void)
  108. #define NPVARIANT_IS_NULL(_v) ((_v).type == NPVariantType_Null)
  109. #define NPVARIANT_IS_BOOLEAN(_v) ((_v).type == NPVariantType_Bool)
  110. #define NPVARIANT_IS_INT32(_v) ((_v).type == NPVariantType_Int32)
  111. #define NPVARIANT_IS_DOUBLE(_v) ((_v).type == NPVariantType_Double)
  112. #define NPVARIANT_IS_STRING(_v) ((_v).type == NPVariantType_String)
  113. #define NPVARIANT_IS_OBJECT(_v) ((_v).type == NPVariantType_Object)
  114. #define NPVARIANT_TO_BOOLEAN(_v) ((_v).value.boolValue)
  115. #define NPVARIANT_TO_INT32(_v) ((_v).value.intValue)
  116. #define NPVARIANT_TO_DOUBLE(_v) ((_v).value.doubleValue)
  117. #define NPVARIANT_TO_STRING(_v) ((_v).value.stringValue)
  118. #define NPVARIANT_TO_OBJECT(_v) ((_v).value.objectValue)
  119. #define VOID_TO_NPVARIANT(_v) \
  120. NP_BEGIN_MACRO \
  121. (_v).type = NPVariantType_Void; \
  122. (_v).value.objectValue = NULL; \
  123. NP_END_MACRO
  124. #define NULL_TO_NPVARIANT(_v) \
  125. NP_BEGIN_MACRO \
  126. (_v).type = NPVariantType_Null; \
  127. (_v).value.objectValue = NULL; \
  128. NP_END_MACRO
  129. #define BOOLEAN_TO_NPVARIANT(_val, _v) \
  130. NP_BEGIN_MACRO \
  131. (_v).type = NPVariantType_Bool; \
  132. (_v).value.boolValue = !!(_val); \
  133. NP_END_MACRO
  134. #define INT32_TO_NPVARIANT(_val, _v) \
  135. NP_BEGIN_MACRO \
  136. (_v).type = NPVariantType_Int32; \
  137. (_v).value.intValue = _val; \
  138. NP_END_MACRO
  139. #define DOUBLE_TO_NPVARIANT(_val, _v) \
  140. NP_BEGIN_MACRO \
  141. (_v).type = NPVariantType_Double; \
  142. (_v).value.doubleValue = _val; \
  143. NP_END_MACRO
  144. #define STRINGZ_TO_NPVARIANT(_val, _v) \
  145. NP_BEGIN_MACRO \
  146. (_v).type = NPVariantType_String; \
  147. NPString str = { _val, uint32_t(strlen(_val)) }; \
  148. (_v).value.stringValue = str; \
  149. NP_END_MACRO
  150. #define STRINGN_TO_NPVARIANT(_val, _len, _v) \
  151. NP_BEGIN_MACRO \
  152. (_v).type = NPVariantType_String; \
  153. NPString str = { _val, uint32_t(_len) }; \
  154. (_v).value.stringValue = str; \
  155. NP_END_MACRO
  156. #define OBJECT_TO_NPVARIANT(_val, _v) \
  157. NP_BEGIN_MACRO \
  158. (_v).type = NPVariantType_Object; \
  159. (_v).value.objectValue = _val; \
  160. NP_END_MACRO
  161. /*
  162. Type mappings (JavaScript types have been used for illustration
  163. purposes):
  164. JavaScript to C (NPVariant with type:)
  165. undefined NPVariantType_Void
  166. null NPVariantType_Null
  167. Boolean NPVariantType_Bool
  168. Number NPVariantType_Double or NPVariantType_Int32
  169. String NPVariantType_String
  170. Object NPVariantType_Object
  171. C (NPVariant with type:) to JavaScript
  172. NPVariantType_Void undefined
  173. NPVariantType_Null null
  174. NPVariantType_Bool Boolean
  175. NPVariantType_Int32 Number
  176. NPVariantType_Double Number
  177. NPVariantType_String String
  178. NPVariantType_Object Object
  179. */
  180. typedef void *NPIdentifier;
  181. /*
  182. NPObjects have methods and properties. Methods and properties are
  183. identified with NPIdentifiers. These identifiers may be reflected
  184. in script. NPIdentifiers can be either strings or integers, IOW,
  185. methods and properties can be identified by either strings or
  186. integers (i.e. foo["bar"] vs foo[1]). NPIdentifiers can be
  187. compared using ==. In case of any errors, the requested
  188. NPIdentifier(s) will be NULL. NPIdentifier lifetime is controlled
  189. by the browser. Plugins do not need to worry about memory management
  190. with regards to NPIdentifiers.
  191. */
  192. NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name);
  193. void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount,
  194. NPIdentifier *identifiers);
  195. NPIdentifier NPN_GetIntIdentifier(int32_t intid);
  196. bool NPN_IdentifierIsString(NPIdentifier identifier);
  197. /*
  198. The NPUTF8 returned from NPN_UTF8FromIdentifier SHOULD be freed.
  199. */
  200. NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier);
  201. /*
  202. Get the integer represented by identifier. If identifier is not an
  203. integer identifier, the behaviour is undefined.
  204. */
  205. int32_t NPN_IntFromIdentifier(NPIdentifier identifier);
  206. /*
  207. NPObject behavior is implemented using the following set of
  208. callback functions.
  209. The NPVariant *result argument of these functions (where
  210. applicable) should be released using NPN_ReleaseVariantValue().
  211. */
  212. typedef NPObject *(*NPAllocateFunctionPtr)(NPP npp, NPClass *aClass);
  213. typedef void (*NPDeallocateFunctionPtr)(NPObject *npobj);
  214. typedef void (*NPInvalidateFunctionPtr)(NPObject *npobj);
  215. typedef bool (*NPHasMethodFunctionPtr)(NPObject *npobj, NPIdentifier name);
  216. typedef bool (*NPInvokeFunctionPtr)(NPObject *npobj, NPIdentifier name,
  217. const NPVariant *args, uint32_t argCount,
  218. NPVariant *result);
  219. typedef bool (*NPInvokeDefaultFunctionPtr)(NPObject *npobj,
  220. const NPVariant *args,
  221. uint32_t argCount,
  222. NPVariant *result);
  223. typedef bool (*NPHasPropertyFunctionPtr)(NPObject *npobj, NPIdentifier name);
  224. typedef bool (*NPGetPropertyFunctionPtr)(NPObject *npobj, NPIdentifier name,
  225. NPVariant *result);
  226. typedef bool (*NPSetPropertyFunctionPtr)(NPObject *npobj, NPIdentifier name,
  227. const NPVariant *value);
  228. typedef bool (*NPRemovePropertyFunctionPtr)(NPObject *npobj,
  229. NPIdentifier name);
  230. typedef bool (*NPEnumerationFunctionPtr)(NPObject *npobj, NPIdentifier **value,
  231. uint32_t *count);
  232. typedef bool (*NPConstructFunctionPtr)(NPObject *npobj,
  233. const NPVariant *args,
  234. uint32_t argCount,
  235. NPVariant *result);
  236. /*
  237. NPObjects returned by create, retain, invoke, and getProperty pass
  238. a reference count to the caller. That is, the callee adds a
  239. reference count which passes to the caller. It is the caller's
  240. responsibility to release the returned object.
  241. NPInvokeFunctionPtr function may return 0 to indicate a void
  242. result.
  243. NPInvalidateFunctionPtr is called by the scripting environment
  244. when the native code is shutdown. Any attempt to message a
  245. NPObject instance after the invalidate callback has been
  246. called will result in undefined behavior, even if the native code
  247. is still retaining those NPObject instances. (The runtime
  248. will typically return immediately, with 0 or NULL, from an attempt
  249. to dispatch to a NPObject, but this behavior should not be
  250. depended upon.)
  251. The NPEnumerationFunctionPtr function may pass an array of
  252. NPIdentifiers back to the caller. The callee allocs the memory of
  253. the array using NPN_MemAlloc(), and it's the caller's responsibility
  254. to release it using NPN_MemFree().
  255. */
  256. struct NPClass
  257. {
  258. uint32_t structVersion;
  259. NPAllocateFunctionPtr allocate;
  260. NPDeallocateFunctionPtr deallocate;
  261. NPInvalidateFunctionPtr invalidate;
  262. NPHasMethodFunctionPtr hasMethod;
  263. NPInvokeFunctionPtr invoke;
  264. NPInvokeDefaultFunctionPtr invokeDefault;
  265. NPHasPropertyFunctionPtr hasProperty;
  266. NPGetPropertyFunctionPtr getProperty;
  267. NPSetPropertyFunctionPtr setProperty;
  268. NPRemovePropertyFunctionPtr removeProperty;
  269. NPEnumerationFunctionPtr enumerate;
  270. NPConstructFunctionPtr construct;
  271. };
  272. #define NP_CLASS_STRUCT_VERSION 3
  273. #define NP_CLASS_STRUCT_VERSION_ENUM 2
  274. #define NP_CLASS_STRUCT_VERSION_CTOR 3
  275. #define NP_CLASS_STRUCT_VERSION_HAS_ENUM(npclass) \
  276. ((npclass)->structVersion >= NP_CLASS_STRUCT_VERSION_ENUM)
  277. #define NP_CLASS_STRUCT_VERSION_HAS_CTOR(npclass) \
  278. ((npclass)->structVersion >= NP_CLASS_STRUCT_VERSION_CTOR)
  279. struct NPObject {
  280. NPClass *_class;
  281. uint32_t referenceCount;
  282. /*
  283. * Additional space may be allocated here by types of NPObjects
  284. */
  285. };
  286. /*
  287. If the class has an allocate function, NPN_CreateObject invokes
  288. that function, otherwise a NPObject is allocated and
  289. returned. This method will initialize the referenceCount member of
  290. the NPObject to 1.
  291. */
  292. NPObject *NPN_CreateObject(NPP npp, NPClass *aClass);
  293. /*
  294. Increment the NPObject's reference count.
  295. */
  296. NPObject *NPN_RetainObject(NPObject *npobj);
  297. /*
  298. Decremented the NPObject's reference count. If the reference
  299. count goes to zero, the class's destroy function is invoke if
  300. specified, otherwise the object is freed directly.
  301. */
  302. void NPN_ReleaseObject(NPObject *npobj);
  303. /*
  304. Functions to access script objects represented by NPObject.
  305. Calls to script objects are synchronous. If a function returns a
  306. value, it will be supplied via the result NPVariant
  307. argument. Successful calls will return true, false will be
  308. returned in case of an error.
  309. Calls made from plugin code to script must be made from the thread
  310. on which the plugin was initialized.
  311. */
  312. bool NPN_Invoke(NPP npp, NPObject *npobj, NPIdentifier methodName,
  313. const NPVariant *args, uint32_t argCount, NPVariant *result);
  314. bool NPN_InvokeDefault(NPP npp, NPObject *npobj, const NPVariant *args,
  315. uint32_t argCount, NPVariant *result);
  316. bool NPN_Evaluate(NPP npp, NPObject *npobj, NPString *script,
  317. NPVariant *result);
  318. bool NPN_GetProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName,
  319. NPVariant *result);
  320. bool NPN_SetProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName,
  321. const NPVariant *value);
  322. bool NPN_RemoveProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName);
  323. bool NPN_HasProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName);
  324. bool NPN_HasMethod(NPP npp, NPObject *npobj, NPIdentifier methodName);
  325. bool NPN_Enumerate(NPP npp, NPObject *npobj, NPIdentifier **identifier,
  326. uint32_t *count);
  327. bool NPN_Construct(NPP npp, NPObject *npobj, const NPVariant *args,
  328. uint32_t argCount, NPVariant *result);
  329. /*
  330. NPN_SetException may be called to trigger a script exception upon
  331. return from entry points into NPObjects. Typical usage:
  332. NPN_SetException (npobj, message);
  333. */
  334. void NPN_SetException(NPObject *npobj, const NPUTF8 *message);
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif