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.
34 #import "PluginObject.h"
36 static void pluginInvalidate(NPObject *obj);
37 static bool pluginHasProperty(NPObject *obj, NPIdentifier name);
38 static bool pluginHasMethod(NPObject *obj, NPIdentifier name);
39 static bool pluginGetProperty(NPObject *obj, NPIdentifier name, NPVariant *variant);
40 static bool pluginSetProperty(NPObject *obj, NPIdentifier name, const NPVariant *variant);
41 static bool pluginInvoke(NPObject *obj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result);
42 static bool pluginInvokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result);
43 static NPObject *pluginAllocate(NPP npp, NPClass *theClass);
44 static void pluginDeallocate(NPObject *obj);
46 NPNetscapeFuncs *browser;
48 static NPClass pluginClass = {
49 NP_CLASS_STRUCT_VERSION,
61 NPClass *getPluginClass(void)
66 static bool identifiersInitialized = false;
68 #define ID_PROPERTY_PROPERTY 0
69 #define NUM_PROPERTY_IDENTIFIERS 1
71 static NPIdentifier pluginPropertyIdentifiers[NUM_PROPERTY_IDENTIFIERS];
72 static const NPUTF8 *pluginPropertyIdentifierNames[NUM_PROPERTY_IDENTIFIERS] = {
76 #define ID_TEST_CALLBACK_METHOD 0
77 #define ID_TEST_GETURL 1
78 #define ID_REMOVE_DEFAULT_METHOD 2
79 #define ID_TEST_DOM_ACCESS 3
81 #define NUM_METHOD_IDENTIFIERS 4
83 static NPIdentifier pluginMethodIdentifiers[NUM_METHOD_IDENTIFIERS];
84 static const NPUTF8 *pluginMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = {
87 "removeDefaultMethod",
91 static NPUTF8* createCStringFromNPVariant(const NPVariant *variant)
93 size_t length = NPVARIANT_TO_STRING(*variant).UTF8Length;
94 NPUTF8* result = malloc(length + 1);
95 memcpy(result, NPVARIANT_TO_STRING(*variant).UTF8Characters, length);
96 result[length] = '\0';
100 static void initializeIdentifiers(void)
102 browser->getstringidentifiers(pluginPropertyIdentifierNames, NUM_PROPERTY_IDENTIFIERS, pluginPropertyIdentifiers);
103 browser->getstringidentifiers(pluginMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, pluginMethodIdentifiers);
106 static bool pluginHasProperty(NPObject *obj, NPIdentifier name)
108 for (int i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++)
109 if (name == pluginPropertyIdentifiers[i])
114 static bool pluginHasMethod(NPObject *obj, NPIdentifier name)
116 for (int i = 0; i < NUM_METHOD_IDENTIFIERS; i++)
117 if (name == pluginMethodIdentifiers[i])
122 static bool pluginGetProperty(NPObject *obj, NPIdentifier name, NPVariant *variant)
124 if (name == pluginPropertyIdentifiers[ID_PROPERTY_PROPERTY]) {
125 STRINGZ_TO_NPVARIANT("property", *variant);
131 static bool pluginSetProperty(NPObject *obj, NPIdentifier name, const NPVariant *variant)
136 static void testDOMAccess(PluginObject *obj)
138 // Get plug-in's DOM element
139 NPObject *elementObject;
140 if (browser->getvalue(obj->npp, NPNVPluginElementNPObject, &elementObject) == NPERR_NO_ERROR) {
142 NPVariant styleVariant;
143 NPIdentifier styleIdentifier = browser->getstringidentifier("style");
144 if (browser->getproperty(obj->npp, elementObject, styleIdentifier, &styleVariant) && NPVARIANT_IS_OBJECT(styleVariant)) {
146 NPIdentifier borderIdentifier = browser->getstringidentifier("border");
147 NPVariant borderVariant;
148 STRINGZ_TO_NPVARIANT("3px solid red", borderVariant);
149 browser->setproperty(obj->npp, NPVARIANT_TO_OBJECT(styleVariant), borderIdentifier, &borderVariant);
150 browser->releasevariantvalue(&styleVariant);
153 browser->releaseobject(elementObject);
157 static bool pluginInvoke(NPObject *header, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
159 PluginObject *obj = (PluginObject *)header;
160 if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD]) {
161 // call whatever method name we're given
162 if (argCount > 0 && NPVARIANT_IS_STRING(args[0])) {
163 NPObject *windowScriptObject;
164 browser->getvalue(obj->npp, NPPVpluginScriptableNPObject, &windowScriptObject);
166 NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
167 NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
168 free(callbackString);
170 NPVariant browserResult;
171 browser->invoke(obj->npp, windowScriptObject, callbackIdentifier, 0, 0, &browserResult);
172 browser->releasevariantvalue(&browserResult);
174 VOID_TO_NPVARIANT(*result);
177 } else if (name == pluginMethodIdentifiers[ID_TEST_GETURL]) {
178 if (argCount == 2 && NPVARIANT_IS_STRING(args[0]) && NPVARIANT_IS_STRING(args[1])) {
179 NPUTF8* urlString = createCStringFromNPVariant(&args[0]);
180 NPUTF8* targetString = createCStringFromNPVariant(&args[1]);
181 browser->geturl(obj->npp, urlString, targetString);
185 VOID_TO_NPVARIANT(*result);
188 } else if (name == pluginMethodIdentifiers[ID_REMOVE_DEFAULT_METHOD]) {
189 pluginClass.invokeDefault = 0;
190 VOID_TO_NPVARIANT(*result);
192 } else if (name == pluginMethodIdentifiers[ID_TEST_DOM_ACCESS]) {
194 VOID_TO_NPVARIANT(*result);
201 static bool pluginInvokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result)
203 INT32_TO_NPVARIANT(1, *result);
207 static void pluginInvalidate(NPObject *obj)
211 static NPObject *pluginAllocate(NPP npp, NPClass *theClass)
213 PluginObject *newInstance = malloc(sizeof(PluginObject));
215 if (!identifiersInitialized) {
216 identifiersInitialized = true;
217 initializeIdentifiers();
220 newInstance->npp = npp;
222 return (NPObject *)newInstance;
225 static void pluginDeallocate(NPObject *obj)