Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "BrowserStreamParent.h"
8 #include "PluginInstanceParent.h"
9 #include "nsNPAPIPlugin.h"
11 #include "mozilla/unused.h"
13 // How much data are we willing to send across the wire
14 // in one chunk?
15 static const int32_t kSendDataChunk = 0x4000;
17 namespace mozilla {
18 namespace plugins {
20 BrowserStreamParent::BrowserStreamParent(PluginInstanceParent* npp,
21 NPStream* stream)
22 : mNPP(npp)
23 , mStream(stream)
24 , mState(ALIVE)
25 {
26 mStream->pdata = static_cast<AStream*>(this);
27 }
29 BrowserStreamParent::~BrowserStreamParent()
30 {
31 }
33 bool
34 BrowserStreamParent::AnswerNPN_RequestRead(const IPCByteRanges& ranges,
35 NPError* result)
36 {
37 PLUGIN_LOG_DEBUG_FUNCTION;
39 switch (mState) {
40 case ALIVE:
41 break;
43 case DYING:
44 *result = NPERR_GENERIC_ERROR;
45 return true;
47 default:
48 NS_ERROR("Unexpected state");
49 return false;
50 }
52 if (!mStream)
53 return false;
55 if (ranges.size() > INT32_MAX)
56 return false;
58 nsAutoArrayPtr<NPByteRange> rp(new NPByteRange[ranges.size()]);
59 for (uint32_t i = 0; i < ranges.size(); ++i) {
60 rp[i].offset = ranges[i].offset;
61 rp[i].length = ranges[i].length;
62 rp[i].next = &rp[i + 1];
63 }
64 rp[ranges.size() - 1].next = nullptr;
66 *result = mNPP->mNPNIface->requestread(mStream, rp);
67 return true;
68 }
70 bool
71 BrowserStreamParent::RecvNPN_DestroyStream(const NPReason& reason)
72 {
73 switch (mState) {
74 case ALIVE:
75 break;
77 case DYING:
78 return true;
80 default:
81 NS_ERROR("Unexpected state");
82 return false;
83 };
85 mNPP->mNPNIface->destroystream(mNPP->mNPP, mStream, reason);
86 return true;
87 }
89 void
90 BrowserStreamParent::NPP_DestroyStream(NPReason reason)
91 {
92 NS_ASSERTION(ALIVE == mState, "NPP_DestroyStream called twice?");
93 mState = DYING;
94 unused << SendNPP_DestroyStream(reason);
95 }
97 bool
98 BrowserStreamParent::RecvStreamDestroyed()
99 {
100 if (DYING != mState) {
101 NS_ERROR("Unexpected state");
102 return false;
103 }
105 mStreamPeer = nullptr;
107 mState = DELETING;
108 return Send__delete__(this);
109 }
111 int32_t
112 BrowserStreamParent::WriteReady()
113 {
114 return kSendDataChunk;
115 }
117 int32_t
118 BrowserStreamParent::Write(int32_t offset,
119 int32_t len,
120 void* buffer)
121 {
122 PLUGIN_LOG_DEBUG_FUNCTION;
124 NS_ASSERTION(ALIVE == mState, "Sending data after NPP_DestroyStream?");
125 NS_ASSERTION(len > 0, "Non-positive length to NPP_Write");
127 if (len > kSendDataChunk)
128 len = kSendDataChunk;
130 return SendWrite(offset,
131 nsCString(static_cast<char*>(buffer), len),
132 mStream->end) ?
133 len : -1;
134 }
136 void
137 BrowserStreamParent::StreamAsFile(const char* fname)
138 {
139 PLUGIN_LOG_DEBUG_FUNCTION;
141 NS_ASSERTION(ALIVE == mState,
142 "Calling streamasfile after NPP_DestroyStream?");
144 // Make sure our stream survives until the plugin process tells us we've
145 // been destroyed (until RecvStreamDestroyed() is called). Since we retain
146 // mStreamPeer at most once, we won't get in trouble if StreamAsFile() is
147 // called more than once.
148 if (!mStreamPeer) {
149 nsNPAPIPlugin::RetainStream(mStream, getter_AddRefs(mStreamPeer));
150 }
152 unused << SendNPP_StreamAsFile(nsCString(fname));
153 return;
154 }
156 } // namespace plugins
157 } // namespace mozilla