1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
6 #include "nsBaseContentStream.h"
7 #include "nsStreamUtils.h"
9 //-----------------------------------------------------------------------------
11 void nsBaseContentStream::DispatchCallback(bool async
) {
12 if (!mCallback
) return;
14 // It's important to clear mCallback and mCallbackTarget up-front because the
15 // OnInputStreamReady implementation may call our AsyncWait method.
17 nsCOMPtr
<nsIInputStreamCallback
> callback
;
19 callback
= NS_NewInputStreamReadyEvent(
20 "nsBaseContentStream::DispatchCallback", mCallback
, mCallbackTarget
);
23 callback
.swap(mCallback
);
25 mCallbackTarget
= nullptr;
27 callback
->OnInputStreamReady(this);
30 //-----------------------------------------------------------------------------
31 // nsBaseContentStream::nsISupports
33 NS_IMPL_ADDREF(nsBaseContentStream
)
34 NS_IMPL_RELEASE(nsBaseContentStream
)
36 // We only support nsIAsyncInputStream when we are in non-blocking mode.
37 NS_INTERFACE_MAP_BEGIN(nsBaseContentStream
)
38 NS_INTERFACE_MAP_ENTRY(nsIInputStream
)
39 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream
, mNonBlocking
)
40 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIInputStream
)
43 //-----------------------------------------------------------------------------
44 // nsBaseContentStream::nsIInputStream
47 nsBaseContentStream::Close() {
48 return IsClosed() ? NS_OK
: CloseWithStatus(NS_BASE_STREAM_CLOSED
);
52 nsBaseContentStream::Available(uint64_t* result
) {
58 nsBaseContentStream::StreamStatus() { return mStatus
; }
61 nsBaseContentStream::Read(char* buf
, uint32_t count
, uint32_t* result
) {
62 return ReadSegments(NS_CopySegmentToBuffer
, buf
, count
, result
);
66 nsBaseContentStream::ReadSegments(nsWriteSegmentFun fun
, void* closure
,
67 uint32_t count
, uint32_t* result
) {
70 if (mStatus
== NS_BASE_STREAM_CLOSED
) return NS_OK
;
73 if (!IsClosed() && IsNonBlocking()) return NS_BASE_STREAM_WOULD_BLOCK
;
79 nsBaseContentStream::IsNonBlocking(bool* result
) {
80 *result
= mNonBlocking
;
84 //-----------------------------------------------------------------------------
85 // nsBaseContentStream::nsIAsyncInputStream
88 nsBaseContentStream::CloseWithStatus(nsresult status
) {
89 if (IsClosed()) return NS_OK
;
91 NS_ENSURE_ARG(NS_FAILED(status
));
99 nsBaseContentStream::AsyncWait(nsIInputStreamCallback
* callback
, uint32_t flags
,
100 uint32_t requestedCount
,
101 nsIEventTarget
* target
) {
102 // Our _only_ consumer is nsInputStreamPump, so we simplify things here by
103 // making assumptions about how we will be called.
104 NS_ASSERTION(target
, "unexpected parameter");
105 NS_ASSERTION(flags
== 0, "unexpected parameter");
106 NS_ASSERTION(requestedCount
== 0, "unexpected parameter");
110 target
->IsOnCurrentThread(&correctThread
);
111 NS_ASSERTION(correctThread
, "event target must be on the current thread");
114 mCallback
= callback
;
115 mCallbackTarget
= target
;
117 if (!mCallback
) return NS_OK
;
119 // If we're already closed, then dispatch this callback immediately.