From 9b90f640dd16f31b755c7886a21514b2f5d5f9e5 Mon Sep 17 00:00:00 2001 From: "cbiesinger%web.de" Date: Tue, 7 Sep 2004 14:42:23 +0000 Subject: [PATCH] Bug 228603 nsIWebBrowserPersist should have a saveChannel method r=adamlock sr=darin --- embedding/browser/webBrowser/nsWebBrowser.cpp | 34 ++++++++++++++++++ .../public/nsIWebBrowserPersist.idl | 9 ++++- .../src/nsWebBrowserPersist.cpp | 36 +++++++++++++++++-- .../src/nsWebBrowserPersist.h | 2 ++ 4 files changed, 77 insertions(+), 4 deletions(-) diff --git a/embedding/browser/webBrowser/nsWebBrowser.cpp b/embedding/browser/webBrowser/nsWebBrowser.cpp index eec086f10e9..d8be688200a 100644 --- a/embedding/browser/webBrowser/nsWebBrowser.cpp +++ b/embedding/browser/webBrowser/nsWebBrowser.cpp @@ -1009,6 +1009,40 @@ NS_IMETHODIMP nsWebBrowser::SaveURI( return rv; } +/* void saveChannel (in nsIChannel aChannel, in nsISupports aFile); */ +NS_IMETHODIMP nsWebBrowser::SaveChannel( + nsIChannel* aChannel, nsISupports *aFile) +{ + if (mPersist) + { + PRUint32 currentState; + mPersist->GetCurrentState(¤tState); + if (currentState == PERSIST_STATE_FINISHED) + { + mPersist = nsnull; + } + else + { + // You can't save again until the last save has completed + return NS_ERROR_FAILURE; + } + } + + // Create a throwaway persistence object to do the work + nsresult rv; + mPersist = do_CreateInstance(NS_WEBBROWSERPERSIST_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + mPersist->SetProgressListener(this); + mPersist->SetPersistFlags(mPersistFlags); + mPersist->GetCurrentState(&mPersistCurrentState); + rv = mPersist->SaveChannel(aChannel, aFile); + if (NS_FAILED(rv)) + { + mPersist = nsnull; + } + return rv; +} + /* void saveDocument (in nsIDOMDocument document, in nsISupports aFile, in nsISupports aDataPath); */ NS_IMETHODIMP nsWebBrowser::SaveDocument( nsIDOMDocument *aDocument, nsISupports *aFile, nsISupports *aDataPath, diff --git a/embedding/components/webbrowserpersist/public/nsIWebBrowserPersist.idl b/embedding/components/webbrowserpersist/public/nsIWebBrowserPersist.idl index f3920956583..45a5183aaa2 100644 --- a/embedding/components/webbrowserpersist/public/nsIWebBrowserPersist.idl +++ b/embedding/components/webbrowserpersist/public/nsIWebBrowserPersist.idl @@ -44,6 +44,7 @@ interface nsIInputStream; interface nsIDOMDocument; interface nsIWebProgressListener; interface nsILocalFile; +interface nsIChannel; /** * @status UNDER_REVIEW @@ -52,7 +53,7 @@ interface nsILocalFile; /** * Interface for persisting DOM documents and URIs to local or remote storage. */ -[scriptable, uuid(814ba433-a816-4785-9f95-ad3ba0a43dab)] +[scriptable, uuid(1c2a6fac-c100-4c67-95d3-281736038700)] interface nsIWebBrowserPersist : nsISupports { /** No special persistence behaviour. */ @@ -160,6 +161,12 @@ interface nsIWebBrowserPersist : nsISupports in nsIURI aReferrer, in nsIInputStream aPostData, in string aExtraHeaders, in nsISupports aFile); + /** + * Save a channel to a file. It must not be opened yet. + * @see saveURI + */ + void saveChannel(in nsIChannel aChannel, in nsISupports aFile); + /** Output only the current selection as opposed to the whole document. */ const unsigned long ENCODE_FLAGS_SELECTION_ONLY = 1; /** diff --git a/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp b/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp index bcbb8d12e39..7cd2a1eeff0 100644 --- a/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp +++ b/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp @@ -388,6 +388,28 @@ NS_IMETHODIMP nsWebBrowserPersist::SaveURI( return NS_FAILED(rv) ? rv : NS_OK; } +/* void saveChannel (in nsIChannel aChannel, in nsISupports aFile); */ +NS_IMETHODIMP nsWebBrowserPersist::SaveChannel( + nsIChannel *aChannel, nsISupports *aFile) +{ + NS_ENSURE_TRUE(mFirstAndOnlyUse, NS_ERROR_FAILURE); + mFirstAndOnlyUse = PR_FALSE; // Stop people from reusing this object! + + nsCOMPtr fileAsURI; + nsresult rv; + rv = GetValidURIFromObject(aFile, getter_AddRefs(fileAsURI)); + NS_ENSURE_SUCCESS(rv, NS_ERROR_INVALID_ARG); + + rv = aChannel->GetURI(getter_AddRefs(mURI)); + NS_ENSURE_SUCCESS(rv, rv); + + // SaveURI doesn't like broken uris. + mPersistFlags |= PERSIST_FLAGS_FAIL_ON_BROKEN_LINKS; + rv = SaveChannelInternal(aChannel, fileAsURI, PR_FALSE); + return NS_FAILED(rv) ? rv : NS_OK; +} + + /* void saveDocument (in nsIDOMDocument aDocument, in nsIURI aFileURI, in nsIURI aDataPathURI, in string aOutputContentType, in unsigned long aEncodingFlags, in unsigned long aWrapColumn); */ @@ -1238,9 +1260,17 @@ nsresult nsWebBrowserPersist::SaveURIInternal( } } } + return SaveChannelInternal(inputChannel, aFile, aCalcFileExt); +} + +nsresult nsWebBrowserPersist::SaveChannelInternal( + nsIChannel *aChannel, nsIURI *aFile, PRBool aCalcFileExt) +{ + NS_ENSURE_ARG_POINTER(aChannel); + NS_ENSURE_ARG_POINTER(aFile); // Read from the input channel - rv = inputChannel->AsyncOpen(this, nsnull); + nsresult rv = aChannel->AsyncOpen(this, nsnull); if (rv == NS_ERROR_NO_CONTENT) { // Assume this is a protocol such as mailto: which does not feed out @@ -1260,9 +1290,9 @@ nsresult nsWebBrowserPersist::SaveURIInternal( else { // Add the output transport to the output map with the channel as the key - nsCOMPtr keyPtr = do_QueryInterface(inputChannel); + nsCOMPtr keyPtr = do_QueryInterface(aChannel); nsISupportsKey key(keyPtr); - mOutputMap.Put(&key, new OutputData(aFile, aURI, aCalcFileExt)); + mOutputMap.Put(&key, new OutputData(aFile, mURI, aCalcFileExt)); } return NS_OK; diff --git a/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.h b/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.h index 5a7c008a5bb..272fc14b22b 100644 --- a/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.h +++ b/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.h @@ -94,6 +94,8 @@ protected: nsIURI *aURI, nsISupports *aCacheKey, nsIURI *aReferrer, nsIInputStream *aPostData, const char *aExtraHeaders, nsIURI *aFile, PRBool aCalcFileExt); + nsresult SaveChannelInternal( + nsIChannel *aChannel, nsIURI *aFile, PRBool aCalcFileExt); nsresult SaveDocumentInternal( nsIDOMDocument *aDocument, nsIURI *aFile, nsIURI *aDataPath); nsresult SaveDocuments();