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