Update TestNetscapePlugIn to build 64-bit using the Cocoa event model.
[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 #if __LP64__
29 #define USE_COCOA_EVENT_MODEL 1
30 #endif
31
32 // Mach-o entry points
33 extern "C" {
34     NPError NP_Initialize(NPNetscapeFuncs *browserFuncs);
35     NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
36     void NP_Shutdown(void);
37 }
38
39 // Mach-o entry points
40 NPError NP_Initialize(NPNetscapeFuncs *browserFuncs)
41 {
42     browser = browserFuncs;
43     return NPERR_NO_ERROR;
44 }
45
46 NPError 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 void NP_Shutdown(void)
68 {
69 }
70
71 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char *argn[], char *argv[], NPSavedData *saved)
72 {
73 #if USE_COCOA_EVENT_MODEL
74     // If the browser supports the Cocoa event model, enable it.
75     NPBool supportsCocoa;
76     if (browser->getvalue(instance, NPNVsupportsCocoaBool, &supportsCocoa) != NPERR_NO_ERROR)
77         supportsCocoa = FALSE;
78
79     if (!supportsCocoa)
80         return NPERR_INCOMPATIBLE_VERSION_ERROR;
81
82     browser->setvalue(instance, NPPVpluginEventModel, (void *)NPEventModelCocoa);
83 #endif
84
85     if (browser->version >= 14) {
86         PluginObject* obj = (PluginObject*)browser->createobject(instance, getPluginClass());
87     
88         obj->onStreamLoad = NULL;
89         
90         for (int i = 0; i < argc; i++) {
91             if (strcasecmp(argn[i], "onstreamload") == 0 && !obj->onStreamLoad)
92                 obj->onStreamLoad = strdup(argv[i]);
93             else if (strcasecmp(argn[i], "src") == 0 &&
94                      strcasecmp(argv[i], "data:application/x-webkit-test-netscape,returnerrorfromnewstream") == 0)
95                 obj->returnErrorFromNewStream = TRUE;
96             else if (strcasecmp(argn[i], "logfirstsetwindow") == 0)
97                 obj->logSetWindow = TRUE;
98         }
99         
100         instance->pdata = obj;
101     }
102     
103     return NPERR_NO_ERROR;
104 }
105
106 NPError NPP_Destroy(NPP instance, NPSavedData **save)
107 {
108     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
109     if (obj) {
110         if (obj->onStreamLoad)
111             free(obj->onStreamLoad);
112         
113         if (obj->logDestroy)
114             printf("PLUGIN: NPP_Destroy\n");
115
116         browser->releaseobject(&obj->header);
117     }
118     return NPERR_NO_ERROR;
119 }
120
121 NPError NPP_SetWindow(NPP instance, NPWindow *window)
122 {
123     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
124
125     if (obj) {
126         if (obj->logSetWindow) {
127             printf("PLUGIN: NPP_SetWindow: %d %d\n", (int)window->width, (int)window->height);
128             obj->logSetWindow = false;
129         }
130     }
131     
132     return NPERR_NO_ERROR;
133 }
134
135 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16 *stype)
136 {
137     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
138     obj->stream = stream;
139     *stype = NP_ASFILEONLY;
140
141     if (obj->returnErrorFromNewStream)
142         return NPERR_GENERIC_ERROR;
143     
144     if (browser->version >= NPVERS_HAS_RESPONSE_HEADERS)
145         notifyStream(obj, stream->url, stream->headers);
146
147     if (obj->onStreamLoad) {
148         NPObject *windowScriptObject;
149         browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
150                 
151         NPString script;
152         script.UTF8Characters = obj->onStreamLoad;
153         script.UTF8Length = strlen(obj->onStreamLoad);
154         
155         NPVariant browserResult;
156         browser->evaluate(obj->npp, windowScriptObject, &script, &browserResult);
157         browser->releasevariantvalue(&browserResult);
158     }
159     
160     return NPERR_NO_ERROR;
161 }
162
163 NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason)
164 {
165     return NPERR_NO_ERROR;
166 }
167
168 int32 NPP_WriteReady(NPP instance, NPStream *stream)
169 {
170     return 0;
171 }
172
173 int32 NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
174 {
175     return 0;
176 }
177
178 void NPP_StreamAsFile(NPP instance, NPStream *stream, const char *fname)
179 {
180 }
181
182 void NPP_Print(NPP instance, NPPrint *platformPrint)
183 {
184 }
185
186 int16 NPP_HandleEvent(NPP instance, void *event)
187 {
188     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
189     if (!obj->eventLogging)
190         return 0;
191
192 #if USE_COCOA_EVENT_MODEL
193     // FIXME: Generate output that will match the Carbon event model
194     // so that the layout tests using this plug-in will work in either model.
195     NPCocoaEvent *cocoaEvent = static_cast<NPCocoaEvent*>(event);
196     switch (cocoaEvent->type) {
197         case NPCocoaEventWindowFocusChanged:
198         case NPCocoaEventFocusChanged:
199             return 1;
200
201         case NPCocoaEventDrawRect:
202             return 1;
203
204         case NPCocoaEventKeyDown:
205         case NPCocoaEventKeyUp:
206         case NPCocoaEventFlagsChanged:
207             return 1;
208
209         case NPCocoaEventMouseDown:
210         case NPCocoaEventMouseUp:
211
212         case NPCocoaEventMouseMoved:
213         case NPCocoaEventMouseEntered:
214         case NPCocoaEventMouseExited:
215         case NPCocoaEventMouseDragged:
216         case NPCocoaEventScrollWheel:
217             return 1;
218     }
219 #else
220     EventRecord* evt = static_cast<EventRecord*>(event);
221     Point pt = { evt->where.v, evt->where.h };
222     switch (evt->what) {
223         case nullEvent:
224             // these are delivered non-deterministically, don't log.
225             break;
226         case mouseDown:
227             GlobalToLocal(&pt);
228             printf("PLUGIN: mouseDown at (%d, %d)\n", pt.h, pt.v);
229             break;
230         case mouseUp:
231             GlobalToLocal(&pt);
232             printf("PLUGIN: mouseUp at (%d, %d)\n", pt.h, pt.v);
233             break;
234         case keyDown:
235             printf("PLUGIN: keyDown '%c'\n", (char)(evt->message & 0xFF));
236             break;
237         case keyUp:
238             printf("PLUGIN: keyUp '%c'\n", (char)(evt->message & 0xFF));
239             break;
240         case autoKey:
241             printf("PLUGIN: autoKey '%c'\n", (char)(evt->message & 0xFF));
242             break;
243         case updateEvt:
244             printf("PLUGIN: updateEvt\n");
245             break;
246         case diskEvt:
247             printf("PLUGIN: diskEvt\n");
248             break;
249         case activateEvt:
250             printf("PLUGIN: activateEvt\n");
251             break;
252         case osEvt:
253             printf("PLUGIN: osEvt - ");
254             switch ((evt->message & 0xFF000000) >> 24) {
255                 case suspendResumeMessage:
256                     printf("%s\n", (evt->message & 0x1) ? "resume" : "suspend");
257                     break;
258                 case mouseMovedMessage:
259                     printf("mouseMoved\n");
260                     break;
261                 default:
262                     printf("%08lX\n", evt->message);
263             }
264             break;
265         case kHighLevelEvent:
266             printf("PLUGIN: kHighLevelEvent\n");
267             break;
268         // NPAPI events
269         case getFocusEvent:
270             printf("PLUGIN: getFocusEvent\n");
271             break;
272         case loseFocusEvent:
273             printf("PLUGIN: loseFocusEvent\n");
274             break;
275         case adjustCursorEvent:
276             printf("PLUGIN: adjustCursorEvent\n");
277             break;
278         default:
279             printf("PLUGIN: event %d\n", evt->what);
280     }
281 #endif
282     return 0;
283 }
284
285 void NPP_URLNotify(NPP instance, const char *url, NPReason reason, void *notifyData)
286 {
287     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
288         
289     handleCallback(obj, url, reason, notifyData);
290 }
291
292 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
293 {
294     if (variable == NPPVpluginScriptableNPObject) {
295         void **v = (void **)value;
296         PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
297         // Return value is expected to be retained
298         browser->retainobject((NPObject *)obj);
299         *v = obj;
300         return NPERR_NO_ERROR;
301     }
302     return NPERR_GENERIC_ERROR;
303 }
304
305 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
306 {
307     return NPERR_GENERIC_ERROR;
308 }