Back to index

lightning-sunbird  0.9+nobinonly
imgRequest.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Stuart Parmenter <pavlov@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef imgRequest_h__
00041 #define imgRequest_h__
00042 
00043 #include "imgILoad.h"
00044 
00045 #include "imgIContainer.h"
00046 #include "imgIDecoder.h"
00047 #include "imgIDecoderObserver.h"
00048 
00049 #include "nsICacheEntryDescriptor.h"
00050 #include "nsIChannel.h"
00051 #include "nsIProperties.h"
00052 #include "nsIStreamListener.h"
00053 #include "nsIURI.h"
00054 
00055 #include "nsCOMPtr.h"
00056 #include "nsString.h"
00057 #include "nsVoidArray.h"
00058 #include "nsWeakReference.h"
00059 
00060 class imgCacheValidator;
00061 
00062 class imgRequestProxy;
00063 
00064 enum {
00065   onStartRequest   = PR_BIT(0),
00066   onStartDecode    = PR_BIT(1),
00067   onStartContainer = PR_BIT(2),
00068   onStopContainer  = PR_BIT(3),
00069   onStopDecode     = PR_BIT(4),
00070   onStopRequest    = PR_BIT(5)
00071 };
00072 
00073 class imgRequest : public imgILoad,
00074                    public imgIDecoderObserver,
00075                    public nsIStreamListener,
00076                    public nsSupportsWeakReference
00077 {
00078 public:
00079   imgRequest();
00080   virtual ~imgRequest();
00081 
00082   NS_DECL_ISUPPORTS
00083 
00084   nsresult Init(nsIChannel *aChannel,
00085                 nsICacheEntryDescriptor *aCacheEntry,
00086                 void *aCacheId,
00087                 void *aLoadId);
00088 
00089   // Callers that pass aNotify==PR_FALSE must call NotifyProxyListener
00090   // later.
00091   nsresult AddProxy   (imgRequestProxy *proxy, PRBool aNotify);
00092 
00093   // aNotify==PR_FALSE still sends OnStopRequest.
00094   nsresult RemoveProxy(imgRequestProxy *proxy, nsresult aStatus, PRBool aNotify);
00095   nsresult NotifyProxyListener(imgRequestProxy *proxy);
00096 
00097   void SniffMimeType(const char *buf, PRUint32 len);
00098 
00099   // a request is "reusable" if it has already been loaded, or it is
00100   // currently being loaded on the same event queue as the new request
00101   // being made...
00102   PRBool IsReusable(void *aCacheId) { return !mLoading || (aCacheId == mCacheId); }
00103 
00104 private:
00105   friend class imgRequestProxy;
00106   friend class imgLoader;
00107   friend class imgCacheValidator;
00108   friend class imgCache;
00109 
00110   inline void SetLoadId(void *aLoadId) {
00111     mLoadId = aLoadId;
00112     mLoadTime = PR_Now();
00113   }
00114   inline PRUint32 GetImageStatus() const { return mImageStatus; }
00115   inline nsresult GetResultFromImageStatus(PRUint32 aStatus) const;
00116   void Cancel(nsresult aStatus);
00117   nsresult GetURI(nsIURI **aURI);
00118   void RemoveFromCache();
00119   inline const char *GetMimeType() const {
00120     return mContentType.get();
00121   }
00122   inline nsIProperties *Properties() {
00123     return mProperties;
00124   }
00125 
00126   // Return true if at least one of our proxies, excluding
00127   // aProxyToIgnore, has an observer.  aProxyToIgnore may be null.
00128   PRBool HaveProxyWithObserver(imgRequestProxy* aProxyToIgnore) const;
00129 
00130   // Return the priority of the underlying network request, or return
00131   // PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
00132   PRInt32 Priority() const;
00133 
00134   // Adjust the priority of the underlying network request by the given delta
00135   // on behalf of the given proxy.
00136   void AdjustPriority(imgRequestProxy *aProxy, PRInt32 aDelta);
00137 
00138 public:
00139   NS_DECL_IMGILOAD
00140   NS_DECL_IMGIDECODEROBSERVER
00141   NS_DECL_IMGICONTAINEROBSERVER
00142   NS_DECL_NSISTREAMLISTENER
00143   NS_DECL_NSIREQUESTOBSERVER
00144 
00145 private:
00146   nsCOMPtr<nsIChannel> mChannel;
00147   nsCOMPtr<nsIURI> mURI;
00148   nsCOMPtr<imgIContainer> mImage;
00149   nsCOMPtr<imgIDecoder> mDecoder;
00150   nsCOMPtr<nsIProperties> mProperties;
00151 
00152   nsVoidArray mObservers;
00153 
00154   PRPackedBool mLoading;
00155   PRPackedBool mProcessing;
00156   PRPackedBool mHadLastPart;
00157 
00158   PRUint32 mImageStatus;
00159   PRUint32 mState;
00160 
00161   nsCString mContentType;
00162 
00163   nsCOMPtr<nsICacheEntryDescriptor> mCacheEntry; /* we hold on to this to this so long as we have observers */
00164 
00165   void *mCacheId;
00166 
00167   void *mLoadId;
00168   PRTime mLoadTime;
00169 
00170   imgCacheValidator *mValidator;
00171   PRBool   mIsMultiPartChannel;
00172 };
00173 
00174 #endif