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.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsStreamTransportService.h" |
michael@0 | 6 | #include "nsXPCOMCIDInternal.h" |
michael@0 | 7 | #include "nsNetSegmentUtils.h" |
michael@0 | 8 | #include "nsTransportUtils.h" |
michael@0 | 9 | #include "nsStreamUtils.h" |
michael@0 | 10 | #include "nsError.h" |
michael@0 | 11 | #include "nsNetCID.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsIAsyncInputStream.h" |
michael@0 | 14 | #include "nsIAsyncOutputStream.h" |
michael@0 | 15 | #include "nsISeekableStream.h" |
michael@0 | 16 | #include "nsIPipe.h" |
michael@0 | 17 | #include "nsITransport.h" |
michael@0 | 18 | #include "nsIObserverService.h" |
michael@0 | 19 | #include "nsIThreadPool.h" |
michael@0 | 20 | #include "mozilla/Services.h" |
michael@0 | 21 | |
michael@0 | 22 | //----------------------------------------------------------------------------- |
michael@0 | 23 | // nsInputStreamTransport |
michael@0 | 24 | // |
michael@0 | 25 | // Implements nsIInputStream as a wrapper around the real input stream. This |
michael@0 | 26 | // allows the transport to support seeking, range-limiting, progress reporting, |
michael@0 | 27 | // and close-when-done semantics while utilizing NS_AsyncCopy. |
michael@0 | 28 | //----------------------------------------------------------------------------- |
michael@0 | 29 | |
michael@0 | 30 | class nsInputStreamTransport : public nsITransport |
michael@0 | 31 | , public nsIInputStream |
michael@0 | 32 | { |
michael@0 | 33 | public: |
michael@0 | 34 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 35 | NS_DECL_NSITRANSPORT |
michael@0 | 36 | NS_DECL_NSIINPUTSTREAM |
michael@0 | 37 | |
michael@0 | 38 | nsInputStreamTransport(nsIInputStream *source, |
michael@0 | 39 | uint64_t offset, |
michael@0 | 40 | uint64_t limit, |
michael@0 | 41 | bool closeWhenDone) |
michael@0 | 42 | : mSource(source) |
michael@0 | 43 | , mOffset(offset) |
michael@0 | 44 | , mLimit(limit) |
michael@0 | 45 | , mCloseWhenDone(closeWhenDone) |
michael@0 | 46 | , mFirstTime(true) |
michael@0 | 47 | , mInProgress(false) |
michael@0 | 48 | { |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | virtual ~nsInputStreamTransport() |
michael@0 | 52 | { |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | private: |
michael@0 | 56 | nsCOMPtr<nsIAsyncInputStream> mPipeIn; |
michael@0 | 57 | |
michael@0 | 58 | // while the copy is active, these members may only be accessed from the |
michael@0 | 59 | // nsIInputStream implementation. |
michael@0 | 60 | nsCOMPtr<nsITransportEventSink> mEventSink; |
michael@0 | 61 | nsCOMPtr<nsIInputStream> mSource; |
michael@0 | 62 | uint64_t mOffset; |
michael@0 | 63 | uint64_t mLimit; |
michael@0 | 64 | bool mCloseWhenDone; |
michael@0 | 65 | bool mFirstTime; |
michael@0 | 66 | |
michael@0 | 67 | // this variable serves as a lock to prevent the state of the transport |
michael@0 | 68 | // from being modified once the copy is in progress. |
michael@0 | 69 | bool mInProgress; |
michael@0 | 70 | }; |
michael@0 | 71 | |
michael@0 | 72 | NS_IMPL_ISUPPORTS(nsInputStreamTransport, |
michael@0 | 73 | nsITransport, |
michael@0 | 74 | nsIInputStream) |
michael@0 | 75 | |
michael@0 | 76 | /** nsITransport **/ |
michael@0 | 77 | |
michael@0 | 78 | NS_IMETHODIMP |
michael@0 | 79 | nsInputStreamTransport::OpenInputStream(uint32_t flags, |
michael@0 | 80 | uint32_t segsize, |
michael@0 | 81 | uint32_t segcount, |
michael@0 | 82 | nsIInputStream **result) |
michael@0 | 83 | { |
michael@0 | 84 | NS_ENSURE_TRUE(!mInProgress, NS_ERROR_IN_PROGRESS); |
michael@0 | 85 | |
michael@0 | 86 | nsresult rv; |
michael@0 | 87 | nsCOMPtr<nsIEventTarget> target = |
michael@0 | 88 | do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); |
michael@0 | 89 | if (NS_FAILED(rv)) return rv; |
michael@0 | 90 | |
michael@0 | 91 | // XXX if the caller requests an unbuffered stream, then perhaps |
michael@0 | 92 | // we'd want to simply return mSource; however, then we would |
michael@0 | 93 | // not be reading mSource on a background thread. is this ok? |
michael@0 | 94 | |
michael@0 | 95 | bool nonblocking = !(flags & OPEN_BLOCKING); |
michael@0 | 96 | |
michael@0 | 97 | net_ResolveSegmentParams(segsize, segcount); |
michael@0 | 98 | |
michael@0 | 99 | nsCOMPtr<nsIAsyncOutputStream> pipeOut; |
michael@0 | 100 | rv = NS_NewPipe2(getter_AddRefs(mPipeIn), |
michael@0 | 101 | getter_AddRefs(pipeOut), |
michael@0 | 102 | nonblocking, true, |
michael@0 | 103 | segsize, segcount); |
michael@0 | 104 | if (NS_FAILED(rv)) return rv; |
michael@0 | 105 | |
michael@0 | 106 | mInProgress = true; |
michael@0 | 107 | |
michael@0 | 108 | // startup async copy process... |
michael@0 | 109 | rv = NS_AsyncCopy(this, pipeOut, target, |
michael@0 | 110 | NS_ASYNCCOPY_VIA_WRITESEGMENTS, segsize); |
michael@0 | 111 | if (NS_SUCCEEDED(rv)) |
michael@0 | 112 | NS_ADDREF(*result = mPipeIn); |
michael@0 | 113 | |
michael@0 | 114 | return rv; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | NS_IMETHODIMP |
michael@0 | 118 | nsInputStreamTransport::OpenOutputStream(uint32_t flags, |
michael@0 | 119 | uint32_t segsize, |
michael@0 | 120 | uint32_t segcount, |
michael@0 | 121 | nsIOutputStream **result) |
michael@0 | 122 | { |
michael@0 | 123 | // this transport only supports reading! |
michael@0 | 124 | NS_NOTREACHED("nsInputStreamTransport::OpenOutputStream"); |
michael@0 | 125 | return NS_ERROR_UNEXPECTED; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | NS_IMETHODIMP |
michael@0 | 129 | nsInputStreamTransport::Close(nsresult reason) |
michael@0 | 130 | { |
michael@0 | 131 | if (NS_SUCCEEDED(reason)) |
michael@0 | 132 | reason = NS_BASE_STREAM_CLOSED; |
michael@0 | 133 | |
michael@0 | 134 | return mPipeIn->CloseWithStatus(reason); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | NS_IMETHODIMP |
michael@0 | 138 | nsInputStreamTransport::SetEventSink(nsITransportEventSink *sink, |
michael@0 | 139 | nsIEventTarget *target) |
michael@0 | 140 | { |
michael@0 | 141 | NS_ENSURE_TRUE(!mInProgress, NS_ERROR_IN_PROGRESS); |
michael@0 | 142 | |
michael@0 | 143 | if (target) |
michael@0 | 144 | return net_NewTransportEventSinkProxy(getter_AddRefs(mEventSink), |
michael@0 | 145 | sink, target); |
michael@0 | 146 | |
michael@0 | 147 | mEventSink = sink; |
michael@0 | 148 | return NS_OK; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | /** nsIInputStream **/ |
michael@0 | 152 | |
michael@0 | 153 | NS_IMETHODIMP |
michael@0 | 154 | nsInputStreamTransport::Close() |
michael@0 | 155 | { |
michael@0 | 156 | if (mCloseWhenDone) |
michael@0 | 157 | mSource->Close(); |
michael@0 | 158 | |
michael@0 | 159 | // make additional reads return early... |
michael@0 | 160 | mOffset = mLimit = 0; |
michael@0 | 161 | return NS_OK; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | NS_IMETHODIMP |
michael@0 | 165 | nsInputStreamTransport::Available(uint64_t *result) |
michael@0 | 166 | { |
michael@0 | 167 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | NS_IMETHODIMP |
michael@0 | 171 | nsInputStreamTransport::Read(char *buf, uint32_t count, uint32_t *result) |
michael@0 | 172 | { |
michael@0 | 173 | if (mFirstTime) { |
michael@0 | 174 | mFirstTime = false; |
michael@0 | 175 | if (mOffset != 0) { |
michael@0 | 176 | // read from current position if offset equal to max |
michael@0 | 177 | if (mOffset != UINT64_MAX) { |
michael@0 | 178 | nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mSource); |
michael@0 | 179 | if (seekable) |
michael@0 | 180 | seekable->Seek(nsISeekableStream::NS_SEEK_SET, mOffset); |
michael@0 | 181 | } |
michael@0 | 182 | // reset offset to zero so we can use it to enforce limit |
michael@0 | 183 | mOffset = 0; |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | // limit amount read |
michael@0 | 188 | uint64_t max = mLimit - mOffset; |
michael@0 | 189 | if (max == 0) { |
michael@0 | 190 | *result = 0; |
michael@0 | 191 | return NS_OK; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | if (count > max) |
michael@0 | 195 | count = static_cast<uint32_t>(max); |
michael@0 | 196 | |
michael@0 | 197 | nsresult rv = mSource->Read(buf, count, result); |
michael@0 | 198 | |
michael@0 | 199 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 200 | mOffset += *result; |
michael@0 | 201 | if (mEventSink) |
michael@0 | 202 | mEventSink->OnTransportStatus(this, NS_NET_STATUS_READING, mOffset, |
michael@0 | 203 | mLimit); |
michael@0 | 204 | } |
michael@0 | 205 | return rv; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | NS_IMETHODIMP |
michael@0 | 209 | nsInputStreamTransport::ReadSegments(nsWriteSegmentFun writer, void *closure, |
michael@0 | 210 | uint32_t count, uint32_t *result) |
michael@0 | 211 | { |
michael@0 | 212 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | NS_IMETHODIMP |
michael@0 | 216 | nsInputStreamTransport::IsNonBlocking(bool *result) |
michael@0 | 217 | { |
michael@0 | 218 | *result = false; |
michael@0 | 219 | return NS_OK; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | //----------------------------------------------------------------------------- |
michael@0 | 223 | // nsOutputStreamTransport |
michael@0 | 224 | // |
michael@0 | 225 | // Implements nsIOutputStream as a wrapper around the real input stream. This |
michael@0 | 226 | // allows the transport to support seeking, range-limiting, progress reporting, |
michael@0 | 227 | // and close-when-done semantics while utilizing NS_AsyncCopy. |
michael@0 | 228 | //----------------------------------------------------------------------------- |
michael@0 | 229 | |
michael@0 | 230 | class nsOutputStreamTransport : public nsITransport |
michael@0 | 231 | , public nsIOutputStream |
michael@0 | 232 | { |
michael@0 | 233 | public: |
michael@0 | 234 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 235 | NS_DECL_NSITRANSPORT |
michael@0 | 236 | NS_DECL_NSIOUTPUTSTREAM |
michael@0 | 237 | |
michael@0 | 238 | nsOutputStreamTransport(nsIOutputStream *sink, |
michael@0 | 239 | uint64_t offset, |
michael@0 | 240 | uint64_t limit, |
michael@0 | 241 | bool closeWhenDone) |
michael@0 | 242 | : mSink(sink) |
michael@0 | 243 | , mOffset(offset) |
michael@0 | 244 | , mLimit(limit) |
michael@0 | 245 | , mCloseWhenDone(closeWhenDone) |
michael@0 | 246 | , mFirstTime(true) |
michael@0 | 247 | , mInProgress(false) |
michael@0 | 248 | { |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | virtual ~nsOutputStreamTransport() |
michael@0 | 252 | { |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | private: |
michael@0 | 256 | nsCOMPtr<nsIAsyncOutputStream> mPipeOut; |
michael@0 | 257 | |
michael@0 | 258 | // while the copy is active, these members may only be accessed from the |
michael@0 | 259 | // nsIOutputStream implementation. |
michael@0 | 260 | nsCOMPtr<nsITransportEventSink> mEventSink; |
michael@0 | 261 | nsCOMPtr<nsIOutputStream> mSink; |
michael@0 | 262 | uint64_t mOffset; |
michael@0 | 263 | uint64_t mLimit; |
michael@0 | 264 | bool mCloseWhenDone; |
michael@0 | 265 | bool mFirstTime; |
michael@0 | 266 | |
michael@0 | 267 | // this variable serves as a lock to prevent the state of the transport |
michael@0 | 268 | // from being modified once the copy is in progress. |
michael@0 | 269 | bool mInProgress; |
michael@0 | 270 | }; |
michael@0 | 271 | |
michael@0 | 272 | NS_IMPL_ISUPPORTS(nsOutputStreamTransport, |
michael@0 | 273 | nsITransport, |
michael@0 | 274 | nsIOutputStream) |
michael@0 | 275 | |
michael@0 | 276 | /** nsITransport **/ |
michael@0 | 277 | |
michael@0 | 278 | NS_IMETHODIMP |
michael@0 | 279 | nsOutputStreamTransport::OpenInputStream(uint32_t flags, |
michael@0 | 280 | uint32_t segsize, |
michael@0 | 281 | uint32_t segcount, |
michael@0 | 282 | nsIInputStream **result) |
michael@0 | 283 | { |
michael@0 | 284 | // this transport only supports writing! |
michael@0 | 285 | NS_NOTREACHED("nsOutputStreamTransport::OpenInputStream"); |
michael@0 | 286 | return NS_ERROR_UNEXPECTED; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | NS_IMETHODIMP |
michael@0 | 290 | nsOutputStreamTransport::OpenOutputStream(uint32_t flags, |
michael@0 | 291 | uint32_t segsize, |
michael@0 | 292 | uint32_t segcount, |
michael@0 | 293 | nsIOutputStream **result) |
michael@0 | 294 | { |
michael@0 | 295 | NS_ENSURE_TRUE(!mInProgress, NS_ERROR_IN_PROGRESS); |
michael@0 | 296 | |
michael@0 | 297 | nsresult rv; |
michael@0 | 298 | nsCOMPtr<nsIEventTarget> target = |
michael@0 | 299 | do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); |
michael@0 | 300 | if (NS_FAILED(rv)) return rv; |
michael@0 | 301 | |
michael@0 | 302 | // XXX if the caller requests an unbuffered stream, then perhaps |
michael@0 | 303 | // we'd want to simply return mSink; however, then we would |
michael@0 | 304 | // not be writing to mSink on a background thread. is this ok? |
michael@0 | 305 | |
michael@0 | 306 | bool nonblocking = !(flags & OPEN_BLOCKING); |
michael@0 | 307 | |
michael@0 | 308 | net_ResolveSegmentParams(segsize, segcount); |
michael@0 | 309 | |
michael@0 | 310 | nsCOMPtr<nsIAsyncInputStream> pipeIn; |
michael@0 | 311 | rv = NS_NewPipe2(getter_AddRefs(pipeIn), |
michael@0 | 312 | getter_AddRefs(mPipeOut), |
michael@0 | 313 | true, nonblocking, |
michael@0 | 314 | segsize, segcount); |
michael@0 | 315 | if (NS_FAILED(rv)) return rv; |
michael@0 | 316 | |
michael@0 | 317 | mInProgress = true; |
michael@0 | 318 | |
michael@0 | 319 | // startup async copy process... |
michael@0 | 320 | rv = NS_AsyncCopy(pipeIn, this, target, |
michael@0 | 321 | NS_ASYNCCOPY_VIA_READSEGMENTS, segsize); |
michael@0 | 322 | if (NS_SUCCEEDED(rv)) |
michael@0 | 323 | NS_ADDREF(*result = mPipeOut); |
michael@0 | 324 | |
michael@0 | 325 | return rv; |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | NS_IMETHODIMP |
michael@0 | 329 | nsOutputStreamTransport::Close(nsresult reason) |
michael@0 | 330 | { |
michael@0 | 331 | if (NS_SUCCEEDED(reason)) |
michael@0 | 332 | reason = NS_BASE_STREAM_CLOSED; |
michael@0 | 333 | |
michael@0 | 334 | return mPipeOut->CloseWithStatus(reason); |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | NS_IMETHODIMP |
michael@0 | 338 | nsOutputStreamTransport::SetEventSink(nsITransportEventSink *sink, |
michael@0 | 339 | nsIEventTarget *target) |
michael@0 | 340 | { |
michael@0 | 341 | NS_ENSURE_TRUE(!mInProgress, NS_ERROR_IN_PROGRESS); |
michael@0 | 342 | |
michael@0 | 343 | if (target) |
michael@0 | 344 | return net_NewTransportEventSinkProxy(getter_AddRefs(mEventSink), |
michael@0 | 345 | sink, target); |
michael@0 | 346 | |
michael@0 | 347 | mEventSink = sink; |
michael@0 | 348 | return NS_OK; |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | /** nsIOutputStream **/ |
michael@0 | 352 | |
michael@0 | 353 | NS_IMETHODIMP |
michael@0 | 354 | nsOutputStreamTransport::Close() |
michael@0 | 355 | { |
michael@0 | 356 | if (mCloseWhenDone) |
michael@0 | 357 | mSink->Close(); |
michael@0 | 358 | |
michael@0 | 359 | // make additional writes return early... |
michael@0 | 360 | mOffset = mLimit = 0; |
michael@0 | 361 | return NS_OK; |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | NS_IMETHODIMP |
michael@0 | 365 | nsOutputStreamTransport::Flush() |
michael@0 | 366 | { |
michael@0 | 367 | return NS_OK; |
michael@0 | 368 | } |
michael@0 | 369 | |
michael@0 | 370 | NS_IMETHODIMP |
michael@0 | 371 | nsOutputStreamTransport::Write(const char *buf, uint32_t count, uint32_t *result) |
michael@0 | 372 | { |
michael@0 | 373 | if (mFirstTime) { |
michael@0 | 374 | mFirstTime = false; |
michael@0 | 375 | if (mOffset != 0) { |
michael@0 | 376 | // write to current position if offset equal to max |
michael@0 | 377 | if (mOffset != UINT64_MAX) { |
michael@0 | 378 | nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mSink); |
michael@0 | 379 | if (seekable) |
michael@0 | 380 | seekable->Seek(nsISeekableStream::NS_SEEK_SET, mOffset); |
michael@0 | 381 | } |
michael@0 | 382 | // reset offset to zero so we can use it to enforce limit |
michael@0 | 383 | mOffset = 0; |
michael@0 | 384 | } |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | // limit amount written |
michael@0 | 388 | uint64_t max = mLimit - mOffset; |
michael@0 | 389 | if (max == 0) { |
michael@0 | 390 | *result = 0; |
michael@0 | 391 | return NS_OK; |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | if (count > max) |
michael@0 | 395 | count = static_cast<uint32_t>(max); |
michael@0 | 396 | |
michael@0 | 397 | nsresult rv = mSink->Write(buf, count, result); |
michael@0 | 398 | |
michael@0 | 399 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 400 | mOffset += *result; |
michael@0 | 401 | if (mEventSink) |
michael@0 | 402 | mEventSink->OnTransportStatus(this, NS_NET_STATUS_WRITING, mOffset, |
michael@0 | 403 | mLimit); |
michael@0 | 404 | } |
michael@0 | 405 | return rv; |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | NS_IMETHODIMP |
michael@0 | 409 | nsOutputStreamTransport::WriteSegments(nsReadSegmentFun reader, void *closure, |
michael@0 | 410 | uint32_t count, uint32_t *result) |
michael@0 | 411 | { |
michael@0 | 412 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | NS_IMETHODIMP |
michael@0 | 416 | nsOutputStreamTransport::WriteFrom(nsIInputStream *in, uint32_t count, uint32_t *result) |
michael@0 | 417 | { |
michael@0 | 418 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | NS_IMETHODIMP |
michael@0 | 422 | nsOutputStreamTransport::IsNonBlocking(bool *result) |
michael@0 | 423 | { |
michael@0 | 424 | *result = false; |
michael@0 | 425 | return NS_OK; |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | #ifdef MOZ_NUWA_PROCESS |
michael@0 | 429 | #include "ipc/Nuwa.h" |
michael@0 | 430 | |
michael@0 | 431 | class STSThreadPoolListener : public nsIThreadPoolListener |
michael@0 | 432 | { |
michael@0 | 433 | public: |
michael@0 | 434 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 435 | NS_DECL_NSITHREADPOOLLISTENER |
michael@0 | 436 | |
michael@0 | 437 | STSThreadPoolListener() {} |
michael@0 | 438 | ~STSThreadPoolListener() {} |
michael@0 | 439 | }; |
michael@0 | 440 | |
michael@0 | 441 | NS_IMPL_ISUPPORTS(STSThreadPoolListener, nsIThreadPoolListener) |
michael@0 | 442 | |
michael@0 | 443 | NS_IMETHODIMP |
michael@0 | 444 | STSThreadPoolListener::OnThreadCreated() |
michael@0 | 445 | { |
michael@0 | 446 | if (IsNuwaProcess()) { |
michael@0 | 447 | NuwaMarkCurrentThread(nullptr, nullptr); |
michael@0 | 448 | } |
michael@0 | 449 | return NS_OK; |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | NS_IMETHODIMP |
michael@0 | 453 | STSThreadPoolListener::OnThreadShuttingDown() |
michael@0 | 454 | { |
michael@0 | 455 | return NS_OK; |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | #endif // MOZ_NUWA_PROCESS |
michael@0 | 459 | |
michael@0 | 460 | //----------------------------------------------------------------------------- |
michael@0 | 461 | // nsStreamTransportService |
michael@0 | 462 | //----------------------------------------------------------------------------- |
michael@0 | 463 | |
michael@0 | 464 | nsStreamTransportService::~nsStreamTransportService() |
michael@0 | 465 | { |
michael@0 | 466 | NS_ASSERTION(!mPool, "thread pool wasn't shutdown"); |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | nsresult |
michael@0 | 470 | nsStreamTransportService::Init() |
michael@0 | 471 | { |
michael@0 | 472 | mPool = do_CreateInstance(NS_THREADPOOL_CONTRACTID); |
michael@0 | 473 | NS_ENSURE_STATE(mPool); |
michael@0 | 474 | |
michael@0 | 475 | // Configure the pool |
michael@0 | 476 | mPool->SetName(NS_LITERAL_CSTRING("StreamTrans")); |
michael@0 | 477 | mPool->SetThreadLimit(25); |
michael@0 | 478 | mPool->SetIdleThreadLimit(1); |
michael@0 | 479 | mPool->SetIdleThreadTimeout(PR_SecondsToInterval(30)); |
michael@0 | 480 | #ifdef MOZ_NUWA_PROCESS |
michael@0 | 481 | if (IsNuwaProcess()) { |
michael@0 | 482 | mPool->SetListener(new STSThreadPoolListener()); |
michael@0 | 483 | } |
michael@0 | 484 | #endif |
michael@0 | 485 | |
michael@0 | 486 | nsCOMPtr<nsIObserverService> obsSvc = |
michael@0 | 487 | mozilla::services::GetObserverService(); |
michael@0 | 488 | if (obsSvc) |
michael@0 | 489 | obsSvc->AddObserver(this, "xpcom-shutdown-threads", false); |
michael@0 | 490 | return NS_OK; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | NS_IMPL_ISUPPORTS(nsStreamTransportService, |
michael@0 | 494 | nsIStreamTransportService, |
michael@0 | 495 | nsIEventTarget, |
michael@0 | 496 | nsIObserver) |
michael@0 | 497 | |
michael@0 | 498 | NS_IMETHODIMP |
michael@0 | 499 | nsStreamTransportService::Dispatch(nsIRunnable *task, uint32_t flags) |
michael@0 | 500 | { |
michael@0 | 501 | NS_ENSURE_TRUE(mPool, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 502 | return mPool->Dispatch(task, flags); |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | NS_IMETHODIMP |
michael@0 | 506 | nsStreamTransportService::IsOnCurrentThread(bool *result) |
michael@0 | 507 | { |
michael@0 | 508 | NS_ENSURE_TRUE(mPool, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 509 | return mPool->IsOnCurrentThread(result); |
michael@0 | 510 | } |
michael@0 | 511 | |
michael@0 | 512 | NS_IMETHODIMP |
michael@0 | 513 | nsStreamTransportService::CreateInputTransport(nsIInputStream *stream, |
michael@0 | 514 | int64_t offset, |
michael@0 | 515 | int64_t limit, |
michael@0 | 516 | bool closeWhenDone, |
michael@0 | 517 | nsITransport **result) |
michael@0 | 518 | { |
michael@0 | 519 | nsInputStreamTransport *trans = |
michael@0 | 520 | new nsInputStreamTransport(stream, offset, limit, closeWhenDone); |
michael@0 | 521 | if (!trans) |
michael@0 | 522 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 523 | NS_ADDREF(*result = trans); |
michael@0 | 524 | return NS_OK; |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | NS_IMETHODIMP |
michael@0 | 528 | nsStreamTransportService::CreateOutputTransport(nsIOutputStream *stream, |
michael@0 | 529 | int64_t offset, |
michael@0 | 530 | int64_t limit, |
michael@0 | 531 | bool closeWhenDone, |
michael@0 | 532 | nsITransport **result) |
michael@0 | 533 | { |
michael@0 | 534 | nsOutputStreamTransport *trans = |
michael@0 | 535 | new nsOutputStreamTransport(stream, offset, limit, closeWhenDone); |
michael@0 | 536 | if (!trans) |
michael@0 | 537 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 538 | NS_ADDREF(*result = trans); |
michael@0 | 539 | return NS_OK; |
michael@0 | 540 | } |
michael@0 | 541 | |
michael@0 | 542 | NS_IMETHODIMP |
michael@0 | 543 | nsStreamTransportService::Observe(nsISupports *subject, const char *topic, |
michael@0 | 544 | const char16_t *data) |
michael@0 | 545 | { |
michael@0 | 546 | NS_ASSERTION(strcmp(topic, "xpcom-shutdown-threads") == 0, "oops"); |
michael@0 | 547 | |
michael@0 | 548 | if (mPool) { |
michael@0 | 549 | mPool->Shutdown(); |
michael@0 | 550 | mPool = nullptr; |
michael@0 | 551 | } |
michael@0 | 552 | return NS_OK; |
michael@0 | 553 | } |