File: ns4xPluginStream.cpp

package info (click to toggle)
mozilla 19981008-0.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 110,368 kB
  • ctags: 148,791
  • sloc: ansic: 674,811; cpp: 548,132; makefile: 20,384; java: 13,005; sh: 3,638; perl: 2,433; yacc: 1,136; asm: 657; lisp: 148; objc: 51; pascal: 41; awk: 20; sed: 18; xml: 13; csh: 11
file content (233 lines) | stat: -rw-r--r-- 6,169 bytes parent folder | download
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.0 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 *
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */


#include "ns4xPluginStream.h"
#include "prlog.h"             // for PR_ASSERT

////////////////////////////////////////////////////////////////////////


ns4xPluginStream::ns4xPluginStream(void)
    : fStreamType(nsPluginStreamType_Normal), fSeekable(PR_FALSE), fPosition(0)
{
    NS_INIT_REFCNT();

    fPeer = nsnull;
    fInstance = nsnull;

    // Initialize the 4.x interface structure
    memset(&fNPStream, 0, sizeof(fNPStream));
}


ns4xPluginStream::~ns4xPluginStream(void)
{
    const NPPluginFuncs *callbacks;
    NPP                 npp;
    nsPluginReason      reason;

    fInstance->GetCallbacks(&callbacks);
    fInstance->GetNPP(&npp);
    fPeer->GetReason(&reason);

    if (callbacks->destroystream != NULL)
    {
        CallNPP_DestroyStreamProc(callbacks->destroystream,
                                  npp,
                                  &fNPStream,
                                  reason);
    }

    NS_IF_RELEASE(fPeer);
    NS_IF_RELEASE(fInstance);
}


////////////////////////////////////////////////////////////////////////

NS_IMPL_ADDREF(ns4xPluginStream);
NS_IMPL_RELEASE(ns4xPluginStream);

static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIPluginStreamIID, NS_IPLUGINSTREAM_IID);
static NS_DEFINE_IID(kISeekablePluginStreamPeerIID, NS_ISEEKABLEPLUGINSTREAMPEER_IID);

NS_IMETHODIMP ns4xPluginStream::QueryInterface(const nsIID& iid, void** instance)
{
    if (instance == NULL)
        return NS_ERROR_NULL_POINTER;

    if (iid.Equals(kIPluginStreamIID))
    {
        *instance = (void *)(nsIPluginStream *)this;
        AddRef();
        return NS_OK;
    }
    else if (iid.Equals(kISupportsIID))
    {
        *instance = (void *)(nsISupports *)this;
        AddRef();
        return NS_OK;
    }

    return NS_NOINTERFACE;
}


////////////////////////////////////////////////////////////////////////


NS_IMETHODIMP ns4xPluginStream::Initialize(ns4xPluginInstance* instance,
                                          nsIPluginStreamPeer* peer)
{
    fInstance = instance;
    fPeer = peer; 

    NS_ASSERTION(fInstance != NULL, "null instance");

    NS_ADDREF(fInstance);

    NS_ASSERTION(fPeer != NULL, "null peer");

    NS_ADDREF(fPeer);

    const char *url;
    PRUint32  length, modified;

    fPeer->GetURL(&url);
    fPeer->GetEnd(&length);
    fPeer->GetLastModified(&modified);

    fNPStream.ndata        = (void*) fPeer;
    fNPStream.url          = url;
    fNPStream.end          = length;
    fNPStream.lastmodified = modified;

    // Are we seekable?

    nsISupports* seekablePeer;

    if (fPeer->QueryInterface(kISeekablePluginStreamPeerIID,
                              (void**) &seekablePeer) == NS_OK)
    {
        fSeekable = TRUE;
        NS_RELEASE(seekablePeer);
    }

    const NPPluginFuncs *callbacks;
    NPP                 npp;
    nsMIMEType          mimetype;

    fInstance->GetCallbacks(&callbacks);
    fInstance->GetNPP(&npp);
    fPeer->GetMIMEType(&mimetype);

    if (callbacks->newstream == NULL)
        return NS_ERROR_FAILURE;

    PRUint16 streamType = (PRUint16) fStreamType;

    nsresult error
        = (nsresult)CallNPP_NewStreamProc(callbacks->newstream,
                                          npp,
                                          (char *)mimetype,
                                          &fNPStream,
                                          fSeekable,
                                          &streamType);

    fStreamType = (nsPluginStreamType) streamType;

    return error;
}

NS_IMETHODIMP ns4xPluginStream::Write(const char* buffer, PRInt32 offset, PRInt32 len, PRInt32 *aWriteCount)
{
    const NPPluginFuncs *callbacks;
    NPP                 npp;
    PRInt32             remaining = len;

    fInstance->GetCallbacks(&callbacks);
    fInstance->GetNPP(&npp);

    if (callbacks->write == NULL)
        return NS_OK;

    while (remaining > 0)
    {
      PRInt32 numtowrite;

      if (callbacks->writeready != NULL)
      {
        numtowrite = CallNPP_WriteReadyProc(callbacks->writeready,
                                            npp,
                                            &fNPStream);

        if (numtowrite > remaining)
          numtowrite = remaining;
      }
      else
        numtowrite = len;

      *aWriteCount = CallNPP_WriteProc(callbacks->write,
                                       npp,
                                       &fNPStream, 
                                       fPosition,
                                       numtowrite,
                                       (void *)buffer);

      remaining -= numtowrite;
    }

    fPosition += len;

    return NS_OK;   //XXX this seems bad...
}

NS_IMETHODIMP ns4xPluginStream::GetStreamType(nsPluginStreamType *result)
{
    *result = fStreamType;
    return NS_OK;
}

NS_IMETHODIMP ns4xPluginStream::AsFile(const char* filename)
{
    const NPPluginFuncs *callbacks;
    NPP                 npp;

    fInstance->GetCallbacks(&callbacks);
    fInstance->GetNPP(&npp);

    if (callbacks->asfile == NULL)
        return NS_OK;

    CallNPP_StreamAsFileProc(callbacks->asfile,
                             npp,
                             &fNPStream,
                             filename);

    return NS_OK;
}

NS_IMETHODIMP ns4xPluginStream :: Close(void)
{
   return NS_OK;
}

////////////////////////////////////////////////////////////////////////