|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * |
|
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/. */ |
|
6 |
|
7 #ifndef imgRequest_h__ |
|
8 #define imgRequest_h__ |
|
9 |
|
10 #include "nsIChannelEventSink.h" |
|
11 #include "nsIInterfaceRequestor.h" |
|
12 #include "nsIStreamListener.h" |
|
13 #include "nsIThreadRetargetableStreamListener.h" |
|
14 #include "nsIPrincipal.h" |
|
15 |
|
16 #include "nsAutoPtr.h" |
|
17 #include "nsCOMPtr.h" |
|
18 #include "nsProxyRelease.h" |
|
19 #include "nsStringGlue.h" |
|
20 #include "nsError.h" |
|
21 #include "nsIAsyncVerifyRedirectCallback.h" |
|
22 |
|
23 class imgCacheValidator; |
|
24 class imgStatusTracker; |
|
25 class imgLoader; |
|
26 class imgRequestProxy; |
|
27 class imgCacheEntry; |
|
28 class imgMemoryReporter; |
|
29 class imgRequestNotifyRunnable; |
|
30 class nsIApplicationCache; |
|
31 class nsIProperties; |
|
32 class nsIRequest; |
|
33 class nsITimedChannel; |
|
34 class nsIURI; |
|
35 |
|
36 namespace mozilla { |
|
37 namespace image { |
|
38 class Image; |
|
39 class ImageURL; |
|
40 } // namespace image |
|
41 } // namespace mozilla |
|
42 |
|
43 class imgRequest : public nsIStreamListener, |
|
44 public nsIThreadRetargetableStreamListener, |
|
45 public nsIChannelEventSink, |
|
46 public nsIInterfaceRequestor, |
|
47 public nsIAsyncVerifyRedirectCallback |
|
48 { |
|
49 public: |
|
50 typedef mozilla::image::ImageURL ImageURL; |
|
51 imgRequest(imgLoader* aLoader); |
|
52 virtual ~imgRequest(); |
|
53 |
|
54 NS_DECL_THREADSAFE_ISUPPORTS |
|
55 |
|
56 nsresult Init(nsIURI *aURI, |
|
57 nsIURI *aCurrentURI, |
|
58 nsIURI *aFirstPartyIsolationURI, |
|
59 nsIRequest *aRequest, |
|
60 nsIChannel *aChannel, |
|
61 imgCacheEntry *aCacheEntry, |
|
62 void *aLoadId, |
|
63 nsIPrincipal* aLoadingPrincipal, |
|
64 int32_t aCORSMode); |
|
65 |
|
66 // Callers must call imgRequestProxy::Notify later. |
|
67 void AddProxy(imgRequestProxy *proxy); |
|
68 |
|
69 nsresult RemoveProxy(imgRequestProxy *proxy, nsresult aStatus); |
|
70 |
|
71 // Cancel, but also ensure that all work done in Init() is undone. Call this |
|
72 // only when the channel has failed to open, and so calling Cancel() on it |
|
73 // won't be sufficient. |
|
74 void CancelAndAbort(nsresult aStatus); |
|
75 |
|
76 // Called or dispatched by cancel for main thread only execution. |
|
77 void ContinueCancel(nsresult aStatus); |
|
78 |
|
79 // Methods that get forwarded to the Image, or deferred until it's |
|
80 // instantiated. |
|
81 nsresult LockImage(); |
|
82 nsresult UnlockImage(); |
|
83 nsresult StartDecoding(); |
|
84 nsresult RequestDecode(); |
|
85 |
|
86 inline void SetInnerWindowID(uint64_t aInnerWindowId) { |
|
87 mInnerWindowId = aInnerWindowId; |
|
88 } |
|
89 |
|
90 inline uint64_t InnerWindowID() const { |
|
91 return mInnerWindowId; |
|
92 } |
|
93 |
|
94 // Set the cache validation information (expiry time, whether we must |
|
95 // validate, etc) on the cache entry based on the request information. |
|
96 // If this function is called multiple times, the information set earliest |
|
97 // wins. |
|
98 static void SetCacheValidation(imgCacheEntry* aEntry, nsIRequest* aRequest); |
|
99 |
|
100 // Check if application cache of the original load is different from |
|
101 // application cache of the new load. Also lack of application cache |
|
102 // on one of the loads is considered a change of a loading cache since |
|
103 // HTTP cache may contain a different data then app cache. |
|
104 bool CacheChanged(nsIRequest* aNewRequest); |
|
105 |
|
106 bool GetMultipart() const { return mIsMultiPartChannel; } |
|
107 |
|
108 // The CORS mode for which we loaded this image. |
|
109 int32_t GetCORSMode() const { return mCORSMode; } |
|
110 |
|
111 // The principal for the document that loaded this image. Used when trying to |
|
112 // validate a CORS image load. |
|
113 already_AddRefed<nsIPrincipal> GetLoadingPrincipal() const |
|
114 { |
|
115 nsCOMPtr<nsIPrincipal> principal = mLoadingPrincipal; |
|
116 return principal.forget(); |
|
117 } |
|
118 |
|
119 // Return the imgStatusTracker associated with this imgRequest. It may live |
|
120 // in |mStatusTracker| or in |mImage.mStatusTracker|, depending on whether |
|
121 // mImage has been instantiated yet. |
|
122 already_AddRefed<imgStatusTracker> GetStatusTracker(); |
|
123 |
|
124 // Get the current principal of the image. No AddRefing. |
|
125 inline nsIPrincipal* GetPrincipal() const { return mPrincipal.get(); } |
|
126 |
|
127 // Resize the cache entry to 0 if it exists |
|
128 void ResetCacheEntry(); |
|
129 |
|
130 // Update the cache entry size based on the image container |
|
131 void UpdateCacheEntrySize(); |
|
132 |
|
133 // OK to use on any thread. |
|
134 nsresult GetURI(ImageURL **aURI); |
|
135 |
|
136 private: |
|
137 friend class imgCacheEntry; |
|
138 friend class imgRequestProxy; |
|
139 friend class imgLoader; |
|
140 friend class imgCacheValidator; |
|
141 friend class imgStatusTracker; |
|
142 friend class imgCacheExpirationTracker; |
|
143 friend class imgRequestNotifyRunnable; |
|
144 |
|
145 inline void SetLoadId(void *aLoadId) { |
|
146 mLoadId = aLoadId; |
|
147 } |
|
148 void Cancel(nsresult aStatus); |
|
149 void RemoveFromCache(); |
|
150 |
|
151 nsresult GetSecurityInfo(nsISupports **aSecurityInfo); |
|
152 |
|
153 inline const char *GetMimeType() const { |
|
154 return mContentType.get(); |
|
155 } |
|
156 inline nsIProperties *Properties() { |
|
157 return mProperties; |
|
158 } |
|
159 |
|
160 // Reset the cache entry after we've dropped our reference to it. Used by the |
|
161 // imgLoader when our cache entry is re-requested after we've dropped our |
|
162 // reference to it. |
|
163 void SetCacheEntry(imgCacheEntry *entry); |
|
164 |
|
165 // Returns whether we've got a reference to the cache entry. |
|
166 bool HasCacheEntry() const; |
|
167 |
|
168 // Return the priority of the underlying network request, or return |
|
169 // PRIORITY_NORMAL if it doesn't support nsISupportsPriority. |
|
170 int32_t Priority() const; |
|
171 |
|
172 // Adjust the priority of the underlying network request by the given delta |
|
173 // on behalf of the given proxy. |
|
174 void AdjustPriority(imgRequestProxy *aProxy, int32_t aDelta); |
|
175 |
|
176 // Return whether we've seen some data at this point |
|
177 bool HasTransferredData() const { return mGotData; } |
|
178 |
|
179 // Set whether this request is stored in the cache. If it isn't, regardless |
|
180 // of whether this request has a non-null mCacheEntry, this imgRequest won't |
|
181 // try to update or modify the image cache. |
|
182 void SetIsInCache(bool cacheable); |
|
183 |
|
184 bool IsBlockingOnload() const; |
|
185 void SetBlockingOnload(bool block) const; |
|
186 |
|
187 public: |
|
188 NS_DECL_NSISTREAMLISTENER |
|
189 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER |
|
190 NS_DECL_NSIREQUESTOBSERVER |
|
191 NS_DECL_NSICHANNELEVENTSINK |
|
192 NS_DECL_NSIINTERFACEREQUESTOR |
|
193 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK |
|
194 |
|
195 // Sets properties for this image; will dispatch to main thread if needed. |
|
196 void SetProperties(nsIChannel* aChan); |
|
197 |
|
198 private: |
|
199 friend class imgMemoryReporter; |
|
200 |
|
201 // Weak reference to parent loader; this request cannot outlive its owner. |
|
202 imgLoader* mLoader; |
|
203 nsCOMPtr<nsIRequest> mRequest; |
|
204 // The original URI we were loaded with. This is the same as the URI we are |
|
205 // keyed on in the cache. We store a string here to avoid off main thread |
|
206 // refcounting issues with nsStandardURL. |
|
207 nsRefPtr<ImageURL> mURI; |
|
208 // The URI of the resource we ended up loading after all redirects, etc. |
|
209 nsCOMPtr<nsIURI> mCurrentURI; |
|
210 // The first party that triggered the load -- for cookie + cache isolation |
|
211 nsCOMPtr<nsIURI> mFirstPartyIsolationURI; |
|
212 // The principal of the document which loaded this image. Used when validating for CORS. |
|
213 nsCOMPtr<nsIPrincipal> mLoadingPrincipal; |
|
214 // The principal of this image. |
|
215 nsCOMPtr<nsIPrincipal> mPrincipal; |
|
216 // Status-tracker -- transferred to mImage, when it gets instantiated |
|
217 nsRefPtr<imgStatusTracker> mStatusTracker; |
|
218 nsRefPtr<mozilla::image::Image> mImage; |
|
219 nsCOMPtr<nsIProperties> mProperties; |
|
220 nsCOMPtr<nsISupports> mSecurityInfo; |
|
221 nsCOMPtr<nsIChannel> mChannel; |
|
222 nsCOMPtr<nsIInterfaceRequestor> mPrevChannelSink; |
|
223 nsCOMPtr<nsIApplicationCache> mApplicationCache; |
|
224 |
|
225 nsCOMPtr<nsITimedChannel> mTimedChannel; |
|
226 |
|
227 nsCString mContentType; |
|
228 |
|
229 nsRefPtr<imgCacheEntry> mCacheEntry; /* we hold on to this to this so long as we have observers */ |
|
230 |
|
231 void *mLoadId; |
|
232 |
|
233 imgCacheValidator *mValidator; |
|
234 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback; |
|
235 nsCOMPtr<nsIChannel> mNewRedirectChannel; |
|
236 |
|
237 // The ID of the inner window origin, used for error reporting. |
|
238 uint64_t mInnerWindowId; |
|
239 |
|
240 // The CORS mode (defined in imgIRequest) this image was loaded with. By |
|
241 // default, imgIRequest::CORS_NONE. |
|
242 int32_t mCORSMode; |
|
243 |
|
244 // Sometimes consumers want to do things before the image is ready. Let them, |
|
245 // and apply the action when the image becomes available. |
|
246 bool mDecodeRequested : 1; |
|
247 |
|
248 bool mIsMultiPartChannel : 1; |
|
249 bool mGotData : 1; |
|
250 bool mIsInCache : 1; |
|
251 bool mBlockingOnload : 1; |
|
252 bool mResniffMimeType : 1; |
|
253 }; |
|
254 |
|
255 #endif |