Bug 1568151 - Replace `target.getInspector()` by `target.getFront("inspector")`....
[gecko.git] / image / imgRequest.h
blobb8302cdaa2fef602e58784eee95bc0b71561efea
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
7 #ifndef mozilla_image_imgRequest_h
8 #define mozilla_image_imgRequest_h
10 #include "nsIChannelEventSink.h"
11 #include "nsIInterfaceRequestor.h"
12 #include "nsIStreamListener.h"
13 #include "nsIThreadRetargetableStreamListener.h"
14 #include "nsIPrincipal.h"
16 #include "nsCOMPtr.h"
17 #include "nsProxyRelease.h"
18 #include "nsString.h"
19 #include "nsError.h"
20 #include "nsIAsyncVerifyRedirectCallback.h"
21 #include "mozilla/Mutex.h"
22 #include "ImageCacheKey.h"
24 class imgCacheValidator;
25 class imgLoader;
26 class imgRequestProxy;
27 class imgCacheEntry;
28 class nsIApplicationCache;
29 class nsIProperties;
30 class nsIRequest;
31 class nsITimedChannel;
32 class nsIURI;
33 class nsIReferrerInfo;
35 namespace mozilla {
36 namespace image {
37 class Image;
38 class ProgressTracker;
39 } // namespace image
40 } // namespace mozilla
42 struct NewPartResult;
44 class imgRequest final : public nsIStreamListener,
45 public nsIThreadRetargetableStreamListener,
46 public nsIChannelEventSink,
47 public nsIInterfaceRequestor,
48 public nsIAsyncVerifyRedirectCallback {
49 typedef mozilla::image::Image Image;
50 typedef mozilla::image::ImageCacheKey ImageCacheKey;
51 typedef mozilla::image::ProgressTracker ProgressTracker;
53 public:
54 imgRequest(imgLoader* aLoader, const ImageCacheKey& aCacheKey);
56 NS_DECL_THREADSAFE_ISUPPORTS
57 NS_DECL_NSISTREAMLISTENER
58 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
59 NS_DECL_NSIREQUESTOBSERVER
60 NS_DECL_NSICHANNELEVENTSINK
61 NS_DECL_NSIINTERFACEREQUESTOR
62 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
64 MOZ_MUST_USE nsresult Init(nsIURI* aURI, nsIURI* aFinalURI,
65 bool aHadInsecureRedirect, nsIRequest* aRequest,
66 nsIChannel* aChannel, imgCacheEntry* aCacheEntry,
67 nsISupports* aCX,
68 nsIPrincipal* aTriggeringPrincipal,
69 int32_t aCORSMode, nsIReferrerInfo* aReferrerInfo);
71 void ClearLoader();
73 // Callers must call imgRequestProxy::Notify later.
74 void AddProxy(imgRequestProxy* proxy);
76 nsresult RemoveProxy(imgRequestProxy* proxy, nsresult aStatus);
78 // Cancel, but also ensure that all work done in Init() is undone. Call this
79 // only when the channel has failed to open, and so calling Cancel() on it
80 // won't be sufficient.
81 void CancelAndAbort(nsresult aStatus);
83 // Called or dispatched by cancel for main thread only execution.
84 void ContinueCancel(nsresult aStatus);
86 // Called or dispatched by EvictFromCache for main thread only execution.
87 void ContinueEvict();
89 // Request that we start decoding the image as soon as data becomes available.
90 void StartDecoding();
92 inline uint64_t InnerWindowID() const { return mInnerWindowId; }
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);
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);
106 bool GetMultipart() const;
108 // Returns whether we went through an insecure (non-HTTPS) redirect at some
109 // point during loading. This does not consider the final URI.
110 bool HadInsecureRedirect() const;
112 // The CORS mode for which we loaded this image.
113 int32_t GetCORSMode() const { return mCORSMode; }
115 // The ReferrerInfo in effect when loading this image.
116 nsIReferrerInfo* GetReferrerInfo() const { return mReferrerInfo; }
118 // The principal for the document that loaded this image. Used when trying to
119 // validate a CORS image load.
120 already_AddRefed<nsIPrincipal> GetTriggeringPrincipal() const {
121 nsCOMPtr<nsIPrincipal> principal = mTriggeringPrincipal;
122 return principal.forget();
125 // Return the ProgressTracker associated with this imgRequest. It may live
126 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
127 // mImage has been instantiated yet.
128 already_AddRefed<ProgressTracker> GetProgressTracker() const;
130 /// Returns the Image associated with this imgRequest, if it's ready.
131 already_AddRefed<Image> GetImage() const;
133 // Get the current principal of the image. No AddRefing.
134 inline nsIPrincipal* GetPrincipal() const { return mPrincipal.get(); }
136 /// Get the ImageCacheKey associated with this request.
137 const ImageCacheKey& CacheKey() const { return mCacheKey; }
139 // Resize the cache entry to 0 if it exists
140 void ResetCacheEntry();
142 // OK to use on any thread.
143 nsresult GetURI(nsIURI** aURI);
144 nsresult GetFinalURI(nsIURI** aURI);
145 bool IsScheme(const char* aScheme) const;
146 bool IsChrome() const;
147 bool IsData() const;
149 nsresult GetImageErrorCode(void);
151 /// Returns a non-owning pointer to this imgRequest's MIME type.
152 const char* GetMimeType() const { return mContentType.get(); }
154 /// @return the priority of the underlying network request, or
155 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
156 int32_t Priority() const;
158 /// Adjust the priority of the underlying network request by @aDelta on behalf
159 /// of @aProxy.
160 void AdjustPriority(imgRequestProxy* aProxy, int32_t aDelta);
162 void BoostPriority(uint32_t aCategory);
164 /// Returns a weak pointer to the underlying request.
165 nsIRequest* GetRequest() const { return mRequest; }
167 nsITimedChannel* GetTimedChannel() const { return mTimedChannel; }
169 imgCacheValidator* GetValidator() const { return mValidator; }
170 void SetValidator(imgCacheValidator* aValidator) { mValidator = aValidator; }
172 void* LoadId() const { return mLoadId; }
173 void SetLoadId(void* aLoadId) { mLoadId = aLoadId; }
175 /// Reset the cache entry after we've dropped our reference to it. Used by
176 /// imgLoader when our cache entry is re-requested after we've dropped our
177 /// reference to it.
178 void SetCacheEntry(imgCacheEntry* aEntry);
180 /// Returns whether we've got a reference to the cache entry.
181 bool HasCacheEntry() const;
183 /// Set whether this request is stored in the cache. If it isn't, regardless
184 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
185 /// try to update or modify the image cache.
186 void SetIsInCache(bool aCacheable);
188 void EvictFromCache();
189 void RemoveFromCache();
191 // Sets properties for this image; will dispatch to main thread if needed.
192 void SetProperties(const nsACString& aContentType,
193 const nsACString& aContentDisposition);
195 nsIProperties* Properties() const { return mProperties; }
197 bool HasConsumers() const;
199 bool ImageAvailable() const;
201 private:
202 friend class FinishPreparingForNewPartRunnable;
204 virtual ~imgRequest();
206 void FinishPreparingForNewPart(const NewPartResult& aResult);
208 void Cancel(nsresult aStatus);
210 // Update the cache entry size based on the image container.
211 void UpdateCacheEntrySize();
213 /// Returns true if StartDecoding() was called.
214 bool IsDecodeRequested() const;
216 void AdjustPriorityInternal(int32_t aDelta);
218 // Weak reference to parent loader; this request cannot outlive its owner.
219 imgLoader* mLoader;
220 nsCOMPtr<nsIRequest> mRequest;
221 // The original URI we were loaded with. This is the same as the URI we are
222 // keyed on in the cache. We store a string here to avoid off main thread
223 // refcounting issues with nsStandardURL.
224 nsCOMPtr<nsIURI> mURI;
225 // The URI of the resource we ended up loading after all redirects, etc.
226 nsCOMPtr<nsIURI> mFinalURI;
227 // The principal which triggered the load of this image. Generally either
228 // the principal of the document the image is being loaded into, or of the
229 // stylesheet which specified the image to load. Used when validating for
230 // CORS.
231 nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
232 // The principal of this image.
233 nsCOMPtr<nsIPrincipal> mPrincipal;
234 nsCOMPtr<nsIProperties> mProperties;
235 nsCOMPtr<nsIChannel> mChannel;
236 nsCOMPtr<nsIInterfaceRequestor> mPrevChannelSink;
237 nsCOMPtr<nsIApplicationCache> mApplicationCache;
239 nsCOMPtr<nsITimedChannel> mTimedChannel;
241 nsCString mContentType;
243 /* we hold on to this to this so long as we have observers */
244 RefPtr<imgCacheEntry> mCacheEntry;
246 /// The key under which this imgRequest is stored in the image cache.
247 ImageCacheKey mCacheKey;
249 void* mLoadId;
251 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
252 /// pointer comparisons; there's no guarantee this will remain valid.
253 void* mFirstProxy;
255 imgCacheValidator* mValidator;
256 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
257 nsCOMPtr<nsIChannel> mNewRedirectChannel;
259 // The ID of the inner window origin, used for error reporting.
260 uint64_t mInnerWindowId;
262 // The CORS mode (defined in imgIRequest) this image was loaded with. By
263 // default, imgIRequest::CORS_NONE.
264 int32_t mCORSMode;
266 // The ReferrerInfo used for this image.
267 nsCOMPtr<nsIReferrerInfo> mReferrerInfo;
269 nsresult mImageErrorCode;
271 // The categories of prioritization strategy that have been requested.
272 uint32_t mBoostCategoriesRequested = 0;
274 // If we've called OnImageAvailable.
275 bool mImageAvailable;
277 mutable mozilla::Mutex mMutex;
279 // Member variables protected by mMutex. Note that *all* flags in our bitfield
280 // are protected by mMutex; if you're adding a new flag that isn'protected, it
281 // must not be a part of this bitfield.
282 RefPtr<ProgressTracker> mProgressTracker;
283 RefPtr<Image> mImage;
284 bool mIsMultiPartChannel : 1;
285 bool mIsInCache : 1;
286 bool mDecodeRequested : 1;
287 bool mNewPartPending : 1;
288 bool mHadInsecureRedirect : 1;
291 #endif // mozilla_image_imgRequest_h