netwerk/base/src/nsBaseContentStream.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsBaseContentStream.h"
michael@0 7 #include "nsStreamUtils.h"
michael@0 8
michael@0 9 //-----------------------------------------------------------------------------
michael@0 10
michael@0 11 void
michael@0 12 nsBaseContentStream::DispatchCallback(bool async)
michael@0 13 {
michael@0 14 if (!mCallback)
michael@0 15 return;
michael@0 16
michael@0 17 // It's important to clear mCallback and mCallbackTarget up-front because the
michael@0 18 // OnInputStreamReady implementation may call our AsyncWait method.
michael@0 19
michael@0 20 nsCOMPtr<nsIInputStreamCallback> callback;
michael@0 21 if (async) {
michael@0 22 callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
michael@0 23 mCallback = nullptr;
michael@0 24 } else {
michael@0 25 callback.swap(mCallback);
michael@0 26 }
michael@0 27 mCallbackTarget = nullptr;
michael@0 28
michael@0 29 callback->OnInputStreamReady(this);
michael@0 30 }
michael@0 31
michael@0 32 //-----------------------------------------------------------------------------
michael@0 33 // nsBaseContentStream::nsISupports
michael@0 34
michael@0 35 NS_IMPL_ADDREF(nsBaseContentStream)
michael@0 36 NS_IMPL_RELEASE(nsBaseContentStream)
michael@0 37
michael@0 38 // We only support nsIAsyncInputStream when we are in non-blocking mode.
michael@0 39 NS_INTERFACE_MAP_BEGIN(nsBaseContentStream)
michael@0 40 NS_INTERFACE_MAP_ENTRY(nsIInputStream)
michael@0 41 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream, mNonBlocking)
michael@0 42 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
michael@0 43 NS_INTERFACE_MAP_END_THREADSAFE
michael@0 44
michael@0 45 //-----------------------------------------------------------------------------
michael@0 46 // nsBaseContentStream::nsIInputStream
michael@0 47
michael@0 48 NS_IMETHODIMP
michael@0 49 nsBaseContentStream::Close()
michael@0 50 {
michael@0 51 return IsClosed() ? NS_OK : CloseWithStatus(NS_BASE_STREAM_CLOSED);
michael@0 52 }
michael@0 53
michael@0 54 NS_IMETHODIMP
michael@0 55 nsBaseContentStream::Available(uint64_t *result)
michael@0 56 {
michael@0 57 *result = 0;
michael@0 58 return mStatus;
michael@0 59 }
michael@0 60
michael@0 61 NS_IMETHODIMP
michael@0 62 nsBaseContentStream::Read(char *buf, uint32_t count, uint32_t *result)
michael@0 63 {
michael@0 64 return ReadSegments(NS_CopySegmentToBuffer, buf, count, result);
michael@0 65 }
michael@0 66
michael@0 67 NS_IMETHODIMP
michael@0 68 nsBaseContentStream::ReadSegments(nsWriteSegmentFun fun, void *closure,
michael@0 69 uint32_t count, uint32_t *result)
michael@0 70 {
michael@0 71 *result = 0;
michael@0 72
michael@0 73 if (mStatus == NS_BASE_STREAM_CLOSED)
michael@0 74 return NS_OK;
michael@0 75
michael@0 76 // No data yet
michael@0 77 if (!IsClosed() && IsNonBlocking())
michael@0 78 return NS_BASE_STREAM_WOULD_BLOCK;
michael@0 79
michael@0 80 return mStatus;
michael@0 81 }
michael@0 82
michael@0 83 NS_IMETHODIMP
michael@0 84 nsBaseContentStream::IsNonBlocking(bool *result)
michael@0 85 {
michael@0 86 *result = mNonBlocking;
michael@0 87 return NS_OK;
michael@0 88 }
michael@0 89
michael@0 90 //-----------------------------------------------------------------------------
michael@0 91 // nsBaseContentStream::nsIAsyncInputStream
michael@0 92
michael@0 93 NS_IMETHODIMP
michael@0 94 nsBaseContentStream::CloseWithStatus(nsresult status)
michael@0 95 {
michael@0 96 if (IsClosed())
michael@0 97 return NS_OK;
michael@0 98
michael@0 99 NS_ENSURE_ARG(NS_FAILED(status));
michael@0 100 mStatus = status;
michael@0 101
michael@0 102 DispatchCallback();
michael@0 103 return NS_OK;
michael@0 104 }
michael@0 105
michael@0 106 NS_IMETHODIMP
michael@0 107 nsBaseContentStream::AsyncWait(nsIInputStreamCallback *callback,
michael@0 108 uint32_t flags, uint32_t requestedCount,
michael@0 109 nsIEventTarget *target)
michael@0 110 {
michael@0 111 // Our _only_ consumer is nsInputStreamPump, so we simplify things here by
michael@0 112 // making assumptions about how we will be called.
michael@0 113 NS_ASSERTION(target, "unexpected parameter");
michael@0 114 NS_ASSERTION(flags == 0, "unexpected parameter");
michael@0 115 NS_ASSERTION(requestedCount == 0, "unexpected parameter");
michael@0 116
michael@0 117 #ifdef DEBUG
michael@0 118 bool correctThread;
michael@0 119 target->IsOnCurrentThread(&correctThread);
michael@0 120 NS_ASSERTION(correctThread, "event target must be on the current thread");
michael@0 121 #endif
michael@0 122
michael@0 123 mCallback = callback;
michael@0 124 mCallbackTarget = target;
michael@0 125
michael@0 126 if (!mCallback)
michael@0 127 return NS_OK;
michael@0 128
michael@0 129 // If we're already closed, then dispatch this callback immediately.
michael@0 130 if (IsClosed()) {
michael@0 131 DispatchCallback();
michael@0 132 return NS_OK;
michael@0 133 }
michael@0 134
michael@0 135 OnCallbackPending();
michael@0 136 return NS_OK;
michael@0 137 }

mercurial