Bug 1586798 - Use WalkerFront from the currently selected element in onTagEdit()...
[gecko.git] / image / imgRequest.h
blob970561ccf3efdf485fdc3e41a97ef65fff67f20c
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;
52 typedef mozilla::dom::ReferrerPolicy ReferrerPolicy;
54 public:
55 imgRequest(imgLoader* aLoader, const ImageCacheKey& aCacheKey);
57 NS_DECL_THREADSAFE_ISUPPORTS
58 NS_DECL_NSISTREAMLISTENER
59 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
60 NS_DECL_NSIREQUESTOBSERVER
61 NS_DECL_NSICHANNELEVENTSINK
62 NS_DECL_NSIINTERFACEREQUESTOR
63 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
65 MOZ_MUST_USE nsresult Init(nsIURI* aURI, nsIURI* aFinalURI,
66 bool aHadInsecureRedirect, nsIRequest* aRequest,
67 nsIChannel* aChannel, imgCacheEntry* aCacheEntry,
68 nsISupports* aCX,
69 nsIPrincipal* aTriggeringPrincipal,
70 int32_t aCORSMode, nsIReferrerInfo* aReferrerInfo);
72 void ClearLoader();
74 // Callers must call imgRequestProxy::Notify later.
75 void AddProxy(imgRequestProxy* proxy);
77 nsresult RemoveProxy(imgRequestProxy* proxy, nsresult aStatus);
79 // Cancel, but also ensure that all work done in Init() is undone. Call this
80 // only when the channel has failed to open, and so calling Cancel() on it
81 // won't be sufficient.
82 void CancelAndAbort(nsresult aStatus);
84 // Called or dispatched by cancel for main thread only execution.
85 void ContinueCancel(nsresult aStatus);
87 // Called or dispatched by EvictFromCache for main thread only execution.
88 void ContinueEvict();
90 // Request that we start decoding the image as soon as data becomes available.
91 void StartDecoding();
93 inline uint64_t InnerWindowID() const { return mInnerWindowId; }
94 void SetInnerWindowID(uint64_t aInnerWindowId) {
95 mInnerWindowId = aInnerWindowId;
98 // Set the cache validation information (expiry time, whether we must
99 // validate, etc) on the cache entry based on the request information.
100 // If this function is called multiple times, the information set earliest
101 // wins.
102 static void SetCacheValidation(imgCacheEntry* aEntry, nsIRequest* aRequest);
104 // Check if application cache of the original load is different from
105 // application cache of the new load. Also lack of application cache
106 // on one of the loads is considered a change of a loading cache since
107 // HTTP cache may contain a different data then app cache.
108 bool CacheChanged(nsIRequest* aNewRequest);
110 bool GetMultipart() const;
112 // Returns whether we went through an insecure (non-HTTPS) redirect at some
113 // point during loading. This does not consider the final URI.
114 bool HadInsecureRedirect() const;
116 // The CORS mode for which we loaded this image.
117 int32_t GetCORSMode() const { return mCORSMode; }
119 // The ReferrerInfo in effect when loading this image.
120 nsIReferrerInfo* GetReferrerInfo() const { return mReferrerInfo; }
122 // The principal for the document that loaded this image. Used when trying to
123 // validate a CORS image load.
124 already_AddRefed<nsIPrincipal> GetTriggeringPrincipal() const {
125 nsCOMPtr<nsIPrincipal> principal = mTriggeringPrincipal;
126 return principal.forget();
129 // Return the ProgressTracker associated with this imgRequest. It may live
130 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
131 // mImage has been instantiated yet.
132 already_AddRefed<ProgressTracker> GetProgressTracker() const;
134 /// Returns the Image associated with this imgRequest, if it's ready.
135 already_AddRefed<Image> GetImage() const;
137 // Get the current principal of the image. No AddRefing.
138 inline nsIPrincipal* GetPrincipal() const { return mPrincipal.get(); }
140 /// Get the ImageCacheKey associated with this request.
141 const ImageCacheKey& CacheKey() const { return mCacheKey; }
143 // Resize the cache entry to 0 if it exists
144 void ResetCacheEntry();
146 // OK to use on any thread.
147 nsresult GetURI(nsIURI** aURI);
148 nsresult GetFinalURI(nsIURI** aURI);
149 bool IsChrome() const;
150 bool IsData() const;
152 nsresult GetImageErrorCode(void);
154 /// Returns a non-owning pointer to this imgRequest's MIME type.
155 const char* GetMimeType() const { return mContentType.get(); }
157 /// @return the priority of the underlying network request, or
158 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
159 int32_t Priority() const;
161 /// Adjust the priority of the underlying network request by @aDelta on behalf
162 /// of @aProxy.
163 void AdjustPriority(imgRequestProxy* aProxy, int32_t aDelta);
165 void BoostPriority(uint32_t aCategory);
167 /// Returns a weak pointer to the underlying request.
168 nsIRequest* GetRequest() const { return mRequest; }
170 nsITimedChannel* GetTimedChannel() const { return mTimedChannel; }
172 imgCacheValidator* GetValidator() const { return mValidator; }
173 void SetValidator(imgCacheValidator* aValidator) { mValidator = aValidator; }
175 void* LoadId() const { return mLoadId; }
176 void SetLoadId(void* aLoadId) { mLoadId = aLoadId; }
178 /// Reset the cache entry after we've dropped our reference to it. Used by
179 /// imgLoader when our cache entry is re-requested after we've dropped our
180 /// reference to it.
181 void SetCacheEntry(imgCacheEntry* aEntry);
183 /// Returns whether we've got a reference to the cache entry.
184 bool HasCacheEntry() const;
186 /// Set whether this request is stored in the cache. If it isn't, regardless
187 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
188 /// try to update or modify the image cache.
189 void SetIsInCache(bool aCacheable);
191 void EvictFromCache();
192 void RemoveFromCache();
194 // Sets properties for this image; will dispatch to main thread if needed.
195 void SetProperties(const nsACString& aContentType,
196 const nsACString& aContentDisposition);
198 nsIProperties* Properties() const { return mProperties; }
200 bool HasConsumers() const;
202 bool ImageAvailable() const;
204 private:
205 friend class FinishPreparingForNewPartRunnable;
207 virtual ~imgRequest();
209 void FinishPreparingForNewPart(const NewPartResult& aResult);
211 void Cancel(nsresult aStatus);
213 // Update the cache entry size based on the image container.
214 void UpdateCacheEntrySize();
216 /// Returns true if StartDecoding() was called.
217 bool IsDecodeRequested() const;
219 void AdjustPriorityInternal(int32_t aDelta);
221 // Weak reference to parent loader; this request cannot outlive its owner.
222 imgLoader* mLoader;
223 nsCOMPtr<nsIRequest> mRequest;
224 // The original URI we were loaded with. This is the same as the URI we are
225 // keyed on in the cache. We store a string here to avoid off main thread
226 // refcounting issues with nsStandardURL.
227 nsCOMPtr<nsIURI> mURI;
228 // The URI of the resource we ended up loading after all redirects, etc.
229 nsCOMPtr<nsIURI> mFinalURI;
230 // The principal which triggered the load of this image. Generally either
231 // the principal of the document the image is being loaded into, or of the
232 // stylesheet which specified the image to load. Used when validating for
233 // CORS.
234 nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
235 // The principal of this image.
236 nsCOMPtr<nsIPrincipal> mPrincipal;
237 nsCOMPtr<nsIProperties> mProperties;
238 nsCOMPtr<nsIChannel> mChannel;
239 nsCOMPtr<nsIInterfaceRequestor> mPrevChannelSink;
240 nsCOMPtr<nsIApplicationCache> mApplicationCache;
242 nsCOMPtr<nsITimedChannel> mTimedChannel;
244 nsCString mContentType;
246 /* we hold on to this to this so long as we have observers */
247 RefPtr<imgCacheEntry> mCacheEntry;
249 /// The key under which this imgRequest is stored in the image cache.
250 ImageCacheKey mCacheKey;
252 void* mLoadId;
254 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
255 /// pointer comparisons; there's no guarantee this will remain valid.
256 void* mFirstProxy;
258 imgCacheValidator* mValidator;
259 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
260 nsCOMPtr<nsIChannel> mNewRedirectChannel;
262 // The ID of the inner window origin, used for error reporting.
263 uint64_t mInnerWindowId;
265 // The CORS mode (defined in imgIRequest) this image was loaded with. By
266 // default, imgIRequest::CORS_NONE.
267 int32_t mCORSMode;
269 // The ReferrerInfo used for this image.
270 nsCOMPtr<nsIReferrerInfo> mReferrerInfo;
272 nsresult mImageErrorCode;
274 // The categories of prioritization strategy that have been requested.
275 uint32_t mBoostCategoriesRequested = 0;
277 // If we've called OnImageAvailable.
278 bool mImageAvailable;
280 mutable mozilla::Mutex mMutex;
282 // Member variables protected by mMutex. Note that *all* flags in our bitfield
283 // are protected by mMutex; if you're adding a new flag that isn'protected, it
284 // must not be a part of this bitfield.
285 RefPtr<ProgressTracker> mProgressTracker;
286 RefPtr<Image> mImage;
287 bool mIsMultiPartChannel : 1;
288 bool mIsInCache : 1;
289 bool mDecodeRequested : 1;
290 bool mNewPartPending : 1;
291 bool mHadInsecureRedirect : 1;
294 #endif // mozilla_image_imgRequest_h