Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / dom / webbrowserpersist / nsWebBrowserPersist.h
bloba34370713f7afafb60148211154f0ca78b9dbc64
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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/. */
7 #ifndef nsWebBrowserPersist_h__
8 #define nsWebBrowserPersist_h__
10 #include "nsCOMPtr.h"
11 #include "nsWeakReference.h"
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 "nsIProgressEventSink.h"
20 #include "nsIFile.h"
21 #include "nsIThreadRetargetableStreamListener.h"
22 #include "nsIWebProgressListener2.h"
23 #include "nsIWebBrowserPersist.h"
24 #include "nsIWebBrowserPersistDocument.h"
26 #include "mozilla/MozPromise.h"
27 #include "mozilla/Mutex.h"
28 #include "mozilla/UniquePtr.h"
29 #include "nsClassHashtable.h"
30 #include "nsHashKeys.h"
31 #include "nsTArray.h"
33 class nsIStorageStream;
34 class nsIWebBrowserPersistDocument;
36 using ClosePromise = mozilla::MozPromise<nsresult, nsresult, true>;
38 class nsWebBrowserPersist final : public nsIInterfaceRequestor,
39 public nsIWebBrowserPersist,
40 public nsIStreamListener,
41 public nsIThreadRetargetableStreamListener,
42 public nsIProgressEventSink,
43 public nsSupportsWeakReference {
44 friend class nsEncoderNodeFixup;
46 // Public members
47 public:
48 nsWebBrowserPersist();
50 NS_DECL_THREADSAFE_ISUPPORTS
51 NS_DECL_NSIINTERFACEREQUESTOR
52 NS_DECL_NSICANCELABLE
53 NS_DECL_NSIWEBBROWSERPERSIST
54 NS_DECL_NSIREQUESTOBSERVER
55 NS_DECL_NSISTREAMLISTENER
56 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
57 NS_DECL_NSIPROGRESSEVENTSINK
59 // Private members
60 private:
61 virtual ~nsWebBrowserPersist();
62 nsresult SaveURIInternal(nsIURI* aURI, nsIPrincipal* aTriggeringPrincipal,
63 nsContentPolicyType aContentPolicyType,
64 uint32_t aCacheKey, nsIReferrerInfo* aReferrerInfo,
65 nsICookieJarSettings* aCookieJarSettings,
66 nsIInputStream* aPostData, const char* aExtraHeaders,
67 nsIURI* aFile, bool aCalcFileExt, bool aIsPrivate);
68 nsresult SaveChannelInternal(nsIChannel* aChannel, nsIURI* aFile,
69 bool aCalcFileExt);
70 nsresult SaveDocumentInternal(nsIWebBrowserPersistDocument* aDocument,
71 nsIURI* aFile, nsIURI* aDataPath);
72 nsresult SaveDocuments();
73 void FinishSaveDocumentInternal(nsIURI* aFile, nsIFile* aDataPath);
74 nsresult GetExtensionForContentType(const char16_t* aContentType,
75 char16_t** aExt);
77 struct CleanupData;
78 struct DocData;
79 struct OutputData;
80 struct UploadData;
81 struct URIData;
82 struct WalkData;
84 class OnWalk;
85 class OnRemoteWalk;
86 class OnWrite;
87 class FlatURIMap;
88 friend class OnWalk;
89 friend class OnWrite;
91 nsresult SaveDocumentDeferred(mozilla::UniquePtr<WalkData>&& aData);
92 void Cleanup();
93 void CleanupLocalFiles();
94 nsresult GetValidURIFromObject(nsISupports* aObject, nsIURI** aURI) const;
95 static nsresult GetLocalFileFromURI(nsIURI* aURI, nsIFile** aLocalFile);
96 static nsresult AppendPathToURI(nsIURI* aURI, const nsAString& aPath,
97 nsCOMPtr<nsIURI>& aOutURI);
98 nsresult MakeAndStoreLocalFilenameInURIMap(
99 nsIURI* aURI, nsIWebBrowserPersistDocument* aDoc,
100 nsContentPolicyType aContentPolicyType, bool aNeedsPersisting,
101 URIData** aData);
102 nsresult MakeOutputStream(nsIURI* aFile, nsIOutputStream** aOutputStream);
103 nsresult MakeOutputStreamFromFile(nsIFile* aFile,
104 nsIOutputStream** aOutputStream);
105 nsresult MakeOutputStreamFromURI(nsIURI* aURI, nsIOutputStream** aOutStream);
106 nsresult CreateChannelFromURI(nsIURI* aURI, nsIChannel** aChannel);
107 nsresult StartUpload(nsIStorageStream* aOutStream, nsIURI* aDestinationURI,
108 const nsACString& aContentType);
109 nsresult StartUpload(nsIInputStream* aInputStream, nsIURI* aDestinationURI,
110 const nsACString& aContentType);
111 nsresult CalculateAndAppendFileExt(nsIURI* aURI, nsIChannel* aChannel,
112 nsIURI* aOriginalURIWithExtension,
113 nsCOMPtr<nsIURI>& aOutURI);
114 nsresult CalculateUniqueFilename(nsIURI* aURI, nsCOMPtr<nsIURI>& aOutURI);
115 nsresult MakeFilenameFromURI(nsIURI* aURI, nsString& aFilename);
116 nsresult StoreURI(const nsACString& aURI, nsIWebBrowserPersistDocument* aDoc,
117 nsContentPolicyType aContentPolicyType,
118 bool aNeedsPersisting = true, URIData** aData = nullptr);
119 nsresult StoreURI(nsIURI* aURI, nsIWebBrowserPersistDocument* aDoc,
120 nsContentPolicyType aContentPolicyType,
121 bool aNeedsPersisting = true, URIData** aData = nullptr);
122 bool DocumentEncoderExists(const char* aContentType);
124 nsresult SaveSubframeContent(nsIWebBrowserPersistDocument* aFrameContent,
125 nsIWebBrowserPersistDocument* aParentDocument,
126 const nsCString& aURISpec, URIData* aData);
127 nsresult SendErrorStatusChange(bool aIsReadError, nsresult aResult,
128 nsIRequest* aRequest, nsIURI* aURI);
130 nsresult FixRedirectedChannelEntry(nsIChannel* aNewChannel);
132 void FinishDownload();
133 void EndDownload(nsresult aResult);
134 void EndDownloadInternal(nsresult aResult);
135 void SerializeNextFile();
136 void CalcTotalProgress();
138 void SetApplyConversionIfNeeded(nsIChannel* aChannel);
140 nsCOMPtr<nsIURI> mCurrentDataPath;
141 bool mCurrentDataPathIsRelative;
142 nsCString mCurrentRelativePathToData;
143 nsCOMPtr<nsIURI> mCurrentBaseURI;
144 nsCString mCurrentCharset;
145 nsCOMPtr<nsIURI> mTargetBaseURI;
146 uint32_t mCurrentThingsToPersist;
148 nsCOMPtr<nsIMIMEService> mMIMEService;
149 nsCOMPtr<nsIURI> mURI;
150 nsCOMPtr<nsIWebProgressListener> mProgressListener;
152 * Progress listener for 64-bit values; this is the same object as
153 * mProgressListener, but is a member to avoid having to qi it for each
154 * progress notification.
156 nsCOMPtr<nsIWebProgressListener2> mProgressListener2;
157 nsCOMPtr<nsIProgressEventSink> mEventSink;
158 mozilla::Mutex mOutputMapMutex;
159 nsClassHashtable<nsISupportsHashKey, OutputData> mOutputMap;
160 nsClassHashtable<nsISupportsHashKey, UploadData> mUploadList;
161 nsCOMPtr<nsISerialEventTarget> mBackgroundQueue;
162 nsTArray<RefPtr<ClosePromise>> mFileClosePromises;
163 nsClassHashtable<nsCStringHashKey, URIData> mURIMap;
164 nsCOMPtr<nsIWebBrowserPersistURIMap> mFlatURIMap;
165 nsTArray<mozilla::UniquePtr<WalkData>> mWalkStack;
166 nsTArray<DocData*> mDocList;
167 nsTArray<CleanupData*> mCleanupList;
168 nsTArray<nsCString> mFilenameList;
169 bool mFirstAndOnlyUse;
170 bool mSavingDocument;
171 // mCancel is used from both the main thread, and (inside OnDataAvailable)
172 // from a background thread.
173 mozilla::Atomic<bool> mCancel;
174 bool mEndCalled;
175 bool mCompleted;
176 bool mStartSaving;
177 bool mReplaceExisting;
178 bool mSerializingOutput;
179 bool mIsPrivate;
180 // mPersistFlags can be modified on the main thread, and can be read from
181 // a background thread when OnDataAvailable calls MakeOutputStreamFromFile.
182 mozilla::Atomic<uint32_t> mPersistFlags;
183 nsresult mPersistResult;
184 int64_t mTotalCurrentProgress;
185 int64_t mTotalMaxProgress;
186 int16_t mWrapColumn;
187 uint32_t mEncodingFlags;
188 nsString mContentType;
191 #endif