summaryrefslogtreecommitdiff
path: root/extensions/source/plugin/unx/npnapi.cxx
blob: 491722c77856ef7e6c98b61ddee8b77b666e3533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*************************************************************************

      Source Code Control System - Header

      $Header: /zpool/svn/migration/cvs_rep_09_09_08/code/extensions/source/plugin/unx/npnapi.cxx,v 1.3 2003-03-25 16:03:42 hr Exp $

*************************************************************************/
#include <plugin/unx/plugcon.hxx>

#include <dlfcn.h>

extern PluginConnector* pConnector;
extern NPWindow aNPWindow;
extern NPSetWindowCallbackStruct aNPSetWindowCallbackStruct;
extern XtAppContext app_context;
extern Widget topLevel;
extern Display* pAppDisplay;
extern int nAppArguments;
extern char** pAppArguments;
void* CreateNewShell( void** );

NPNetscapeFuncs aNetscapeFuncs =
{
    sizeof(aNetscapeFuncs),
    (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR,
    NPN_GetURL,
    NPN_PostURL,
    NPN_RequestRead,
    NPN_NewStream,
    NPN_Write,
    NPN_DestroyStream,
    NPN_Status,
    NPN_UserAgent,
    NPN_MemAlloc,
    NPN_MemFree,
    NPN_MemFlush,
    NPN_ReloadPlugins,
    NPN_GetJavaEnv,
    NPN_GetJavaPeer,
    NPN_GetURLNotify,
    NPN_PostURLNotify,
    NPN_GetValue,
    NPN_SetValue,
    NPN_InvalidateRect,
    NPN_InvalidateRegion,
    NPN_ForceRedraw
};

NPPluginFuncs aPluginFuncs =
{
    sizeof(aPluginFuncs),
    (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};


void* pPluginLib = NULL;
char*(*pNPP_GetMIMEDescription)()                               = NULL;
NPError (*pNP_Initialize)(NPNetscapeFuncs*,NPPluginFuncs*)      = NULL;
NPError (*pNP_Shutdown)()                                       = NULL;

PluginConnectorList PluginConnector::allConnectors;

PluginConnector::PluginConnector( int nSocket ) :
        Mediator( nSocket )
{
    SetNewMessageHdl( LINK( this, PluginConnector, NewMessageHdl ) );
}

PluginConnector::~PluginConnector()
{
}

IMPL_LINK( PluginConnector, WorkOnNewMessageHdl, Mediator*, pMediator )
{
    MediatorMessage* pMessage;
    CommandAtoms nCommand;
    if( pMessage = GetNextMessage( FALSE ) )
    {
        nCommand = (CommandAtoms)pMessage->GetUINT32();
        medDebug( 1, "%s\n", GetCommandName( nCommand ) );
        switch( nCommand )
        {
            case eNPP_DestroyStream:
            {
                UINT32 nInstance    = pMessage->GetUINT32();
                NPP instance        = m_aInstances.GetObject( nInstance )->instance;
                UINT32 nFileID      = pMessage->GetUINT32();
                NPStream* pStream   = m_aNPWrapStreams.GetObject( nFileID );
                NPError aReason     = GetNPError( pMessage );
                aReason = aPluginFuncs.destroystream( instance, pStream, aReason );
                Respond( pMessage->m_nID,
                         (char*)&aReason, sizeof( aReason ),
                         NULL );
            }
            break;
            case eNPP_Destroy:
            {
                UINT32 nInstance    = pMessage->GetUINT32();
                NPP instance        = m_aInstances.GetObject( nInstance )->instance;
                NPSavedData* pSave = NULL;
                NPError aRet = aPluginFuncs.destroy( instance, &pSave );
                if( pSave )
                    Respond( pMessage->m_nID,
                             (char*)&aRet, sizeof( aRet ),
                             pSave->buf, pSave->len,
                             NULL );
                else
                    Respond( pMessage->m_nID,
                             (char*)&aRet, sizeof( aRet ),
                             "0000", 4,
                             NULL );
                delete pSave->buf;
                delete m_aInstances.Remove( nInstance );
                delete instance;
            }
            break;
            case eNPP_NewStream:
            {
                UINT32 nInstance        = pMessage->GetUINT32();
                NPP instance            = m_aInstances.GetObject( nInstance )->instance;
                char* pType             = pMessage->GetString();
                NPStream* pStream       = new NPStream;
                pStream->url            = pMessage->GetString();
                pStream->end            = pMessage->GetUINT32();
                pStream->lastmodified   = pMessage->GetUINT32();
                NPBool* pSeekable       = (NPBool*)pMessage->GetBytes();
                m_aNPWrapStreams.Insert( pStream, LIST_APPEND );
                uint16 nStype;
                NPError aRet = aPluginFuncs.newstream( instance, pType, pStream,
                                                       *pSeekable, &nStype );
                medDebug( aRet, "xhello: NPP_NewStream returns %d\n", (int) aRet );
                Respond( pMessage->m_nID,
                         (char*)&aRet, sizeof( aRet ),
                         &nStype, sizeof( nStype ),
                         NULL );
                delete pType;
            }
            break;
            case eNPP_New:
            {
                char* pType     = pMessage->GetString();
                uint16* pMode   = (uint16*)pMessage->GetBytes();
                int16*  pArgc   = (int16*)pMessage->GetBytes();
                NPP instance    = new NPP_t;
                instance->pdata = NULL;
                ULONG nArgnBytes, nArgvBytes;
                char* pArgn = (char*)pMessage->GetBytes( nArgnBytes );
                char* pArgv = (char*)pMessage->GetBytes( nArgvBytes );
                ULONG nSaveBytes;
                char* pSavedData = (char*)pMessage->GetBytes( nSaveBytes );
                ConnectorInstance* pInst =
                    new ConnectorInstance( instance, pType,
                                           *pArgc,
                                           pArgn, nArgnBytes,
                                           pArgv, nArgvBytes,
                                           pSavedData, nSaveBytes );
                m_aInstances.Insert( pInst, LIST_APPEND );
                NPError aRet;
                aRet = aPluginFuncs.newp( pInst->pMimeType, instance, *pMode, *pArgc,
                                          *pArgc ? pInst->argn : NULL,
                                          *pArgc ? pInst->argv : NULL,
                                          ( nSaveBytes == 4 && *(UINT32*)pSavedData == '0000' ) ?
                                          &(pInst->aData) : NULL );
                medDebug( aRet, "xhello: NPP_New returns %d\n", (int) aRet );
                Respond( pMessage->m_nID,
                         (char*)&aRet, sizeof( aRet ),
                         NULL );
                delete pMode;
                delete pArgc;
                delete pType;
            }
            break;
            case eNPP_SetWindow:
            {
                UINT32 nInstance        = pMessage->GetUINT32();
                ConnectorInstance* pInst= m_aInstances.GetObject( nInstance );
                NPWindow* pWindow       = (NPWindow*)pMessage->GetBytes();
                memcpy( &pInst->window, pWindow, sizeof( NPWindow ) );
                pInst->window.ws_info = &pInst->ws_info;
                memcpy( &pInst->ws_info, &aNPSetWindowCallbackStruct,
                        sizeof( NPSetWindowCallbackStruct ) );
                pInst->ws_info.type = 1;
                if( ! pInst->pWidget )
                {
                    pInst->pWidget = CreateNewShell( &(pInst->pShell) );

                    medDebug( 1, "Reparenting new widget %x to %x\n",
                              XtWindow( (Widget)pInst->pWidget ),
                              (XLIB_Window)pWindow->window );
                    XReparentWindow( pAppDisplay,
                                     XtWindow( (Widget)pInst->pShell ),
                                     (XLIB_Window)pWindow->window,
                                     0, 0 );
                    XSync( pAppDisplay, False );

                    XtRealizeWidget( (Widget)pInst->pWidget );
                    XtResizeWidget( (Widget)pInst->pShell,
                                    pWindow->width, pWindow->height, 0 );
                     XWithdrawWindow( pAppDisplay,
                                      XtWindow( topLevel ),
                                      DefaultScreen( pAppDisplay )
                                      );
                    XtMapWidget( (Widget)pInst->pWidget );
                    XRaiseWindow( pAppDisplay, XtWindow((Widget)pInst->pWidget) );
                    XSync( pAppDisplay, False );
                }

                pInst->window.window =
                    (void*)XtWindow( (Widget)pInst->pWidget );
                XtResizeWidget( (Widget)pInst->pShell,
                                pInst->window.width,
                                pInst->window.height,
                                0 );
                NPError aRet = aPluginFuncs.setwindow( pInst->instance, &pInst->window );
                medDebug( aRet, "xhello: NPP_SetWindow returns %d\n", (int) aRet );
                Respond( pMessage->m_nID,
                         (char*)&aRet, sizeof( aRet ),
                         NULL );
                delete pWindow;
            }
            break;
            case eNPP_StreamAsFile:
            {
                UINT32 nInstance        = pMessage->GetUINT32();
                NPP instance            = m_aInstances.GetObject( nInstance )->instance;
                UINT32 nFileID          = pMessage->GetUINT32();
                NPStream* pStream       = m_aNPWrapStreams.GetObject( nFileID );
                char* fname             = pMessage->GetString();
                medDebug( 1, "NPP_StreamAsFile %s\n", fname );
                aPluginFuncs.asfile( instance, pStream, fname );
                delete fname;
            }
            break;
            case eNPP_URLNotify:
            {
                UINT32 nInstance        = pMessage->GetUINT32();
                NPP instance            = m_aInstances.GetObject( nInstance )->instance;
                char* url               = pMessage->GetString();
                NPReason* pReason       = (NPReason*)pMessage->GetBytes();
                void** notifyData       = (void**)pMessage->GetBytes();
                aPluginFuncs.urlnotify( instance, url, *pReason, *notifyData );
                delete url;
                delete pReason;
                delete notifyData;
            }
            break;
            case eNPP_WriteReady:
            {
                UINT32 nInstance        = pMessage->GetUINT32();
                NPP instance            = m_aInstances.GetObject( nInstance )->instance;
                UINT32 nFileID          = pMessage->GetUINT32();
                NPStream* pStream       = m_aNPWrapStreams.GetObject( nFileID );
                int32 nRet = aPluginFuncs.writeready( instance, pStream );
                Respond( pMessage->m_nID,
                         (char*)&nRet, sizeof( nRet ),
                         NULL );
            }
            break;
            case eNPP_Write:
            {
                UINT32 nInstance        = pMessage->GetUINT32();
                NPP instance            = m_aInstances.GetObject( nInstance )->instance;
                UINT32 nFileID          = pMessage->GetUINT32();
                NPStream* pStream       = m_aNPWrapStreams.GetObject( nFileID );
                int32 offset            = pMessage->GetUINT32();
                ULONG len;
                char* buffer            = (char*)pMessage->GetBytes( len );
                int32 nRet = aPluginFuncs.write( instance, pStream, offset, len, buffer );
                Respond( pMessage->m_nID,
                         (char*)&nRet, sizeof( nRet ),
                         NULL );
                delete buffer;
            }
            break;
            case eNPP_GetMIMEDescription:
            {
                if( ! pNPP_GetMIMEDescription )
                    pNPP_GetMIMEDescription = (char*(*)())
                        dlsym( pPluginLib, "NPP_GetMIMEDescription" );
                char* pMIME = pNPP_GetMIMEDescription();
                Respond( pMessage->m_nID,
                         POST_STRING( pMIME ),
                         NULL );
            }
            break;
            case eNPP_Initialize:
            {

                pNP_Initialize =
                    (NPError(*)(NPNetscapeFuncs*, NPPluginFuncs*))
                    dlsym( pPluginLib, "NP_Initialize" );
                pNP_Shutdown = (NPError(*)())
                    dlsym( pPluginLib, "NP_Shutdown" );

                medDebug( 1, "entering NP_Initialize\n" );
                NPError aRet = pNP_Initialize( &aNetscapeFuncs, &aPluginFuncs );
                medDebug( 1, "xhello: NP_Initialize returns %d\n", (int) aRet );
                Respond( pMessage->m_nID, (char*)&aRet, sizeof( aRet ), NULL );
            }
            break;
            case eNPP_Shutdown:
            {
                pNP_Shutdown();
                dlclose( pPluginLib );
                exit( 0 );
            }
            break;
            default:
                medDebug( 1, "caught unknown NPP request %d\n", nCommand );
        }
        delete pMessage;
    }
    return 0;
}

// begin Netscape plugin api calls
extern "C" {

    void* NPN_MemAlloc( UINT32 nBytes )
    {
        void* pMem = malloc( nBytes );
        return pMem;
    }

    void NPN_MemFree( void* pMem )
{
    free( pMem );
}

    UINT32 NPN_MemFlush( UINT32 nSize )
{
    return 0;
}

NPError NPN_DestroyStream( NPP instance, NPStream* stream, NPError reason )
{
    UINT32 nInstance = pConnector->GetNPPID( instance );
    MediatorMessage* pMes=
        pConnector->
        Transact( eNPN_DestroyStream,
                  &nInstance, sizeof( nInstance ),
                  pConnector->GetStreamID( stream ), sizeof( int ),
                  POST_STRING( stream->url ),
                  reason, sizeof( reason ),
                  NULL );

    if( ! pMes )
        return NPERR_GENERIC_ERROR;

    pConnector->m_aNPWrapStreams.Remove( stream );
    delete stream->url;
    delete stream;
    // returns NPError
    NPError aRet = pConnector->GetNPError( pMes );
    delete pMes;
    return aRet;
}

    JRIEnv* NPN_GetJavaEnv()
{
    // no java in this program
    return NULL;
}

    jref NPN_GetJavaPeer( NPP instance )
{
    return NULL;
}

    NPError NPN_GetURL( NPP instance, const char* url, const char* window )
{
    UINT32 nInstance = pConnector->GetNPPID( instance );
    MediatorMessage* pMes=
        pConnector->
        Transact( eNPN_GetURL,
                  &nInstance, sizeof( nInstance ),
                  POST_STRING(url),
                  POST_STRING(window),
                  NULL );
    if( ! pMes )
        return NPERR_GENERIC_ERROR;

    // returns NPError
    NPError aRet = pConnector->GetNPError( pMes );
    delete pMes;
    return aRet;
}

    NPError NPN_GetURLNotify( NPP instance, const char* url, const char* target,
                              void* notifyData )
{
    UINT32 nInstance = pConnector->GetNPPID( instance );
    MediatorMessage* pMes=
        pConnector->
        Transact( eNPN_GetURLNotify,
                  &nInstance, sizeof( nInstance ),
                  POST_STRING(url),
                  POST_STRING(target),
                  &notifyData, sizeof( void* ), // transmit the actual pointer
                  // since it is a pointer to private data fed back
                  // by NPP_URLNotify; this can be thought of as an ID
                  NULL );
    if( ! pMes )
        return NPERR_GENERIC_ERROR;

    // returns NPError
    NPError aRet = pConnector->GetNPError( pMes );
    delete pMes;
    return aRet;
}

    NPError NPN_NewStream( NPP instance, NPMIMEType type, const char* target,
                           NPStream** stream )
        // stream is a return value
{
    UINT32 nInstance = pConnector->GetNPPID( instance );
    MediatorMessage* pMes=
        pConnector->
        Transact( eNPN_NewStream,
                  &nInstance, sizeof( nInstance ),
                  POST_STRING(type),
                  POST_STRING(target),
                  NULL );
    if( ! pMes )
        return NPERR_GENERIC_ERROR;

    // returns a new NPStream and an error
    NPError aRet = pConnector->GetNPError( pMes );
    if( ! aRet )
    {
        NPStream* pStream = new NPStream;
        pStream->url = pMes->GetString();
        pStream->end = pMes->GetUINT32();
        pStream->lastmodified = pMes->GetUINT32();
        pConnector->m_aNPWrapStreams.Insert( pStream, LIST_APPEND );
        *stream = pStream;
    }

    delete pMes;
    return aRet;
}

    NPError NPN_PostURLNotify( NPP instance, const char* url, const char* target, UINT32 len, const char* buf, NPBool file, void* notifyData )
{
    UINT32 nInstance = pConnector->GetNPPID( instance );
    MediatorMessage* pMes = pConnector->
        Transact( eNPN_PostURLNotify,
                  &nInstance, sizeof( nInstance ),
                  POST_STRING( url ),
                  POST_STRING( target ),
                  &len, sizeof( len ),
                  buf, len,
                  &file, sizeof( NPBool ),
                  &notifyData, sizeof( void* ), // send the real pointer
                  NULL );

    if( ! pMes )
        return NPERR_GENERIC_ERROR;

    NPError aRet = pConnector->GetNPError( pMes );
    delete pMes;
    return aRet;
}

NPError NPN_PostURL( NPP instance, const char* url, const char* window, UINT32 len, const char* buf, NPBool file )
{
    UINT32 nInstance = pConnector->GetNPPID( instance );
    MediatorMessage* pMes = pConnector->
        Transact( eNPN_PostURL,
                  &nInstance, sizeof( nInstance ),
                  POST_STRING( url ),
                  POST_STRING( window ),
                  &len, sizeof( len ),
                  buf, len,
                  &file, sizeof( NPBool ),
                  NULL );
    if( ! pMes )
        return NPERR_GENERIC_ERROR;

    NPError aRet = pConnector->GetNPError( pMes );
    delete pMes;
    return aRet;
}

NPError NPN_RequestRead( NPStream* stream, NPByteRange* rangeList )
{
    NPByteRange* pRange = rangeList;
    UINT32 nRanges = 0;
    while( pRange )
    {
        nRanges++;
        pRange = pRange->next;
    }

    UINT32* pArray = new UINT32[ 2 * nRanges ];
    pRange = rangeList;
    UINT32 n = 0;
    while( pRange )
    {
        pArray[ 2*n    ] = (UINT32)pRange->offset;
        pArray[ 2*n + 1] = (UINT32)pRange->length;
        n++;
        pRange = pRange->next;
    }
    UINT32 nFileID = pConnector->GetStreamID( stream );
    MediatorMessage* pMes = pConnector->
        Transact( eNPN_RequestRead,
                  &nFileID, sizeof( nFileID ),
                  &nRanges, sizeof( nRanges ),
                  pArray, sizeof( UINT32 ) * 2 * nRanges,
                  NULL );

    if( ! pMes )
        return NPERR_GENERIC_ERROR;

    NPError aRet = pConnector->GetNPError( pMes );
    delete pArray;
    delete pMes;
    return aRet;
}

void NPN_Status( NPP instance, const char* message )
{
    UINT32 nInstance = pConnector->GetNPPID( instance );
    pConnector->Send( eNPN_Status,
                      &nInstance, sizeof( nInstance ),
                      POST_STRING( message ),
                      NULL );
}

const char* NPN_UserAgent( NPP instance )
{
    static char* pAgent = NULL;

    UINT32 nInstance = pConnector->GetNPPID( instance );
    MediatorMessage* pMes = pConnector->
        Transact( eNPN_UserAgent,
                  &nInstance, sizeof( nInstance ),
                  NULL );
    if( ! pMes )
        return pAgent;

    if( pAgent )
        delete pAgent;
    pAgent = pMes->GetString();

    delete pMes;
    return pAgent;
}

void NPN_Version( int* major, int* minor, int* net_major, int* net_minor )
{
    MediatorMessage* pMes = pConnector->
        Transact( eNPN_Version,
                  NULL );

    if( ! pMes )
        return;

    *major = pMes->GetUINT32();
    *minor = pMes->GetUINT32();
    *net_major = pMes->GetUINT32();
    *net_minor = pMes->GetUINT32();
    delete pMes;
}

int32 NPN_Write( NPP instance, NPStream* stream, int32 len,
                 void* buffer )
{
    UINT32 nFileID = pConnector->GetStreamID( stream );
    UINT32 nInstance = pConnector->GetNPPID( instance );
    MediatorMessage* pMes = pConnector->
        Transact( eNPN_Write,
                  &nInstance, sizeof( nInstance ),
                  &nFileID, sizeof( nFileID ),
                  &len, sizeof( len ),
                  buffer, len,
                  NULL );

    if( ! pMes )
        return 0;

    INT32 nRet = pMes->GetUINT32();
    return nRet;
}

void NPN_ReloadPlugins( NPBool reloadPages )
{
    medDebug( 1, "NPN_ReloadPlugins: SNI\n" );
}

NPError NPN_GetValue( NPP instance, NPNVariable variable, void* value )
{
    switch( variable )
    {
        case NPNVxDisplay:
            *((Display**)value) = pAppDisplay;
            medDebug( 1, "Display requested\n" );
            break;
        case NPNVxtAppContext:
            *((XtAppContext*)value) = app_context;
            medDebug( 1, "AppContext requested\n" );
            break;
        default:
            medDebug( 1, "unknown NPNVariable %d requested\n", variable );
            return 1;
    }
    return 0;
}

NPError NPN_SetValue(NPP instance, NPPVariable variable,
                         void *value)
{
    return 0;
}

void NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
{
}

void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
{
}

void NPN_ForceRedraw(NPP instance)
{
}

}