aeaee3071971de24936bd99eaa91ee1909e19c25
[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") && strstr(argv[i], "plugin-document-has-focus.pl"))
187             obj->testKeyboardFocusForPlugins = TRUE;
188         else if (!strcasecmp(argn[i], "evaluatescript")) {
189             char* script = argv[i];
190             if (script == strstr(script, "mouse::")) {
191                 obj->mouseDownForEvaluateScript = true;
192                 obj->evaluateScriptOnMouseDownOrKeyDown = strdup(script + sizeof("mouse::") - 1);
193             } else if (script == strstr(script, "key::")) {
194                 obj->evaluateScriptOnMouseDownOrKeyDown = strdup(script + sizeof("key::") - 1);
195             }
196             // When testing evaluate script on mouse-down or key-down, allow event logging to handle events.
197             if (obj->evaluateScriptOnMouseDownOrKeyDown)
198                 obj->eventLogging = true;
199         }
200     }
201
202 #if XP_MACOSX
203     browser->setvalue(instance, NPPVpluginDrawingModel, (void *)drawingModelToUse);
204 #if !defined(BUILDING_ON_TIGER)
205     if (drawingModelToUse == NPDrawingModelCoreAnimation)
206         obj->coreAnimationLayer = createCoreAnimationLayer();
207 #endif
208 #endif
209
210     browser->getvalue(instance, NPNVprivateModeBool, (void *)&obj->cachedPrivateBrowsingMode);
211         
212     return NPERR_NO_ERROR;
213 }
214
215 NPError NPP_Destroy(NPP instance, NPSavedData **save)
216 {
217     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
218     if (obj) {
219         if (obj->testGetURLOnDestroy)
220             browser->geturlnotify(obj->npp, "about:blank", "", 0);
221
222         if (obj->onDestroy) {
223             executeScript(obj, obj->onDestroy);
224             free(obj->onDestroy);
225         }
226
227         if (obj->onStreamLoad)
228             free(obj->onStreamLoad);
229
230         if (obj->onStreamDestroy)
231             free(obj->onStreamDestroy);
232
233         if (obj->onURLNotify)
234             free(obj->onURLNotify);
235
236         if (obj->onSetWindow)
237             free(obj->onSetWindow);
238         
239         if (obj->logDestroy)
240             pluginLog(instance, "NPP_Destroy");
241
242 #if XP_MACOSX && !defined(BUILDING_ON_TIGER)
243         if (obj->coreAnimationLayer)
244             CFRelease(obj->coreAnimationLayer);
245 #endif
246
247         browser->releaseobject(&obj->header);
248     }
249     return NPERR_NO_ERROR;
250 }
251
252 NPError NPP_SetWindow(NPP instance, NPWindow *window)
253 {
254     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
255
256     if (obj) {
257         obj->lastWindow = *window;
258
259         if (obj->logSetWindow) {
260             pluginLog(instance, "NPP_SetWindow: %d %d", (int)window->width, (int)window->height);
261             obj->logSetWindow = FALSE;
262         }
263
264         if (obj->onSetWindow)
265             executeScript(obj, obj->onSetWindow);
266
267         if (obj->testWindowOpen) {
268             testWindowOpen(instance);
269             obj->testWindowOpen = FALSE;
270         }
271
272         if (obj->testKeyboardFocusForPlugins) {
273             obj->eventLogging = true;
274             executeScript(obj, "eventSender.keyDown('A');");
275         }
276     }
277     
278     return NPERR_NO_ERROR;
279 }
280
281 static void executeScript(const PluginObject* obj, const char* script)
282 {
283     NPObject *windowScriptObject;
284     browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
285
286     NPString npScript;
287     npScript.UTF8Characters = script;
288     npScript.UTF8Length = strlen(script);
289
290     NPVariant browserResult;
291     browser->evaluate(obj->npp, windowScriptObject, &npScript, &browserResult);
292     browser->releasevariantvalue(&browserResult);
293 }
294
295 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16_t *stype)
296 {
297     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
298     obj->stream = stream;
299     *stype = NP_NORMAL;
300
301     if (obj->returnErrorFromNewStream)
302         return NPERR_GENERIC_ERROR;
303     
304     if (browser->version >= NPVERS_HAS_RESPONSE_HEADERS)
305         notifyStream(obj, stream->url, stream->headers);
306
307     if (obj->onStreamLoad)
308         executeScript(obj, obj->onStreamLoad);
309
310     return NPERR_NO_ERROR;
311 }
312
313 NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason)
314 {
315     PluginObject* obj = (PluginObject*)instance->pdata;
316
317     if (obj->onStreamDestroy) {
318         NPObject* windowObject = 0;
319         NPError error = browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
320         
321         if (error == NPERR_NO_ERROR) {
322             NPVariant onStreamDestroyVariant;
323             if (browser->getproperty(instance, windowObject, browser->getstringidentifier(obj->onStreamDestroy), &onStreamDestroyVariant)) {
324                 if (NPVARIANT_IS_OBJECT(onStreamDestroyVariant)) {
325                     NPObject* onStreamDestroyFunction = NPVARIANT_TO_OBJECT(onStreamDestroyVariant);
326
327                     NPVariant reasonVariant;
328                     INT32_TO_NPVARIANT(reason, reasonVariant);
329
330                     NPVariant result;
331                     browser->invokeDefault(instance, onStreamDestroyFunction, &reasonVariant, 1, &result);
332                     browser->releasevariantvalue(&result);
333                 }
334                 browser->releasevariantvalue(&onStreamDestroyVariant);
335             }
336             browser->releaseobject(windowObject);
337         }
338     }
339
340     if (obj->testDocumentOpenInDestroyStream) {
341         testDocumentOpen(instance);
342         obj->testDocumentOpenInDestroyStream = FALSE;
343     }
344
345     return NPERR_NO_ERROR;
346 }
347
348 int32_t NPP_WriteReady(NPP instance, NPStream *stream)
349 {
350     return 4096;
351 }
352
353 int32_t NPP_Write(NPP instance, NPStream *stream, int32_t offset, int32_t len, void *buffer)
354 {
355     PluginObject* obj = (PluginObject*)instance->pdata;
356
357     if (obj->returnNegativeOneFromWrite)
358         return -1;
359
360     return len;
361 }
362
363 void NPP_StreamAsFile(NPP instance, NPStream *stream, const char *fname)
364 {
365 }
366
367 void NPP_Print(NPP instance, NPPrint *platformPrint)
368 {
369 }
370
371 #if XP_MACOSX
372 #ifndef NP_NO_CARBON
373 static int16_t handleEventCarbon(NPP instance, PluginObject* obj, EventRecord* event)
374 {
375     Point pt = { event->where.v, event->where.h };
376
377     switch (event->what) {
378         case nullEvent:
379             // these are delivered non-deterministically, don't log.
380             break;
381         case mouseDown:
382             GlobalToLocal(&pt);
383             pluginLog(instance, "mouseDown at (%d, %d)", pt.h, pt.v);
384             if (obj->evaluateScriptOnMouseDownOrKeyDown && obj->mouseDownForEvaluateScript)
385                 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
386             break;
387         case mouseUp:
388             GlobalToLocal(&pt);
389             pluginLog(instance, "mouseUp at (%d, %d)", pt.h, pt.v);
390             break;
391         case keyDown:
392             pluginLog(instance, "keyDown '%c'", (char)(event->message & 0xFF));
393             if (obj->evaluateScriptOnMouseDownOrKeyDown && !obj->mouseDownForEvaluateScript)
394                 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
395             break;
396         case keyUp:
397             pluginLog(instance, "keyUp '%c'", (char)(event->message & 0xFF));
398             if (obj->testKeyboardFocusForPlugins) {
399                 obj->eventLogging = false;
400                 obj->testKeyboardFocusForPlugins = FALSE;
401                 executeScript(obj, "layoutTestController.notifyDone();");
402             }
403             break;
404         case autoKey:
405             pluginLog(instance, "autoKey '%c'", (char)(event->message & 0xFF));
406             break;
407         case updateEvt:
408             pluginLog(instance, "updateEvt");
409             break;
410         case diskEvt:
411             pluginLog(instance, "diskEvt");
412             break;
413         case activateEvt:
414             pluginLog(instance, "activateEvt");
415             break;
416         case osEvt:
417             printf("PLUGIN: osEvt - ");
418             switch ((event->message & 0xFF000000) >> 24) {
419                 case suspendResumeMessage:
420                     printf("%s\n", (event->message & 0x1) ? "resume" : "suspend");
421                     break;
422                 case mouseMovedMessage:
423                     printf("mouseMoved\n");
424                     break;
425                 default:
426                     printf("%08lX\n", event->message);
427             }
428             break;
429         case kHighLevelEvent:
430             pluginLog(instance, "kHighLevelEvent");
431             break;
432         // NPAPI events
433         case getFocusEvent:
434             pluginLog(instance, "getFocusEvent");
435             break;
436         case loseFocusEvent:
437             pluginLog(instance, "loseFocusEvent");
438             break;
439         case adjustCursorEvent:
440             pluginLog(instance, "adjustCursorEvent");
441             break;
442         default:
443             pluginLog(instance, "event %d", event->what);
444     }
445     
446     return 0;
447 }
448 #endif
449
450 static int16_t handleEventCocoa(NPP instance, PluginObject* obj, NPCocoaEvent* event)
451 {
452     switch (event->type) {
453         case NPCocoaEventWindowFocusChanged:
454             
455         case NPCocoaEventFocusChanged:
456             if (event->data.focus.hasFocus)
457                 pluginLog(instance, "getFocusEvent");
458             else
459                 pluginLog(instance, "loseFocusEvent");
460             return 1;
461
462         case NPCocoaEventDrawRect:
463             return 1;
464
465         case NPCocoaEventKeyDown:
466             if (event->data.key.characters)
467                 pluginLog(instance, "keyDown '%c'", CFStringGetCharacterAtIndex(reinterpret_cast<CFStringRef>(event->data.key.characters), 0));
468             if (obj->evaluateScriptOnMouseDownOrKeyDown && !obj->mouseDownForEvaluateScript)
469                 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
470             return 1;
471
472         case NPCocoaEventKeyUp:
473             if (event->data.key.characters) {
474                 pluginLog(instance, "keyUp '%c'", CFStringGetCharacterAtIndex(reinterpret_cast<CFStringRef>(event->data.key.characters), 0));
475                 if (obj->testKeyboardFocusForPlugins) {
476                     obj->eventLogging = false;
477                     obj->testKeyboardFocusForPlugins = FALSE;
478                     executeScript(obj, "layoutTestController.notifyDone();");
479                 }
480             }
481             return 1;
482
483         case NPCocoaEventFlagsChanged:
484             return 1;
485
486         case NPCocoaEventMouseDown:
487             pluginLog(instance, "mouseDown at (%d, %d)", 
488                    (int)event->data.mouse.pluginX,
489                    (int)event->data.mouse.pluginY);
490             if (obj->evaluateScriptOnMouseDownOrKeyDown && obj->mouseDownForEvaluateScript)
491                 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
492             return 1;
493         case NPCocoaEventMouseUp:
494             pluginLog(instance, "mouseUp at (%d, %d)", 
495                    (int)event->data.mouse.pluginX,
496                    (int)event->data.mouse.pluginY);
497             return 1;
498             
499         case NPCocoaEventMouseMoved:
500         case NPCocoaEventMouseEntered:
501         case NPCocoaEventMouseExited:
502         case NPCocoaEventMouseDragged:
503         case NPCocoaEventScrollWheel:
504         case NPCocoaEventTextInput:
505             return 1;
506     }
507     
508     return 0;
509 }
510
511 #endif // XP_MACOSX
512
513 int16_t NPP_HandleEvent(NPP instance, void *event)
514 {
515     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
516     if (!obj->eventLogging)
517         return 0;
518
519 #if XP_MACOSX
520 #ifndef NP_NO_CARBON
521     if (obj->eventModel == NPEventModelCarbon)
522         return handleEventCarbon(instance, obj, static_cast<EventRecord*>(event));
523 #endif
524
525     assert(obj->eventModel == NPEventModelCocoa);
526     return handleEventCocoa(instance, obj, static_cast<NPCocoaEvent*>(event));
527 #else
528     // FIXME: Implement for other platforms.
529     return 0;
530 #endif // XP_MACOSX
531 }
532
533 void NPP_URLNotify(NPP instance, const char *url, NPReason reason, void *notifyData)
534 {
535     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
536  
537      if (obj->onURLNotify)
538          executeScript(obj, obj->onURLNotify);
539
540     handleCallback(obj, url, reason, notifyData);
541 }
542
543 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
544 {
545     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
546
547     if (variable == NPPVpluginScriptableNPObject) {
548         void **v = (void **)value;
549         // Return value is expected to be retained
550         browser->retainobject((NPObject *)obj);
551         *v = obj;
552         return NPERR_NO_ERROR;
553     }
554     
555 #if XP_MACOSX && !defined(BUILDING_ON_TIGER)
556     if (variable == NPPVpluginCoreAnimationLayer) {
557         if (!obj->coreAnimationLayer)
558             return NPERR_GENERIC_ERROR;
559         
560         void **v = (void **)value;
561         *v = (void*)CFRetain(obj->coreAnimationLayer);
562         return NPERR_NO_ERROR;
563     }
564 #endif
565     
566     return NPERR_GENERIC_ERROR;
567 }
568
569 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
570 {
571     PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
572
573     switch (variable) {
574         case NPNVprivateModeBool:
575             obj->cachedPrivateBrowsingMode = *(NPBool*)value;
576             return NPERR_NO_ERROR;
577         default:
578             return NPERR_GENERIC_ERROR;
579     }
580 }