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