1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2001
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Stuart Parmenter <pavlov@netscape.com>
25 : * Bobby Holley <bobbyholley@gmail.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef imgRequest_h__
42 : #define imgRequest_h__
43 :
44 : #include "imgIDecoderObserver.h"
45 :
46 : #include "nsIChannelEventSink.h"
47 : #include "nsIContentSniffer.h"
48 : #include "nsIInterfaceRequestor.h"
49 : #include "nsIRequest.h"
50 : #include "nsIProperties.h"
51 : #include "nsIStreamListener.h"
52 : #include "nsIURI.h"
53 : #include "nsIPrincipal.h"
54 : #include "nsITimedChannel.h"
55 :
56 : #include "nsCategoryCache.h"
57 : #include "nsCOMPtr.h"
58 : #include "nsString.h"
59 : #include "nsTObserverArray.h"
60 : #include "nsWeakReference.h"
61 : #include "ImageErrors.h"
62 : #include "imgIRequest.h"
63 : #include "imgStatusTracker.h"
64 : #include "nsIAsyncVerifyRedirectCallback.h"
65 :
66 : class imgCacheValidator;
67 :
68 : class imgRequestProxy;
69 : class imgCacheEntry;
70 : class imgMemoryReporter;
71 : class imgRequestNotifyRunnable;
72 :
73 : namespace mozilla {
74 : namespace image {
75 : class Image;
76 : } // namespace image
77 : } // namespace mozilla
78 :
79 : class imgRequest : public imgIDecoderObserver,
80 : public nsIStreamListener,
81 : public nsSupportsWeakReference,
82 : public nsIChannelEventSink,
83 : public nsIInterfaceRequestor,
84 : public nsIAsyncVerifyRedirectCallback
85 : {
86 : public:
87 : imgRequest();
88 : virtual ~imgRequest();
89 :
90 : NS_DECL_ISUPPORTS
91 :
92 : nsresult Init(nsIURI *aURI,
93 : nsIURI *aCurrentURI,
94 : nsIRequest *aRequest,
95 : nsIChannel *aChannel,
96 : imgCacheEntry *aCacheEntry,
97 : void *aLoadId,
98 : nsIPrincipal* aLoadingPrincipal,
99 : PRInt32 aCORSMode);
100 :
101 : // Callers must call imgRequestProxy::Notify later.
102 : nsresult AddProxy(imgRequestProxy *proxy);
103 :
104 : // aNotify==false still sends OnStopRequest.
105 : nsresult RemoveProxy(imgRequestProxy *proxy, nsresult aStatus, bool aNotify);
106 :
107 : void SniffMimeType(const char *buf, PRUint32 len);
108 :
109 : // Cancel, but also ensure that all work done in Init() is undone. Call this
110 : // only when the channel has failed to open, and so calling Cancel() on it
111 : // won't be sufficient.
112 : void CancelAndAbort(nsresult aStatus);
113 :
114 : // Methods that get forwarded to the Image, or deferred until it's
115 : // instantiated.
116 : nsresult LockImage();
117 : nsresult UnlockImage();
118 : nsresult RequestDecode();
119 :
120 0 : inline void SetInnerWindowID(PRUint64 aInnerWindowId) {
121 0 : mInnerWindowId = aInnerWindowId;
122 0 : }
123 :
124 : inline PRUint64 InnerWindowID() const {
125 : return mInnerWindowId;
126 : }
127 :
128 : // Set the cache validation information (expiry time, whether we must
129 : // validate, etc) on the cache entry based on the request information.
130 : // If this function is called multiple times, the information set earliest
131 : // wins.
132 : static void SetCacheValidation(imgCacheEntry* aEntry, nsIRequest* aRequest);
133 :
134 : // The CORS mode for which we loaded this image.
135 14 : PRInt32 GetCORSMode() const { return mCORSMode; }
136 :
137 : // The principal for the document that loaded this image. Used when trying to
138 : // validate a CORS image load.
139 6 : already_AddRefed<nsIPrincipal> GetLoadingPrincipal() const
140 : {
141 12 : nsCOMPtr<nsIPrincipal> principal = mLoadingPrincipal;
142 6 : return principal.forget();
143 : }
144 :
145 : private:
146 : friend class imgCacheEntry;
147 : friend class imgRequestProxy;
148 : friend class imgLoader;
149 : friend class imgCacheValidator;
150 : friend class imgStatusTracker;
151 : friend class imgCacheExpirationTracker;
152 : friend class imgRequestNotifyRunnable;
153 :
154 17 : inline void SetLoadId(void *aLoadId) {
155 17 : mLoadId = aLoadId;
156 17 : }
157 : void Cancel(nsresult aStatus);
158 : void RemoveFromCache();
159 :
160 : nsresult GetURI(nsIURI **aURI);
161 : nsresult GetSecurityInfo(nsISupports **aSecurityInfo);
162 :
163 0 : inline const char *GetMimeType() const {
164 0 : return mContentType.get();
165 : }
166 0 : inline nsIProperties *Properties() {
167 0 : return mProperties;
168 : }
169 :
170 : // Return the imgStatusTracker associated with this imgRequest. It may live
171 : // in |mStatusTracker| or in |mImage.mStatusTracker|, depending on whether
172 : // mImage has been instantiated yet..
173 : imgStatusTracker& GetStatusTracker();
174 :
175 : // Reset the cache entry after we've dropped our reference to it. Used by the
176 : // imgLoader when our cache entry is re-requested after we've dropped our
177 : // reference to it.
178 : void SetCacheEntry(imgCacheEntry *entry);
179 :
180 : // Returns whether we've got a reference to the cache entry.
181 : bool HasCacheEntry() const;
182 :
183 : // Return true if at least one of our proxies, excluding
184 : // aProxyToIgnore, has an observer. aProxyToIgnore may be null.
185 : bool HaveProxyWithObserver(imgRequestProxy* aProxyToIgnore) const;
186 :
187 : // Return the priority of the underlying network request, or return
188 : // PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
189 : PRInt32 Priority() const;
190 :
191 : // Adjust the priority of the underlying network request by the given delta
192 : // on behalf of the given proxy.
193 : void AdjustPriority(imgRequestProxy *aProxy, PRInt32 aDelta);
194 :
195 : // Return whether we've seen some data at this point
196 0 : bool HasTransferredData() const { return mGotData; }
197 :
198 : // Set whether this request is stored in the cache. If it isn't, regardless
199 : // of whether this request has a non-null mCacheEntry, this imgRequest won't
200 : // try to update or modify the image cache.
201 : void SetIsInCache(bool cacheable);
202 :
203 : // Update the cache entry size based on the image container
204 : void UpdateCacheEntrySize();
205 :
206 : public:
207 : NS_DECL_IMGIDECODEROBSERVER
208 : NS_DECL_IMGICONTAINEROBSERVER
209 : NS_DECL_NSISTREAMLISTENER
210 : NS_DECL_NSIREQUESTOBSERVER
211 : NS_DECL_NSICHANNELEVENTSINK
212 : NS_DECL_NSIINTERFACEREQUESTOR
213 : NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
214 :
215 : private:
216 : friend class imgMemoryReporter;
217 :
218 : nsCOMPtr<nsIRequest> mRequest;
219 : // The original URI we were loaded with. This is the same as the URI we are
220 : // keyed on in the cache.
221 : nsCOMPtr<nsIURI> mURI;
222 : // The URI of the resource we ended up loading after all redirects, etc.
223 : nsCOMPtr<nsIURI> mCurrentURI;
224 : // The principal of the document which loaded this image. Used when validating for CORS.
225 : nsCOMPtr<nsIPrincipal> mLoadingPrincipal;
226 : // The principal of this image.
227 : nsCOMPtr<nsIPrincipal> mPrincipal;
228 : // Status-tracker -- transferred to mImage, when it gets instantiated
229 : nsAutoPtr<imgStatusTracker> mStatusTracker;
230 : nsRefPtr<mozilla::image::Image> mImage;
231 : nsCOMPtr<nsIProperties> mProperties;
232 : nsCOMPtr<nsISupports> mSecurityInfo;
233 : nsCOMPtr<nsIChannel> mChannel;
234 : nsCOMPtr<nsIInterfaceRequestor> mPrevChannelSink;
235 :
236 : nsTObserverArray<imgRequestProxy*> mObservers;
237 :
238 : nsCOMPtr<nsITimedChannel> mTimedChannel;
239 :
240 : nsCString mContentType;
241 :
242 : nsRefPtr<imgCacheEntry> mCacheEntry; /* we hold on to this to this so long as we have observers */
243 :
244 : void *mLoadId;
245 :
246 : imgCacheValidator *mValidator;
247 : nsCategoryCache<nsIContentSniffer> mImageSniffers;
248 : nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
249 : nsCOMPtr<nsIChannel> mNewRedirectChannel;
250 :
251 : // The ID of the inner window origin, used for error reporting.
252 : PRUint64 mInnerWindowId;
253 :
254 : // The CORS mode (defined in imgIRequest) this image was loaded with. By
255 : // default, imgIRequest::CORS_NONE.
256 : PRInt32 mCORSMode;
257 :
258 : // Sometimes consumers want to do things before the image is ready. Let them,
259 : // and apply the action when the image becomes available.
260 : bool mDecodeRequested : 1;
261 :
262 : bool mIsMultiPartChannel : 1;
263 : bool mGotData : 1;
264 : bool mIsInCache : 1;
265 : };
266 :
267 : #endif
|