From cce20c37bb4a671bab723214d02b0bdbba920860 Mon Sep 17 00:00:00 2001 From: "radha%netscape.com" Date: Wed, 7 Feb 2001 00:31:18 +0000 Subject: [PATCH] Changes related to history listener interface 65608 r=valeski sr=rpotts --- xpfe/components/shistory/public/MANIFEST_IDL | 3 +- xpfe/components/shistory/public/Makefile.in | 1 + xpfe/components/shistory/public/makefile.win | 1 + .../shistory/public/nsISHistory.idl | 20 ++- xpfe/components/shistory/src/nsSHistory.cpp | 118 ++++++++++++++++-- xpfe/components/shistory/src/nsSHistory.h | 5 +- 6 files changed, 131 insertions(+), 17 deletions(-) diff --git a/xpfe/components/shistory/public/MANIFEST_IDL b/xpfe/components/shistory/public/MANIFEST_IDL index a15c4e4543e6..f5b5362e6c88 100644 --- a/xpfe/components/shistory/public/MANIFEST_IDL +++ b/xpfe/components/shistory/public/MANIFEST_IDL @@ -1,4 +1,5 @@ nsIShEntry.idl \ nsISHTransaction.idl \ nsISHContainer.idl \ -nsISHistory.idl \ No newline at end of file +nsISHistory.idl \ +nsISHistoryListener.idl \ No newline at end of file diff --git a/xpfe/components/shistory/public/Makefile.in b/xpfe/components/shistory/public/Makefile.in index 65c2b512bf52..2c80680c0e93 100644 --- a/xpfe/components/shistory/public/Makefile.in +++ b/xpfe/components/shistory/public/Makefile.in @@ -32,6 +32,7 @@ XPIDLSRCS = nsISHEntry.idl \ nsISHContainer.idl \ nsISHTransaction.idl \ nsISHistory.idl \ + nsISHistoryListener.idl \ $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/xpfe/components/shistory/public/makefile.win b/xpfe/components/shistory/public/makefile.win index acb1314e2672..6c1414ee264b 100644 --- a/xpfe/components/shistory/public/makefile.win +++ b/xpfe/components/shistory/public/makefile.win @@ -28,6 +28,7 @@ XPIDLSRCS = \ .\nsISHContainer.idl \ .\nsISHTransaction.idl \ .\nsISHistory.idl \ + .\nsISHistoryListener.idl \ $(NULL) include <$(DEPTH)\config\rules.mak> diff --git a/xpfe/components/shistory/public/nsISHistory.idl b/xpfe/components/shistory/public/nsISHistory.idl index dc1862170712..61b1bde9f7cf 100644 --- a/xpfe/components/shistory/public/nsISHistory.idl +++ b/xpfe/components/shistory/public/nsISHistory.idl @@ -25,8 +25,8 @@ #include "nsISHEntry.idl" #include "nsISHTransaction.idl" #include "nsIDocShell.idl" +#include "nsISHistoryListener.idl" -//interface nsIDocShell; %{C++ #define NS_SHISTORY_CID \ @@ -40,10 +40,10 @@ interface nsISHistory: nsISupports { /** * Add a new Entry to the History List - * @param aEntry - The entry to add - * @param aPersist - If true this specifies that the entry should persist - * in the list. If false, this means that when new entries are added - * this element will not appear in the session history list. + * @param aEntry - The entry to add + * @param aPersist - If true this specifies that the entry should persist + * in the list. If false, this means that when new entries are added + * this element will not appear in the session history list. */ void addEntry(in nsISHEntry aEntry, in boolean aPersist); @@ -92,4 +92,14 @@ interface nsISHistory: nsISupports */ void PurgeHistory(in long numEntries); + /** + * Register a Session History Listener to be notified on SHistory events + */ + void addSHistoryListener(in nsISHistoryListener aListener); + + /** + * Remove a previously registered Session History Listener + */ + void removeSHistoryListener(in nsISHistoryListener aListener); + }; diff --git a/xpfe/components/shistory/src/nsSHistory.cpp b/xpfe/components/shistory/src/nsSHistory.cpp index b1101be633eb..14c78490f0d5 100644 --- a/xpfe/components/shistory/src/nsSHistory.cpp +++ b/xpfe/components/shistory/src/nsSHistory.cpp @@ -113,6 +113,16 @@ nsSHistory::AddEntry(nsISHEntry * aSHEntry, PRBool aPersist) nsCOMPtr txn(do_CreateInstance(NS_SHTRANSACTION_CONTRACTID)); NS_ENSURE_TRUE(txn, NS_ERROR_FAILURE); + // Notify any listener about the new addition + if (mListener) { + nsCOMPtr listener(do_QueryInterface(mListener)); + if (listener) { + nsCOMPtr uri; + aSHEntry->GetURI(getter_AddRefs(uri)); + listener->OnHistoryNewEntry(uri); + } + } + // Set the ShEntry and parent for the transaction. setting the // parent will properly set the parent child relationship txn->SetPersist(aPersist); @@ -346,7 +356,21 @@ nsSHistory::PurgeHistory(PRInt32 aEntries) if (mLength <= 0 || aEntries <= 0) return NS_ERROR_FAILURE; - PRInt32 cnt = 0; + PRBool purgeHistory = PR_TRUE; + // Notify the listener about the history purge + if (mListener) { + nsCOMPtr listener(do_QueryInterface(mListener)); + if (listener) { + listener->OnHistoryPurge(aEntries, &purgeHistory); + } + } + + if (!purgeHistory) { + // Listener asked us not to purge + return NS_OK; + } + + PRInt32 cnt = 0; while (cnt < aEntries) { nsCOMPtr txn = mListRoot; nsCOMPtr nextTxn; @@ -361,6 +385,29 @@ nsSHistory::PurgeHistory(PRInt32 aEntries) return NS_OK; } + +NS_IMETHODIMP +nsSHistory::AddSHistoryListener(nsISHistoryListener * aListener) +{ + NS_ENSURE_ARG_POINTER(aListener); + + // Check if the listener supports Weak Reference. This is a must. + // This listener functionality is used by embedders and we want to + // have the right ownership with who ever listens to SHistory + nsWeakPtr listener = getter_AddRefs(NS_GetWeakReference(aListener)); + if (!listener) return NS_ERROR_FAILURE; + mListener = listener; + return NS_OK; +} + + +NS_IMETHODIMP +nsSHistory::RemoveSHistoryListener(nsISHistoryListener * aListener) +{ + mListener = nsnull; + return NS_OK; +} + //***************************************************************************** // nsSHistory: nsIWebNavigation //***************************************************************************** @@ -405,7 +452,7 @@ nsSHistory::GoBack() GetCanGoBack(&canGoBack); if (!canGoBack) // Can't go back return NS_ERROR_UNEXPECTED; - return GotoIndex(mIndex-1); + return GotoIndex(mIndex-1); } @@ -417,13 +464,14 @@ nsSHistory::GoForward() GetCanGoForward(&canGoForward); if (!canGoForward) // Can't go forward return NS_ERROR_UNEXPECTED; - return GotoIndex(mIndex+1); + return GotoIndex(mIndex+1); } NS_IMETHODIMP nsSHistory::Reload(PRUint32 aReloadFlags) { + nsresult rv; nsDocShellInfoLoadType loadType; if (aReloadFlags & nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY && aReloadFlags & nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE) @@ -442,7 +490,25 @@ nsSHistory::Reload(PRUint32 aReloadFlags) { loadType = nsIDocShellLoadInfo::loadReloadNormal; } - return LoadEntry(mIndex, PR_TRUE, loadType); + + // Notify listeners + PRBool canNavigate = PR_TRUE; + if (mListener) { + nsCOMPtr listener(do_QueryInterface(mListener)); + // We are reloading. Send Reload notifications. + // nsDocShellLoadFlagType is not public, where as nsIWebNavigation + // is public. So send the reload notifications with the + // nsIWebNavigation flags. + if (listener) { + nsCOMPtr currentURI; + rv = GetCurrentURI(getter_AddRefs(currentURI)); + listener->OnHistoryReload(currentURI, aReloadFlags, &canNavigate); + } + } + if (!canNavigate) + return NS_OK; + + return LoadEntry(mIndex, loadType); } NS_IMETHODIMP @@ -463,12 +529,15 @@ nsSHistory::GetDocument(nsIDOMDocument** aDocument) NS_IMETHODIMP -nsSHistory::GetCurrentURI(nsIURI** aCurrentURI) +nsSHistory::GetCurrentURI(nsIURI** aResultURI) { - // Not implemented - return NS_OK; -} + NS_ENSURE_ARG_POINTER(aResultURI); + nsCOMPtr currentEntry; + nsresult rv = GetEntryAtIndex(mIndex, PR_FALSE, getter_AddRefs(currentEntry)); + if (NS_FAILED(rv) && !currentEntry) return rv; + return currentEntry->GetURI(aResultURI); +} NS_IMETHODIMP @@ -496,11 +565,11 @@ nsSHistory::LoadURI(const PRUnichar* aURI, PRUint32 aLoadFlags) NS_IMETHODIMP nsSHistory::GotoIndex(PRInt32 aIndex) { - return LoadEntry(aIndex, PR_FALSE, nsIDocShellLoadInfo::loadHistory); + return LoadEntry(aIndex, nsIDocShellLoadInfo::loadHistory); } NS_IMETHODIMP -nsSHistory::LoadEntry(PRInt32 aIndex, PRBool aReloadFlag, long aLoadType) +nsSHistory::LoadEntry(PRInt32 aIndex, long aLoadType) { nsCOMPtr docShell; nsCOMPtr shEntry; @@ -513,6 +582,35 @@ nsSHistory::LoadEntry(PRInt32 aIndex, PRBool aReloadFlag, long aLoadType) nsCOMPtr nextEntry; GetEntryAtIndex(mIndex, PR_FALSE, getter_AddRefs(nextEntry)); + // Send appropriate listener notifications + PRBool canNavigate = PR_TRUE; + if(mListener) { + nsCOMPtr uri; + nextEntry->GetURI(getter_AddRefs(uri)); + nsCOMPtr listener(do_QueryInterface(mListener)); + if (listener) { + if (mIndex+1 == oldIndex) { + // We are going back one entry. Send GoBack notifications + listener->OnHistoryGoBack(uri, &canNavigate); + } + else if (mIndex-1 == oldIndex) { + // We are going forward. Send GoForward notification + listener->OnHistoryGoForward(uri, &canNavigate); + } + else if (mIndex != oldIndex) { + // We are going somewhere else. This is not reload either + listener->OnHistoryGotoIndex(mIndex, uri, &canNavigate); + } + } + } + + if (!canNavigate) { + // reset the index back to what it was, if the listener + // asked us not to proceed with the operation. + mIndex = oldIndex; + return NS_OK; // XXX Maybe I can return some other error code? + } + nsCOMPtr nexturi; PRInt32 pCount=0, nCount=0; nsCOMPtr prevAsContainer(do_QueryInterface(prevEntry)); diff --git a/xpfe/components/shistory/src/nsSHistory.h b/xpfe/components/shistory/src/nsSHistory.h index 3ca566a90672..358102bf98ed 100644 --- a/xpfe/components/shistory/src/nsSHistory.h +++ b/xpfe/components/shistory/src/nsSHistory.h @@ -30,6 +30,7 @@ #include "nsISHistory.h" #include "nsISHTransaction.h" #include "nsIWebNavigation.h" +#include "nsIWeakReference.h" class nsIDocShell; @@ -54,12 +55,14 @@ protected: NS_IMETHOD GetTransactionAtIndex(PRInt32 aIndex, nsISHTransaction ** aResult); PRBool CompareSHEntry(nsISHEntry * prevEntry, nsISHEntry * nextEntry, nsIDocShell * rootDocShell, nsIDocShell ** aResultDocShell, nsISHEntry ** aResultSHEntry); - NS_IMETHOD LoadEntry(PRInt32 aIndex, PRBool aReloadFlag, long aLoadType); + NS_IMETHOD LoadEntry(PRInt32 aIndex, long aLoadType); protected: nsCOMPtr mListRoot; PRInt32 mIndex; PRInt32 mLength; + // Session History listener + nsWeakPtr mListener; // Weak reference. Do not refcount this. nsIDocShell * mRootDocShell; };