Fix trace_tests.py when showing output. (r=jorendorff)
[mozilla-central.git] / dom / plugins / BrowserStreamParent.cpp
blob38ff60b677b0f42bf2fc1aa425f0bab4d3a775c7
1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
3 #include "BrowserStreamParent.h"
4 #include "PluginInstanceParent.h"
6 #include "mozilla/unused.h"
8 // How much data are we willing to send across the wire
9 // in one chunk?
10 static const int32_t kSendDataChunk = 0x4000;
12 namespace mozilla {
13 namespace plugins {
15 BrowserStreamParent::BrowserStreamParent(PluginInstanceParent* npp,
16 NPStream* stream)
17 : mNPP(npp)
18 , mStream(stream)
19 , mState(ALIVE)
21 mStream->pdata = static_cast<AStream*>(this);
24 BrowserStreamParent::~BrowserStreamParent()
28 bool
29 BrowserStreamParent::AnswerNPN_RequestRead(const IPCByteRanges& ranges,
30 NPError* result)
32 PLUGIN_LOG_DEBUG_FUNCTION;
34 switch (mState) {
35 case ALIVE:
36 break;
38 case DYING:
39 *result = NPERR_GENERIC_ERROR;
40 return true;
42 default:
43 NS_ERROR("Unexpected state");
44 return false;
47 if (!mStream)
48 return false;
50 if (ranges.size() > PR_INT32_MAX)
51 return false;
53 nsAutoArrayPtr<NPByteRange> rp(new NPByteRange[ranges.size()]);
54 for (PRUint32 i = 0; i < ranges.size(); ++i) {
55 rp[i].offset = ranges[i].offset;
56 rp[i].length = ranges[i].length;
57 rp[i].next = &rp[i + 1];
59 rp[ranges.size() - 1].next = NULL;
61 *result = mNPP->mNPNIface->requestread(mStream, rp);
62 return true;
65 bool
66 BrowserStreamParent::RecvNPN_DestroyStream(const NPReason& reason)
68 switch (mState) {
69 case ALIVE:
70 break;
72 case DYING:
73 return true;
75 default:
76 NS_ERROR("Unexpected state");
77 return false;
80 mNPP->mNPNIface->destroystream(mNPP->mNPP, mStream, reason);
81 return true;
84 void
85 BrowserStreamParent::NPP_DestroyStream(NPReason reason)
87 NS_ASSERTION(ALIVE == mState, "NPP_DestroyStream called twice?");
88 mState = DYING;
89 unused << SendNPP_DestroyStream(reason);
92 bool
93 BrowserStreamParent::RecvStreamDestroyed()
95 if (DYING != mState) {
96 NS_ERROR("Unexpected state");
97 return false;
100 mState = DELETING;
101 return Send__delete__(this);
104 int32_t
105 BrowserStreamParent::WriteReady()
107 return kSendDataChunk;
110 int32_t
111 BrowserStreamParent::Write(int32_t offset,
112 int32_t len,
113 void* buffer)
115 PLUGIN_LOG_DEBUG_FUNCTION;
117 NS_ASSERTION(ALIVE == mState, "Sending data after NPP_DestroyStream?");
118 NS_ASSERTION(len > 0, "Non-positive length to NPP_Write");
120 if (len > kSendDataChunk)
121 len = kSendDataChunk;
123 return SendWrite(offset,
124 nsCString(static_cast<char*>(buffer), len),
125 mStream->end) ?
126 len : -1;
129 void
130 BrowserStreamParent::StreamAsFile(const char* fname)
132 PLUGIN_LOG_DEBUG_FUNCTION;
134 NS_ASSERTION(ALIVE == mState,
135 "Calling streamasfile after NPP_DestroyStream?");
137 unused << CallNPP_StreamAsFile(nsCString(fname));
138 return;
141 } // namespace plugins
142 } // namespace mozilla