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 <cstdlib>
30 #include <cstring>
31 #include <string>
32
33 #ifdef XP_UNIX
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #endif
37
38 #if !defined(NP_NO_CARBON) && defined(QD_HEADERS_ARE_PRIVATE) && QD_HEADERS_ARE_PRIVATE
39 extern "C" void GlobalToLocal(Point*);
40 #endif
41
42 using namespace std;
43
44 #define CRASH() do { \
45 *(int *)(uintptr_t)0xbbadbeef = 0; \
46 ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
47 } while(false)
48
49 static bool getEntryPointsWasCalled = false;
50 static bool initializeWasCalled = false;
51 static NPClass* pluginObjectClass = 0;
52
53 #if defined(XP_WIN)
54 #define STDCALL __stdcall
55
strcasecmp(const char * s1,const char * s2)56 static inline int strcasecmp(const char* s1, const char* s2)
57 {
58 return _stricmp(s1, s2);
59 }
60
61 #else
62 #define STDCALL
63 #endif
64
65 extern "C" {
66 NPError STDCALL NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
67 }
68
69 // Entry points
70 extern "C"
NP_Initialize(NPNetscapeFuncs * browserFuncs,NPPluginFuncs * pluginFuncs)71 NPError STDCALL NP_Initialize(NPNetscapeFuncs *browserFuncs
72 #ifdef XP_UNIX
73 , NPPluginFuncs *pluginFuncs
74 #endif
75 )
76 {
77 // Create a copy of the PluginObject NPClass that we can trash on shutdown.
78 pluginObjectClass = createPluginClass();
79
80 initializeWasCalled = true;
81
82 #if defined(XP_WIN)
83 // Simulate Flash and QuickTime's behavior of crashing when NP_Initialize is called before NP_GetEntryPoints.
84 if (!getEntryPointsWasCalled)
85 CRASH();
86 #endif
87
88 browser = browserFuncs;
89
90 #ifdef XP_UNIX
91 return NP_GetEntryPoints(pluginFuncs);
92 #else
93 return NPERR_NO_ERROR;
94 #endif
95 }
96
97 extern "C"
NP_GetEntryPoints(NPPluginFuncs * pluginFuncs)98 NPError STDCALL NP_GetEntryPoints(NPPluginFuncs *pluginFuncs)
99 {
100 getEntryPointsWasCalled = true;
101
102 #ifdef XP_MACOSX
103 // Simulate Silverlight's behavior of crashing when NP_GetEntryPoints is called before NP_Initialize.
104 if (!initializeWasCalled)
105 CRASH();
106 #endif
107
108 pluginFunctions = pluginFuncs;
109
110 pluginFuncs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
111 pluginFuncs->size = sizeof(pluginFuncs);
112 pluginFuncs->newp = NPP_New;
113 pluginFuncs->destroy = NPP_Destroy;
114 pluginFuncs->setwindow = NPP_SetWindow;
115 pluginFuncs->newstream = NPP_NewStream;
116 pluginFuncs->destroystream = NPP_DestroyStream;
117 pluginFuncs->asfile = NPP_StreamAsFile;
118 pluginFuncs->writeready = NPP_WriteReady;
119 pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
120 pluginFuncs->print = NPP_Print;
121 pluginFuncs->event = NPP_HandleEvent;
122 pluginFuncs->urlnotify = NPP_URLNotify;
123 pluginFuncs->getvalue = NPP_GetValue;
124 pluginFuncs->setvalue = NPP_SetValue;
125
126 return NPERR_NO_ERROR;
127 }
128
129 extern "C"
NP_Shutdown(void)130 void STDCALL NP_Shutdown(void)
131 {
132 // Trash the PluginObject NPClass so that the process will deterministically
133 // crash if Blink tries to call into the plugin's NPObjects after unloading
134 // it, rather than relying on OS-specific DLL unload behaviour.
135 // Note that we leak the NPClass copy, to act as a guard for the lifetime of
136 // the process.
137 memset(pluginObjectClass, 0xf00dbeef, sizeof(NPClass));
138
139 PluginTest::NP_Shutdown();
140 }
141
142 static void executeScript(const PluginObject* obj, const char* script);
143
NPP_New(NPMIMEType pluginType,NPP instance,uint16_t mode,int16_t argc,char * argn[],char * argv[],NPSavedData * saved)144 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved)
145 {
146 #ifdef XP_MACOSX
147 NPEventModel eventModel;
148
149 // Always turn on the CG model
150 NPBool supportsCoreGraphics;
151 if (browser->getvalue(instance, NPNVsupportsCoreGraphicsBool, &supportsCoreGraphics) != NPERR_NO_ERROR)
152 supportsCoreGraphics = false;
153
154 if (!supportsCoreGraphics)
155 return NPERR_INCOMPATIBLE_VERSION_ERROR;
156
157 NPDrawingModel drawingModelToUse = NPDrawingModelCoreGraphics;
158
159 NPBool supportsCoreAnimation;
160 if (browser->getvalue(instance, NPNVsupportsCoreAnimationBool, &supportsCoreAnimation) != NPERR_NO_ERROR)
161 supportsCoreAnimation = false;
162
163 #ifndef NP_NO_CARBON
164 NPBool supportsCarbon = false;
165 #endif
166 NPBool supportsCocoa = false;
167
168 #ifndef NP_NO_CARBON
169 // A browser that doesn't know about NPNVsupportsCarbonBool is one that only supports Carbon event model.
170 if (browser->getvalue(instance, NPNVsupportsCarbonBool, &supportsCarbon) != NPERR_NO_ERROR)
171 supportsCarbon = true;
172 #endif
173
174 if (browser->getvalue(instance, NPNVsupportsCocoaBool, &supportsCocoa) != NPERR_NO_ERROR)
175 supportsCocoa = false;
176
177 if (supportsCocoa) {
178 eventModel = NPEventModelCocoa;
179 #ifndef NP_NO_CARBON
180 } else if (supportsCarbon) {
181 eventModel = NPEventModelCarbon;
182 #endif
183 } else {
184 return NPERR_INCOMPATIBLE_VERSION_ERROR;
185 }
186
187 browser->setvalue(instance, NPPVpluginEventModel, (void *)eventModel);
188 #endif // XP_MACOSX
189
190 PluginObject* obj = (PluginObject*)browser->createobject(instance, pluginObjectClass);
191 instance->pdata = obj;
192
193 #ifdef XP_MACOSX
194 obj->eventModel = eventModel;
195 obj->coreAnimationLayer = 0;
196 #endif // XP_MACOSX
197
198 string testIdentifier;
199 const char* onNewScript = 0;
200
201 for (int i = 0; i < argc; i++) {
202 if (strcasecmp(argn[i], "test") == 0)
203 testIdentifier = argv[i];
204 if (strcasecmp(argn[i], "onstreamload") == 0 && !obj->onStreamLoad)
205 obj->onStreamLoad = strdup(argv[i]);
206 else if (strcasecmp(argn[i], "onStreamDestroy") == 0 && !obj->onStreamDestroy)
207 obj->onStreamDestroy = strdup(argv[i]);
208 else if (strcasecmp(argn[i], "onURLNotify") == 0 && !obj->onURLNotify)
209 obj->onURLNotify = strdup(argv[i]);
210 else if (strcasecmp(argn[i], "src") == 0 &&
211 strcasecmp(argv[i], "data:application/x-webkit-test-netscape,returnerrorfromnewstream") == 0)
212 obj->returnErrorFromNewStream = true;
213 else if (strcasecmp(argn[i], "src") == 0 &&
214 strcasecmp(argv[i], "data:application/x-webkit-test-netscape,alertwhenloaded") == 0)
215 executeScript(obj, "alert('Plugin Loaded!')");
216 else if (strcasecmp(argn[i], "src") == 0 &&
217 strcasecmp(argv[i], "data:application/x-webkit-test-netscape,logifloaded") == 0) {
218 for (int j = 0; j < argc; j++) {
219 if (strcasecmp(argn[j], "log") == 0) {
220 int length = 26 + strlen(argv[j]) + 1;
221 char* buffer = (char*) malloc(length);
222 snprintf(buffer, length, "xWebkitTestNetscapeLog('%s')", argv[j]);
223 executeScript(obj, buffer);
224 free(buffer);
225 }
226 }
227 } else if (strcasecmp(argn[i], "onSetWindow") == 0 && !obj->onSetWindow)
228 obj->onSetWindow = strdup(argv[i]);
229 else if (strcasecmp(argn[i], "onNew") == 0 && !onNewScript)
230 onNewScript = argv[i];
231 else if (strcasecmp(argn[i], "onPaintEvent") == 0 && !obj->onPaintEvent)
232 obj->onPaintEvent = strdup(argv[i]);
233 else if (strcasecmp(argn[i], "logfirstsetwindow") == 0)
234 obj->logSetWindow = true;
235 else if (strcasecmp(argn[i], "testnpruntime") == 0)
236 testNPRuntime(instance);
237 else if (strcasecmp(argn[i], "logSrc") == 0) {
238 for (int i = 0; i < argc; i++)
239 if (strcasecmp(argn[i], "src") == 0)
240 pluginLog(instance, "src: %s", argv[i]);
241 } else if (strcasecmp(argn[i], "cleardocumentduringnew") == 0)
242 executeScript(obj, "document.body.innerHTML = ''");
243 else if (!strcasecmp(argn[i], "ondestroy"))
244 obj->onDestroy = strdup(argv[i]);
245 else if (strcasecmp(argn[i], "testwindowopen") == 0)
246 obj->testWindowOpen = true;
247 else if (strcasecmp(argn[i], "drawingmodel") == 0) {
248 #ifdef XP_MACOSX
249 const char* value = argv[i];
250 if (strcasecmp(value, "coreanimation") == 0) {
251 if (supportsCoreAnimation)
252 drawingModelToUse = NPDrawingModelCoreAnimation;
253 else
254 return NPERR_INCOMPATIBLE_VERSION_ERROR;
255 } else if (strcasecmp(value, "coregraphics") == 0) {
256 if (supportsCoreGraphics)
257 drawingModelToUse = NPDrawingModelCoreGraphics;
258 else
259 return NPERR_INCOMPATIBLE_VERSION_ERROR;
260 } else
261 return NPERR_INCOMPATIBLE_VERSION_ERROR;
262 #endif
263 } else if (strcasecmp(argn[i], "testGetURLOnDestroy") == 0) {
264 #if defined(XP_WIN)
265 // FIXME: When https://bugs.webkit.org/show_bug.cgi?id=41831 is fixed, this #ifdef can be removed.
266 obj->testGetURLOnDestroy = TRUE;
267 #endif
268 } else if (!strcasecmp(argn[i], "src") && strstr(argv[i], "plugin-document-has-focus.pl"))
269 obj->testKeyboardFocusForPlugins = true;
270 else if (!strcasecmp(argn[i], "evaluatescript")) {
271 char* script = argv[i];
272 if (script == strstr(script, "mouse::")) {
273 obj->mouseDownForEvaluateScript = true;
274 obj->evaluateScriptOnMouseDownOrKeyDown = strdup(script + sizeof("mouse::") - 1);
275 } else if (script == strstr(script, "key::")) {
276 obj->evaluateScriptOnMouseDownOrKeyDown = strdup(script + sizeof("key::") - 1);
277 }
278 // When testing evaluate script on mouse-down or key-down, allow event logging to handle events.
279 if (obj->evaluateScriptOnMouseDownOrKeyDown)
280 obj->eventLogging = true;
281 } else if (!strcasecmp(argn[i], "windowedPlugin")) {
282 void* windowed = 0;
283 if (!strcasecmp(argv[i], "false") || !strcasecmp(argv[i], "0"))
284 windowed = 0;
285 else if (!strcasecmp(argv[i], "true") || !strcasecmp(argv[i], "1"))
286 windowed = reinterpret_cast<void*>(1);
287 else
288 assert(false);
289 browser->setvalue(instance, NPPVpluginWindowBool, windowed);
290 }
291 }
292
293 #ifdef XP_MACOSX
294 browser->setvalue(instance, NPPVpluginDrawingModel, (void *)drawingModelToUse);
295 if (drawingModelToUse == NPDrawingModelCoreAnimation)
296 obj->coreAnimationLayer = createCoreAnimationLayer();
297 #endif
298
299 obj->pluginTest = PluginTest::create(instance, testIdentifier);
300
301 if (!obj->pluginTest) {
302 pluginLog(instance, "NPP_New: Could not find a test named \"%s\", maybe its .cpp file wasn't added to the build system?", testIdentifier.c_str());
303 return NPERR_GENERIC_ERROR;
304 }
305
306 if (onNewScript)
307 executeScript(obj, onNewScript);
308
309 return obj->pluginTest->NPP_New(pluginType, mode, argc, argn, argv, saved);
310 }
311
NPP_Destroy(NPP instance,NPSavedData ** save)312 NPError NPP_Destroy(NPP instance, NPSavedData **save)
313 {
314 PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
315
316 if (obj) {
317 if (obj->testGetURLOnDestroy)
318 browser->geturlnotify(obj->npp, "about:blank", "", 0);
319
320 if (obj->onDestroy) {
321 executeScript(obj, obj->onDestroy);
322 free(obj->onDestroy);
323 }
324
325 if (obj->onStreamLoad)
326 free(obj->onStreamLoad);
327
328 if (obj->onStreamDestroy)
329 free(obj->onStreamDestroy);
330
331 if (obj->onURLNotify)
332 free(obj->onURLNotify);
333
334 if (obj->onSetWindow)
335 free(obj->onSetWindow);
336
337 if (obj->onPaintEvent)
338 free(obj->onPaintEvent);
339
340 if (obj->evaluateScriptOnMouseDownOrKeyDown)
341 free(obj->evaluateScriptOnMouseDownOrKeyDown);
342
343 if (obj->logDestroy)
344 pluginLog(instance, "NPP_Destroy");
345
346 #ifdef XP_MACOSX
347 if (obj->coreAnimationLayer)
348 CFRelease(obj->coreAnimationLayer);
349 #endif
350
351 if (obj->pluginTest)
352 obj->pluginTest->NPP_Destroy(save);
353
354 browser->releaseobject(&obj->header);
355 }
356 return NPERR_NO_ERROR;
357 }
358
NPP_SetWindow(NPP instance,NPWindow * window)359 NPError NPP_SetWindow(NPP instance, NPWindow *window)
360 {
361 PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
362
363 if (obj) {
364 obj->lastWindow = *window;
365
366 if (obj->logSetWindow) {
367 pluginLog(instance, "NPP_SetWindow: %d %d", (int)window->width, (int)window->height);
368 obj->logSetWindow = false;
369 executeScript(obj, "testRunner.notifyDone();");
370 }
371
372 if (obj->onSetWindow)
373 executeScript(obj, obj->onSetWindow);
374
375 if (obj->testWindowOpen) {
376 testWindowOpen(instance);
377 obj->testWindowOpen = false;
378 }
379
380 if (obj->testKeyboardFocusForPlugins) {
381 obj->eventLogging = true;
382 executeScript(obj, "eventSender.keyDown('A');");
383 }
384 }
385
386 return obj->pluginTest->NPP_SetWindow(window);
387 }
388
executeScript(const PluginObject * obj,const char * script)389 static void executeScript(const PluginObject* obj, const char* script)
390 {
391 NPObject *windowScriptObject;
392 browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
393
394 NPString npScript;
395 npScript.UTF8Characters = script;
396 npScript.UTF8Length = strlen(script);
397
398 NPVariant browserResult;
399 browser->evaluate(obj->npp, windowScriptObject, &npScript, &browserResult);
400 browser->releasevariantvalue(&browserResult);
401 }
402
NPP_NewStream(NPP instance,NPMIMEType type,NPStream * stream,NPBool seekable,uint16_t * stype)403 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16_t *stype)
404 {
405 PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
406 obj->stream = stream;
407 *stype = NP_NORMAL;
408
409 if (obj->returnErrorFromNewStream)
410 return NPERR_GENERIC_ERROR;
411
412 if (browser->version >= NPVERS_HAS_RESPONSE_HEADERS)
413 notifyStream(obj, stream->url, stream->headers);
414
415 if (obj->onStreamLoad)
416 executeScript(obj, obj->onStreamLoad);
417
418 return obj->pluginTest->NPP_NewStream(type, stream, seekable, stype);
419 }
420
NPP_DestroyStream(NPP instance,NPStream * stream,NPReason reason)421 NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason)
422 {
423 PluginObject* obj = (PluginObject*)instance->pdata;
424
425 if (obj->onStreamDestroy) {
426 NPObject* windowObject = 0;
427 NPError error = browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
428
429 if (error == NPERR_NO_ERROR) {
430 NPVariant onStreamDestroyVariant;
431 if (browser->getproperty(instance, windowObject, browser->getstringidentifier(obj->onStreamDestroy), &onStreamDestroyVariant)) {
432 if (NPVARIANT_IS_OBJECT(onStreamDestroyVariant)) {
433 NPObject* onStreamDestroyFunction = NPVARIANT_TO_OBJECT(onStreamDestroyVariant);
434
435 NPVariant reasonVariant;
436 INT32_TO_NPVARIANT(reason, reasonVariant);
437
438 NPVariant result;
439 browser->invokeDefault(instance, onStreamDestroyFunction, &reasonVariant, 1, &result);
440 browser->releasevariantvalue(&result);
441 }
442 browser->releasevariantvalue(&onStreamDestroyVariant);
443 }
444 browser->releaseobject(windowObject);
445 }
446 }
447
448 return obj->pluginTest->NPP_DestroyStream(stream, reason);
449 }
450
NPP_WriteReady(NPP instance,NPStream * stream)451 int32_t NPP_WriteReady(NPP instance, NPStream *stream)
452 {
453 PluginObject* obj = (PluginObject*)instance->pdata;
454 return obj->pluginTest->NPP_WriteReady(stream);
455 }
456
NPP_Write(NPP instance,NPStream * stream,int32_t offset,int32_t len,void * buffer)457 int32_t NPP_Write(NPP instance, NPStream *stream, int32_t offset, int32_t len, void *buffer)
458 {
459 PluginObject* obj = (PluginObject*)instance->pdata;
460
461 if (obj->returnNegativeOneFromWrite)
462 return -1;
463
464 return obj->pluginTest->NPP_Write(stream, offset, len, buffer);
465 }
466
NPP_StreamAsFile(NPP instance,NPStream * stream,const char * fname)467 void NPP_StreamAsFile(NPP instance, NPStream *stream, const char *fname)
468 {
469 }
470
NPP_Print(NPP instance,NPPrint * platformPrint)471 void NPP_Print(NPP instance, NPPrint *platformPrint)
472 {
473 }
474
475 #ifdef XP_MACOSX
476 #ifndef NP_NO_CARBON
handleEventCarbon(NPP instance,PluginObject * obj,EventRecord * event)477 static int16_t handleEventCarbon(NPP instance, PluginObject* obj, EventRecord* event)
478 {
479 Point pt = { event->where.v, event->where.h };
480
481 switch (event->what) {
482 case nullEvent:
483 // these are delivered non-deterministically, don't log.
484 break;
485 case mouseDown:
486 if (obj->eventLogging) {
487 #if __clang__
488 #pragma clang diagnostic push
489 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
490 #endif
491 GlobalToLocal(&pt);
492 #if __clang__
493 #pragma clang diagnostic pop
494 #endif
495 pluginLog(instance, "mouseDown at (%d, %d)", pt.h, pt.v);
496 }
497 if (obj->evaluateScriptOnMouseDownOrKeyDown && obj->mouseDownForEvaluateScript)
498 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
499 break;
500 case mouseUp:
501 if (obj->eventLogging) {
502 #if __clang__
503 #pragma clang diagnostic push
504 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
505 #endif
506 GlobalToLocal(&pt);
507 #if __clang__
508 #pragma clang diagnostic pop
509 #endif
510 pluginLog(instance, "mouseUp at (%d, %d)", pt.h, pt.v);
511 }
512 break;
513 case keyDown:
514 if (obj->eventLogging)
515 pluginLog(instance, "keyDown '%c'", (char)(event->message & 0xFF));
516 if (obj->evaluateScriptOnMouseDownOrKeyDown && !obj->mouseDownForEvaluateScript)
517 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
518 break;
519 case keyUp:
520 if (obj->eventLogging)
521 pluginLog(instance, "keyUp '%c'", (char)(event->message & 0xFF));
522 if (obj->testKeyboardFocusForPlugins) {
523 obj->eventLogging = false;
524 obj->testKeyboardFocusForPlugins = FALSE;
525 executeScript(obj, "testRunner.notifyDone();");
526 }
527 break;
528 case autoKey:
529 if (obj->eventLogging)
530 pluginLog(instance, "autoKey '%c'", (char)(event->message & 0xFF));
531 break;
532 case updateEvt:
533 if (obj->eventLogging)
534 pluginLog(instance, "updateEvt");
535 break;
536 case diskEvt:
537 if (obj->eventLogging)
538 pluginLog(instance, "diskEvt");
539 break;
540 case activateEvt:
541 if (obj->eventLogging)
542 pluginLog(instance, "activateEvt");
543 break;
544 case osEvt:
545 if (!obj->eventLogging)
546 break;
547 printf("PLUGIN: osEvt - ");
548 switch ((event->message & 0xFF000000) >> 24) {
549 case suspendResumeMessage:
550 printf("%s\n", (event->message & 0x1) ? "resume" : "suspend");
551 break;
552 case mouseMovedMessage:
553 printf("mouseMoved\n");
554 break;
555 default:
556 printf("%08lX\n", event->message);
557 }
558 break;
559 case kHighLevelEvent:
560 if (obj->eventLogging)
561 pluginLog(instance, "kHighLevelEvent");
562 break;
563 // NPAPI events
564 case NPEventType_GetFocusEvent:
565 if (obj->eventLogging)
566 pluginLog(instance, "getFocusEvent");
567 break;
568 case NPEventType_LoseFocusEvent:
569 if (obj->eventLogging)
570 pluginLog(instance, "loseFocusEvent");
571 break;
572 case NPEventType_AdjustCursorEvent:
573 if (obj->eventLogging)
574 pluginLog(instance, "adjustCursorEvent");
575 break;
576 default:
577 if (obj->eventLogging)
578 pluginLog(instance, "event %d", event->what);
579 }
580
581 return 0;
582 }
583 #endif
584
handleEventCocoa(NPP instance,PluginObject * obj,NPCocoaEvent * event)585 static int16_t handleEventCocoa(NPP instance, PluginObject* obj, NPCocoaEvent* event)
586 {
587 switch (event->type) {
588 case NPCocoaEventWindowFocusChanged:
589
590 case NPCocoaEventFocusChanged:
591 if (obj->eventLogging) {
592 if (event->data.focus.hasFocus)
593 pluginLog(instance, "getFocusEvent");
594 else
595 pluginLog(instance, "loseFocusEvent");
596 }
597 return 1;
598
599 case NPCocoaEventDrawRect: {
600 if (obj->onPaintEvent)
601 executeScript(obj, obj->onPaintEvent);
602 return 1;
603 }
604
605 case NPCocoaEventKeyDown:
606 if (obj->eventLogging && event->data.key.characters)
607 pluginLog(instance, "keyDown '%c'", CFStringGetCharacterAtIndex(reinterpret_cast<CFStringRef>(event->data.key.characters), 0));
608 if (obj->evaluateScriptOnMouseDownOrKeyDown && !obj->mouseDownForEvaluateScript)
609 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
610 return 1;
611
612 case NPCocoaEventKeyUp:
613 if (obj->eventLogging && event->data.key.characters) {
614 pluginLog(instance, "keyUp '%c'", CFStringGetCharacterAtIndex(reinterpret_cast<CFStringRef>(event->data.key.characters), 0));
615 if (obj->testKeyboardFocusForPlugins) {
616 obj->eventLogging = false;
617 obj->testKeyboardFocusForPlugins = FALSE;
618 executeScript(obj, "testRunner.notifyDone();");
619 }
620 }
621 return 1;
622
623 case NPCocoaEventFlagsChanged:
624 return 1;
625
626 case NPCocoaEventMouseDown:
627 if (obj->eventLogging) {
628 pluginLog(instance, "mouseDown at (%d, %d)",
629 (int)event->data.mouse.pluginX,
630 (int)event->data.mouse.pluginY);
631 }
632 if (obj->evaluateScriptOnMouseDownOrKeyDown && obj->mouseDownForEvaluateScript)
633 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
634 return 1;
635 case NPCocoaEventMouseUp:
636 if (obj->eventLogging) {
637 pluginLog(instance, "mouseUp at (%d, %d)",
638 (int)event->data.mouse.pluginX,
639 (int)event->data.mouse.pluginY);
640 }
641 return 1;
642
643 case NPCocoaEventMouseMoved:
644 case NPCocoaEventMouseEntered:
645 case NPCocoaEventMouseExited:
646 case NPCocoaEventMouseDragged:
647 case NPCocoaEventScrollWheel:
648 case NPCocoaEventTextInput:
649 return 1;
650 }
651
652 return 0;
653 }
654
655 #endif // XP_MACOSX
656
657 #ifdef XP_UNIX
658
keyEventToChar(XKeyEvent * event)659 static char keyEventToChar(XKeyEvent* event)
660 {
661 char c = ' ';
662 XLookupString(event, &c, sizeof(c), 0, 0);
663 return c;
664 }
665
handleEventX11(NPP instance,PluginObject * obj,XEvent * event)666 static int16_t handleEventX11(NPP instance, PluginObject* obj, XEvent* event)
667 {
668 switch (event->type) {
669 case ButtonPress:
670 if (obj->eventLogging)
671 pluginLog(instance, "mouseDown at (%d, %d)", event->xbutton.x, event->xbutton.y);
672 if (obj->evaluateScriptOnMouseDownOrKeyDown && obj->mouseDownForEvaluateScript)
673 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
674 break;
675 case ButtonRelease:
676 if (obj->eventLogging)
677 pluginLog(instance, "mouseUp at (%d, %d)", event->xbutton.x, event->xbutton.y);
678 break;
679 case KeyPress:
680 // FIXME: extract key code
681 if (obj->eventLogging)
682 pluginLog(instance, "keyDown '%c'", keyEventToChar(&event->xkey));
683 if (obj->evaluateScriptOnMouseDownOrKeyDown && !obj->mouseDownForEvaluateScript)
684 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
685 break;
686 case KeyRelease:
687 // FIXME: extract key code
688 if (obj->eventLogging)
689 pluginLog(instance, "keyUp '%c'", keyEventToChar(&event->xkey));
690 if (obj->testKeyboardFocusForPlugins) {
691 obj->eventLogging = false;
692 obj->testKeyboardFocusForPlugins = false;
693 executeScript(obj, "testRunner.notifyDone();");
694 }
695 break;
696 case GraphicsExpose:
697 if (obj->eventLogging)
698 pluginLog(instance, "updateEvt");
699 if (obj->onPaintEvent)
700 executeScript(obj, obj->onPaintEvent);
701 break;
702 // NPAPI events
703 case FocusIn:
704 if (obj->eventLogging)
705 pluginLog(instance, "getFocusEvent");
706 break;
707 case FocusOut:
708 if (obj->eventLogging)
709 pluginLog(instance, "loseFocusEvent");
710 break;
711 case EnterNotify:
712 case LeaveNotify:
713 case MotionNotify:
714 break;
715 default:
716 if (obj->eventLogging)
717 pluginLog(instance, "event %d", event->type);
718 }
719
720 fflush(stdout);
721 return 0;
722 }
723 #endif // XP_UNIX
724
725 #ifdef XP_WIN
handleEventWin(NPP instance,PluginObject * obj,NPEvent * event)726 static int16_t handleEventWin(NPP instance, PluginObject* obj, NPEvent* event)
727 {
728 switch (event->event) {
729 case WM_PAINT:
730 if (obj->onPaintEvent)
731 executeScript(obj, obj->onPaintEvent);
732 break;
733 case WM_KEYDOWN:
734 if (obj->eventLogging)
735 pluginLog(instance, "keyDown '%c'", event->wParam);
736 if (obj->evaluateScriptOnMouseDownOrKeyDown && !obj->mouseDownForEvaluateScript)
737 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
738 break;
739 case WM_CHAR:
740 break;
741 case WM_KEYUP:
742 if (obj->eventLogging)
743 pluginLog(instance, "keyUp '%c'", event->wParam);
744 if (obj->testKeyboardFocusForPlugins) {
745 obj->eventLogging = false;
746 obj->testKeyboardFocusForPlugins = FALSE;
747 executeScript(obj, "testRunner.notifyDone();");
748 }
749 break;
750 case WM_LBUTTONDOWN:
751 case WM_MBUTTONDOWN:
752 case WM_RBUTTONDOWN:
753 if (obj->eventLogging)
754 pluginLog(instance, "mouseDown at (%d, %d)", LOWORD(event->lParam), HIWORD(event->lParam));
755 if (obj->evaluateScriptOnMouseDownOrKeyDown && obj->mouseDownForEvaluateScript)
756 executeScript(obj, obj->evaluateScriptOnMouseDownOrKeyDown);
757 break;
758 case WM_LBUTTONUP:
759 case WM_MBUTTONUP:
760 case WM_RBUTTONUP:
761 if (obj->eventLogging)
762 pluginLog(instance, "mouseUp at (%d, %d)", LOWORD(event->lParam), HIWORD(event->lParam));
763 break;
764 case WM_SETFOCUS:
765 if (obj->eventLogging)
766 pluginLog(instance, "getFocusEvent");
767 break;
768 case WM_KILLFOCUS:
769 if (obj->eventLogging)
770 pluginLog(instance, "loseFocusEvent");
771 break;
772 }
773 return 0;
774 }
775 #endif // XP_WIN
776
NPP_HandleEvent(NPP instance,void * event)777 int16_t NPP_HandleEvent(NPP instance, void *event)
778 {
779 PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
780
781 if (obj->pluginTest->NPP_HandleEvent(event) == 1)
782 return 1;
783
784 #ifdef XP_MACOSX
785 #ifndef NP_NO_CARBON
786 if (obj->eventModel == NPEventModelCarbon)
787 return handleEventCarbon(instance, obj, static_cast<EventRecord*>(event));
788 #endif
789
790 assert(obj->eventModel == NPEventModelCocoa);
791 return handleEventCocoa(instance, obj, static_cast<NPCocoaEvent*>(event));
792 #elif defined(XP_UNIX)
793 return handleEventX11(instance, obj, static_cast<XEvent*>(event));
794 #elif defined(XP_WIN)
795 return handleEventWin(instance, obj, static_cast<NPEvent*>(event));
796 #else
797 // FIXME: Implement for other platforms.
798 return 0;
799 #endif // XP_MACOSX
800 }
801
NPP_URLNotify(NPP instance,const char * url,NPReason reason,void * notifyData)802 void NPP_URLNotify(NPP instance, const char *url, NPReason reason, void *notifyData)
803 {
804 PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
805 if (obj->pluginTest->NPP_URLNotify(url, reason, notifyData))
806 return;
807
808 if (obj->onURLNotify)
809 executeScript(obj, obj->onURLNotify);
810
811 handleCallback(obj, url, reason, notifyData);
812 }
813
NPP_GetValue(NPP instance,NPPVariable variable,void * value)814 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
815 {
816 #ifdef XP_UNIX
817 if (variable == NPPVpluginNameString) {
818 *((char **)value) = const_cast<char*>("WebKit Test PlugIn");
819 return NPERR_NO_ERROR;
820 }
821 if (variable == NPPVpluginDescriptionString) {
822 *((char **)value) = const_cast<char*>("Simple Netscape® plug-in that handles test content for WebKit");
823 return NPERR_NO_ERROR;
824 }
825 if (variable == NPPVpluginNeedsXEmbed) {
826 *((NPBool *)value) = true;
827 return NPERR_NO_ERROR;
828 }
829 #endif
830
831 if (!instance)
832 return NPERR_GENERIC_ERROR;
833 PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
834
835 // First, check if the PluginTest object supports getting this value.
836 if (obj->pluginTest->NPP_GetValue(variable, value) == NPERR_NO_ERROR)
837 return NPERR_NO_ERROR;
838
839 if (variable == NPPVpluginScriptableNPObject) {
840 void **v = (void **)value;
841 // Return value is expected to be retained
842 browser->retainobject((NPObject *)obj);
843 *v = obj;
844 return NPERR_NO_ERROR;
845 }
846
847 #ifdef XP_MACOSX
848 if (variable == NPPVpluginCoreAnimationLayer) {
849 if (!obj->coreAnimationLayer)
850 return NPERR_GENERIC_ERROR;
851
852 void **v = (void **)value;
853 *v = (void*)CFRetain(obj->coreAnimationLayer);
854 return NPERR_NO_ERROR;
855 }
856 #endif
857
858 return NPERR_GENERIC_ERROR;
859 }
860
NPP_SetValue(NPP instance,NPNVariable variable,void * value)861 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
862 {
863 PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
864 return obj->pluginTest->NPP_SetValue(variable, value);
865 }
866
867 #ifdef XP_UNIX
868 extern "C"
NP_GetMIMEDescription(void)869 const char* NP_GetMIMEDescription(void)
870 {
871 return "application/x-webkit-test-netscape:testnetscape:test netscape content;image/png:png:PNG image";
872 }
873
874 extern "C"
NP_GetValue(NPP instance,NPPVariable variable,void * value)875 NPError NP_GetValue(NPP instance, NPPVariable variable, void* value)
876 {
877 return NPP_GetValue(instance, variable, value);
878 }
879 #endif
880