diff --git a/docshell/shistory/src/Makefile.in b/docshell/shistory/src/Makefile.in index 409ce1860d28..5caf54653dea 100644 --- a/docshell/shistory/src/Makefile.in +++ b/docshell/shistory/src/Makefile.in @@ -31,8 +31,9 @@ LIBRARY_NAME = shistory IS_COMPONENT = 1 CPPSRCS = nsSHEntry.cpp \ - nsSHTransaction.cpp \ - nsSHistoryModule.cpp \ + nsSHTransaction.cpp \ + nsSHistoryModule.cpp \ + nsSHistory.cpp \ $(NULL) EXTRA_DSO_LDOPTS = \ diff --git a/docshell/shistory/src/nsSHTransaction.cpp b/docshell/shistory/src/nsSHTransaction.cpp index 2e925ba709a1..121197253389 100644 --- a/docshell/shistory/src/nsSHTransaction.cpp +++ b/docshell/shistory/src/nsSHTransaction.cpp @@ -60,19 +60,18 @@ nsSHTransaction::~nsSHTransaction() NS_IMETHODIMP nsSHTransaction::Create(nsISHEntry * aSHEntry, nsISHTransaction * aParent) { - SetCurrentSHistoryEntry(aSHEntry); + SetSHEntry(aSHEntry); if (aParent) { - // This will correctly set the parent child pointers - + // This will correctly set the parent child pointers ((nsSHTransaction *)aParent)->SetChild(this); } else - SetParent(aParent); + SetParent(nsnull); return NS_OK; } NS_IMETHODIMP -nsSHTransaction::GetCurrentSHistoryEntry(nsISHEntry ** aResult) +nsSHTransaction::GetSHEntry(nsISHEntry ** aResult) { NS_ENSURE_ARG_POINTER(aResult); *aResult = mSHEntry; @@ -82,7 +81,7 @@ nsSHTransaction::GetCurrentSHistoryEntry(nsISHEntry ** aResult) nsresult -nsSHTransaction::SetCurrentSHistoryEntry(nsISHEntry * aSHEntry) +nsSHTransaction::SetSHEntry(nsISHEntry * aSHEntry) { NS_IF_RELEASE(mSHEntry); mSHEntry = aSHEntry; @@ -106,20 +105,21 @@ nsSHTransaction::SetChild(nsISHTransaction * aChild) { if (mChild) { // There is already a child. Move the child to the LRV list - if (mLRVList) { - //Delete any old LRV item that was there. - - } - // Make the child the LRV item - + NS_IF_RELEASE(mLRVList); + mLRVList = mChild; + NS_ADDREF(mLRVList); + //SetLRVList(mChild); } + NS_ENSURE_SUCCESS(((nsSHTransaction *)aChild)->SetParent(this), NS_ERROR_FAILURE); + NS_IF_RELEASE(mChild); mChild = aChild; NS_IF_ADDREF(aChild); return NS_OK; } + nsresult nsSHTransaction::SetParent(nsISHTransaction * aParent) { @@ -128,6 +128,17 @@ nsSHTransaction::SetParent(nsISHTransaction * aParent) return NS_OK; } +#if 0 +NS_IMETHODIMP +nsSHTransaction::SetLRVList(nsISHTransaction * aLRVList) { + + NS_IF_RELEASE(mLRVList); + mLRVList = aLRVList; + NS_IF_ADDREF(mLRVList); + return NS_OK; + +} +#endif /* 0 */ nsresult nsSHTransaction::GetParent(nsISHTransaction ** aResult) @@ -138,7 +149,6 @@ nsSHTransaction::GetParent(nsISHTransaction ** aResult) return NS_OK; } - NS_IMETHODIMP nsSHTransaction::GetLrvList(nsISHTransaction ** aResult) { diff --git a/docshell/shistory/src/nsSHTransaction.h b/docshell/shistory/src/nsSHTransaction.h index 92481bffac83..c395529cdffc 100644 --- a/docshell/shistory/src/nsSHTransaction.h +++ b/docshell/shistory/src/nsSHTransaction.h @@ -43,13 +43,15 @@ private: NS_NewSHTransaction(nsISupports * aOuter, REFNSIID aIID, void** aResult); nsresult SetChild(nsISHTransaction * aChild); nsresult SetParent(nsISHTransaction * aParent); - nsresult SetCurrentSHistoryEntry(nsISHEntry * aSHEntry); - - nsISHEntry * mSHEntry; + nsresult SetSHEntry(nsISHEntry * aSHEntry); + //nsresult SetLRVList(nsISHTransaction * aLRVList); + /* Weak reference to parent */ nsISHTransaction * mParent; nsISHTransaction * mChild; nsISHTransaction * mLRVList; + nsISHEntry * mSHEntry; + }; diff --git a/docshell/shistory/src/nsSHistory.cpp b/docshell/shistory/src/nsSHistory.cpp new file mode 100644 index 000000000000..e5faa4b34d5b --- /dev/null +++ b/docshell/shistory/src/nsSHistory.cpp @@ -0,0 +1,191 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is the Mozilla browser. + * + * The Initial Developer of the Original Code is Netscape + * Communications, Inc. Portions created by Netscape are + * Copyright (C) 1999, Mozilla. All Rights Reserved. + * + * Contributor(s): + * Radha Kulkarni + */ + +#include "nsISupportsUtils.h" +#include "nsCOMPtr.h" +#include "nsSHistory.h" +#include "nsIGenericFactory.h" + + +#ifdef XXX_NS_DEBUG // XXX: we'll need a logging facility for debugging +#define WEB_TRACE(_bit,_args) \ + PR_BEGIN_MACRO \ + if (WEB_LOG_TEST(gLogModule,_bit)) { \ + PR_LogPrint _args; \ + } \ + PR_END_MACRO +#else +#define WEB_TRACE(_bit,_args) +#endif + +NS_IMPL_ISUPPORTS1(nsSHistory, nsISHistory) + +nsSHistory::nsSHistory() : mListRoot(nsnull), mIndex(-1), mLength(0) +{ +NS_INIT_REFCNT(); +} + + +nsSHistory::~nsSHistory() +{ + //NS_IF_RELEASE(mListRoot); +} + +/* Add an entry to the History list at mIndex and + * increment the index to point to the new entry + */ +NS_IMETHODIMP +nsSHistory::Add(nsISHEntry * aSHEntry) +{ + nsresult rv; + + NS_PRECONDITION(aSHEntry != nsnull, "null ptr"); + if (! aSHEntry) + return NS_ERROR_NULL_POINTER; + + //nsISHTransaction * txn=nsnull; + nsCOMPtr txn; + rv = nsComponentManager::CreateInstance(NS_SHTRANSACTION_PROGID, + nsnull, + nsISHTransaction::GetIID(), + getter_AddRefs(txn)); + nsCOMPtr parent; + + if (NS_SUCCEEDED(rv) && txn) { + if (mListRoot) { + GetTransactionForIndex(mIndex, getter_AddRefs(parent)); + } + // Set the ShEntry and parent for the transaction. setting the + // parent will properly set the parent child relationship + rv = txn->Create(aSHEntry, parent); + if (NS_SUCCEEDED(rv)) { + mLength++; + mIndex++; + // If this is the very first transaction, initialize the list + if (!mListRoot) + mListRoot = txn; + //NS_ADDREF(txn); + + } + } + return NS_OK; +} + +/* Get length of the history list */ +NS_IMETHODIMP +nsSHistory::GetLength(PRInt32 * aResult) +{ + NS_ENSURE_ARG_POINTER(aResult); + *aResult = mLength; + return NS_OK; +} + +/* Get index of the history list */ +NS_IMETHODIMP +nsSHistory::GetIndex(PRInt32 * aResult) +{ + NS_ENSURE_ARG_POINTER(aResult); + *aResult = mIndex; + return NS_OK; +} + +NS_IMETHODIMP +nsSHistory::GetRootEntry(nsISHTransaction ** aResult) +{ + NS_ENSURE_ARG_POINTER(aResult); + + *aResult = mListRoot; + NS_IF_ADDREF(*aResult); + return NS_OK; +} + +NS_IMETHODIMP +nsSHistory::GetTransactionForIndex(PRInt32 aIndex, nsISHTransaction ** aResult) +{ + nsresult rv; + NS_ENSURE_ARG_POINTER(aResult); + + if (mLength <= 0) + return NS_ERROR_FAILURE; + + if (mListRoot) { + if (aIndex == 0) + { + *aResult = mListRoot; + NS_ADDREF(*aResult); + return NS_OK; + } + PRInt32 cnt=0; + nsCOMPtr tempPtr; + GetRootEntry(getter_AddRefs(tempPtr)); + + while(1) { + nsCOMPtr ptr; + rv = tempPtr->GetChild(getter_AddRefs(ptr)); + if (NS_SUCCEEDED(rv) && ptr) { + cnt++; + if (cnt == aIndex) { + *aResult = ptr; + NS_ADDREF(*aResult); + break; + } + else { + // XXX Not sure if this will maintain reference + tempPtr = ptr; + continue; + } + } //NS_SUCCEEDED + else + return NS_ERROR_FAILURE; + } // while + } // mListRoot + return NS_OK; +} + +NS_IMETHODIMP +NS_NewSHistory(nsISupports* aOuter, REFNSIID aIID, void** aResult) +{ + NS_PRECONDITION(aResult != nsnull, "null ptr"); + if (! aResult) + return NS_ERROR_NULL_POINTER; + + NS_PRECONDITION(aOuter == nsnull, "no aggregation"); + if (aOuter) + return NS_ERROR_NO_AGGREGATION; + + nsresult rv = NS_OK; + + nsSHistory* result = new nsSHistory(); + if (! result) + return NS_ERROR_OUT_OF_MEMORY; + + + rv = result->QueryInterface(aIID, aResult); + + if (NS_FAILED(rv)) { + delete result; + *aResult = nsnull; + return rv; + } + + return rv; +} diff --git a/docshell/shistory/src/nsSHistory.h b/docshell/shistory/src/nsSHistory.h new file mode 100644 index 000000000000..53ec2cabca77 --- /dev/null +++ b/docshell/shistory/src/nsSHistory.h @@ -0,0 +1,54 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is the Mozilla browser. + * + * The Initial Developer of the Original Code is Netscape + * Communications, Inc. Portions created by Netscape are + * Copyright (C) 1999, Mozilla. All Rights Reserved. + * + * Contributor(s): + * Radha Kulkarni + */ + +#ifndef nsSHistory_h +#define nsSHistory_h + +#include "nsCOMPtr.h" +#include "nsISHistory.h" + +class nsISHEntry; +class nsISHTransaction; + +class nsSHistory: public nsISHistory +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSISHISTORY + + nsSHistory(); + +protected: + virtual ~nsSHistory(); + +private: + friend NS_IMETHODIMP + NS_NewSHistory(nsISupports * aOuter, REFNSIID aIID, void** aResult); + NS_IMETHOD GetRootEntry(nsISHTransaction ** aResult); + //nsISHTransaction * mListRoot; + nsCOMPtr mListRoot; + PRInt32 mIndex; + PRInt32 mLength; +}; + + +#endif /* nsSHistory */