~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Tools/DumpRenderTree/TestNetscapePlugIn/main.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
Import upstream version 2.3

Show diffs side-by-side

added added

removed removed

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