|
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/. */ |
|
5 |
|
6 |
|
7 #include "BrowserStreamParent.h" |
|
8 #include "PluginInstanceParent.h" |
|
9 #include "nsNPAPIPlugin.h" |
|
10 |
|
11 #include "mozilla/unused.h" |
|
12 |
|
13 // How much data are we willing to send across the wire |
|
14 // in one chunk? |
|
15 static const int32_t kSendDataChunk = 0x4000; |
|
16 |
|
17 namespace mozilla { |
|
18 namespace plugins { |
|
19 |
|
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 } |
|
28 |
|
29 BrowserStreamParent::~BrowserStreamParent() |
|
30 { |
|
31 } |
|
32 |
|
33 bool |
|
34 BrowserStreamParent::AnswerNPN_RequestRead(const IPCByteRanges& ranges, |
|
35 NPError* result) |
|
36 { |
|
37 PLUGIN_LOG_DEBUG_FUNCTION; |
|
38 |
|
39 switch (mState) { |
|
40 case ALIVE: |
|
41 break; |
|
42 |
|
43 case DYING: |
|
44 *result = NPERR_GENERIC_ERROR; |
|
45 return true; |
|
46 |
|
47 default: |
|
48 NS_ERROR("Unexpected state"); |
|
49 return false; |
|
50 } |
|
51 |
|
52 if (!mStream) |
|
53 return false; |
|
54 |
|
55 if (ranges.size() > INT32_MAX) |
|
56 return false; |
|
57 |
|
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; |
|
65 |
|
66 *result = mNPP->mNPNIface->requestread(mStream, rp); |
|
67 return true; |
|
68 } |
|
69 |
|
70 bool |
|
71 BrowserStreamParent::RecvNPN_DestroyStream(const NPReason& reason) |
|
72 { |
|
73 switch (mState) { |
|
74 case ALIVE: |
|
75 break; |
|
76 |
|
77 case DYING: |
|
78 return true; |
|
79 |
|
80 default: |
|
81 NS_ERROR("Unexpected state"); |
|
82 return false; |
|
83 }; |
|
84 |
|
85 mNPP->mNPNIface->destroystream(mNPP->mNPP, mStream, reason); |
|
86 return true; |
|
87 } |
|
88 |
|
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 } |
|
96 |
|
97 bool |
|
98 BrowserStreamParent::RecvStreamDestroyed() |
|
99 { |
|
100 if (DYING != mState) { |
|
101 NS_ERROR("Unexpected state"); |
|
102 return false; |
|
103 } |
|
104 |
|
105 mStreamPeer = nullptr; |
|
106 |
|
107 mState = DELETING; |
|
108 return Send__delete__(this); |
|
109 } |
|
110 |
|
111 int32_t |
|
112 BrowserStreamParent::WriteReady() |
|
113 { |
|
114 return kSendDataChunk; |
|
115 } |
|
116 |
|
117 int32_t |
|
118 BrowserStreamParent::Write(int32_t offset, |
|
119 int32_t len, |
|
120 void* buffer) |
|
121 { |
|
122 PLUGIN_LOG_DEBUG_FUNCTION; |
|
123 |
|
124 NS_ASSERTION(ALIVE == mState, "Sending data after NPP_DestroyStream?"); |
|
125 NS_ASSERTION(len > 0, "Non-positive length to NPP_Write"); |
|
126 |
|
127 if (len > kSendDataChunk) |
|
128 len = kSendDataChunk; |
|
129 |
|
130 return SendWrite(offset, |
|
131 nsCString(static_cast<char*>(buffer), len), |
|
132 mStream->end) ? |
|
133 len : -1; |
|
134 } |
|
135 |
|
136 void |
|
137 BrowserStreamParent::StreamAsFile(const char* fname) |
|
138 { |
|
139 PLUGIN_LOG_DEBUG_FUNCTION; |
|
140 |
|
141 NS_ASSERTION(ALIVE == mState, |
|
142 "Calling streamasfile after NPP_DestroyStream?"); |
|
143 |
|
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 } |
|
151 |
|
152 unused << SendNPP_StreamAsFile(nsCString(fname)); |
|
153 return; |
|
154 } |
|
155 |
|
156 } // namespace plugins |
|
157 } // namespace mozilla |