1 /* -*- Mode: C++; tab-width: 4; 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/. */
5 
6 #ifndef nsJARChannel_h__
7 #define nsJARChannel_h__
8 
9 #include "mozilla/net/MemoryDownloader.h"
10 #include "nsIJARChannel.h"
11 #include "nsIJARURI.h"
12 #include "nsIEventTarget.h"
13 #include "nsIInputStreamPump.h"
14 #include "nsIInterfaceRequestor.h"
15 #include "nsIProgressEventSink.h"
16 #include "nsIStreamListener.h"
17 #include "nsIZipReader.h"
18 #include "nsILoadGroup.h"
19 #include "nsILoadInfo.h"
20 #include "nsIThreadRetargetableRequest.h"
21 #include "nsIThreadRetargetableStreamListener.h"
22 #include "nsHashPropertyBag.h"
23 #include "nsIFile.h"
24 #include "nsIURI.h"
25 #include "nsCOMPtr.h"
26 #include "nsString.h"
27 #include "mozilla/Atomics.h"
28 #include "mozilla/Logging.h"
29 
30 class nsJARInputThunk;
31 class nsJARProtocolHandler;
32 class nsInputStreamPump;
33 
34 //-----------------------------------------------------------------------------
35 
36 class nsJARChannel final : public nsIJARChannel,
37                            public nsIStreamListener,
38                            public nsIThreadRetargetableRequest,
39                            public nsIThreadRetargetableStreamListener,
40                            public nsHashPropertyBag {
41  public:
42   NS_DECL_ISUPPORTS_INHERITED
43   NS_DECL_NSIREQUEST
44   NS_DECL_NSICHANNEL
45   NS_DECL_NSIJARCHANNEL
46   NS_DECL_NSIREQUESTOBSERVER
47   NS_DECL_NSISTREAMLISTENER
48   NS_DECL_NSITHREADRETARGETABLEREQUEST
49   NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
50 
51   nsJARChannel();
52 
53   nsresult Init(nsIURI* uri);
54 
55   void SetFile(nsIFile* file);
56 
57  private:
58   virtual ~nsJARChannel();
59 
60   nsresult CreateJarInput(nsIZipReaderCache*, nsJARInputThunk**);
61   nsresult LookupFile();
62   nsresult OpenLocalFile();
63   nsresult ContinueOpenLocalFile(nsJARInputThunk* aInput, bool aIsSyncCall);
64   nsresult OnOpenLocalFileComplete(nsresult aResult, bool aIsSyncCall);
65   nsresult CheckPendingEvents();
66   void NotifyError(nsresult aError);
67   void FireOnProgress(uint64_t aProgress);
68 
69   nsCString mSpec;
70 
71   bool mOpened;
72   Atomic<bool, ReleaseAcquire> mCanceled;
73   bool mOnDataCalled = false;
74 
75   RefPtr<nsJARProtocolHandler> mJarHandler;
76   nsCOMPtr<nsIJARURI> mJarURI;
77   nsCOMPtr<nsIURI> mOriginalURI;
78   nsCOMPtr<nsISupports> mOwner;
79   nsCOMPtr<nsILoadInfo> mLoadInfo;
80   nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
81   nsCOMPtr<nsISupports> mSecurityInfo;
82   nsCOMPtr<nsIProgressEventSink> mProgressSink;
83   nsCOMPtr<nsILoadGroup> mLoadGroup;
84   nsCOMPtr<nsIStreamListener> mListener;
85   nsCString mContentType;
86   nsCString mContentCharset;
87   int64_t mContentLength;
88   uint32_t mLoadFlags;
89   Atomic<nsresult, ReleaseAcquire> mStatus;
90   bool mIsPending;  // the AsyncOpen is in progress.
91 
92   bool mEnableOMT;
93   // |Cancel()|, |Suspend()|, and |Resume()| might be called during AsyncOpen.
94   struct {
95     bool isCanceled;
96     Atomic<uint32_t> suspendCount;
97   } mPendingEvent;
98 
99   nsCOMPtr<nsIInputStreamPump> mPump;
100   // mRequest is only non-null during OnStartRequest, so we'll have a pointer
101   // to the request if we get called back via RetargetDeliveryTo.
102   nsCOMPtr<nsIRequest> mRequest;
103   nsCOMPtr<nsIFile> mJarFile;
104   nsCOMPtr<nsIFile> mJarFileOverride;
105   nsCOMPtr<nsIZipReader> mPreCachedJarReader;
106   nsCOMPtr<nsIURI> mJarBaseURI;
107   nsCString mJarEntry;
108   nsCString mInnerJarEntry;
109 
110   // use StreamTransportService as background thread
111   nsCOMPtr<nsIEventTarget> mWorker;
112 };
113 
114 #endif  // nsJARChannel_h__
115