Back to index

lightning-sunbird  0.9+nobinonly
nsWebBrowserPersist.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Adam Lock <adamlock@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 nsWebBrowserPersist_h__
00041 #define nsWebBrowserPersist_h__
00042 
00043 #include "nsCOMPtr.h"
00044 #include "nsWeakReference.h"
00045 
00046 #include "nsIInterfaceRequestor.h"
00047 #include "nsIMIMEService.h"
00048 #include "nsIStreamListener.h"
00049 #include "nsIOutputStream.h"
00050 #include "nsIInputStream.h"
00051 #include "nsIChannel.h"
00052 #include "nsIStyleSheet.h"
00053 #include "nsIDocumentEncoder.h"
00054 #include "nsITransport.h"
00055 #include "nsIProgressEventSink.h"
00056 #include "nsILocalFile.h"
00057 #include "nsIWebProgressListener2.h"
00058 
00059 #include "nsHashtable.h"
00060 #include "nsVoidArray.h"
00061 #include "nsInt64.h"
00062 
00063 #include "nsCWebBrowserPersist.h"
00064 
00065 class nsEncoderNodeFixup;
00066 class nsIStorageStream;
00067 
00068 struct URIData;
00069 
00070 class nsWebBrowserPersist : public nsIInterfaceRequestor,
00071                             public nsIWebBrowserPersist,
00072                             public nsIStreamListener,
00073                             public nsIProgressEventSink,
00074                             public nsSupportsWeakReference
00075 {
00076     friend class nsEncoderNodeFixup;
00077 
00078 // Public members
00079 public:
00080     nsWebBrowserPersist();
00081     
00082     NS_DECL_ISUPPORTS
00083     NS_DECL_NSIINTERFACEREQUESTOR
00084     NS_DECL_NSICANCELABLE
00085     NS_DECL_NSIWEBBROWSERPERSIST
00086     NS_DECL_NSIREQUESTOBSERVER
00087     NS_DECL_NSISTREAMLISTENER
00088     NS_DECL_NSIPROGRESSEVENTSINK
00089 
00090 // Protected members
00091 protected:    
00092     virtual ~nsWebBrowserPersist();
00093     nsresult CloneNodeWithFixedUpURIAttributes(
00094         nsIDOMNode *aNodeIn, nsIDOMNode **aNodeOut);
00095     nsresult SaveURIInternal(
00096         nsIURI *aURI, nsISupports *aCacheKey, nsIURI *aReferrer,
00097         nsIInputStream *aPostData, const char *aExtraHeaders, nsIURI *aFile,
00098         PRBool aCalcFileExt);
00099     nsresult SaveChannelInternal(
00100         nsIChannel *aChannel, nsIURI *aFile, PRBool aCalcFileExt);
00101     nsresult SaveDocumentInternal(
00102         nsIDOMDocument *aDocument, nsIURI *aFile, nsIURI *aDataPath);
00103     nsresult SaveDocuments();
00104     nsresult GetDocEncoderContentType(
00105         nsIDOMDocument *aDocument, const PRUnichar *aContentType,
00106         PRUnichar **aRealContentType);
00107     nsresult GetExtensionForContentType(
00108         const PRUnichar *aContentType, PRUnichar **aExt);
00109     nsresult GetDocumentExtension(nsIDOMDocument *aDocument, PRUnichar **aExt);
00110 
00111 // Private members
00112 private:
00113     void Cleanup();
00114     void CleanupLocalFiles();
00115     nsresult GetValidURIFromObject(nsISupports *aObject, nsIURI **aURI) const;
00116     nsresult GetLocalFileFromURI(nsIURI *aURI, nsILocalFile **aLocalFile) const;
00117     nsresult AppendPathToURI(nsIURI *aURI, const nsAString & aPath) const;
00118     nsresult MakeAndStoreLocalFilenameInURIMap(
00119         const char *aURI, PRBool aNeedsPersisting, URIData **aData);
00120     nsresult MakeOutputStream(
00121         nsIURI *aFile, nsIOutputStream **aOutputStream);
00122     nsresult MakeOutputStreamFromFile(
00123         nsILocalFile *aFile, nsIOutputStream **aOutputStream);
00124     nsresult MakeOutputStreamFromURI(nsIURI *aURI, nsIOutputStream  **aOutStream);
00125     nsresult CreateChannelFromURI(nsIURI *aURI, nsIChannel **aChannel);
00126     nsresult StartUpload(nsIStorageStream *aOutStream, nsIURI *aDestinationURI,
00127         const nsACString &aContentType);
00128     nsresult CalculateAndAppendFileExt(nsIURI *aURI, nsIChannel *aChannel,
00129         nsIURI *aOriginalURIWithExtension);
00130     nsresult CalculateUniqueFilename(nsIURI *aURI);
00131     nsresult MakeFilenameFromURI(
00132         nsIURI *aURI, nsString &aFilename);
00133     nsresult StoreURI(
00134         const char *aURI,
00135         PRBool aNeedsPersisting = PR_TRUE,
00136         URIData **aData = nsnull);
00137     nsresult StoreURIAttribute(
00138         nsIDOMNode *aNode, const char *aAttribute,
00139         PRBool aNeedsPersisting = PR_TRUE,
00140         URIData **aData = nsnull);
00141     PRBool GetQuotedAttributeValue(
00142     const nsAString &aSource, const nsAString &aAttribute, nsAString &aValue);
00143 
00144     nsresult GetNodeToFixup(nsIDOMNode *aNodeIn, nsIDOMNode **aNodeOut);
00145     nsresult FixupURI(nsAString &aURI);
00146     nsresult FixupNodeAttribute(nsIDOMNode *aNode, const char *aAttribute);
00147     nsresult FixupAnchor(nsIDOMNode *aNode);
00148     nsresult FixupXMLStyleSheetLink(nsIDOMProcessingInstruction *aPI, const nsAString &aHref);
00149     nsresult GetXMLStyleSheetLink(nsIDOMProcessingInstruction *aPI, nsAString &aHref);
00150 
00151     nsresult StoreAndFixupStyleSheet(nsIStyleSheet *aStyleSheet);
00152     nsresult SaveDocumentWithFixup(
00153         nsIDocument *pDocument, nsIDocumentEncoderNodeFixup *pFixup,
00154         nsIURI *aFile, PRBool aReplaceExisting, const nsACString &aFormatType,
00155         const nsCString &aSaveCharset, PRUint32  aFlags);
00156     nsresult SaveSubframeContent(
00157         nsIDOMDocument *aFrameContent, URIData *aData);
00158     nsresult SetDocumentBase(nsIDOMDocument *aDocument, nsIURI *aBaseURI);
00159     nsresult SendErrorStatusChange(
00160         PRBool aIsReadError, nsresult aResult, nsIRequest *aRequest, nsIURI *aURI);
00161     nsresult OnWalkDOMNode(nsIDOMNode *aNode);
00162 
00163     nsresult FixRedirectedChannelEntry(nsIChannel *aNewChannel);
00164 
00165     void EndDownload(nsresult aResult = NS_OK);
00166     nsresult SaveGatheredURIs(nsIURI *aFileAsURI);
00167     PRBool SerializeNextFile();
00168     void CalcTotalProgress();
00169 
00170     void SetApplyConversionIfNeeded(nsIChannel *aChannel);
00171 
00172     // Hash table enumerators
00173     static PRBool PR_CALLBACK EnumPersistURIs(
00174         nsHashKey *aKey, void *aData, void* closure);
00175     static PRBool PR_CALLBACK EnumCleanupURIMap(
00176         nsHashKey *aKey, void *aData, void* closure);
00177     static PRBool PR_CALLBACK EnumCleanupOutputMap(
00178         nsHashKey *aKey, void *aData, void* closure);
00179     static PRBool PR_CALLBACK EnumCleanupUploadList(
00180         nsHashKey *aKey, void *aData, void* closure);
00181     static PRBool PR_CALLBACK EnumCalcProgress(
00182         nsHashKey *aKey, void *aData, void* closure);
00183     static PRBool PR_CALLBACK EnumCalcUploadProgress(
00184         nsHashKey *aKey, void *aData, void* closure);
00185     static PRBool PR_CALLBACK EnumFixRedirect(
00186         nsHashKey *aKey, void *aData, void* closure);
00187     static PRBool PR_CALLBACK EnumCountURIsToPersist(
00188         nsHashKey *aKey, void *aData, void* closure);
00189 
00190     nsCOMPtr<nsIURI>          mCurrentDataPath;
00191     PRBool                    mCurrentDataPathIsRelative;
00192     nsCString                 mCurrentRelativePathToData;
00193     nsCOMPtr<nsIURI>          mCurrentBaseURI;
00194     nsCString                 mCurrentCharset;
00195     nsCOMPtr<nsIURI>          mTargetBaseURI;
00196     PRUint32                  mCurrentThingsToPersist;
00197 
00198     nsCOMPtr<nsIMIMEService>  mMIMEService;
00199     nsCOMPtr<nsIURI>          mURI;
00200     nsCOMPtr<nsIWebProgressListener> mProgressListener;
00206     nsCOMPtr<nsIWebProgressListener2> mProgressListener2;
00207     nsHashtable               mOutputMap;
00208     nsHashtable               mUploadList;
00209     nsHashtable               mURIMap;
00210     nsVoidArray               mDocList;
00211     nsVoidArray               mCleanupList;
00212     nsCStringArray            mFilenameList;
00213     PRPackedBool              mFirstAndOnlyUse;
00214     PRPackedBool              mCancel;
00215     PRPackedBool              mJustStartedLoading;
00216     PRPackedBool              mCompleted;
00217     PRPackedBool              mStartSaving;
00218     PRPackedBool              mReplaceExisting;
00219     PRPackedBool              mSerializingOutput;
00220     PRUint32                  mPersistFlags;
00221     PRUint32                  mPersistResult;
00222     nsInt64                   mTotalCurrentProgress;
00223     nsInt64                   mTotalMaxProgress;
00224     PRInt16                   mWrapColumn;
00225     PRUint32                  mEncodingFlags;
00226     nsString                  mContentType;
00227 };
00228 
00229 // Helper class does node fixup during persistence
00230 class nsEncoderNodeFixup : public nsIDocumentEncoderNodeFixup
00231 {
00232 public:
00233     nsEncoderNodeFixup();
00234     
00235     NS_DECL_ISUPPORTS
00236     NS_IMETHOD FixupNode(nsIDOMNode *aNode, nsIDOMNode **aOutNode);
00237     
00238     nsWebBrowserPersist *mWebBrowserPersist;
00239 
00240 protected:    
00241     virtual ~nsEncoderNodeFixup();
00242 };
00243 
00244 #endif