2010-07-14 Sheriff Bot <webkit.review.bot@gmail.com>
[WebKit-https.git] / WebKitTools / DumpRenderTree / TestNetscapePlugIn / 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 #include "PluginObject.h"
27
28 #if XP_WIN
29 #define STDCALL __stdcall
30
31 static inline int strcasecmp(const char* s1, const char* s2)
32 {
33     return _stricmp(s1, s2);
34 }
35
36 #else
37 #define STDCALL
38 #endif
39
40 // Entry points
41 extern "C"
42 NPError STDCALL NP_Initialize(NPNetscapeFuncs *browserFuncs)
43 {
44     browser = browserFuncs;
45     return NPERR_NO_ERROR;
46 }
47
48 extern "C"
49 NPError STDCALL NP_GetEntryPoints(NPPluginFuncs *pluginFuncs)
50 {
51     pluginFuncs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
52     pluginFuncs->size = sizeof(pluginFuncs);
53     pluginFuncs->newp = NPP_New;
54     pluginFuncs->destroy = NPP_Destroy;
55     pluginFuncs->setwindow = NPP_SetWindow;
56     pluginFuncs->newstream = NPP_NewStream;
57     pluginFuncs->destroystream = NPP_DestroyStream;
58     pluginFuncs->asfile = NPP_StreamAsFile;
59     pluginFuncs->writeready = NPP_WriteReady;
60     pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
61     pluginFuncs->print = NPP_Print;
62     pluginFuncs->event = NPP_HandleEvent;
63     pluginFuncs->urlnotify = NPP_URLNotify;
64     pluginFuncs->getvalue = NPP_GetValue;
65     pluginFuncs->setvalue = NPP_SetValue;
66     
67     return NPERR_NO_ERROR;
68 }
69
70 extern "C"
71 void STDCALL NP_Shutdown(void)
72 {
73 }
74
75 static void executeScript(const PluginObject* obj, const char* script);
76
77 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved)
78 {
79     bool forceCarbon = false;
80
81 #if XP_MACOSX
82     NPEventModel eventModel;
83     
84     // Always turn on the CG model
85     NPBool supportsCoreGraphics;
86     if (browser->getvalue(instance, NPNVsupportsCoreGraphicsBool, &supportsCoreGraphics) != NPERR_NO_ERROR)
87         supportsCoreGraphics = false;
88     
89     if (!supportsCoreGraphics)
90         return NPERR_INCOMPATIBLE_VERSION_ERROR;
91
92     NPDrawingModel drawingModelToUse = NPDrawingModelCoreGraphics;
93
94     NPBool supportsCoreAnimation;
95     if (browser->getvalue(instance, NPNVsupportsCoreAnimationBool, &supportsCoreAnimation) != NPERR_NO_ERROR)
96         supportsCoreAnimation = false;
97
98 #ifndef NP_NO_CARBON
99     NPBool supportsCarbon = false;
100 #endif
101     NPBool supportsCocoa = false;
102
103 #ifndef NP_NO_CARBON
104     // A browser that doesn't know about NPNVsupportsCarbonBool is one that only supports Carbon event model.
105     if (browser->getvalue(instance, NPNVsupportsCarbonBool, &supportsCarbon) != NPERR_NO_ERROR)
106         supportsCarbon = true;
107 #endif
108
109     if (browser->getvalue(instance, NPNVsupportsCocoaBool, &supportsCocoa) != NPERR_NO_ERROR)
110         supportsCocoa = false;
111
112     if (supportsCocoa && !forceCarbon) {
113         eventModel = NPEventModelCocoa;
114 #ifndef NP_NO_CARBON
115     } else if (supportsCarbon) {
116         eventModel = NPEventModelCarbon;
117 #endif
118     } else {
119         return NPERR_INCOMPATIBLE_VERSION_ERROR;
120     }
121
122      browser->setvalue(instance, NPPVpluginEventModel, (void *)eventModel);
123 #endif // XP_MACOSX
124
125     PluginObject* obj = (PluginObject*)browser->createobject(instance, getPluginClass());
126     instance->pdata = obj;
127
128 #if XP_MACOSX
129     obj->eventModel = eventModel;
130 #if !defined(BUILDING_ON_TIGER)
131     obj->coreAnimationLayer = 0;
132 #endif
133 #endif // XP_MACOSX
134
135     for (int i = 0; i < argc; i++) {
136         if (strcasecmp(argn[i], "onstreamload") == 0 && !obj->onStreamLoad)
137             obj->onStreamLoad = strdup(argv[i]);
138         else if (strcasecmp(argn[i], "onStreamDestroy") == 0 && !obj->onStreamDestroy)
139             obj->onStreamDestroy = strdup(argv[i]);
140         else if (strcasecmp(argn[i], "onURLNotify") == 0 && !obj->onURLNotify)
141             obj->onURLNotify = strdup(argv[i]);
142         else if (strcasecmp(argn[i], "src") == 0 &&
143                  strcasecmp(argv[i], "data:application/x-webkit-test-netscape,returnerrorfromnewstream") == 0)
144             obj->returnErrorFromNewStream = TRUE;
145         else if (strcasecmp(argn[i], "onSetWindow") == 0 && !obj->onSetWindow)
146             obj->onSetWindow = strdup(argv[i]);
147         else if (strcasecmp(argn[i], "logfirstsetwindow") == 0)
148             obj->logSetWindow = TRUE;
149         else if (strcasecmp(argn[i], "testnpruntime") == 0)
150             testNPRuntime(instance);
151         else if (strcasecmp(argn[i], "forcecarbon") == 0)
152             forceCarbon = true;
153         else if (strcasecmp(argn[i], "logSrc") == 0) {
154             for (int i = 0; i < argc; i++)
155                 if (strcasecmp(argn[i], "src") == 0)
156                     pluginLog(instance, "src: %s", argv[i]);
157         } else if (strcasecmp(argn[i], "cleardocumentduringnew") == 0)
158             executeScript(obj, "document.body.innerHTML = ''");
159         else if (!strcasecmp(argn[i], "ondestroy"))
160             obj->onDestroy = strdup(argv[i]);
161         else if (strcasecmp(argn[i], "testdocumentopenindestroystream") == 0)
162             obj->testDocumentOpenInDestroyStream = TRUE;
163         else if (strcasecmp(argn[i], "testwindowopen") == 0)
164             obj->testWindowOpen = TRUE;
165         else if (strcasecmp(argn[i], "drawingmodel") == 0) {
166 #if XP_MACOSX && !defined(BUILDING_ON_TIGER)
167             const char* value = argv[i];
168             if (strcasecmp(value, "coreanimation") == 0) {
169                 if (supportsCoreAnimation)
170                     drawingModelToUse = NPDrawingModelCoreAnimation;
171                 else
172                     return NPERR_INCOMPATIBLE_VERSION_ERROR;
173              } else if (strcasecmp(value, "coregraphics") == 0) {
174                 if (supportsCoreGraphics)
175                     drawingModelToUse = NPDrawingModelCoreGraphics;
176                 else
177                     return NPERR_INCOMPATIBLE_VERSION_ERROR;
178              } else
179                 return NPERR_INCOMPATIBLE_VERSION_ERROR;
180 #endif
181         } else if (strcasecmp(argn[i], "testGetURLOnDestroy") == 0) {
182 #if XP_WIN
183             // FIXME: When https://bugs.webkit.org/show_bug.cgi?id=41831 is fixed, this #ifdef can be removed.
184             obj->testGetURLOnDestroy = TRUE;
185 #endif
186         } else if (strcasecmp(argn[i], "src") == 0 && strstr(argv[i], "plugin-document-has-focus.pl"))
187             obj->testKeyboardFocusForPlugins = TRUE;
188     }
189
190 #if XP_MACOSX
191     browser->setvalue(instance, NPPVpluginDrawingModel, (void *)drawingModelToUse);
192 #if !defined(BUILDING_ON_TIGER)
193     if (drawingModelToUse == NPDrawingModelCoreAnimation)
194         obj->coreAnimationLayer = createCoreAnimationLayer();
195 #endif
196 #endif
197
198     browser->getvalue(instance, NPNVprivateModeBool, (void *)&obj->cachedPrivateBrowsingMode);
199         
200     return NPERR_NO_ERROR;
201 }
202
203 NPError NPP_Destroy(NPP instance, NPSavedData **save)
204 {
205     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
206     if (obj) {
207         if (obj->testGetURLOnDestroy)
208             browser->geturlnotify(obj->npp, "about:blank", "", 0);
209
210         if (obj->onDestroy) {
211             executeScript(obj, obj->onDestroy);
212             free(obj->onDestroy);
213         }
214
215         if (obj->onStreamLoad)
216             free(obj->onStreamLoad);
217
218         if (obj->onStreamDestroy)
219             free(obj->onStreamDestroy);
220
221         if (obj->onURLNotify)
222             free(obj->onURLNotify);
223
224         if (obj->onSetWindow)
225             free(obj->onSetWindow);
226         
227         if (obj->logDestroy)
228             pluginLog(instance, "NPP_Destroy");
229
230 #if XP_MACOSX && !defined(BUILDING_ON_TIGER)
231         if (obj->coreAnimationLayer)
232             CFRelease(obj->coreAnimationLayer);
233 #endif
234
235         browser->releaseobject(&obj->header);
236     }
237     return NPERR_NO_ERROR;
238 }
239
240 NPError NPP_SetWindow(NPP instance, NPWindow *window)
241 {
242     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
243
244     if (obj) {
245         obj->lastWindow = *window;
246
247         if (obj->logSetWindow) {
248             pluginLog(instance, "NPP_SetWindow: %d %d", (int)window->width, (int)window->height);
249             obj->logSetWindow = FALSE;
250         }
251
252         if (obj->onSetWindow)
253             executeScript(obj, obj->onSetWindow);
254
255         if (obj->testWindowOpen) {
256             testWindowOpen(instance);
257             obj->testWindowOpen = FALSE;
258         }
259
260         if (obj->testKeyboardFocusForPlugins) {
261             obj->eventLogging = true;
262             executeScript(obj, "eventSender.keyDown('A');");
263         }
264     }
265     
266     return NPERR_NO_ERROR;
267 }
268
269 static void executeScript(const PluginObject* obj, const char* script)
270 {
271     NPObject *windowScriptObject;
272     browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
273
274     NPString npScript;
275     npScript.UTF8Characters = script;
276     npScript.UTF8Length = strlen(script);
277
278     NPVariant browserResult;
279     browser->evaluate(obj->npp, windowScriptObject, &npScript, &browserResult);
280     browser->releasevariantvalue(&browserResult);
281 }
282
283 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16_t *stype)
284 {
285     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
286     obj->stream = stream;
287     *stype = NP_NORMAL;
288
289     if (obj->returnErrorFromNewStream)
290         return NPERR_GENERIC_ERROR;
291     
292     if (browser->version >= NPVERS_HAS_RESPONSE_HEADERS)
293         notifyStream(obj, stream->url, stream->headers);
294
295     if (obj->onStreamLoad)
296         executeScript(obj, obj->onStreamLoad);
297
298     return NPERR_NO_ERROR;
299 }
300
301 NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason)
302 {
303     PluginObject* obj = (PluginObject*)instance->pdata;
304
305     if (obj->onStreamDestroy) {
306         NPObject* windowObject = 0;
307         NPError error = browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
308         
309         if (error == NPERR_NO_ERROR) {
310             NPVariant onStreamDestroyVariant;
311             if (browser->getproperty(instance, windowObject, browser->getstringidentifier(obj->onStreamDestroy), &onStreamDestroyVariant)) {
312                 if (NPVARIANT_IS_OBJECT(onStreamDestroyVariant)) {
313                     NPObject* onStreamDestroyFunction = NPVARIANT_TO_OBJECT(onStreamDestroyVariant);
314
315                     NPVariant reasonVariant;
316                     INT32_TO_NPVARIANT(reason, reasonVariant);
317
318                     NPVariant result;
319                     browser->invokeDefault(instance, onStreamDestroyFunction, &reasonVariant, 1, &result);
320                     browser->releasevariantvalue(&result);
321                 }
322                 browser->releasevariantvalue(&onStreamDestroyVariant);
323             }
324             browser->releaseobject(windowObject);
325         }
326     }
327
328     if (obj->testDocumentOpenInDestroyStream) {
329         testDocumentOpen(instance);
330         obj->testDocumentOpenInDestroyStream = FALSE;
331     }
332
333     return NPERR_NO_ERROR;
334 }
335
336 int32_t NPP_WriteReady(NPP instance, NPStream *stream)
337 {
338     return 4096;
339 }
340
341 int32_t NPP_Write(NPP instance, NPStream *stream, int32_t offset, int32_t len, void *buffer)
342 {
343     PluginObject* obj = (PluginObject*)instance->pdata;
344
345     if (obj->returnNegativeOneFromWrite)
346         return -1;
347
348     return len;
349 }
350
351 void NPP_StreamAsFile(NPP instance, NPStream *stream, const char *fname)
352 {
353 }
354
355 void NPP_Print(NPP instance, NPPrint *platformPrint)
356 {
357 }
358
359 #if XP_MACOSX
360 #ifndef NP_NO_CARBON
361 static int16_t handleEventCarbon(NPP instance, PluginObject* obj, EventRecord* event)
362 {
363     Point pt = { event->where.v, event->where.h };
364
365     switch (event->what) {
366         case nullEvent:
367             // these are delivered non-deterministically, don't log.
368             break;
369         case mouseDown:
370             GlobalToLocal(&pt);
371             pluginLog(instance, "mouseDown at (%d, %d)", pt.h, pt.v);
372             break;
373         case mouseUp:
374             GlobalToLocal(&pt);
375             pluginLog(instance, "mouseUp at (%d, %d)", pt.h, pt.v);
376             break;
377         case keyDown:
378             pluginLog(instance, "keyDown '%c'", (char)(event->message & 0xFF));
379             break;
380         case keyUp:
381             pluginLog(instance, "keyUp '%c'", (char)(event->message & 0xFF));
382             if (obj->testKeyboardFocusForPlugins) {
383                 obj->eventLogging = false;
384                 obj->testKeyboardFocusForPlugins = FALSE;
385                 executeScript(obj, "layoutTestController.notifyDone();");
386             }
387             break;
388         case autoKey:
389             pluginLog(instance, "autoKey '%c'", (char)(event->message & 0xFF));
390             break;
391         case updateEvt:
392             pluginLog(instance, "updateEvt");
393             break;
394         case diskEvt:
395             pluginLog(instance, "diskEvt");
396             break;
397         case activateEvt:
398             pluginLog(instance, "activateEvt");
399             break;
400         case osEvt:
401             printf("PLUGIN: osEvt - ");
402             switch ((event->message & 0xFF000000) >> 24) {
403                 case suspendResumeMessage:
404                     printf("%s\n", (event->message & 0x1) ? "resume" : "suspend");
405                     break;
406                 case mouseMovedMessage:
407                     printf("mouseMoved\n");
408                     break;
409                 default:
410                     printf("%08lX\n", event->message);
411             }
412             break;
413         case kHighLevelEvent:
414             pluginLog(instance, "kHighLevelEvent");
415             break;
416         // NPAPI events
417         case getFocusEvent:
418             pluginLog(instance, "getFocusEvent");
419             break;
420         case loseFocusEvent:
421             pluginLog(instance, "loseFocusEvent");
422             break;
423         case adjustCursorEvent:
424             pluginLog(instance, "adjustCursorEvent");
425             break;
426         default:
427             pluginLog(instance, "event %d", event->what);
428     }
429     
430     return 0;
431 }
432 #endif
433
434 static int16_t handleEventCocoa(NPP instance, PluginObject* obj, NPCocoaEvent* event)
435 {
436     switch (event->type) {
437         case NPCocoaEventWindowFocusChanged:
438             
439         case NPCocoaEventFocusChanged:
440             if (event->data.focus.hasFocus)
441                 pluginLog(instance, "getFocusEvent");
442             else
443                 pluginLog(instance, "loseFocusEvent");
444             return 1;
445
446         case NPCocoaEventDrawRect:
447             return 1;
448
449         case NPCocoaEventKeyDown:
450             if (event->data.key.characters)
451                 pluginLog(instance, "keyDown '%c'", CFStringGetCharacterAtIndex(reinterpret_cast<CFStringRef>(event->data.key.characters), 0));
452             return 1;
453
454         case NPCocoaEventKeyUp:
455             if (event->data.key.characters) {
456                 pluginLog(instance, "keyUp '%c'", CFStringGetCharacterAtIndex(reinterpret_cast<CFStringRef>(event->data.key.characters), 0));
457                 if (obj->testKeyboardFocusForPlugins) {
458                     obj->eventLogging = false;
459                     obj->testKeyboardFocusForPlugins = FALSE;
460                     executeScript(obj, "layoutTestController.notifyDone();");
461                 }
462             }
463             return 1;
464
465         case NPCocoaEventFlagsChanged:
466             return 1;
467
468         case NPCocoaEventMouseDown:
469             pluginLog(instance, "mouseDown at (%d, %d)", 
470                    (int)event->data.mouse.pluginX,
471                    (int)event->data.mouse.pluginY);
472             return 1;
473         case NPCocoaEventMouseUp:
474             pluginLog(instance, "mouseUp at (%d, %d)", 
475                    (int)event->data.mouse.pluginX,
476                    (int)event->data.mouse.pluginY);
477             return 1;
478             
479         case NPCocoaEventMouseMoved:
480         case NPCocoaEventMouseEntered:
481         case NPCocoaEventMouseExited:
482         case NPCocoaEventMouseDragged:
483         case NPCocoaEventScrollWheel:
484         case NPCocoaEventTextInput:
485             return 1;
486     }
487     
488     return 0;
489 }
490
491 #endif // XP_MACOSX
492
493 int16_t NPP_HandleEvent(NPP instance, void *event)
494 {
495     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
496     if (!obj->eventLogging)
497         return 0;
498
499 #if XP_MACOSX
500 #ifndef NP_NO_CARBON
501     if (obj->eventModel == NPEventModelCarbon)
502         return handleEventCarbon(instance, obj, static_cast<EventRecord*>(event));
503 #endif
504
505     assert(obj->eventModel == NPEventModelCocoa);
506     return handleEventCocoa(instance, obj, static_cast<NPCocoaEvent*>(event));
507 #else
508     // FIXME: Implement for other platforms.
509     return 0;
510 #endif // XP_MACOSX
511 }
512
513 void NPP_URLNotify(NPP instance, const char *url, NPReason reason, void *notifyData)
514 {
515     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
516  
517      if (obj->onURLNotify)
518          executeScript(obj, obj->onURLNotify);
519
520     handleCallback(obj, url, reason, notifyData);
521 }
522
523 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
524 {
525     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
526
527     if (variable == NPPVpluginScriptableNPObject) {
528         void **v = (void **)value;
529         // Return value is expected to be retained
530         browser->retainobject((NPObject *)obj);
531         *v = obj;
532         return NPERR_NO_ERROR;
533     }
534     
535 #if XP_MACOSX && !defined(BUILDING_ON_TIGER)
536     if (variable == NPPVpluginCoreAnimationLayer) {
537         if (!obj->coreAnimationLayer)
538             return NPERR_GENERIC_ERROR;
539         
540         void **v = (void **)value;
541         *v = (void*)CFRetain(obj->coreAnimationLayer);
542         return NPERR_NO_ERROR;
543     }
544 #endif
545     
546     return NPERR_GENERIC_ERROR;
547 }
548
549 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
550 {
551     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
552
553     switch (variable) {
554         case NPNVprivateModeBool:
555             obj->cachedPrivateBrowsingMode = *(NPBool*)value;
556             return NPERR_NO_ERROR;
557         default:
558             return NPERR_GENERIC_ERROR;
559     }
560 }