2 IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
3 consideration of your agreement to the following terms, and your use, installation,
4 modification or redistribution of this Apple software constitutes acceptance of these
5 terms. If you do not agree with these terms, please do not use, install, modify or
6 redistribute this Apple software.
8 In consideration of your agreement to abide by the following terms, and subject to these
9 terms, Apple grants you a personal, non-exclusive license, under AppleĆs copyrights in
10 this original Apple software (the "Apple Software"), to use, reproduce, modify and
11 redistribute the Apple Software, with or without modifications, in source and/or binary
12 forms; provided that if you redistribute the Apple Software in its entirety and without
13 modifications, you must retain this notice and the following text and disclaimers in all
14 such redistributions of the Apple Software. Neither the name, trademarks, service marks
15 or logos of Apple Computer, Inc. may be used to endorse or promote products derived from
16 the Apple Software without specific prior written permission from Apple. Except as expressly
17 stated in this notice, no other rights or licenses, express or implied, are granted by Apple
18 herein, including but not limited to any patent rights that may be infringed by your
19 derivative works or by other works in which the Apple Software may be incorporated.
21 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
22 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
24 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
26 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
29 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
30 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
31 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #import "PluginObject.h"
35 void pluginInvalidate ();
36 bool pluginHasProperty (NPClass *theClass, NPIdentifier name);
37 bool pluginHasMethod (NPClass *theClass, NPIdentifier name);
38 void pluginGetProperty (PluginObject *obj, NPIdentifier name, NPVariant *variant);
39 void pluginSetProperty (PluginObject *obj, NPIdentifier name, const NPVariant *variant);
40 void pluginInvoke (PluginObject *obj, NPIdentifier name, NPVariant *args, uint32_t argCount, NPVariant *result);
41 void pluginInvokeDefault (PluginObject *obj, NPVariant *args, uint32_t argCount, NPVariant *result);
42 NPObject *pluginAllocate (NPP npp, NPClass *theClass);
43 void pluginDeallocate (PluginObject *obj);
45 static NPClass _pluginFunctionPtrs = {
46 NP_CLASS_STRUCT_VERSION,
47 (NPAllocateFunctionPtr) pluginAllocate,
48 (NPDeallocateFunctionPtr) pluginDeallocate,
49 (NPInvalidateFunctionPtr) pluginInvalidate,
50 (NPHasMethodFunctionPtr) pluginHasMethod,
51 (NPInvokeFunctionPtr) pluginInvoke,
52 (NPInvokeDefaultFunctionPtr) pluginInvokeDefault,
53 (NPHasPropertyFunctionPtr) pluginHasProperty,
54 (NPGetPropertyFunctionPtr) pluginGetProperty,
55 (NPSetPropertyFunctionPtr) pluginSetProperty,
58 NPClass *getPluginClass(void)
60 return &_pluginFunctionPtrs;
63 static bool identifiersInitialized = false;
65 #define ID_PROPERTY_PROPERTY 0
66 #define NUM_PROPERTY_IDENTIFIERS 1
68 static NPIdentifier pluginPropertyIdentifiers[NUM_PROPERTY_IDENTIFIERS];
69 static const NPUTF8 *pluginPropertyIdentifierNames[NUM_PROPERTY_IDENTIFIERS] = {
73 #define ID_TEST_CALLBACK_METHOD 0
74 #define NUM_METHOD_IDENTIFIERS 1
76 static NPIdentifier pluginMethodIdentifiers[NUM_METHOD_IDENTIFIERS];
77 static const NPUTF8 *pluginMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = {
81 static void initializeIdentifiers()
83 browser->getstringidentifiers (pluginPropertyIdentifierNames, NUM_PROPERTY_IDENTIFIERS, pluginPropertyIdentifiers);
84 browser->getstringidentifiers (pluginMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, pluginMethodIdentifiers);
87 bool pluginHasProperty (NPClass *theClass, NPIdentifier name)
89 for (int i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++) {
90 if (name == pluginPropertyIdentifiers[i])
96 bool pluginHasMethod (NPClass *theClass, NPIdentifier name)
98 for (int i = 0; i < NUM_METHOD_IDENTIFIERS; i++) {
99 if (name == pluginMethodIdentifiers[i])
105 void pluginGetProperty (PluginObject *obj, NPIdentifier name, NPVariant *variant)
107 if (name == pluginPropertyIdentifiers[ID_PROPERTY_PROPERTY]) {
108 // just return "property"
109 NPString propertyNameString;
110 propertyNameString.UTF8Characters = "property";
111 propertyNameString.UTF8Length = sizeof("property") - 1;
112 variant->type = NPVariantType_String;
113 variant->value.stringValue = propertyNameString;
115 variant->type = NPVariantType_Void;
118 void pluginSetProperty (PluginObject *obj, NPIdentifier name, const NPVariant *variant)
122 void pluginInvoke (PluginObject *obj, NPIdentifier name, NPVariant *args, unsigned argCount, NPVariant *result)
124 if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD]) {
125 // call whatever method name we're given
126 if (argCount > 0 && args->type == NPVariantType_String) {
127 NPVariant browserResult;
129 NPObject *windowScriptObject;
130 browser->getvalue(obj->npp, NPPVpluginScriptableNPObject, &windowScriptObject);
132 NPString argString = args[0].value.stringValue;
133 int size = argString.UTF8Length + 1;
134 NPUTF8 callbackString[size];
135 strncpy(callbackString, argString.UTF8Characters, argString.UTF8Length);
136 callbackString[size - 1] = '\0';
138 NPIdentifier callbackMethodID = browser->getstringidentifier(callbackString);
139 browser->invoke(obj->npp, windowScriptObject, callbackMethodID, 0, 0, &browserResult);
143 result->type = NPVariantType_Void;
146 void pluginInvokeDefault (PluginObject *obj, NPVariant *args, unsigned argCount, NPVariant *result)
148 result->type = NPVariantType_Void;
151 void pluginInvalidate ()
153 // Make sure we've released any remainging references to JavaScript
157 NPObject *pluginAllocate (NPP npp, NPClass *theClass)
159 PluginObject *newInstance = (PluginObject *)malloc (sizeof(PluginObject));
161 if (!identifiersInitialized) {
162 identifiersInitialized = true;
163 initializeIdentifiers();
166 newInstance->npp = npp;
168 return (NPObject *)newInstance;
171 void pluginDeallocate (PluginObject *obj)