image/src/imgRequest.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/src/imgRequest.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,255 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef imgRequest_h__
    1.11 +#define imgRequest_h__
    1.12 +
    1.13 +#include "nsIChannelEventSink.h"
    1.14 +#include "nsIInterfaceRequestor.h"
    1.15 +#include "nsIStreamListener.h"
    1.16 +#include "nsIThreadRetargetableStreamListener.h"
    1.17 +#include "nsIPrincipal.h"
    1.18 +
    1.19 +#include "nsAutoPtr.h"
    1.20 +#include "nsCOMPtr.h"
    1.21 +#include "nsProxyRelease.h"
    1.22 +#include "nsStringGlue.h"
    1.23 +#include "nsError.h"
    1.24 +#include "nsIAsyncVerifyRedirectCallback.h"
    1.25 +
    1.26 +class imgCacheValidator;
    1.27 +class imgStatusTracker;
    1.28 +class imgLoader;
    1.29 +class imgRequestProxy;
    1.30 +class imgCacheEntry;
    1.31 +class imgMemoryReporter;
    1.32 +class imgRequestNotifyRunnable;
    1.33 +class nsIApplicationCache;
    1.34 +class nsIProperties;
    1.35 +class nsIRequest;
    1.36 +class nsITimedChannel;
    1.37 +class nsIURI;
    1.38 +
    1.39 +namespace mozilla {
    1.40 +namespace image {
    1.41 +class Image;
    1.42 +class ImageURL;
    1.43 +} // namespace image
    1.44 +} // namespace mozilla
    1.45 +
    1.46 +class imgRequest : public nsIStreamListener,
    1.47 +                   public nsIThreadRetargetableStreamListener,
    1.48 +                   public nsIChannelEventSink,
    1.49 +                   public nsIInterfaceRequestor,
    1.50 +                   public nsIAsyncVerifyRedirectCallback
    1.51 +{
    1.52 +public:
    1.53 +  typedef mozilla::image::ImageURL ImageURL;
    1.54 +  imgRequest(imgLoader* aLoader);
    1.55 +  virtual ~imgRequest();
    1.56 +
    1.57 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.58 +
    1.59 +  nsresult Init(nsIURI *aURI,
    1.60 +                nsIURI *aCurrentURI,
    1.61 +                nsIURI *aFirstPartyIsolationURI,
    1.62 +                nsIRequest *aRequest,
    1.63 +                nsIChannel *aChannel,
    1.64 +                imgCacheEntry *aCacheEntry,
    1.65 +                void *aLoadId,
    1.66 +                nsIPrincipal* aLoadingPrincipal,
    1.67 +                int32_t aCORSMode);
    1.68 +
    1.69 +  // Callers must call imgRequestProxy::Notify later.
    1.70 +  void AddProxy(imgRequestProxy *proxy);
    1.71 +
    1.72 +  nsresult RemoveProxy(imgRequestProxy *proxy, nsresult aStatus);
    1.73 +
    1.74 +  // Cancel, but also ensure that all work done in Init() is undone. Call this
    1.75 +  // only when the channel has failed to open, and so calling Cancel() on it
    1.76 +  // won't be sufficient.
    1.77 +  void CancelAndAbort(nsresult aStatus);
    1.78 +
    1.79 +  // Called or dispatched by cancel for main thread only execution.
    1.80 +  void ContinueCancel(nsresult aStatus);
    1.81 +
    1.82 +  // Methods that get forwarded to the Image, or deferred until it's
    1.83 +  // instantiated.
    1.84 +  nsresult LockImage();
    1.85 +  nsresult UnlockImage();
    1.86 +  nsresult StartDecoding();
    1.87 +  nsresult RequestDecode();
    1.88 +
    1.89 +  inline void SetInnerWindowID(uint64_t aInnerWindowId) {
    1.90 +    mInnerWindowId = aInnerWindowId;
    1.91 +  }
    1.92 +
    1.93 +  inline uint64_t InnerWindowID() const {
    1.94 +    return mInnerWindowId;
    1.95 +  }
    1.96 +
    1.97 +  // Set the cache validation information (expiry time, whether we must
    1.98 +  // validate, etc) on the cache entry based on the request information.
    1.99 +  // If this function is called multiple times, the information set earliest
   1.100 +  // wins.
   1.101 +  static void SetCacheValidation(imgCacheEntry* aEntry, nsIRequest* aRequest);
   1.102 +
   1.103 +  // Check if application cache of the original load is different from
   1.104 +  // application cache of the new load.  Also lack of application cache
   1.105 +  // on one of the loads is considered a change of a loading cache since
   1.106 +  // HTTP cache may contain a different data then app cache.
   1.107 +  bool CacheChanged(nsIRequest* aNewRequest);
   1.108 +
   1.109 +  bool GetMultipart() const { return mIsMultiPartChannel; }
   1.110 +
   1.111 +  // The CORS mode for which we loaded this image.
   1.112 +  int32_t GetCORSMode() const { return mCORSMode; }
   1.113 +
   1.114 +  // The principal for the document that loaded this image. Used when trying to
   1.115 +  // validate a CORS image load.
   1.116 +  already_AddRefed<nsIPrincipal> GetLoadingPrincipal() const
   1.117 +  {
   1.118 +    nsCOMPtr<nsIPrincipal> principal = mLoadingPrincipal;
   1.119 +    return principal.forget();
   1.120 +  }
   1.121 +
   1.122 +  // Return the imgStatusTracker associated with this imgRequest. It may live
   1.123 +  // in |mStatusTracker| or in |mImage.mStatusTracker|, depending on whether
   1.124 +  // mImage has been instantiated yet.
   1.125 +  already_AddRefed<imgStatusTracker> GetStatusTracker();
   1.126 +
   1.127 +  // Get the current principal of the image. No AddRefing.
   1.128 +  inline nsIPrincipal* GetPrincipal() const { return mPrincipal.get(); }
   1.129 +
   1.130 +  // Resize the cache entry to 0 if it exists
   1.131 +  void ResetCacheEntry();
   1.132 +
   1.133 +  // Update the cache entry size based on the image container
   1.134 +  void UpdateCacheEntrySize();
   1.135 +
   1.136 +  // OK to use on any thread.
   1.137 +  nsresult GetURI(ImageURL **aURI);
   1.138 +
   1.139 +private:
   1.140 +  friend class imgCacheEntry;
   1.141 +  friend class imgRequestProxy;
   1.142 +  friend class imgLoader;
   1.143 +  friend class imgCacheValidator;
   1.144 +  friend class imgStatusTracker;
   1.145 +  friend class imgCacheExpirationTracker;
   1.146 +  friend class imgRequestNotifyRunnable;
   1.147 +
   1.148 +  inline void SetLoadId(void *aLoadId) {
   1.149 +    mLoadId = aLoadId;
   1.150 +  }
   1.151 +  void Cancel(nsresult aStatus);
   1.152 +  void RemoveFromCache();
   1.153 +
   1.154 +  nsresult GetSecurityInfo(nsISupports **aSecurityInfo);
   1.155 +
   1.156 +  inline const char *GetMimeType() const {
   1.157 +    return mContentType.get();
   1.158 +  }
   1.159 +  inline nsIProperties *Properties() {
   1.160 +    return mProperties;
   1.161 +  }
   1.162 +
   1.163 +  // Reset the cache entry after we've dropped our reference to it. Used by the
   1.164 +  // imgLoader when our cache entry is re-requested after we've dropped our
   1.165 +  // reference to it.
   1.166 +  void SetCacheEntry(imgCacheEntry *entry);
   1.167 +
   1.168 +  // Returns whether we've got a reference to the cache entry.
   1.169 +  bool HasCacheEntry() const;
   1.170 +
   1.171 +  // Return the priority of the underlying network request, or return
   1.172 +  // PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
   1.173 +  int32_t Priority() const;
   1.174 +
   1.175 +  // Adjust the priority of the underlying network request by the given delta
   1.176 +  // on behalf of the given proxy.
   1.177 +  void AdjustPriority(imgRequestProxy *aProxy, int32_t aDelta);
   1.178 +
   1.179 +  // Return whether we've seen some data at this point
   1.180 +  bool HasTransferredData() const { return mGotData; }
   1.181 +
   1.182 +  // Set whether this request is stored in the cache. If it isn't, regardless
   1.183 +  // of whether this request has a non-null mCacheEntry, this imgRequest won't
   1.184 +  // try to update or modify the image cache.
   1.185 +  void SetIsInCache(bool cacheable);
   1.186 +
   1.187 +  bool IsBlockingOnload() const;
   1.188 +  void SetBlockingOnload(bool block) const;
   1.189 +
   1.190 +public:
   1.191 +  NS_DECL_NSISTREAMLISTENER
   1.192 +  NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
   1.193 +  NS_DECL_NSIREQUESTOBSERVER
   1.194 +  NS_DECL_NSICHANNELEVENTSINK
   1.195 +  NS_DECL_NSIINTERFACEREQUESTOR
   1.196 +  NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
   1.197 +
   1.198 +  // Sets properties for this image; will dispatch to main thread if needed.
   1.199 +  void SetProperties(nsIChannel* aChan);
   1.200 +
   1.201 +private:
   1.202 +  friend class imgMemoryReporter;
   1.203 +
   1.204 +  // Weak reference to parent loader; this request cannot outlive its owner.
   1.205 +  imgLoader* mLoader;
   1.206 +  nsCOMPtr<nsIRequest> mRequest;
   1.207 +  // The original URI we were loaded with. This is the same as the URI we are
   1.208 +  // keyed on in the cache. We store a string here to avoid off main thread
   1.209 +  // refcounting issues with nsStandardURL.
   1.210 +  nsRefPtr<ImageURL> mURI;
   1.211 +  // The URI of the resource we ended up loading after all redirects, etc.
   1.212 +  nsCOMPtr<nsIURI> mCurrentURI;
   1.213 +  // The first party that triggered the load -- for cookie + cache isolation
   1.214 +  nsCOMPtr<nsIURI> mFirstPartyIsolationURI;
   1.215 +  // The principal of the document which loaded this image. Used when validating for CORS.
   1.216 +  nsCOMPtr<nsIPrincipal> mLoadingPrincipal;
   1.217 +  // The principal of this image.
   1.218 +  nsCOMPtr<nsIPrincipal> mPrincipal;
   1.219 +  // Status-tracker -- transferred to mImage, when it gets instantiated
   1.220 +  nsRefPtr<imgStatusTracker> mStatusTracker;
   1.221 +  nsRefPtr<mozilla::image::Image> mImage;
   1.222 +  nsCOMPtr<nsIProperties> mProperties;
   1.223 +  nsCOMPtr<nsISupports> mSecurityInfo;
   1.224 +  nsCOMPtr<nsIChannel> mChannel;
   1.225 +  nsCOMPtr<nsIInterfaceRequestor> mPrevChannelSink;
   1.226 +  nsCOMPtr<nsIApplicationCache> mApplicationCache;
   1.227 +
   1.228 +  nsCOMPtr<nsITimedChannel> mTimedChannel;
   1.229 +
   1.230 +  nsCString mContentType;
   1.231 +
   1.232 +  nsRefPtr<imgCacheEntry> mCacheEntry; /* we hold on to this to this so long as we have observers */
   1.233 +
   1.234 +  void *mLoadId;
   1.235 +
   1.236 +  imgCacheValidator *mValidator;
   1.237 +  nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
   1.238 +  nsCOMPtr<nsIChannel> mNewRedirectChannel;
   1.239 +
   1.240 +  // The ID of the inner window origin, used for error reporting.
   1.241 +  uint64_t mInnerWindowId;
   1.242 +
   1.243 +  // The CORS mode (defined in imgIRequest) this image was loaded with. By
   1.244 +  // default, imgIRequest::CORS_NONE.
   1.245 +  int32_t mCORSMode;
   1.246 +
   1.247 +  // Sometimes consumers want to do things before the image is ready. Let them,
   1.248 +  // and apply the action when the image becomes available.
   1.249 +  bool mDecodeRequested : 1;
   1.250 +
   1.251 +  bool mIsMultiPartChannel : 1;
   1.252 +  bool mGotData : 1;
   1.253 +  bool mIsInCache : 1;
   1.254 +  bool mBlockingOnload : 1;
   1.255 +  bool mResniffMimeType : 1;
   1.256 +};
   1.257 +
   1.258 +#endif

mercurial