Changes related to history listener interface 65608 r=valeski sr=rpotts

This commit is contained in:
radha%netscape.com 2001-02-07 00:31:18 +00:00
parent 9e8b0a29b7
commit cce20c37bb
6 changed files with 131 additions and 17 deletions

View File

@ -1,4 +1,5 @@
nsIShEntry.idl \
nsISHTransaction.idl \
nsISHContainer.idl \
nsISHistory.idl
nsISHistory.idl \
nsISHistoryListener.idl

View File

@ -32,6 +32,7 @@ XPIDLSRCS = nsISHEntry.idl \
nsISHContainer.idl \
nsISHTransaction.idl \
nsISHistory.idl \
nsISHistoryListener.idl \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -28,6 +28,7 @@ XPIDLSRCS = \
.\nsISHContainer.idl \
.\nsISHTransaction.idl \
.\nsISHistory.idl \
.\nsISHistoryListener.idl \
$(NULL)
include <$(DEPTH)\config\rules.mak>

View File

@ -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);
};

View File

@ -113,6 +113,16 @@ nsSHistory::AddEntry(nsISHEntry * aSHEntry, PRBool aPersist)
nsCOMPtr<nsISHTransaction> txn(do_CreateInstance(NS_SHTRANSACTION_CONTRACTID));
NS_ENSURE_TRUE(txn, NS_ERROR_FAILURE);
// Notify any listener about the new addition
if (mListener) {
nsCOMPtr<nsISHistoryListener> listener(do_QueryInterface(mListener));
if (listener) {
nsCOMPtr<nsIURI> 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<nsISHistoryListener> 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<nsISHTransaction> txn = mListRoot;
nsCOMPtr<nsISHTransaction> 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<nsISHistoryListener> 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<nsIURI> 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<nsISHEntry> 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<nsIDocShell> docShell;
nsCOMPtr<nsISHEntry> shEntry;
@ -513,6 +582,35 @@ nsSHistory::LoadEntry(PRInt32 aIndex, PRBool aReloadFlag, long aLoadType)
nsCOMPtr<nsISHEntry> nextEntry;
GetEntryAtIndex(mIndex, PR_FALSE, getter_AddRefs(nextEntry));
// Send appropriate listener notifications
PRBool canNavigate = PR_TRUE;
if(mListener) {
nsCOMPtr<nsIURI> uri;
nextEntry->GetURI(getter_AddRefs(uri));
nsCOMPtr<nsISHistoryListener> 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<nsIURI> nexturi;
PRInt32 pCount=0, nCount=0;
nsCOMPtr<nsISHContainer> prevAsContainer(do_QueryInterface(prevEntry));

View File

@ -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<nsISHTransaction> mListRoot;
PRInt32 mIndex;
PRInt32 mLength;
// Session History listener
nsWeakPtr mListener;
// Weak reference. Do not refcount this.
nsIDocShell * mRootDocShell;
};