WebCore:
[WebKit-https.git] / WebKitTools / DumpRenderTree / TestNetscapePlugIn.subproj / main.cpp
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "PluginObject.h"
27
28 // Mach-o entry points
29 extern "C" {
30     NPError NP_Initialize(NPNetscapeFuncs *browserFuncs);
31     NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
32     void NP_Shutdown(void);
33 }
34
35 // Mach-o entry points
36 NPError NP_Initialize(NPNetscapeFuncs *browserFuncs)
37 {
38     browser = browserFuncs;
39     return NPERR_NO_ERROR;
40 }
41
42 NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs)
43 {
44     pluginFuncs->version = 11;
45     pluginFuncs->size = sizeof(pluginFuncs);
46     pluginFuncs->newp = NPP_New;
47     pluginFuncs->destroy = NPP_Destroy;
48     pluginFuncs->setwindow = NPP_SetWindow;
49     pluginFuncs->newstream = NPP_NewStream;
50     pluginFuncs->destroystream = NPP_DestroyStream;
51     pluginFuncs->asfile = NPP_StreamAsFile;
52     pluginFuncs->writeready = NPP_WriteReady;
53     pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
54     pluginFuncs->print = NPP_Print;
55     pluginFuncs->event = NPP_HandleEvent;
56     pluginFuncs->urlnotify = NPP_URLNotify;
57     pluginFuncs->getvalue = NPP_GetValue;
58     pluginFuncs->setvalue = NPP_SetValue;
59     
60     return NPERR_NO_ERROR;
61 }
62
63 void NP_Shutdown(void)
64 {
65 }
66
67 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char *argn[], char *argv[], NPSavedData *saved)
68 {
69     if (browser->version >= 14) {
70         PluginObject* obj = (PluginObject*)browser->createobject(instance, getPluginClass());
71     
72         obj->onStreamLoad = NULL;
73         
74         for (int i = 0; i < argc; i++) {
75             if (strcasecmp(argn[i], "onstreamload") == 0 && !obj->onStreamLoad)
76                 obj->onStreamLoad = strdup(argv[i]);
77             else if (strcasecmp(argn[i], "src") == 0 &&
78                      strcasecmp(argv[i], "data:application/x-webkit-test-netscape,returnerrorfromnewstream") == 0)
79                 obj->returnErrorFromNewStream = TRUE;
80             else if (strcasecmp(argn[i], "logfirstsetwindow") == 0)
81                 obj->logSetWindow = TRUE;
82         }
83         
84         instance->pdata = obj;
85     }
86     
87     return NPERR_NO_ERROR;
88 }
89
90 NPError NPP_Destroy(NPP instance, NPSavedData **save)
91 {
92     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
93     if (obj) {
94         if (obj->onStreamLoad)
95             free(obj->onStreamLoad);
96         
97         if (obj->logDestroy)
98             printf("PLUGIN: NPP_Destroy\n");
99
100         browser->releaseobject(&obj->header);
101     }
102     return NPERR_NO_ERROR;
103 }
104
105 NPError NPP_SetWindow(NPP instance, NPWindow *window)
106 {
107     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
108
109     if (obj) {
110         if (obj->logSetWindow) {
111             printf("PLUGIN: NPP_SetWindow: %d %d\n", (int)window->width, (int)window->height);
112             obj->logSetWindow = false;
113         }
114     }
115     
116     return NPERR_NO_ERROR;
117 }
118
119 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16 *stype)
120 {
121     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
122     obj->stream = stream;
123     *stype = NP_ASFILEONLY;
124
125     if (obj->returnErrorFromNewStream)
126         return NPERR_GENERIC_ERROR;
127     
128     if (browser->version >= NPVERS_HAS_RESPONSE_HEADERS)
129         notifyStream(obj, stream->url, stream->headers);
130
131     if (obj->onStreamLoad) {
132         NPObject *windowScriptObject;
133         browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
134                 
135         NPString script;
136         script.UTF8Characters = obj->onStreamLoad;
137         script.UTF8Length = strlen(obj->onStreamLoad);
138         
139         NPVariant browserResult;
140         browser->evaluate(obj->npp, windowScriptObject, &script, &browserResult);
141         browser->releasevariantvalue(&browserResult);
142     }
143     
144     return NPERR_NO_ERROR;
145 }
146
147 NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason)
148 {
149     return NPERR_NO_ERROR;
150 }
151
152 int32 NPP_WriteReady(NPP instance, NPStream *stream)
153 {
154     return 0;
155 }
156
157 int32 NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
158 {
159     return 0;
160 }
161
162 void NPP_StreamAsFile(NPP instance, NPStream *stream, const char *fname)
163 {
164 }
165
166 void NPP_Print(NPP instance, NPPrint *platformPrint)
167 {
168 }
169
170 int16 NPP_HandleEvent(NPP instance, void *event)
171 {
172     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
173     if (!obj->eventLogging)
174         return 0;
175     
176     EventRecord* evt = static_cast<EventRecord*>(event);
177     Point pt = { evt->where.v, evt->where.h };
178     switch (evt->what) {
179         case nullEvent:
180             // these are delivered non-deterministically, don't log.
181             break;
182         case mouseDown:
183             GlobalToLocal(&pt);
184             printf("PLUGIN: mouseDown at (%d, %d)\n", pt.h, pt.v);
185             break;
186         case mouseUp:
187             GlobalToLocal(&pt);
188             printf("PLUGIN: mouseUp at (%d, %d)\n", pt.h, pt.v);
189             break;
190         case keyDown:
191             printf("PLUGIN: keyDown '%c'\n", (char)(evt->message & 0xFF));
192             break;
193         case keyUp:
194             printf("PLUGIN: keyUp '%c'\n", (char)(evt->message & 0xFF));
195             break;
196         case autoKey:
197             printf("PLUGIN: autoKey '%c'\n", (char)(evt->message & 0xFF));
198             break;
199         case updateEvt:
200             printf("PLUGIN: updateEvt\n");
201             break;
202         case diskEvt:
203             printf("PLUGIN: diskEvt\n");
204             break;
205         case activateEvt:
206             printf("PLUGIN: activateEvt\n");
207             break;
208         case osEvt:
209             printf("PLUGIN: osEvt - ");
210             switch ((evt->message & 0xFF000000) >> 24) {
211                 case suspendResumeMessage:
212                     printf("%s\n", (evt->message & 0x1) ? "resume" : "suspend");
213                     break;
214                 case mouseMovedMessage:
215                     printf("mouseMoved\n");
216                     break;
217                 default:
218                     printf("%08lX\n", evt->message);
219             }
220             break;
221         case kHighLevelEvent:
222             printf("PLUGIN: kHighLevelEvent\n");
223             break;
224         // NPAPI events
225         case getFocusEvent:
226             printf("PLUGIN: getFocusEvent\n");
227             break;
228         case loseFocusEvent:
229             printf("PLUGIN: loseFocusEvent\n");
230             break;
231         case adjustCursorEvent:
232             printf("PLUGIN: adjustCursorEvent\n");
233             break;
234         default:
235             printf("PLUGIN: event %d\n", evt->what);
236     }
237     
238     return 0;
239 }
240
241 void NPP_URLNotify(NPP instance, const char *url, NPReason reason, void *notifyData)
242 {
243     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
244         
245     handleCallback(obj, url, reason, notifyData);
246 }
247
248 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
249 {
250     if (variable == NPPVpluginScriptableNPObject) {
251         void **v = (void **)value;
252         PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
253         // Return value is expected to be retained
254         browser->retainobject((NPObject *)obj);
255         *v = obj;
256         return NPERR_NO_ERROR;
257     }
258     return NPERR_GENERIC_ERROR;
259 }
260
261 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
262 {
263     return NPERR_GENERIC_ERROR;
264 }