Bug 1852754: part 9) Add tests for dynamically loading <link rel="prefetch"> elements...
[gecko.git] / xpcom / io / InputStreamLengthWrapper.h
bloba11a808fa7112eb4aa1a694a72847bb3cd6fc06a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef InputStreamLengthWrapper_h
8 #define InputStreamLengthWrapper_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Mutex.h"
12 #include "nsCOMPtr.h"
13 #include "nsIAsyncInputStream.h"
14 #include "nsICloneableInputStream.h"
15 #include "nsIIPCSerializableInputStream.h"
16 #include "nsISeekableStream.h"
17 #include "nsIInputStreamLength.h"
19 namespace mozilla {
21 // A wrapper keeps an inputStream together with its length.
22 // This class can be used for nsIInputStreams that do not implement
23 // nsIInputStreamLength.
25 class InputStreamLengthWrapper final : public nsIAsyncInputStream,
26 public nsICloneableInputStream,
27 public nsIIPCSerializableInputStream,
28 public nsISeekableStream,
29 public nsIInputStreamCallback,
30 public nsIInputStreamLength {
31 public:
32 NS_DECL_THREADSAFE_ISUPPORTS
33 NS_DECL_NSIINPUTSTREAM
34 NS_DECL_NSIASYNCINPUTSTREAM
35 NS_DECL_NSICLONEABLEINPUTSTREAM
36 NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
37 NS_DECL_NSISEEKABLESTREAM
38 NS_DECL_NSITELLABLESTREAM
39 NS_DECL_NSIINPUTSTREAMCALLBACK
40 NS_DECL_NSIINPUTSTREAMLENGTH
42 // This method creates a InputStreamLengthWrapper around aInputStream if
43 // this doesn't implement nsIInputStreamLength or
44 // nsIInputStreamAsyncLength interface, but it implements
45 // nsIAsyncInputStream. For this kind of streams,
46 // InputStreamLengthHelper is not able to retrieve the length. This
47 // method will make such streams ready to be used with
48 // InputStreamLengthHelper.
49 static already_AddRefed<nsIInputStream> MaybeWrap(
50 already_AddRefed<nsIInputStream> aInputStream, int64_t aLength);
52 // The length here will be used when nsIInputStreamLength::Length() is called.
53 InputStreamLengthWrapper(already_AddRefed<nsIInputStream> aInputStream,
54 int64_t aLength);
56 // This CTOR is meant to be used just for IPC.
57 InputStreamLengthWrapper();
59 private:
60 ~InputStreamLengthWrapper();
62 void SetSourceStream(already_AddRefed<nsIInputStream> aInputStream);
64 nsCOMPtr<nsIInputStream> mInputStream;
66 // Raw pointers because these are just QI of mInputStream.
67 nsICloneableInputStream* mWeakCloneableInputStream;
68 nsIIPCSerializableInputStream* mWeakIPCSerializableInputStream;
69 nsISeekableStream* mWeakSeekableInputStream;
70 nsITellableStream* mWeakTellableInputStream;
71 nsIAsyncInputStream* mWeakAsyncInputStream;
73 int64_t mLength;
74 bool mConsumed;
76 mozilla::Mutex mMutex;
78 // This is used for AsyncWait and it's protected by mutex.
79 nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback MOZ_GUARDED_BY(mMutex);
82 } // namespace mozilla
84 #endif // InputStreamLengthWrapper_h