|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
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/. */ |
|
6 |
|
7 #ifndef nsWebBrowserPersist_h__ |
|
8 #define nsWebBrowserPersist_h__ |
|
9 |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsWeakReference.h" |
|
12 |
|
13 #include "nsIInterfaceRequestor.h" |
|
14 #include "nsIMIMEService.h" |
|
15 #include "nsIStreamListener.h" |
|
16 #include "nsIOutputStream.h" |
|
17 #include "nsIInputStream.h" |
|
18 #include "nsIChannel.h" |
|
19 #include "nsIStyleSheet.h" |
|
20 #include "nsIDocumentEncoder.h" |
|
21 #include "nsITransport.h" |
|
22 #include "nsIProgressEventSink.h" |
|
23 #include "nsIFile.h" |
|
24 #include "nsIWebProgressListener2.h" |
|
25 |
|
26 #include "nsClassHashtable.h" |
|
27 #include "nsHashKeys.h" |
|
28 #include "nsTArray.h" |
|
29 |
|
30 #include "nsCWebBrowserPersist.h" |
|
31 |
|
32 class nsEncoderNodeFixup; |
|
33 class nsIStorageStream; |
|
34 |
|
35 struct CleanupData; |
|
36 struct DocData; |
|
37 struct OutputData; |
|
38 struct UploadData; |
|
39 struct URIData; |
|
40 |
|
41 class nsWebBrowserPersist : public nsIInterfaceRequestor, |
|
42 public nsIWebBrowserPersist, |
|
43 public nsIStreamListener, |
|
44 public nsIProgressEventSink, |
|
45 public nsSupportsWeakReference |
|
46 { |
|
47 friend class nsEncoderNodeFixup; |
|
48 |
|
49 // Public members |
|
50 public: |
|
51 nsWebBrowserPersist(); |
|
52 |
|
53 NS_DECL_ISUPPORTS |
|
54 NS_DECL_NSIINTERFACEREQUESTOR |
|
55 NS_DECL_NSICANCELABLE |
|
56 NS_DECL_NSIWEBBROWSERPERSIST |
|
57 NS_DECL_NSIREQUESTOBSERVER |
|
58 NS_DECL_NSISTREAMLISTENER |
|
59 NS_DECL_NSIPROGRESSEVENTSINK |
|
60 |
|
61 // Protected members |
|
62 protected: |
|
63 virtual ~nsWebBrowserPersist(); |
|
64 nsresult CloneNodeWithFixedUpAttributes( |
|
65 nsIDOMNode *aNodeIn, bool *aSerializeCloneKids, nsIDOMNode **aNodeOut); |
|
66 nsresult SaveURIInternal( |
|
67 nsIURI *aURI, nsISupports *aCacheKey, nsIURI *aReferrer, |
|
68 nsIInputStream *aPostData, const char *aExtraHeaders, nsIURI *aFile, |
|
69 bool aCalcFileExt, bool aIsPrivate); |
|
70 nsresult SaveChannelInternal( |
|
71 nsIChannel *aChannel, nsIURI *aFile, bool aCalcFileExt); |
|
72 nsresult SaveDocumentInternal( |
|
73 nsIDOMDocument *aDocument, nsIURI *aFile, nsIURI *aDataPath); |
|
74 nsresult SaveDocuments(); |
|
75 nsresult GetDocEncoderContentType( |
|
76 nsIDOMDocument *aDocument, const char16_t *aContentType, |
|
77 char16_t **aRealContentType); |
|
78 nsresult GetExtensionForContentType( |
|
79 const char16_t *aContentType, char16_t **aExt); |
|
80 nsresult GetDocumentExtension(nsIDOMDocument *aDocument, char16_t **aExt); |
|
81 |
|
82 // Private members |
|
83 private: |
|
84 void Cleanup(); |
|
85 void CleanupLocalFiles(); |
|
86 nsresult GetValidURIFromObject(nsISupports *aObject, nsIURI **aURI) const; |
|
87 nsresult GetLocalFileFromURI(nsIURI *aURI, nsIFile **aLocalFile) const; |
|
88 nsresult AppendPathToURI(nsIURI *aURI, const nsAString & aPath) const; |
|
89 nsresult MakeAndStoreLocalFilenameInURIMap( |
|
90 nsIURI *aURI, bool aNeedsPersisting, URIData **aData); |
|
91 nsresult MakeOutputStream( |
|
92 nsIURI *aFile, nsIOutputStream **aOutputStream); |
|
93 nsresult MakeOutputStreamFromFile( |
|
94 nsIFile *aFile, nsIOutputStream **aOutputStream); |
|
95 nsresult MakeOutputStreamFromURI(nsIURI *aURI, nsIOutputStream **aOutStream); |
|
96 nsresult CreateChannelFromURI(nsIURI *aURI, nsIChannel **aChannel); |
|
97 nsresult StartUpload(nsIStorageStream *aOutStream, nsIURI *aDestinationURI, |
|
98 const nsACString &aContentType); |
|
99 nsresult StartUpload(nsIInputStream *aInputStream, nsIURI *aDestinationURI, |
|
100 const nsACString &aContentType); |
|
101 nsresult CalculateAndAppendFileExt(nsIURI *aURI, nsIChannel *aChannel, |
|
102 nsIURI *aOriginalURIWithExtension); |
|
103 nsresult CalculateUniqueFilename(nsIURI *aURI); |
|
104 nsresult MakeFilenameFromURI( |
|
105 nsIURI *aURI, nsString &aFilename); |
|
106 nsresult StoreURI( |
|
107 const char *aURI, |
|
108 bool aNeedsPersisting = true, |
|
109 URIData **aData = nullptr); |
|
110 nsresult StoreURI( |
|
111 nsIURI *aURI, |
|
112 bool aNeedsPersisting = true, |
|
113 URIData **aData = nullptr); |
|
114 nsresult StoreURIAttributeNS( |
|
115 nsIDOMNode *aNode, const char *aNamespaceURI, const char *aAttribute, |
|
116 bool aNeedsPersisting = true, |
|
117 URIData **aData = nullptr); |
|
118 nsresult StoreURIAttribute( |
|
119 nsIDOMNode *aNode, const char *aAttribute, |
|
120 bool aNeedsPersisting = true, |
|
121 URIData **aData = nullptr) |
|
122 { |
|
123 return StoreURIAttributeNS(aNode, "", aAttribute, aNeedsPersisting, aData); |
|
124 } |
|
125 bool DocumentEncoderExists(const char16_t *aContentType); |
|
126 |
|
127 nsresult GetNodeToFixup(nsIDOMNode *aNodeIn, nsIDOMNode **aNodeOut); |
|
128 nsresult FixupURI(nsAString &aURI); |
|
129 nsresult FixupNodeAttributeNS(nsIDOMNode *aNode, const char *aNamespaceURI, const char *aAttribute); |
|
130 nsresult FixupNodeAttribute(nsIDOMNode *aNode, const char *aAttribute) |
|
131 { |
|
132 return FixupNodeAttributeNS(aNode, "", aAttribute); |
|
133 } |
|
134 nsresult FixupAnchor(nsIDOMNode *aNode); |
|
135 nsresult FixupXMLStyleSheetLink(nsIDOMProcessingInstruction *aPI, const nsAString &aHref); |
|
136 nsresult GetXMLStyleSheetLink(nsIDOMProcessingInstruction *aPI, nsAString &aHref); |
|
137 |
|
138 nsresult StoreAndFixupStyleSheet(nsIStyleSheet *aStyleSheet); |
|
139 nsresult SaveDocumentWithFixup( |
|
140 nsIDOMDocument *pDocument, nsIDocumentEncoderNodeFixup *pFixup, |
|
141 nsIURI *aFile, bool aReplaceExisting, const nsACString &aFormatType, |
|
142 const nsCString &aSaveCharset, uint32_t aFlags); |
|
143 nsresult SaveSubframeContent( |
|
144 nsIDOMDocument *aFrameContent, URIData *aData); |
|
145 nsresult SetDocumentBase(nsIDOMDocument *aDocument, nsIURI *aBaseURI); |
|
146 nsresult SendErrorStatusChange( |
|
147 bool aIsReadError, nsresult aResult, nsIRequest *aRequest, nsIURI *aURI); |
|
148 nsresult OnWalkDOMNode(nsIDOMNode *aNode); |
|
149 |
|
150 nsresult FixRedirectedChannelEntry(nsIChannel *aNewChannel); |
|
151 |
|
152 void EndDownload(nsresult aResult = NS_OK); |
|
153 nsresult SaveGatheredURIs(nsIURI *aFileAsURI); |
|
154 bool SerializeNextFile(); |
|
155 void CalcTotalProgress(); |
|
156 |
|
157 void SetApplyConversionIfNeeded(nsIChannel *aChannel); |
|
158 |
|
159 // Hash table enumerators |
|
160 static PLDHashOperator EnumPersistURIs( |
|
161 const nsACString &aKey, URIData *aData, void* aClosure); |
|
162 static PLDHashOperator EnumCleanupOutputMap( |
|
163 nsISupports *aKey, OutputData *aData, void* aClosure); |
|
164 static PLDHashOperator EnumCleanupUploadList( |
|
165 nsISupports *aKey, UploadData *aData, void* aClosure); |
|
166 static PLDHashOperator EnumCalcProgress( |
|
167 nsISupports *aKey, OutputData *aData, void* aClosure); |
|
168 static PLDHashOperator EnumCalcUploadProgress( |
|
169 nsISupports *aKey, UploadData *aData, void* aClosure); |
|
170 static PLDHashOperator EnumFixRedirect( |
|
171 nsISupports *aKey, OutputData *aData, void* aClosure); |
|
172 static PLDHashOperator EnumCountURIsToPersist( |
|
173 const nsACString &aKey, URIData *aData, void* aClosure); |
|
174 |
|
175 nsCOMPtr<nsIURI> mCurrentDataPath; |
|
176 bool mCurrentDataPathIsRelative; |
|
177 nsCString mCurrentRelativePathToData; |
|
178 nsCOMPtr<nsIURI> mCurrentBaseURI; |
|
179 nsCString mCurrentCharset; |
|
180 nsCOMPtr<nsIURI> mTargetBaseURI; |
|
181 uint32_t mCurrentThingsToPersist; |
|
182 |
|
183 nsCOMPtr<nsIMIMEService> mMIMEService; |
|
184 nsCOMPtr<nsIURI> mURI; |
|
185 nsCOMPtr<nsIWebProgressListener> mProgressListener; |
|
186 /** |
|
187 * Progress listener for 64-bit values; this is the same object as |
|
188 * mProgressListener, but is a member to avoid having to qi it for each |
|
189 * progress notification. |
|
190 */ |
|
191 nsCOMPtr<nsIWebProgressListener2> mProgressListener2; |
|
192 nsCOMPtr<nsIProgressEventSink> mEventSink; |
|
193 nsClassHashtable<nsISupportsHashKey, OutputData> mOutputMap; |
|
194 nsClassHashtable<nsISupportsHashKey, UploadData> mUploadList; |
|
195 nsClassHashtable<nsCStringHashKey, URIData> mURIMap; |
|
196 nsTArray<DocData*> mDocList; |
|
197 nsTArray<CleanupData*> mCleanupList; |
|
198 nsTArray<nsCString> mFilenameList; |
|
199 bool mFirstAndOnlyUse; |
|
200 bool mCancel; |
|
201 bool mJustStartedLoading; |
|
202 bool mCompleted; |
|
203 bool mStartSaving; |
|
204 bool mReplaceExisting; |
|
205 bool mSerializingOutput; |
|
206 bool mIsPrivate; |
|
207 uint32_t mPersistFlags; |
|
208 nsresult mPersistResult; |
|
209 int64_t mTotalCurrentProgress; |
|
210 int64_t mTotalMaxProgress; |
|
211 int16_t mWrapColumn; |
|
212 uint32_t mEncodingFlags; |
|
213 nsString mContentType; |
|
214 }; |
|
215 |
|
216 // Helper class does node fixup during persistence |
|
217 class nsEncoderNodeFixup : public nsIDocumentEncoderNodeFixup |
|
218 { |
|
219 public: |
|
220 nsEncoderNodeFixup(); |
|
221 |
|
222 NS_DECL_ISUPPORTS |
|
223 NS_IMETHOD FixupNode(nsIDOMNode *aNode, bool *aSerializeCloneKids, nsIDOMNode **aOutNode); |
|
224 |
|
225 nsWebBrowserPersist *mWebBrowserPersist; |
|
226 |
|
227 protected: |
|
228 virtual ~nsEncoderNodeFixup(); |
|
229 }; |
|
230 |
|
231 #endif |