1/*
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.
7
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.
20
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.
25
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.
32 */
33
34#include "PluginObject.h"
35
36#include <stdio.h>
37
38extern "C"
39NPError __stdcall NP_Initialize(NPNetscapeFuncs* browserFuncs)
40{
41    browser = browserFuncs;
42    return NPERR_NO_ERROR;
43}
44
45extern "C"
46NPError __stdcall NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
47{
48    pluginFuncs->version = 11;
49    pluginFuncs->size = sizeof(pluginFuncs);
50    pluginFuncs->newp = NPP_New;
51    pluginFuncs->destroy = NPP_Destroy;
52    pluginFuncs->setwindow = NPP_SetWindow;
53    pluginFuncs->newstream = NPP_NewStream;
54    pluginFuncs->destroystream = NPP_DestroyStream;
55    pluginFuncs->asfile = NPP_StreamAsFile;
56    pluginFuncs->writeready = NPP_WriteReady;
57    pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
58    pluginFuncs->print = NPP_Print;
59    pluginFuncs->event = NPP_HandleEvent;
60    pluginFuncs->urlnotify = NPP_URLNotify;
61    pluginFuncs->getvalue = NPP_GetValue;
62    pluginFuncs->setvalue = NPP_SetValue;
63
64    return NPERR_NO_ERROR;
65}
66
67
68extern "C"
69NPError __stdcall NP_Shutdown()
70{
71    return NPERR_NO_ERROR;
72}
73
74static void executeScript(const PluginObject* object, const char* script)
75{
76    NPObject *windowScriptObject;
77    browser->getvalue(object->npp, NPNVWindowNPObject, &windowScriptObject);
78
79    NPString npScript;
80    npScript.UTF8Characters = script;
81    npScript.UTF8Length = strlen(script);
82
83    NPVariant browserResult;
84    browser->evaluate(object->npp, windowScriptObject, &npScript, &browserResult);
85    browser->releasevariantvalue(&browserResult);
86}
87
88NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char *argn[], char *argv[], NPSavedData *saved)
89{
90    if (browser->version >= 14) {
91        PluginObject* obj = (PluginObject*)browser->createobject(instance, getPluginClass());
92
93        for (int16 i = 0; i < argc; i++) {
94            if (_stricmp(argn[i], "onstreamload") == 0 && !obj->onStreamLoad)
95                obj->onStreamLoad = _strdup(argv[i]);
96            else if (_stricmp(argn[i], "onStreamDestroy") == 0 && !obj->onStreamDestroy)
97                obj->onStreamDestroy = _strdup(argv[i]);
98            else if (_stricmp(argn[i], "onURLNotify") == 0 && !obj->onURLNotify)
99                obj->onURLNotify = _strdup(argv[i]);
100            else if (_stricmp(argn[i], "onDestroy") == 0 && !obj->onDestroy)
101                obj->onDestroy = _strdup(argv[i]);
102            else if (_stricmp(argn[i], "logSrc") == 0) {
103                for (int i = 0; i < argc; i++)
104                    if (_stricmp(argn[i], "src") == 0)
105                        pluginLog(instance, "src: %s", argv[i]);
106            } else if (_stricmp(argn[i], "testdocumentopenindestroystream") == 0)
107                obj->testDocumentOpenInDestroyStream = TRUE;
108              else if (_stricmp(argn[i], "testwindowopen") == 0)
109                obj->testWindowOpen = TRUE;
110        }
111
112        instance->pdata = obj;
113    }
114
115    return NPERR_NO_ERROR;
116}
117
118NPError NPP_Destroy(NPP instance, NPSavedData **save)
119{
120    PluginObject *obj = (PluginObject*)instance->pdata;
121    if (obj) {
122        if (obj->onStreamLoad)
123            free(obj->onStreamLoad);
124
125        if (obj->onURLNotify)
126            free(obj->onURLNotify);
127
128        if (obj->onStreamDestroy)
129            free(obj->onStreamDestroy);
130
131        if (obj->onDestroy) {
132            executeScript(obj, obj->onDestroy);
133            free(obj->onDestroy);
134        }
135
136        if (obj->logDestroy)
137            printf("PLUGIN: NPP_Destroy\n");
138
139        browser->releaseobject(&obj->header);
140    }
141    return NPERR_NO_ERROR;
142}
143
144NPError NPP_SetWindow(NPP instance, NPWindow *window)
145{
146    PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
147
148    if (obj) {
149        if (obj->testWindowOpen) {
150            testWindowOpen(instance);
151            obj->testWindowOpen = FALSE;
152        }
153    }
154
155    return NPERR_NO_ERROR;
156}
157
158NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16 *stype)
159{
160    PluginObject* obj = (PluginObject*)instance->pdata;
161
162    if (obj->returnErrorFromNewStream)
163        return NPERR_GENERIC_ERROR;
164
165    obj->stream = stream;
166    *stype = NP_ASFILEONLY;
167
168    if (obj->onStreamLoad)
169        executeScript(obj, obj->onStreamLoad);
170
171    return NPERR_NO_ERROR;
172}
173
174NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason)
175{
176    PluginObject* obj = (PluginObject*)instance->pdata;
177
178    if (obj->onStreamDestroy)
179        executeScript(obj, obj->onStreamDestroy);
180
181    if (obj->testDocumentOpenInDestroyStream) {
182        testDocumentOpen(instance);
183    }
184
185    return NPERR_NO_ERROR;
186}
187
188int32 NPP_WriteReady(NPP instance, NPStream *stream)
189{
190    return 0;
191}
192
193int32 NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
194{
195    return 0;
196}
197
198void NPP_StreamAsFile(NPP instance, NPStream *stream, const char *fname)
199{
200}
201
202void NPP_Print(NPP instance, NPPrint *platformPrint)
203{
204}
205
206int16 NPP_HandleEvent(NPP instance, void *event)
207{
208    PluginObject *obj = (PluginObject*)instance->pdata;
209    if (!obj->eventLogging)
210        return 0;
211
212    // FIXME: Implement this
213    return 0;
214}
215
216void NPP_URLNotify(NPP instance, const char *url, NPReason reason, void *notifyData)
217{
218    PluginObject *obj = (PluginObject*)instance->pdata;
219
220    if (obj->onURLNotify)
221        executeScript(obj, obj->onURLNotify);
222
223    handleCallback(obj, url, reason, notifyData);
224}
225
226NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
227{
228    if (variable == NPPVpluginScriptableNPObject) {
229        void **v = (void **)value;
230        PluginObject *obj = (PluginObject*)instance->pdata;
231        // Return value is expected to be retained
232        browser->retainobject((NPObject *)obj);
233        *v = obj;
234        return NPERR_NO_ERROR;
235    }
236    return NPERR_GENERIC_ERROR;
237}
238
239NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
240{
241    return NPERR_GENERIC_ERROR;
242}
243