nsStreamTransportService.cpp 16 KB

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