~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/modules/plugin/samples/backward/badapter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
17
 * The Initial Developer of the Original Code is 
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Pierre Phaneuf <pp@ludusdesign.com>
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the NPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
////////////////////////////////////////////////////////////////////////////////
 
40
// Backward Adapter
 
41
// This acts as a adapter layer to allow 5.0 plugins work with the 4.0/3.0 
 
42
// browser.
 
43
////////////////////////////////////////////////////////////////////////////////
 
44
 
 
45
////////////////////////////////////////////////////////////////////////////////
 
46
// SECTION 1 - Includes
 
47
////////////////////////////////////////////////////////////////////////////////
 
48
 
 
49
// extern "C" {
 
50
#include "npapi.h"
 
51
// }
 
52
#include "nsIPluginManager.h"
 
53
#include "nsIServiceManager.h"
 
54
#include "nsIAllocator.h"
 
55
#include "nsLiveConnect.h"
 
56
#include "nsplugin.h"
 
57
#include "nsDebug.h"
 
58
 
 
59
////////////////////////////////////////////////////////////////////////////////
 
60
// SECTION 3 - Classes
 
61
////////////////////////////////////////////////////////////////////////////////
 
62
 
 
63
////////////////////////////////////////////////////////////////////////////////
 
64
//
 
65
// CPluginManager
 
66
//
 
67
// This is the dummy plugin manager that interacts with the 5.0 plugin.
 
68
//
 
69
class CPluginManager : public nsIPluginManager, public nsIServiceManager, public nsIAllocator {
 
70
public:
 
71
        // Need an operator new for this.
 
72
        void* operator new(size_t size) { return ::NPN_MemAlloc(size); }
 
73
        void operator delete(void* ptr) { ::NPN_MemFree(ptr); }
 
74
 
 
75
    CPluginManager(void);
 
76
    virtual ~CPluginManager(void);
 
77
 
 
78
    NS_DECL_ISUPPORTS
 
79
 
 
80
    ////////////////////////////////////////////////////////////////////////////
 
81
    // from nsIPluginManager:
 
82
 
 
83
    // (Corresponds to NPN_GetValue.)
 
84
    NS_IMETHOD
 
85
    GetValue(nsPluginManagerVariable variable, void *value);
 
86
 
 
87
    // (Corresponds to NPN_SetValue.)
 
88
    NS_IMETHOD
 
89
    SetValue(nsPluginManagerVariable variable, void *value);
 
90
    
 
91
    NS_IMETHOD
 
92
    ReloadPlugins(PRBool reloadPages);
 
93
 
 
94
    // (Corresponds to NPN_UserAgent.)
 
95
    NS_IMETHOD
 
96
    UserAgent(const char* *result);
 
97
 
 
98
    NS_IMETHOD
 
99
    GetURL(nsISupports* pluginInst, 
 
100
           const char* url, 
 
101
           const char* target = NULL,
 
102
           nsIPluginStreamListener* streamListener = NULL,
 
103
           const char* altHost = NULL,
 
104
           const char* referrer = NULL,
 
105
           PRBool forceJSEnabled = PR_FALSE);
 
106
 
 
107
    NS_IMETHOD
 
108
    PostURL(nsISupports* pluginInst,
 
109
            const char* url,
 
110
            PRUint32 postDataLen, 
 
111
            const char* postData,
 
112
            PRBool isFile = PR_FALSE,
 
113
            const char* target = NULL,
 
114
            nsIPluginStreamListener* streamListener = NULL,
 
115
            const char* altHost = NULL, 
 
116
            const char* referrer = NULL,
 
117
            PRBool forceJSEnabled = PR_FALSE,
 
118
            PRUint32 postHeadersLength = 0, 
 
119
            const char* postHeaders = NULL);
 
120
 
 
121
    NS_IMETHOD
 
122
    RegisterPlugin(REFNSIID aCID,
 
123
                   const char* aPluginName,
 
124
                   const char* aDescription,
 
125
                   const char** aMimeTypes,
 
126
                   const char** aMimeDescriptions,
 
127
                   const char** aFileExtensions,
 
128
                   PRInt32 aCount);
 
129
 
 
130
    NS_IMETHOD
 
131
    UnregisterPlugin(REFNSIID aCID);
 
132
 
 
133
 
 
134
   /**
 
135
     * RegisterService may be called explicitly to register a service
 
136
     * with the service manager. If a service is not registered explicitly,
 
137
     * the component manager will be used to create an instance according
 
138
     * to the class ID specified.
 
139
     */
 
140
    NS_IMETHOD
 
141
    RegisterService(const nsCID& aClass, nsISupports* aService)
 
142
    {
 
143
        return NS_ERROR_NOT_IMPLEMENTED;
 
144
        }
 
145
 
 
146
    /**
 
147
     * Requests a service to be shut down, possibly unloading its DLL.
 
148
     *
 
149
     * @returns NS_OK - if shutdown was successful and service was unloaded,
 
150
     * @returns NS_ERROR_SERVICE_NOT_FOUND - if shutdown failed because
 
151
     *          the service was not currently loaded
 
152
     * @returns NS_ERROR_SERVICE_IN_USE - if shutdown failed because some
 
153
     *          user of the service wouldn't voluntarily release it by using
 
154
     *          a shutdown listener.
 
155
     */
 
156
    NS_IMETHOD
 
157
    UnregisterService(const nsCID& aClass)
 
158
    {
 
159
        return NS_ERROR_NOT_IMPLEMENTED;
 
160
        }
 
161
 
 
162
    NS_IMETHOD
 
163
    GetService(const nsCID& aClass, const nsIID& aIID,
 
164
               nsISupports* *result,
 
165
               nsIShutdownListener* shutdownListener = NULL);
 
166
 
 
167
    NS_IMETHOD
 
168
    ReleaseService(const nsCID& aClass, nsISupports* service,
 
169
                   nsIShutdownListener* shutdownListener = NULL);
 
170
 
 
171
    NS_IMETHOD
 
172
    GetService(const char* aContractID, const nsIID& aIID,
 
173
               nsISupports* *result,
 
174
               nsIShutdownListener* shutdownListener = NULL)
 
175
    {
 
176
        return NS_ERROR_NOT_IMPLEMENTED;
 
177
        }
 
178
 
 
179
    NS_IMETHOD
 
180
    ReleaseService(const char* aContractID, nsISupports* service,
 
181
                   nsIShutdownListener* shutdownListener = NULL)
 
182
    {
 
183
        return NS_ERROR_NOT_IMPLEMENTED;
 
184
        }
 
185
 
 
186
    /**
 
187
     * Allocates a block of memory of a particular size. 
 
188
     *
 
189
     * @param size - the size of the block to allocate
 
190
     * @result the block of memory
 
191
     */
 
192
    NS_IMETHOD_(void*)
 
193
    Alloc(PRUint32 size);
 
194
 
 
195
    /**
 
196
     * Reallocates a block of memory to a new size.
 
197
     *
 
198
     * @param ptr - the block of memory to reallocate
 
199
     * @param size - the new size
 
200
     * @result the rellocated block of memory
 
201
     */
 
202
    NS_IMETHOD_(void*)
 
203
    Realloc(void* ptr, PRUint32 size);
 
204
 
 
205
    /**
 
206
     * Frees a block of memory. 
 
207
     *
 
208
     * @param ptr - the block of memory to free
 
209
     */
 
210
    NS_IMETHOD
 
211
    Free(void* ptr);
 
212
 
 
213
    /**
 
214
     * Attempts to shrink the heap.
 
215
     */
 
216
    NS_IMETHOD
 
217
    HeapMinimize(void);
 
218
 
 
219
private:
 
220
        nsILiveconnect* mLiveconnect;
 
221
};
 
222
 
 
223
////////////////////////////////////////////////////////////////////////////////
 
224
//
 
225
// CPluginManagerStream
 
226
//
 
227
// This is the dummy plugin manager stream that interacts with the 5.0 plugin.
 
228
//
 
229
class CPluginManagerStream : public nsIOutputStream {
 
230
 
 
231
public:
 
232
 
 
233
    CPluginManagerStream(NPP npp, NPStream* pstr);
 
234
    virtual ~CPluginManagerStream(void);
 
235
 
 
236
    NS_DECL_ISUPPORTS
 
237
 
 
238
    //////////////////////////////////////////////////////////////////////////
 
239
    //
 
240
    // Taken from nsIStream
 
241
    //
 
242
    
 
243
    /** Write data into the stream.
 
244
     *  @param aBuf the buffer into which the data is read
 
245
     *  @param aCount the maximum number of bytes to read
 
246
     *  @param errorResult the error code if an error occurs
 
247
     *  @return number of bytes read or -1 if error
 
248
     */   
 
249
    NS_IMETHOD
 
250
    Write(const char* aBuf, PRUint32 aCount, PRUint32 *aWriteCount); 
 
251
 
 
252
    NS_IMETHOD Flush() {
 
253
        return NS_OK;
 
254
    }
 
255
 
 
256
    //////////////////////////////////////////////////////////////////////////
 
257
    //
 
258
    // Specific methods to nsIPluginManagerStream.
 
259
    //
 
260
    
 
261
    // Corresponds to NPStream's url field.
 
262
    NS_IMETHOD
 
263
    GetURL(const char*  *result);
 
264
 
 
265
    // Corresponds to NPStream's end field.
 
266
    NS_IMETHOD
 
267
    GetEnd(PRUint32 *result);
 
268
 
 
269
    // Corresponds to NPStream's lastmodfied field.
 
270
    NS_IMETHOD
 
271
    GetLastModified(PRUint32 *result);
 
272
 
 
273
    // Corresponds to NPStream's notifyData field.
 
274
    NS_IMETHOD
 
275
    GetNotifyData(void*  *result);
 
276
 
 
277
    // Corresponds to NPStream's url field.
 
278
    NS_IMETHOD Close(void);
 
279
 
 
280
protected:
 
281
 
 
282
    // npp
 
283
    // The plugin instance that the manager stream belongs to.
 
284
    NPP npp;
 
285
 
 
286
    // pstream
 
287
    // The stream the class is using.
 
288
    NPStream* pstream;
 
289
 
 
290
};
 
291
 
 
292
////////////////////////////////////////////////////////////////////////////////
 
293
//
 
294
// CPluginInstancePeer
 
295
//
 
296
// This is the dummy instance peer that interacts with the 5.0 plugin.
 
297
// In order to do LiveConnect, the class subclasses nsILiveConnectPluginInstancePeer.
 
298
//
 
299
class CPluginInstancePeer : public nsIPluginInstancePeer, public nsIPluginTagInfo {
 
300
 
 
301
public:
 
302
 
 
303
    // XXX - I add parameters to the constructor because I wasn't sure if
 
304
    // XXX - the 4.0 browser had the npp_instance struct implemented.
 
305
    // XXX - If so, then I can access npp_instance through npp->ndata.
 
306
    CPluginInstancePeer(nsIPluginInstance* pluginInstance, NPP npp, nsMIMEType typeString, nsPluginMode type,
 
307
        PRUint16 attribute_cnt, const char** attribute_list, const char** values_list);
 
308
 
 
309
    virtual ~CPluginInstancePeer(void);
 
310
 
 
311
    NS_DECL_ISUPPORTS
 
312
 
 
313
    // (Corresponds to NPN_GetValue.)
 
314
    NS_IMETHOD
 
315
    GetValue(nsPluginInstancePeerVariable variable, void *value);
 
316
 
 
317
    // (Corresponds to NPN_SetValue.)
 
318
    NS_IMETHOD
 
319
    SetValue(nsPluginInstancePeerVariable variable, void *value);
 
320
 
 
321
    // Corresponds to NPP_New's MIMEType argument.
 
322
    NS_IMETHOD
 
323
    GetMIMEType(nsMIMEType *result);
 
324
 
 
325
    // Corresponds to NPP_New's mode argument.
 
326
    NS_IMETHOD
 
327
    GetMode(nsPluginMode *result);
 
328
 
 
329
    // Get a ptr to the paired list of attribute names and values,
 
330
    // returns the length of the array.
 
331
    //
 
332
    // Each name or value is a null-terminated string.
 
333
    NS_IMETHOD
 
334
    GetAttributes(PRUint16& n, const char* const*& names, const char* const*& values);
 
335
 
 
336
    // Get the value for the named attribute.  Returns null
 
337
    // if the attribute was not set.
 
338
    NS_IMETHOD
 
339
    GetAttribute(const char* name, const char* *result);
 
340
 
 
341
    // Corresponds to NPN_NewStream.
 
342
    NS_IMETHOD
 
343
    NewStream(nsMIMEType type, const char* target, nsIOutputStream* *result);
 
344
 
 
345
    // Corresponds to NPN_ShowStatus.
 
346
    NS_IMETHOD
 
347
    ShowStatus(const char* message);
 
348
 
 
349
    NS_IMETHOD
 
350
    SetWindowSize(PRUint32 width, PRUint32 height);
 
351
 
 
352
        nsIPluginInstance* GetInstance(void) { return mInstance; }
 
353
        NPP GetNPPInstance(void) { return npp; }
 
354
        
 
355
        void SetWindow(NPWindow* window) { mWindow = window; }
 
356
        NPWindow* GetWindow() { return mWindow; }
 
357
        
 
358
protected:
 
359
 
 
360
    NPP npp;
 
361
    // XXX - The next five variables may need to be here since I
 
362
    // XXX - don't think np_instance is available in 4.0X.
 
363
    nsIPluginInstance* mInstance;
 
364
    NPWindow* mWindow;
 
365
    nsMIMEType typeString;
 
366
        nsPluginMode type;
 
367
        PRUint16 attribute_cnt;
 
368
        char** attribute_list;
 
369
        char** values_list;
 
370
};
 
371
 
 
372
class CPluginStreamInfo : public nsIPluginStreamInfo {
 
373
public:
 
374
    NS_DECL_ISUPPORTS
 
375
 
 
376
        CPluginStreamInfo(const char* URL, nsIPluginInputStream* inStr, nsMIMEType type, PRBool seekable)
 
377
                 : mURL(URL), mInputStream(inStr), mMimeType(type), mIsSeekable(seekable) {}
 
378
 
 
379
        virtual ~CPluginStreamInfo() {}
 
380
 
 
381
        NS_METHOD
 
382
        GetContentType(nsMIMEType* result)
 
383
        {
 
384
                *result = mMimeType;
 
385
                return NS_OK;
 
386
        }
 
387
 
 
388
        NS_METHOD
 
389
        IsSeekable(PRBool* result)
 
390
        {
 
391
                *result = mIsSeekable;
 
392
                return NS_OK;
 
393
        }
 
394
 
 
395
        NS_METHOD
 
396
        GetLength(PRUint32* result)
 
397
        {
 
398
                return mInputStream->GetLength(result);
 
399
        }
 
400
 
 
401
        NS_METHOD
 
402
        GetLastModified(PRUint32* result)
 
403
        {
 
404
                return mInputStream->GetLastModified(result);
 
405
        }
 
406
 
 
407
        NS_METHOD
 
408
        GetURL(const char** result)
 
409
        {
 
410
                *result = mURL;
 
411
                return NS_OK;
 
412
        }
 
413
 
 
414
        NS_METHOD
 
415
        RequestRead(nsByteRange* rangeList)
 
416
        {
 
417
                return mInputStream->RequestRead(rangeList);
 
418
        }
 
419
 
 
420
private:
 
421
        const char* mURL;
 
422
        nsIPluginInputStream* mInputStream;
 
423
        nsMIMEType mMimeType;
 
424
        PRBool mIsSeekable;
 
425
};
 
426
 
 
427
class CPluginInputStream : public nsIPluginInputStream {
 
428
public:
 
429
 
 
430
    NS_DECL_ISUPPORTS
 
431
 
 
432
    NS_DECL_NSIINPUTSTREAM
 
433
 
 
434
    ////////////////////////////////////////////////////////////////////////////
 
435
    // from nsIPluginInputStream:
 
436
 
 
437
    // (Corresponds to NPStream's lastmodified field.)
 
438
    NS_IMETHOD
 
439
    GetLastModified(PRUint32 *result);
 
440
 
 
441
    NS_IMETHOD
 
442
    RequestRead(nsByteRange* rangeList);
 
443
 
 
444
    ////////////////////////////////////////////////////////////////////////////
 
445
    // CPluginInputStream specific methods:
 
446
 
 
447
    CPluginInputStream(nsIPluginStreamListener* listener);
 
448
    virtual ~CPluginInputStream(void);
 
449
 
 
450
    void SetStreamInfo(NPP npp, NPStream* stream) {
 
451
        mNPP = npp;
 
452
        mStream = stream;
 
453
    }
 
454
 
 
455
    nsIPluginStreamListener* GetListener(void) { return mListener; }
 
456
    nsPluginStreamType GetStreamType(void) { return mStreamType; }
 
457
 
 
458
    nsresult SetReadBuffer(PRUint32 len, const char* buffer) {
 
459
        // XXX this has to be way more sophisticated
 
460
        mBuffer = dup(len, buffer);
 
461
        mBufferLength = len;
 
462
        mAmountRead = 0;
 
463
        return NS_OK;
 
464
    }
 
465
    
 
466
    char* dup(PRUint32 len, const char* buffer) {
 
467
        char* result = new char[len];
 
468
        if (result != NULL) {
 
469
                const char *src = buffer;
 
470
                char *dest = result; 
 
471
                while (len-- > 0)
 
472
                        *dest++ = *src++;
 
473
        }
 
474
        return result;
 
475
    }
 
476
 
 
477
        nsIPluginStreamInfo* CreatePluginStreamInfo(const char* url, nsMIMEType type, PRBool seekable) {
 
478
                if (mStreamInfo == NULL) {
 
479
                        mStreamInfo = new CPluginStreamInfo(url, this, type, seekable);
 
480
                        mStreamInfo->AddRef();
 
481
                }
 
482
                return mStreamInfo;
 
483
        }
 
484
        
 
485
        nsIPluginStreamInfo* GetPluginStreamInfo() {
 
486
                return mStreamInfo;
 
487
        }
 
488
 
 
489
protected:
 
490
    const char* mURL;
 
491
    nsIPluginStreamListener* mListener;
 
492
    nsPluginStreamType mStreamType;
 
493
    NPP mNPP;
 
494
    NPStream* mStream;
 
495
    char* mBuffer;
 
496
    PRUint32 mBufferLength;
 
497
    PRUint32 mAmountRead;
 
498
    nsIPluginStreamInfo* mStreamInfo;
 
499
};
 
500
 
 
501
//////////////////////////////////////////////////////////////////////////////
 
502
 
 
503
#ifdef XP_UNIX
 
504
#define TRACE(foo) trace(foo)
 
505
#endif
 
506
 
 
507
#ifdef XP_MAC
 
508
#undef assert
 
509
#define assert(cond)
 
510
#endif
 
511
 
 
512
#if defined(__cplusplus)
 
513
extern "C" {
 
514
#endif
 
515
 
 
516
////////////////////////////////////////////////////////////////////////////////
 
517
// SECTION 1 - Includes
 
518
////////////////////////////////////////////////////////////////////////////////
 
519
 
 
520
#if defined(XP_UNIX) || defined(XP_MAC)
 
521
#include <stdio.h>
 
522
#include <stdlib.h>
 
523
#include <string.h>
 
524
#else
 
525
#include <windows.h>
 
526
#endif
 
527
 
 
528
////////////////////////////////////////////////////////////////////////////////
 
529
// SECTION 2 - Global Variables
 
530
////////////////////////////////////////////////////////////////////////////////
 
531
 
 
532
//
 
533
// thePlugin and thePluginManager are used in the life of the plugin.
 
534
//
 
535
// These two will be created on NPP_Initialize and destroyed on NPP_Shutdown.
 
536
//
 
537
nsIPluginManager* thePluginManager = NULL;
 
538
nsIPlugin* thePlugin = NULL;
 
539
 
 
540
//
 
541
// nsISupports IDs
 
542
//
 
543
// Interface IDs for nsISupports
 
544
//
 
545
NS_DEFINE_IID(kPluginCID, NS_PLUGIN_CID);
 
546
static NS_DEFINE_IID(kPluginManagerCID, NS_PLUGINMANAGER_CID);
 
547
static NS_DEFINE_IID(kAllocatorCID, NS_ALLOCATOR_CID);
 
548
 
 
549
// mapping from NPError to nsresult
 
550
nsresult fromNPError[] = {
 
551
    NS_OK,                          // NPERR_NO_ERROR,
 
552
    NS_ERROR_FAILURE,               // NPERR_GENERIC_ERROR,
 
553
    NS_ERROR_FAILURE,               // NPERR_INVALID_INSTANCE_ERROR,
 
554
    NS_ERROR_NOT_INITIALIZED,       // NPERR_INVALID_FUNCTABLE_ERROR,
 
555
    NS_ERROR_FACTORY_NOT_LOADED,    // NPERR_MODULE_LOAD_FAILED_ERROR,
 
556
    NS_ERROR_OUT_OF_MEMORY,         // NPERR_OUT_OF_MEMORY_ERROR,
 
557
    NS_NOINTERFACE,                 // NPERR_INVALID_PLUGIN_ERROR,
 
558
    NS_ERROR_ILLEGAL_VALUE,         // NPERR_INVALID_PLUGIN_DIR_ERROR,
 
559
    NS_NOINTERFACE,                 // NPERR_INCOMPATIBLE_VERSION_ERROR,
 
560
    NS_ERROR_ILLEGAL_VALUE,         // NPERR_INVALID_PARAM,
 
561
    NS_ERROR_ILLEGAL_VALUE,         // NPERR_INVALID_URL,
 
562
    NS_ERROR_ILLEGAL_VALUE,         // NPERR_FILE_NOT_FOUND,
 
563
    NS_ERROR_FAILURE,               // NPERR_NO_DATA,
 
564
    NS_ERROR_FAILURE                // NPERR_STREAM_NOT_SEEKABLE,
 
565
};
 
566
 
 
567
////////////////////////////////////////////////////////////////////////////////
 
568
// SECTION 4 - API Shim Plugin Implementations
 
569
// Glue code to the 5.0x Plugin.
 
570
//
 
571
// Most of the NPP_* functions that interact with the plug-in will need to get 
 
572
// the instance peer from npp->pdata so it can get the plugin instance from the
 
573
// peer. Once the plugin instance is available, the appropriate 5.0 plug-in
 
574
// function can be called:
 
575
//          
 
576
//  CPluginInstancePeer* peer = (CPluginInstancePeer* )instance->pdata;
 
577
//  nsIPluginInstance* inst = peer->GetUserInstance();
 
578
//  inst->NewPluginAPIFunction();
 
579
//
 
580
// Similar steps takes place with streams.  The stream peer is stored in NPStream's
 
581
// pdata.  Get the peer, get the stream, call the function.
 
582
//
 
583
 
 
584
////////////////////////////////////////////////////////////////////////////////
 
585
// UNIX-only API calls
 
586
////////////////////////////////////////////////////////////////////////////////
 
587
 
 
588
#ifdef XP_UNIX
 
589
char* NPP_GetMIMEDescription(void)
 
590
{
 
591
    int freeFac = 0;
 
592
    //fprintf(stderr, "MIME description\n");
 
593
    if (thePlugin == NULL) {
 
594
        freeFac = 1;
 
595
        NSGetFactory(thePluginManager, kPluginCID, NULL, NULL, (nsIFactory** )&thePlugin);
 
596
    }
 
597
    //fprintf(stderr, "Allocated Plugin 0x%08x\n", thePlugin);
 
598
    const char * ret;
 
599
    nsresult err = thePlugin->GetMIMEDescription(&ret);
 
600
    if (err) return NULL;
 
601
    //fprintf(stderr, "Get response %s\n", ret);
 
602
    if (freeFac) {
 
603
        //fprintf(stderr, "Freeing plugin...");
 
604
        thePlugin->Release();
 
605
        thePlugin = NULL;
 
606
    }
 
607
    //fprintf(stderr, "Done\n");
 
608
    return (char*)ret;
 
609
}
 
610
 
 
611
 
 
612
//------------------------------------------------------------------------------
 
613
// Cross-Platform Plug-in API Calls
 
614
//------------------------------------------------------------------------------
 
615
 
 
616
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
617
// NPP_SetValue:
 
618
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
619
 
 
620
NPError
 
621
NPP_SetValue(NPP instance, NPNVariable variable, void *value)
 
622
{
 
623
    return NPERR_GENERIC_ERROR; // nothing to set
 
624
}
 
625
 
 
626
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
627
// NPP_GetValue:
 
628
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
629
 
 
630
NPError
 
631
NPP_GetValue(NPP instance, NPPVariable variable, void *value) {
 
632
    int freeFac = 0;
 
633
    //fprintf(stderr, "MIME description\n");
 
634
    if (thePlugin == NULL) {
 
635
        freeFac = 1;
 
636
        if (NSGetFactory(thePluginManager, kPluginCID, NULL, NULL, (nsIFactory** )&thePlugin) != NS_OK)
 
637
            return NPERR_GENERIC_ERROR;
 
638
    }
 
639
    //fprintf(stderr, "Allocated Plugin 0x%08x\n", thePlugin);
 
640
    nsresult err = thePlugin->GetValue((nsPluginVariable)variable, value);
 
641
    if (err) return NPERR_GENERIC_ERROR;
 
642
    //fprintf(stderr, "Get response %08x\n", ret);
 
643
    if (freeFac) {
 
644
        //fprintf(stderr, "Freeing plugin...");
 
645
        thePlugin->Release();
 
646
        thePlugin = NULL;
 
647
    }
 
648
    //fprintf(stderr, "Done\n");
 
649
    return NPERR_NO_ERROR;
 
650
}
 
651
#endif // XP_UNIX
 
652
 
 
653
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
654
// NPP_Initialize:
 
655
// Provides global initialization for a plug-in, and returns an error value. 
 
656
//
 
657
// This function is called once when a plug-in is loaded, before the first instance
 
658
// is created. thePluginManager and thePlugin are both initialized.
 
659
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
660
 
 
661
NPError
 
662
NPP_Initialize(void)
 
663
{
 
664
//    TRACE("NPP_Initialize\n");
 
665
 
 
666
    // Only call initialize the plugin if it hasn't been created.
 
667
    // This could happen if GetJavaClass() is called before
 
668
    // NPP Initialize.  
 
669
    if (thePluginManager == NULL) {
 
670
        // Create the plugin manager and plugin classes.
 
671
        thePluginManager = new CPluginManager();        
 
672
        if ( thePluginManager == NULL ) 
 
673
            return NPERR_OUT_OF_MEMORY_ERROR;  
 
674
        thePluginManager->AddRef();
 
675
    }
 
676
    nsresult error = NS_OK;  
 
677
    // On UNIX the plugin might have been created when calling NPP_GetMIMEType.
 
678
    if (thePlugin == NULL) {
 
679
        // create nsIPlugin factory
 
680
        error = (NPError)NSGetFactory(thePluginManager, kPluginCID, NULL, NULL, (nsIFactory** )&thePlugin);
 
681
#if 0
 
682
                // beard: this will leak reference counts.       
 
683
            if (error == NS_OK) {
 
684
                thePlugin->AddRef();
 
685
            }
 
686
#endif
 
687
        }
 
688
        
 
689
    return (NPError) error;     
 
690
}
 
691
 
 
692
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
693
// NPP_GetJavaClass:
 
694
// New in Netscape Navigator 3.0. 
 
695
// 
 
696
// NPP_GetJavaClass is called during initialization to ask your plugin
 
697
// what its associated Java class is. If you don't have one, just return
 
698
// NULL. Otherwise, use the javah-generated "use_" function to both
 
699
// initialize your class and return it. If you can't find your class, an
 
700
// error will be signalled by "use_" and will cause the Navigator to
 
701
// complain to the user.
 
702
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
703
 
 
704
jref
 
705
NPP_GetJavaClass(void)
 
706
{
 
707
    // Only call initialize the plugin if it hasn't been `d.
 
708
#if 0
 
709
    if (thePluginManager == NULL) {
 
710
        // Create the plugin manager and plugin objects.
 
711
        NPError result = CPluginManager::Create();      
 
712
        if (result) return NULL;
 
713
        assert( thePluginManager != NULL );
 
714
        thePluginManager->AddRef();
 
715
        NP_CreatePlugin(thePluginManager, (nsIPlugin** )(&thePlugin));
 
716
        assert( thePlugin != NULL );
 
717
    }
 
718
    return thePlugin->GetJavaClass();
 
719
#endif
 
720
    return NULL;
 
721
}
 
722
 
 
723
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
724
// NPP_Shutdown:
 
725
// Provides global deinitialization for a plug-in. 
 
726
// 
 
727
// This function is called once after the last instance of your plug-in 
 
728
// is destroyed.  thePluginManager and thePlugin are delete at this time.
 
729
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
730
 
 
731
void
 
732
NPP_Shutdown(void)
 
733
{
 
734
//    TRACE("NPP_Shutdown\n");
 
735
 
 
736
    if (thePlugin)
 
737
    {
 
738
        thePlugin->Shutdown();
 
739
        thePlugin->Release();
 
740
        thePlugin = NULL;
 
741
    }
 
742
 
 
743
    if (thePluginManager)  {
 
744
        thePluginManager->Release();
 
745
        thePluginManager = NULL;
 
746
    }
 
747
    
 
748
    return;
 
749
}
 
750
 
 
751
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
752
// NPP_New:
 
753
// Creates a new instance of a plug-in and returns an error value. 
 
754
// 
 
755
// A plugin instance peer and instance peer is created.  After
 
756
// a successful instansiation, the peer is stored in the plugin
 
757
// instance's pdata.
 
758
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
759
 
 
760
NPError 
 
761
NPP_New(NPMIMEType pluginType,
 
762
        NPP instance,
 
763
        PRUint16 mode,
 
764
        int16 argc,
 
765
        char* argn[],
 
766
        char* argv[],
 
767
        NPSavedData* saved)
 
768
{
 
769
//    TRACE("NPP_New\n");
 
770
    
 
771
    if (instance == NULL)
 
772
        return NPERR_INVALID_INSTANCE_ERROR;
 
773
 
 
774
    // Create a new plugin instance and start it.
 
775
    nsIPluginInstance* pluginInstance = NULL;
 
776
    thePlugin->CreatePluginInstance(thePluginManager, NS_GET_IID(nsIPluginInstance), pluginType, (void**)&pluginInstance);
 
777
    if (pluginInstance == NULL) {
 
778
        return NPERR_OUT_OF_MEMORY_ERROR;
 
779
    }
 
780
    
 
781
    // Create a new plugin instance peer,
 
782
    // XXX - Since np_instance is not implemented in the 4.0x browser, I
 
783
    // XXX - had to save the plugin parameter in the peer class.
 
784
    // XXX - Ask Warren about np_instance.
 
785
    CPluginInstancePeer* peer = new CPluginInstancePeer(pluginInstance, instance, (nsMIMEType)pluginType, 
 
786
                                                                                (nsPluginMode)mode, (PRUint16)argc, (const char** )argn, (const char** )argv);
 
787
    assert( peer != NULL );
 
788
    if (!peer) return NPERR_OUT_OF_MEMORY_ERROR;
 
789
    peer->AddRef();
 
790
    pluginInstance->Initialize(peer);
 
791
    pluginInstance->Start();
 
792
    // Set the user instance and store the peer in npp->pdata.
 
793
    instance->pdata = peer;
 
794
    peer->Release();
 
795
 
 
796
    return NPERR_NO_ERROR;
 
797
}
 
798
 
 
799
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
800
// NPP_Destroy:
 
801
// Deletes a specific instance of a plug-in and returns an error value. 
 
802
//
 
803
// The plugin instance peer and plugin instance are destroyed.
 
804
// The instance's pdata is set to NULL.
 
805
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
806
 
 
807
NPError 
 
808
NPP_Destroy(NPP instance, NPSavedData** save)
 
809
{
 
810
//    TRACE("NPP_Destroy\n");
 
811
    
 
812
    if (instance == NULL)
 
813
        return NPERR_INVALID_INSTANCE_ERROR;
 
814
    
 
815
    CPluginInstancePeer* peer = (CPluginInstancePeer*) instance->pdata;
 
816
    nsIPluginInstance* pluginInstance = peer->GetInstance();
 
817
    pluginInstance->Stop();
 
818
    pluginInstance->Destroy();
 
819
    pluginInstance->Release();
 
820
        // peer->Release();
 
821
    instance->pdata = NULL;
 
822
    
 
823
    return NPERR_NO_ERROR;
 
824
}
 
825
 
 
826
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
827
// NPP_SetWindow:
 
828
// Sets the window in which a plug-in draws, and returns an error value. 
 
829
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
830
 
 
831
NPError 
 
832
NPP_SetWindow(NPP instance, NPWindow* window)
 
833
{
 
834
//    TRACE("NPP_SetWindow\n");
 
835
    
 
836
    if (instance == NULL)
 
837
        return NPERR_INVALID_INSTANCE_ERROR;
 
838
 
 
839
    CPluginInstancePeer* peer = (CPluginInstancePeer*) instance->pdata;
 
840
    if ( peer == NULL)
 
841
        return NPERR_INVALID_PLUGIN_ERROR;
 
842
 
 
843
        // record the window in the peer, so we can deliver proper events.
 
844
        peer->SetWindow(window);
 
845
 
 
846
    nsIPluginInstance* pluginInstance = peer->GetInstance();
 
847
    if( pluginInstance == 0 )
 
848
        return NPERR_INVALID_PLUGIN_ERROR;
 
849
 
 
850
    return (NPError)pluginInstance->SetWindow((nsPluginWindow* ) window );
 
851
}
 
852
 
 
853
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
854
// NPP_NewStream:
 
855
// Notifies an instance of a new data stream and returns an error value. 
 
856
//
 
857
// Create a stream peer and stream.  If succesful, save
 
858
// the stream peer in NPStream's pdata.
 
859
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
860
 
 
861
NPError 
 
862
NPP_NewStream(NPP instance,
 
863
              NPMIMEType type,
 
864
              NPStream *stream, 
 
865
              NPBool seekable,
 
866
              PRUint16 *stype)
 
867
{
 
868
    // XXX - How do you set the fields of the peer stream and stream?
 
869
    // XXX - Looks like these field will have to be created since
 
870
    // XXX - We are not using np_stream.
 
871
   
 
872
//    TRACE("NPP_NewStream\n");
 
873
 
 
874
    if (instance == NULL)
 
875
        return NPERR_INVALID_INSTANCE_ERROR;
 
876
                                
 
877
    CPluginInputStream* inStr = (CPluginInputStream*)stream->notifyData;
 
878
    if (inStr == NULL)
 
879
        return NPERR_GENERIC_ERROR;
 
880
    
 
881
    nsIPluginStreamInfo* info = inStr->CreatePluginStreamInfo(stream->url, type, seekable);
 
882
    nsresult err = inStr->GetListener()->OnStartBinding(info);
 
883
    if (err) return err;
 
884
 
 
885
    inStr->SetStreamInfo(instance, stream);
 
886
    stream->pdata = inStr;
 
887
    *stype = inStr->GetStreamType();
 
888
 
 
889
    return NPERR_NO_ERROR;
 
890
}
 
891
 
 
892
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
893
// NPP_WriteReady:
 
894
// Returns the maximum number of bytes that an instance is prepared to accept
 
895
// from the stream. 
 
896
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
897
 
 
898
int32 
 
899
NPP_WriteReady(NPP instance, NPStream *stream)
 
900
{
 
901
//    TRACE("NPP_WriteReady\n");
 
902
 
 
903
    if (instance == NULL)
 
904
        return -1;
 
905
 
 
906
    CPluginInputStream* inStr = (CPluginInputStream*)stream->pdata;
 
907
    if (inStr == NULL)
 
908
        return -1;
 
909
    return NP_MAXREADY;
 
910
}
 
911
 
 
912
 
 
913
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
914
// NPP_Write:
 
915
// Delivers data from a stream and returns the number of bytes written. 
 
916
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
917
 
 
918
int32 
 
919
NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
 
920
{
 
921
//    TRACE("NPP_Write\n");
 
922
 
 
923
    if (instance == NULL)
 
924
        return -1;
 
925
        
 
926
    CPluginInputStream* inStr = (CPluginInputStream*)stream->pdata;
 
927
    if (inStr == NULL)
 
928
        return -1;
 
929
    nsresult err = inStr->SetReadBuffer((PRUint32)len, (const char*)buffer);
 
930
    if (err != NS_OK) return -1;
 
931
    err = inStr->GetListener()->OnDataAvailable(inStr->GetPluginStreamInfo(), inStr, len);
 
932
    if (err != NS_OK) return -1;
 
933
    return len;
 
934
}
 
935
 
 
936
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
937
// NPP_DestroyStream:
 
938
// Indicates the closure and deletion of a stream, and returns an error value. 
 
939
//
 
940
// The stream peer and stream are destroyed.  NPStream's
 
941
// pdata is set to NULL.
 
942
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
943
 
 
944
NPError 
 
945
NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason)
 
946
{
 
947
//    TRACE("NPP_DestroyStream\n");
 
948
 
 
949
    if (instance == NULL)
 
950
        return NPERR_INVALID_INSTANCE_ERROR;
 
951
                
 
952
    CPluginInputStream* inStr = (CPluginInputStream*)stream->pdata;
 
953
    if (inStr == NULL)
 
954
        return NPERR_GENERIC_ERROR;
 
955
    inStr->GetListener()->OnStopBinding(inStr->GetPluginStreamInfo(), (nsPluginReason)reason);
 
956
    // inStr->Release();
 
957
    stream->pdata = NULL;
 
958
        
 
959
    return NPERR_NO_ERROR;
 
960
}
 
961
 
 
962
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
963
// NPP_StreamAsFile:
 
964
// Provides a local file name for the data from a stream. 
 
965
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
966
 
 
967
void 
 
968
NPP_StreamAsFile(NPP instance, NPStream *stream, const char* fname)
 
969
{
 
970
//    TRACE("NPP_StreamAsFile\n");
 
971
 
 
972
    if (instance == NULL)
 
973
        return;
 
974
                
 
975
    CPluginInputStream* inStr = (CPluginInputStream*)stream->pdata;
 
976
    if (inStr == NULL)
 
977
        return;
 
978
    (void)inStr->GetListener()->OnFileAvailable(inStr->GetPluginStreamInfo(), fname);
 
979
}
 
980
 
 
981
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
982
// NPP_Print:
 
983
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
984
 
 
985
void 
 
986
NPP_Print(NPP instance, NPPrint* printInfo)
 
987
{
 
988
//    TRACE("NPP_Print\n");
 
989
 
 
990
    if(printInfo == NULL)   // trap invalid parm
 
991
        return;
 
992
 
 
993
    if (instance != NULL)
 
994
    {
 
995
        CPluginInstancePeer* peer = (CPluginInstancePeer*) instance->pdata;
 
996
        nsIPluginInstance* pluginInstance = peer->GetInstance();
 
997
        pluginInstance->Print((nsPluginPrint* ) printInfo );
 
998
    }
 
999
}
 
1000
 
 
1001
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1002
// NPP_URLNotify:
 
1003
// Notifies the instance of the completion of a URL request. 
 
1004
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1005
 
 
1006
void
 
1007
NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
 
1008
{
 
1009
//    TRACE("NPP_URLNotify\n");
 
1010
 
 
1011
    if (instance != NULL) {
 
1012
        CPluginInputStream* inStr = (CPluginInputStream*)notifyData;
 
1013
        (void)inStr->GetListener()->OnStopBinding(inStr->GetPluginStreamInfo(), (nsPluginReason)reason);
 
1014
        inStr->Release();
 
1015
    }
 
1016
}
 
1017
 
 
1018
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1019
// NPP_HandleEvent:
 
1020
// Mac-only, but stub must be present for Windows
 
1021
// Delivers a platform-specific event to the instance. 
 
1022
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1023
 
 
1024
#ifndef XP_UNIX
 
1025
int16
 
1026
NPP_HandleEvent(NPP instance, void* event)
 
1027
{
 
1028
//    TRACE("NPP_HandleEvent\n");
 
1029
    int16 eventHandled = FALSE;
 
1030
    if (instance == NULL)
 
1031
        return eventHandled;
 
1032
        
 
1033
    NPEvent* npEvent = (NPEvent*) event;
 
1034
    nsPluginEvent pluginEvent = {
 
1035
#ifdef XP_MAC
 
1036
        npEvent, NULL
 
1037
#else
 
1038
        npEvent->event, npEvent->wParam, npEvent->lParam
 
1039
#endif
 
1040
    };
 
1041
        
 
1042
    CPluginInstancePeer* peer = (CPluginInstancePeer*) instance->pdata;
 
1043
    nsIPluginInstance* pluginInstance = peer->GetInstance();
 
1044
    if (pluginInstance) {
 
1045
        PRBool handled;
 
1046
        nsresult err = pluginInstance->HandleEvent(&pluginEvent, &handled);
 
1047
        if (err) return FALSE;
 
1048
        eventHandled = (handled == PR_TRUE);
 
1049
    }
 
1050
        
 
1051
    return eventHandled;
 
1052
}
 
1053
#endif // ndef XP_UNIX 
 
1054
 
 
1055
//////////////////////////////////////////////////////////////////////////////
 
1056
// SECTION 5 - API Browser Implementations
 
1057
//
 
1058
// Glue code to the 4.0x Browser.
 
1059
//////////////////////////////////////////////////////////////////////////////
 
1060
 
 
1061
//////////////////////////////////////////////////////////////////////////////
 
1062
//
 
1063
// CPluginManager
 
1064
//
 
1065
 
 
1066
//******************************************************************************
 
1067
//
 
1068
// Once we moved to the new APIs, we need to implement fJVMMgr.
 
1069
//
 
1070
//******************************************************************************
 
1071
 
 
1072
CPluginManager::CPluginManager(void)
 
1073
{
 
1074
    mLiveconnect = NULL;
 
1075
}
 
1076
 
 
1077
CPluginManager::~CPluginManager(void) 
 
1078
{
 
1079
        NS_IF_RELEASE(mLiveconnect);
 
1080
}
 
1081
 
 
1082
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1083
// ReloadPlugins:
 
1084
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1085
 
 
1086
NS_METHOD
 
1087
CPluginManager::ReloadPlugins(PRBool reloadPages)
 
1088
{
 
1089
    NPN_ReloadPlugins(reloadPages);
 
1090
    return NS_OK;
 
1091
}
 
1092
 
 
1093
NS_METHOD
 
1094
CPluginManager::GetURL(nsISupports* pluginInst, 
 
1095
                       const char* url, 
 
1096
                       const char* target,
 
1097
                       nsIPluginStreamListener* streamListener,
 
1098
                       const char* altHost,
 
1099
                       const char* referrer,
 
1100
                       PRBool forceJSEnabled)
 
1101
{
 
1102
    if (altHost != NULL || referrer != NULL || forceJSEnabled != PR_FALSE) {
 
1103
        return NPERR_INVALID_PARAM;
 
1104
    }
 
1105
 
 
1106
    nsIPluginInstance* inst = NULL;
 
1107
    nsresult rslt = pluginInst->QueryInterface(NS_GET_IID(nsIPluginInstance), (void**)&inst);
 
1108
    if (rslt != NS_OK) return rslt;
 
1109
        CPluginInstancePeer* instancePeer = NULL;
 
1110
    rslt = inst->GetPeer((nsIPluginInstancePeer**)&instancePeer);
 
1111
    if (rslt != NS_OK) {
 
1112
        inst->Release();
 
1113
        return rslt;
 
1114
    }
 
1115
    NPP npp = instancePeer->GetNPPInstance();
 
1116
 
 
1117
    NPError err;
 
1118
    if (streamListener) {
 
1119
        CPluginInputStream* inStr = new CPluginInputStream(streamListener);
 
1120
        if (inStr == NULL) {
 
1121
            instancePeer->Release();
 
1122
            inst->Release();
 
1123
            return NS_ERROR_OUT_OF_MEMORY;
 
1124
        }
 
1125
        inStr->AddRef();
 
1126
    
 
1127
        err = NPN_GetURLNotify(npp, url, target, inStr);
 
1128
    }
 
1129
    else {
 
1130
        err = NPN_GetURL(npp, url, target);
 
1131
    }
 
1132
    instancePeer->Release();
 
1133
    inst->Release();
 
1134
    return fromNPError[err];
 
1135
}
 
1136
 
 
1137
NS_METHOD
 
1138
CPluginManager::PostURL(nsISupports* pluginInst,
 
1139
                        const char* url,
 
1140
                        PRUint32 postDataLen, 
 
1141
                        const char* postData,
 
1142
                        PRBool isFile,
 
1143
                        const char* target,
 
1144
                        nsIPluginStreamListener* streamListener,
 
1145
                        const char* altHost, 
 
1146
                        const char* referrer,
 
1147
                        PRBool forceJSEnabled,
 
1148
                        PRUint32 postHeadersLength, 
 
1149
                        const char* postHeaders)
 
1150
{
 
1151
    if (altHost != NULL || referrer != NULL || forceJSEnabled != PR_FALSE
 
1152
        || postHeadersLength != 0 || postHeaders != NULL) {
 
1153
        return NPERR_INVALID_PARAM;
 
1154
    }
 
1155
 
 
1156
    nsIPluginInstance* inst = NULL;
 
1157
    nsresult rslt = pluginInst->QueryInterface(NS_GET_IID(nsIPluginInstance), (void**)&inst);
 
1158
    if (rslt != NS_OK) return rslt;
 
1159
        CPluginInstancePeer* instancePeer = NULL;
 
1160
    rslt = inst->GetPeer((nsIPluginInstancePeer**)&instancePeer);
 
1161
    if (rslt != NS_OK) {
 
1162
        inst->Release();
 
1163
        return rslt;
 
1164
    }
 
1165
    NPP npp = instancePeer->GetNPPInstance();
 
1166
 
 
1167
    NPError err;
 
1168
    if (streamListener) {
 
1169
        CPluginInputStream* inStr = new CPluginInputStream(streamListener);
 
1170
        if (inStr == NULL) {
 
1171
            instancePeer->Release();
 
1172
            inst->Release();
 
1173
            return NS_ERROR_OUT_OF_MEMORY;
 
1174
        }
 
1175
        inStr->AddRef();
 
1176
    
 
1177
        err = NPN_PostURLNotify(npp, url, target, postDataLen, postData, isFile, inStr);
 
1178
    }
 
1179
    else {
 
1180
        err = NPN_PostURL(npp, url, target, postDataLen, postData, isFile);
 
1181
    }
 
1182
    instancePeer->Release();
 
1183
    inst->Release();
 
1184
    return fromNPError[err];
 
1185
}
 
1186
 
 
1187
 
 
1188
NS_IMETHODIMP
 
1189
CPluginManager::RegisterPlugin(REFNSIID aCID,
 
1190
                               const char* aPluginName,
 
1191
                               const char* aDescription,
 
1192
                               const char** aMimeTypes,
 
1193
                               const char** aMimeDescriptions,
 
1194
                               const char** aFileExtensions,
 
1195
                               PRInt32 aCount)
 
1196
{
 
1197
    // XXXwaterson I don't think we need to do anything here.
 
1198
    return NS_OK;
 
1199
}
 
1200
 
 
1201
 
 
1202
NS_IMETHODIMP
 
1203
CPluginManager::UnregisterPlugin(REFNSIID aCID)
 
1204
{
 
1205
    // XXXwaterson I don't think we need to do anything here.
 
1206
    return NS_OK;
 
1207
}
 
1208
 
 
1209
NS_METHOD
 
1210
CPluginManager::GetService(const nsCID& aClass, const nsIID& aIID,
 
1211
               nsISupports* *result,
 
1212
               nsIShutdownListener* shutdownListener)
 
1213
{
 
1214
        // the only service we support currently is nsIAllocator.
 
1215
        if (aClass.Equals(kPluginManagerCID) || aClass.Equals(kAllocatorCID)) {
 
1216
                return QueryInterface(aIID, (void**) result);
 
1217
        }
 
1218
        if (aClass.Equals(nsILiveconnect::GetCID())) {
 
1219
                if (mLiveconnect == NULL) {
 
1220
                        mLiveconnect = new nsLiveconnect;
 
1221
                        NS_IF_ADDREF(mLiveconnect);
 
1222
                }
 
1223
                return mLiveconnect->QueryInterface(aIID, result);
 
1224
        }
 
1225
        return NS_ERROR_SERVICE_NOT_FOUND;
 
1226
}
 
1227
 
 
1228
NS_METHOD
 
1229
CPluginManager::ReleaseService(const nsCID& aClass, nsISupports* service,
 
1230
                   nsIShutdownListener* shutdownListener)
 
1231
{
 
1232
        NS_RELEASE(service);
 
1233
        return NS_OK;
 
1234
}
 
1235
 
 
1236
NS_METHOD_(void*)
 
1237
CPluginManager::Alloc(PRUint32 size)
 
1238
{
 
1239
        return ::NPN_MemAlloc(size);
 
1240
}
 
1241
 
 
1242
NS_METHOD_(void*)
 
1243
CPluginManager::Realloc(void* ptr, PRUint32 size)
 
1244
{
 
1245
        if (ptr != NULL) {
 
1246
                void* new_ptr = Alloc(size);
 
1247
                if (new_ptr != NULL) {
 
1248
                        ::memcpy(new_ptr, ptr, size);
 
1249
                        Free(ptr);
 
1250
                }
 
1251
                ptr = new_ptr;
 
1252
        }
 
1253
        return ptr;
 
1254
}
 
1255
 
 
1256
NS_METHOD
 
1257
CPluginManager::Free(void* ptr)
 
1258
{
 
1259
        if (ptr != NULL) {
 
1260
                ::NPN_MemFree(ptr);
 
1261
                return NS_OK;
 
1262
        }
 
1263
        return NS_ERROR_NULL_POINTER;
 
1264
}
 
1265
 
 
1266
NS_METHOD
 
1267
CPluginManager::HeapMinimize()
 
1268
{
 
1269
#ifdef XP_MAC
 
1270
        ::NPN_MemFlush(1024);
 
1271
#endif
 
1272
        return NS_OK;
 
1273
}
 
1274
 
 
1275
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1276
// UserAgent:
 
1277
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1278
 
 
1279
NS_METHOD
 
1280
CPluginManager::UserAgent(const char* *result)
 
1281
{
 
1282
    *result = NPN_UserAgent(NULL);
 
1283
    return NS_OK;
 
1284
}
 
1285
 
 
1286
 
 
1287
int varMap[] = {
 
1288
    (int)NPNVxDisplay,                  // nsPluginManagerVariable_XDisplay = 1,
 
1289
    (int)NPNVxtAppContext,              // nsPluginManagerVariable_XtAppContext,
 
1290
    (int)NPNVnetscapeWindow,            // nsPluginManagerVariable_NetscapeWindow,
 
1291
    (int)NPPVpluginWindowBool,          // nsPluginInstancePeerVariable_WindowBool,
 
1292
    (int)NPPVpluginTransparentBool,     // nsPluginInstancePeerVariable_TransparentBool,
 
1293
    (int)NPPVjavaClass,                 // nsPluginInstancePeerVariable_JavaClass,
 
1294
    (int)NPPVpluginWindowSize,          // nsPluginInstancePeerVariable_WindowSize,
 
1295
    (int)NPPVpluginTimerInterval,       // nsPluginInstancePeerVariable_TimerInterval
 
1296
};
 
1297
 
 
1298
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1299
// GetValue:
 
1300
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1301
 
 
1302
NS_METHOD
 
1303
CPluginManager::GetValue(nsPluginManagerVariable variable, void *value)
 
1304
{
 
1305
#ifdef XP_UNIX
 
1306
    return fromNPError[NPN_GetValue(NULL, (NPNVariable)varMap[(int)variable], value)];
 
1307
#else
 
1308
    return fromNPError[NPERR_GENERIC_ERROR];
 
1309
#endif // XP_UNIX
 
1310
}
 
1311
 
 
1312
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1313
// SetValue:
 
1314
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1315
 
 
1316
NS_METHOD
 
1317
CPluginManager::SetValue(nsPluginManagerVariable variable, void *value) 
 
1318
{
 
1319
#ifdef XP_UNIX
 
1320
    return fromNPError[NPN_SetValue(NULL, (NPPVariable)varMap[(int)variable], value)];
 
1321
#else
 
1322
    return fromNPError[NPERR_GENERIC_ERROR];
 
1323
#endif // XP_UNIX
 
1324
}
 
1325
 
 
1326
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1327
// nsISupports functions
 
1328
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1329
 
 
1330
NS_IMPL_ADDREF(CPluginManager)
 
1331
NS_IMPL_RELEASE(CPluginManager)
 
1332
 
 
1333
NS_METHOD
 
1334
CPluginManager::QueryInterface(const nsIID& iid, void** ptr) 
 
1335
{                                                                        
 
1336
    if (NULL == ptr) {                                            
 
1337
        return NS_ERROR_NULL_POINTER;                                        
 
1338
    }                                                                      
 
1339
  
 
1340
    if (iid.Equals(NS_GET_IID(nsIServiceManager))) {                                                          
 
1341
        *ptr = (void*) (nsIServiceManager*)this;                                        
 
1342
        AddRef();                                                            
 
1343
        return NS_OK;                                                        
 
1344
    }
 
1345
    if (iid.Equals(NS_GET_IID(nsIAllocator))) {                                                          
 
1346
        *ptr = (void*) (nsIAllocator*)this;                                        
 
1347
        AddRef();                                                            
 
1348
        return NS_OK;                                                        
 
1349
    }
 
1350
    if (iid.Equals(NS_GET_IID(nsIPluginManager)) || iid.Equals(NS_GET_IID(nsISupports))) {
 
1351
        *ptr = (void*) ((nsIPluginManager*)this);                        
 
1352
        AddRef();                                                            
 
1353
        return NS_OK;                                                        
 
1354
    }                                                                      
 
1355
    return NS_NOINTERFACE;                                                 
 
1356
}
 
1357
 
 
1358
//////////////////////////////////////////////////////////////////////////////
 
1359
//
 
1360
// CPluginInstancePeer
 
1361
//
 
1362
 
 
1363
CPluginInstancePeer::CPluginInstancePeer(nsIPluginInstance* pluginInstance,
 
1364
                                         NPP npp,
 
1365
                                         nsMIMEType typeString, 
 
1366
                                         nsPluginMode type,
 
1367
                                         PRUint16 attr_cnt, 
 
1368
                                         const char** attr_list,
 
1369
                                         const char** val_list)
 
1370
    :   mInstance(pluginInstance), mWindow(NULL),
 
1371
                npp(npp), typeString(typeString), type(type), attribute_cnt(attr_cnt),
 
1372
                attribute_list(NULL), values_list(NULL)
 
1373
{
 
1374
    NS_IF_ADDREF(mInstance);
 
1375
 
 
1376
    attribute_list = (char**) NPN_MemAlloc(attr_cnt * sizeof(const char*));
 
1377
    values_list = (char**) NPN_MemAlloc(attr_cnt * sizeof(const char*));
 
1378
 
 
1379
    if (attribute_list != NULL && values_list != NULL) {
 
1380
        for (int i = 0; i < attribute_cnt; i++)   {
 
1381
            attribute_list[i] = (char*) NPN_MemAlloc(strlen(attr_list[i]) + 1);
 
1382
            if (attribute_list[i] != NULL)
 
1383
                strcpy(attribute_list[i], attr_list[i]);
 
1384
 
 
1385
            values_list[i] = (char*) NPN_MemAlloc(strlen(val_list[i]) + 1);
 
1386
            if (values_list[i] != NULL)
 
1387
                strcpy(values_list[i], val_list[i]);
 
1388
        }
 
1389
    }
 
1390
}
 
1391
 
 
1392
CPluginInstancePeer::~CPluginInstancePeer(void) 
 
1393
{
 
1394
    if (attribute_list != NULL && values_list != NULL) {
 
1395
        for (int i = 0; i < attribute_cnt; i++)   {
 
1396
            NPN_MemFree(attribute_list[i]);
 
1397
            NPN_MemFree(values_list[i]);
 
1398
        }
 
1399
 
 
1400
        NPN_MemFree(attribute_list);
 
1401
        NPN_MemFree(values_list);
 
1402
    }
 
1403
    
 
1404
    NS_IF_RELEASE(mInstance);
 
1405
}
 
1406
 
 
1407
 
 
1408
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1409
// GetValue:
 
1410
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1411
 
 
1412
NS_METHOD
 
1413
CPluginInstancePeer::GetValue(nsPluginInstancePeerVariable variable, void *value)
 
1414
{
 
1415
#ifdef XP_UNIX
 
1416
    return fromNPError[NPN_GetValue(NULL, (NPNVariable)varMap[(int)variable], value)];
 
1417
#else
 
1418
    return fromNPError[NPERR_GENERIC_ERROR];
 
1419
#endif // XP_UNIX
 
1420
}
 
1421
 
 
1422
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1423
// SetValue:
 
1424
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1425
 
 
1426
NS_METHOD
 
1427
CPluginInstancePeer::SetValue(nsPluginInstancePeerVariable variable, void *value) 
 
1428
{
 
1429
#ifdef XP_UNIX
 
1430
    return fromNPError[NPN_SetValue(NULL, (NPPVariable)varMap[(int)variable], value)];
 
1431
#else
 
1432
    return fromNPError[NPERR_GENERIC_ERROR];
 
1433
#endif // XP_UNIX
 
1434
}
 
1435
 
 
1436
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1437
// GetMIMEType:
 
1438
// Corresponds to NPP_New's MIMEType argument.
 
1439
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1440
 
 
1441
NS_METHOD
 
1442
CPluginInstancePeer::GetMIMEType(nsMIMEType *result) 
 
1443
{
 
1444
    *result = typeString;
 
1445
    return NS_OK;
 
1446
}
 
1447
 
 
1448
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1449
// GetMode:
 
1450
// Corresponds to NPP_New's mode argument.
 
1451
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1452
 
 
1453
NS_METHOD
 
1454
CPluginInstancePeer::GetMode(nsPluginMode *result)
 
1455
{
 
1456
    *result = type;
 
1457
    return NS_OK;
 
1458
}
 
1459
 
 
1460
 
 
1461
// Get a ptr to the paired list of attribute names and values,
 
1462
// returns the length of the array.
 
1463
//
 
1464
// Each name or value is a null-terminated string.
 
1465
NS_METHOD
 
1466
CPluginInstancePeer::GetAttributes(PRUint16& n, const char* const*& names, const char* const*& values)  
 
1467
{
 
1468
    n = attribute_cnt;
 
1469
    names = attribute_list;
 
1470
    values = values_list;
 
1471
 
 
1472
    return NS_OK;
 
1473
}
 
1474
 
 
1475
#if defined(XP_MAC)
 
1476
 
 
1477
inline unsigned char toupper(unsigned char c)
 
1478
{
 
1479
    return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;
 
1480
}
 
1481
 
 
1482
static int strcasecmp(const char * str1, const char * str2)
 
1483
{
 
1484
#if __POWERPC__
 
1485
        
 
1486
    const       unsigned char * p1 = (unsigned char *) str1 - 1;
 
1487
    const       unsigned char * p2 = (unsigned char *) str2 - 1;
 
1488
    unsigned long               c1, c2;
 
1489
                
 
1490
    while (toupper(c1 = *++p1) == toupper(c2 = *++p2))
 
1491
        if (!c1)
 
1492
            return(0);
 
1493
 
 
1494
#else
 
1495
        
 
1496
    const       unsigned char * p1 = (unsigned char *) str1;
 
1497
    const       unsigned char * p2 = (unsigned char *) str2;
 
1498
    unsigned char               c1, c2;
 
1499
        
 
1500
    while (toupper(c1 = *p1++) == toupper(c2 = *p2++))
 
1501
        if (!c1)
 
1502
            return(0);
 
1503
 
 
1504
#endif
 
1505
        
 
1506
    return(toupper(c1) - toupper(c2));
 
1507
}
 
1508
 
 
1509
#endif /* XP_MAC */
 
1510
 
 
1511
// Get the value for the named attribute.  Returns null
 
1512
// if the attribute was not set.
 
1513
NS_METHOD
 
1514
CPluginInstancePeer::GetAttribute(const char* name, const char* *result) 
 
1515
{
 
1516
    for (int i=0; i < attribute_cnt; i++)  {
 
1517
#if defined(XP_UNIX) || defined(XP_MAC)
 
1518
        if (strcasecmp(name, attribute_list[i]) == 0)
 
1519
#else
 
1520
            if (stricmp(name, attribute_list[i]) == 0) 
 
1521
#endif
 
1522
            {
 
1523
                *result = values_list[i];
 
1524
                return NS_OK;
 
1525
            }
 
1526
    }
 
1527
 
 
1528
    return NS_ERROR_FAILURE;
 
1529
}
 
1530
 
 
1531
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1532
// NewStream:
 
1533
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1534
NS_METHOD
 
1535
CPluginInstancePeer::NewStream(nsMIMEType type, const char* target, 
 
1536
                               nsIOutputStream* *result)
 
1537
{
 
1538
    assert( npp != NULL );
 
1539
    
 
1540
    // Create a new NPStream.
 
1541
    NPStream* stream = NULL;
 
1542
    NPError error = NPN_NewStream(npp, (NPMIMEType)type, target, &stream);
 
1543
    if (error != NPERR_NO_ERROR)
 
1544
        return fromNPError[error];
 
1545
    
 
1546
    // Create a new Plugin Manager Stream.
 
1547
    // XXX - Do we have to Release() the manager stream before doing this?
 
1548
    // XXX - See the BAM doc for more info.
 
1549
    CPluginManagerStream* mstream = new CPluginManagerStream(npp, stream);
 
1550
    if (mstream == NULL)
 
1551
        return NS_ERROR_OUT_OF_MEMORY;
 
1552
    mstream->AddRef();
 
1553
    *result = (nsIOutputStream* )mstream;
 
1554
 
 
1555
    return NS_OK;
 
1556
}
 
1557
 
 
1558
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1559
// ShowStatus:
 
1560
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1561
 
 
1562
NS_METHOD
 
1563
CPluginInstancePeer::ShowStatus(const char* message)
 
1564
{
 
1565
    assert( message != NULL );
 
1566
 
 
1567
    NPN_Status(npp, message);
 
1568
        return NS_OK;
 
1569
}
 
1570
 
 
1571
NS_METHOD
 
1572
CPluginInstancePeer::SetWindowSize(PRUint32 width, PRUint32 height)
 
1573
{
 
1574
    NPError err;
 
1575
    NPSize size;
 
1576
    size.width = width;
 
1577
    size.height = height;
 
1578
    err = NPN_SetValue(npp, NPPVpluginWindowSize, &size);
 
1579
    return fromNPError[err];
 
1580
}
 
1581
 
 
1582
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1583
// nsISupports functions
 
1584
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1585
 
 
1586
NS_IMPL_ADDREF(CPluginInstancePeer)
 
1587
NS_IMPL_RELEASE(CPluginInstancePeer)
 
1588
 
 
1589
NS_METHOD
 
1590
CPluginInstancePeer::QueryInterface(const nsIID& iid, void** ptr) 
 
1591
{                                                                        
 
1592
    if (NULL == ptr) {                                            
 
1593
        return NS_ERROR_NULL_POINTER;                                        
 
1594
    }                                                                      
 
1595
  
 
1596
    if (iid.Equals(NS_GET_IID(nsIPluginInstancePeer))) {
 
1597
        *ptr = (void*) this;                                        
 
1598
        AddRef();                                                            
 
1599
        return NS_OK;                                                        
 
1600
    }                                                                      
 
1601
    if (iid.Equals(NS_GET_IID(nsIPluginTagInfo)) || iid.Equals(NS_GET_IID(nsISupports))) {                                      
 
1602
        *ptr = (void*) ((nsIPluginTagInfo*)this);                        
 
1603
        AddRef();                                                            
 
1604
        return NS_OK;                                                        
 
1605
    }                                                                      
 
1606
    return NS_NOINTERFACE;                                                 
 
1607
}
 
1608
 
 
1609
//////////////////////////////////////////////////////////////////////////////
 
1610
//
 
1611
// CPluginManagerStream
 
1612
//
 
1613
 
 
1614
CPluginManagerStream::CPluginManagerStream(NPP npp, NPStream* pstr)
 
1615
    : npp(npp), pstream(pstr)
 
1616
{
 
1617
}
 
1618
 
 
1619
CPluginManagerStream::~CPluginManagerStream(void)
 
1620
{
 
1621
    //pstream = NULL;
 
1622
    NPN_DestroyStream(npp, pstream, NPRES_DONE);
 
1623
}
 
1624
 
 
1625
 
 
1626
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1627
// Write:
 
1628
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1629
 
 
1630
NS_METHOD
 
1631
CPluginManagerStream::Write(const char* buffer, PRUint32 len, PRUint32 *aWriteCount)
 
1632
{
 
1633
    assert( npp != NULL );
 
1634
    assert( pstream != NULL );
 
1635
 
 
1636
    *aWriteCount = NPN_Write(npp, pstream, len, (void* )buffer);
 
1637
    return *aWriteCount >= 0 ? NS_OK : NS_ERROR_FAILURE;
 
1638
}
 
1639
 
 
1640
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1641
// GetURL:
 
1642
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1643
 
 
1644
NS_METHOD
 
1645
CPluginManagerStream::GetURL(const char* *result)
 
1646
{
 
1647
    assert( pstream != NULL );
 
1648
 
 
1649
    *result = pstream->url;
 
1650
        return NS_OK;
 
1651
}
 
1652
 
 
1653
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1654
// GetEnd:
 
1655
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1656
 
 
1657
NS_METHOD
 
1658
CPluginManagerStream::GetEnd(PRUint32 *result)
 
1659
{
 
1660
    assert( pstream != NULL );
 
1661
 
 
1662
    *result = pstream->end;
 
1663
        return NS_OK;
 
1664
}
 
1665
 
 
1666
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1667
// GetLastModified:
 
1668
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1669
 
 
1670
NS_METHOD
 
1671
CPluginManagerStream::GetLastModified(PRUint32 *result)
 
1672
{
 
1673
    assert( pstream != NULL );
 
1674
 
 
1675
    *result = pstream->lastmodified;
 
1676
        return NS_OK;
 
1677
}
 
1678
 
 
1679
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1680
// GetNotifyData:
 
1681
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1682
 
 
1683
NS_METHOD
 
1684
CPluginManagerStream::GetNotifyData(void* *result)
 
1685
{
 
1686
    assert( pstream != NULL );
 
1687
 
 
1688
    *result = pstream->notifyData;
 
1689
        return NS_OK;
 
1690
}
 
1691
 
 
1692
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1693
// GetNotifyData:
 
1694
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1695
 
 
1696
NS_METHOD
 
1697
CPluginManagerStream::Close(void)
 
1698
{
 
1699
    assert( pstream != NULL );
 
1700
 
 
1701
    return NS_OK;
 
1702
}
 
1703
 
 
1704
 
 
1705
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1706
// nsISupports functions
 
1707
//+++++++++++++++++++++++++++++++++++++++++++++++++
 
1708
 
 
1709
NS_IMPL_ISUPPORTS1(CPluginManagerStream, nsIOutputStream)
 
1710
 
 
1711
//////////////////////////////////////////////////////////////////////////////
 
1712
 
 
1713
NS_IMPL_ISUPPORTS1(CPluginStreamInfo, nsIPluginStreamInfo)
 
1714
 
 
1715
CPluginInputStream::CPluginInputStream(nsIPluginStreamListener* listener)
 
1716
    : mListener(listener), mStreamType(nsPluginStreamType_Normal),
 
1717
      mNPP(NULL), mStream(NULL),
 
1718
      mBuffer(NULL), mBufferLength(0), mAmountRead(0),
 
1719
      mStreamInfo(NULL)
 
1720
{
 
1721
    if (mListener != NULL) {
 
1722
        mListener->AddRef();
 
1723
        mListener->GetStreamType(&mStreamType);
 
1724
    }
 
1725
}
 
1726
 
 
1727
CPluginInputStream::~CPluginInputStream(void)
 
1728
{
 
1729
        NS_IF_RELEASE(mListener);
 
1730
 
 
1731
    delete mBuffer;
 
1732
    
 
1733
    NS_IF_RELEASE(mStreamInfo);
 
1734
}
 
1735
 
 
1736
NS_IMPL_ISUPPORTS1(CPluginInputStream, nsIPluginInputStream)
 
1737
 
 
1738
NS_METHOD
 
1739
CPluginInputStream::Close(void)
 
1740
{
 
1741
    if (mNPP == NULL || mStream == NULL)
 
1742
        return NS_ERROR_FAILURE;
 
1743
    NPError err = NPN_DestroyStream(mNPP, mStream, NPRES_USER_BREAK);
 
1744
    return fromNPError[err];
 
1745
}
 
1746
 
 
1747
NS_METHOD
 
1748
CPluginInputStream::GetLength(PRUint32 *aLength)
 
1749
{
 
1750
    *aLength = mStream->end;
 
1751
    return NS_OK;
 
1752
}
 
1753
 
 
1754
NS_METHOD
 
1755
CPluginInputStream::Read(char* aBuf, PRUint32 aCount, PRUint32 *aReadCount)
 
1756
{
 
1757
    PRUint32 cnt = PR_MIN(aCount, mBufferLength);
 
1758
    memcpy(aBuf, mBuffer + mAmountRead, cnt);
 
1759
    *aReadCount = cnt;
 
1760
    mAmountRead += cnt;
 
1761
    mBufferLength -= cnt;
 
1762
    return NS_OK;
 
1763
}
 
1764
 
 
1765
NS_METHOD
 
1766
CPluginInputStream::GetLastModified(PRUint32 *result)
 
1767
{
 
1768
    *result = mStream->lastmodified;
 
1769
    return NS_OK;
 
1770
}
 
1771
 
 
1772
NS_METHOD
 
1773
CPluginInputStream::RequestRead(nsByteRange* rangeList)
 
1774
{
 
1775
    NPError err = NPN_RequestRead(mStream, (NPByteRange*)rangeList);
 
1776
    return fromNPError[err];
 
1777
}
 
1778
 
 
1779
 
 
1780
//////////////////////////////////////////////////////////////////////////////
 
1781
 
 
1782
#if defined(__cplusplus)
 
1783
} /* extern "C" */
 
1784
#endif
 
1785