From d74ced9377077d446a2cf1b12b69d57ce6e8d37d Mon Sep 17 00:00:00 2001 From: "radha%netscape.com" Date: Fri, 1 Sep 2000 21:29:48 +0000 Subject: [PATCH] partial fix for 49498. r=nisheeth. --- docshell/base/nsDocShell.cpp | 292 +++++++++++++---------------------- dom/src/base/nsHistory.cpp | 37 +++-- 2 files changed, 135 insertions(+), 194 deletions(-) diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index 152836e2023..9dd1fb0609e 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -1125,194 +1125,113 @@ NS_IMETHODIMP nsDocShell::GetGlobalHistory(nsIGlobalHistory** aGlobalHistory) NS_IMETHODIMP nsDocShell::GetCanGoBack(PRBool* aCanGoBack) { -#ifndef SH_IN_FRAMES - NS_ENSURE_ARG_POINTER(aCanGoBack); - *aCanGoBack = PR_FALSE; - if (mSessionHistory == nsnull) { - return NS_OK; - } + nsCOMPtr root; + //Get the root docshell + GetSameTypeRootTreeItem(getter_AddRefs(root)); + NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); + // QI to nsIWebNavigation + nsCOMPtr rootAsWebnav(do_QueryInterface(root)); + if (rootAsWebnav) { + // Get the handle to SH from the root docshell + nsCOMPtr rootSH; + rootAsWebnav->GetSessionHistory(getter_AddRefs(rootSH)); + NS_ENSURE_TRUE(rootSH, NS_ERROR_FAILURE); + // QI SH to nsIWebNavigation + nsCOMPtr webNav(do_QueryInterface(rootSH)); + if (webNav) + return webNav->GetCanGoBack(aCanGoBack); + } + return NS_ERROR_FAILURE; - NS_ENSURE_STATE(mSessionHistory); - - PRInt32 index = -1; - NS_ENSURE_SUCCESS(mSessionHistory->GetIndex(&index), NS_ERROR_FAILURE); - if(index > 0) - *aCanGoBack = PR_TRUE; -#else - if (mSessionHistory) { - nsCOMPtr webNav(do_QueryInterface(mSessionHistory)); - - if (webNav) { - return webNav->GetCanGoBack(aCanGoBack); - } - } - else { - nsCOMPtr root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); - nsCOMPtr rootAsWebnav(do_QueryInterface(root)); - if (rootAsWebnav) { - rootAsWebnav->GetCanGoBack(aCanGoBack); - } - } -#endif - return NS_OK; } NS_IMETHODIMP nsDocShell::GetCanGoForward(PRBool* aCanGoForward) { -#ifndef SH_IN_FRAMES - NS_ENSURE_ARG_POINTER(aCanGoForward); - *aCanGoForward = PR_FALSE; - if (mSessionHistory == nsnull) { - return NS_OK; - } - - NS_ENSURE_STATE(mSessionHistory); - - PRInt32 index = -1; - PRInt32 count = -1; - - NS_ENSURE_SUCCESS(mSessionHistory->GetIndex(&index), NS_ERROR_FAILURE); - NS_ENSURE_SUCCESS(mSessionHistory->GetCount(&count), NS_ERROR_FAILURE); - - if((index >= 0) && (index < (count - 1))) - *aCanGoForward = PR_TRUE; -#else - if (mSessionHistory) { - nsCOMPtr webNav(do_QueryInterface(mSessionHistory)); - - if (webNav) { - return webNav->GetCanGoForward(aCanGoForward); - } - } - else { - nsCOMPtr root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); - nsCOMPtr rootAsWebnav(do_QueryInterface(root)); - if (rootAsWebnav) { - rootAsWebnav->GetCanGoForward(aCanGoForward); - } - } -#endif - return NS_OK; + nsCOMPtr root; + //Get the root docshell + GetSameTypeRootTreeItem(getter_AddRefs(root)); + NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); + // QI to nsIWebNavigation + nsCOMPtr rootAsWebnav(do_QueryInterface(root)); + if (rootAsWebnav) { + // Get the handle to SH from the root docshell + nsCOMPtr rootSH; + rootAsWebnav->GetSessionHistory(getter_AddRefs(rootSH)); + NS_ENSURE_TRUE(rootSH, NS_ERROR_FAILURE); + // QI SH to nsIWebNavigation + nsCOMPtr webNav(do_QueryInterface(rootSH)); + if (webNav) + return webNav->GetCanGoForward(aCanGoForward); + } + return NS_ERROR_FAILURE; + } NS_IMETHODIMP nsDocShell::GoBack() { -#ifndef SH_IN_FRAMES - if (mSessionHistory == nsnull) { - return NS_OK; - } - nsCOMPtr root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - if(root.get() != NS_STATIC_CAST(nsIDocShellTreeItem*, this)) - { - nsCOMPtr rootAsNav(do_QueryInterface(root)); - return rootAsNav->GoBack(); + nsCOMPtr root; + //Get the root docshell + GetSameTypeRootTreeItem(getter_AddRefs(root)); + NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); + // QI to nsIWebNavigation + nsCOMPtr rootAsWebnav(do_QueryInterface(root)); + if (rootAsWebnav) { + // Get the handle to SH from the root docshell + nsCOMPtr rootSH; + rootAsWebnav->GetSessionHistory(getter_AddRefs(rootSH)); + NS_ENSURE_TRUE(rootSH, NS_ERROR_FAILURE); + // QI SH to nsIWebNavigation + nsCOMPtr webNav(do_QueryInterface(rootSH)); + if (webNav) + return webNav->GoBack(); } - - NS_ENSURE_STATE(mSessionHistory); - - - UpdateCurrentSessionHistory(); - - nsCOMPtr previousEntry; - - NS_ENSURE_SUCCESS(mSessionHistory->GetPreviousEntry(PR_TRUE, - getter_AddRefs(previousEntry)), NS_ERROR_FAILURE); - NS_ENSURE_TRUE(previousEntry, NS_ERROR_FAILURE); - + return NS_ERROR_FAILURE; - NS_ENSURE_SUCCESS(LoadHistoryEntry(previousEntry), NS_ERROR_FAILURE); -#else - if (mSessionHistory) { - nsCOMPtr webNav(do_QueryInterface(mSessionHistory)); - - if (webNav) { - return webNav->GoBack(); - } - } - else { - nsCOMPtr root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); - nsCOMPtr rootAsWebnav(do_QueryInterface(root)); - if (rootAsWebnav) { - rootAsWebnav->GoBack(); - } - } -#endif - return NS_OK; } NS_IMETHODIMP nsDocShell::GoForward() { -#ifndef SH_IN_FRAMES - if (mSessionHistory == nsnull) { - return NS_OK; - } - nsCOMPtr root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - if(root.get() != NS_STATIC_CAST(nsIDocShellTreeItem*, this)) - { - nsCOMPtr rootAsNav(do_QueryInterface(root)); - return rootAsNav->GoForward(); - } - - NS_ENSURE_STATE(mSessionHistory); + nsCOMPtr root; + //Get the root docshell + GetSameTypeRootTreeItem(getter_AddRefs(root)); + NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); + // QI to nsIWebNavigation + nsCOMPtr rootAsWebnav(do_QueryInterface(root)); + if (rootAsWebnav) { + // Get the handle to SH from the root docshell + nsCOMPtr rootSH; + rootAsWebnav->GetSessionHistory(getter_AddRefs(rootSH)); + NS_ENSURE_TRUE(rootSH, NS_ERROR_FAILURE); + // QI SH to nsIWebNavigation + nsCOMPtr webNav(do_QueryInterface(rootSH)); + if (webNav) + return webNav->GoForward(); + } + return NS_ERROR_FAILURE; - UpdateCurrentSessionHistory(); - - nsCOMPtr nextEntry; - - NS_ENSURE_SUCCESS(mSessionHistory->GetNextEntry(PR_TRUE, - getter_AddRefs(nextEntry)), NS_ERROR_FAILURE); - NS_ENSURE_TRUE(nextEntry, NS_ERROR_FAILURE); - - - NS_ENSURE_SUCCESS(LoadHistoryEntry(nextEntry), NS_ERROR_FAILURE); -#else - if (mSessionHistory) { - nsCOMPtr webNav(do_QueryInterface(mSessionHistory)); - - if (webNav) { - return webNav->GoForward(); - } - } - else { - nsCOMPtr root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); - nsCOMPtr rootAsWebnav(do_QueryInterface(root)); - if (rootAsWebnav) { - rootAsWebnav->GoForward(); - } - } -#endif - return NS_OK; } NS_IMETHODIMP nsDocShell::GotoIndex(PRInt32 aIndex) { - if (mSessionHistory) { - nsCOMPtrwebNav(do_QueryInterface(mSessionHistory)); - if (webNav) { - return webNav->GotoIndex(aIndex); - } - } - else { - nsCOMPtr root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); - nsCOMPtr rootAsWebnav(do_QueryInterface(root)); - if (rootAsWebnav) { - rootAsWebnav->GotoIndex(aIndex); - } - } - return NS_OK; + nsCOMPtr root; + //Get the root docshell + GetSameTypeRootTreeItem(getter_AddRefs(root)); + NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); + // QI to nsIWebNavigation + nsCOMPtr rootAsWebnav(do_QueryInterface(root)); + if (rootAsWebnav) { + // Get the handle to SH from the root docshell + nsCOMPtr rootSH; + rootAsWebnav->GetSessionHistory(getter_AddRefs(rootSH)); + NS_ENSURE_TRUE(rootSH, NS_ERROR_FAILURE); + // QI SH to nsIWebNavigation + nsCOMPtr webNav(do_QueryInterface(rootSH)); + if (webNav) + return webNav->GotoIndex(aIndex); + } + return NS_ERROR_FAILURE; + } @@ -1480,35 +1399,38 @@ NS_IMETHODIMP nsDocShell::GetCurrentURI(nsIURI** aURI) NS_IMETHODIMP nsDocShell::SetSessionHistory(nsISHistory* aSessionHistory) { - mSessionHistory = aSessionHistory; -#if defined(SH_IN_FRAMES) - if (mSessionHistory) { - mSessionHistory->SetRootDocShell(this); - } -#endif /* SH_IN_FRAMES */ + NS_ENSURE_TRUE(aSessionHistory, NS_ERROR_FAILURE); + // make sure that we are the root docshell and + // set a handle to root docshell in SH. + + nsCOMPtr root; + /* Get the root docshell. If *this* is the root docshell + * then save a handle to *this* in SH. SH needs it to do + * traversions thro' its entries + */ + GetSameTypeRootTreeItem(getter_AddRefs(root)); + NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); + if (root.get() == NS_STATIC_CAST(nsIDocShellTreeItem *, this)) { + mSessionHistory = aSessionHistory; + mSessionHistory->SetRootDocShell(this); + return NS_OK; + } + return NS_ERROR_FAILURE; - return NS_OK; } NS_IMETHODIMP nsDocShell::GetSessionHistory(nsISHistory** aSessionHistory) { NS_ENSURE_ARG_POINTER(aSessionHistory); - if (mSessionHistory) { *aSessionHistory = mSessionHistory; NS_IF_ADDREF(*aSessionHistory); + return NS_OK; } - else { - nsCOMPtr root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); - nsCOMPtr rootAsWebnav(do_QueryInterface(root)); - if (rootAsWebnav) { - rootAsWebnav->GetSessionHistory(aSessionHistory); - } - } - return NS_OK; + + return NS_ERROR_FAILURE; + } //***************************************************************************** diff --git a/dom/src/base/nsHistory.cpp b/dom/src/base/nsHistory.cpp index a2633aa63b3..3cc96ce305e 100644 --- a/dom/src/base/nsHistory.cpp +++ b/dom/src/base/nsHistory.cpp @@ -28,6 +28,7 @@ #include "nsIScriptGlobalObject.h" #include "nsIWebShell.h" #include "nsIDocShell.h" +#include "nsIDocShellTreeItem.h" #include "nsIWebNavigation.h" #include "nsISHistory.h" #include "nsISHEntry.h" @@ -166,16 +167,34 @@ HistoryImpl::Forward() NS_IMETHODIMP HistoryImpl::Go(JSContext* cx, jsval* argv, PRUint32 argc) { - nsresult result = NS_OK; - nsCOMPtr sHistory; - - //Get nsIWebNavigation from docshell - nsCOMPtr webNav(do_QueryInterface(mDocShell)); - NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE); + nsresult result = NS_OK; + nsCOMPtr sHistory; + + NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE); + /* The docshell we have may or may not be + * the root docshell. So, get a handle to + * SH from the root docshell; + */ + // QI mDocShell to nsIDocShellTreeItem + nsCOMPtr dsTreeItem(do_QueryInterface(mDocShell)); + NS_ENSURE_TRUE(dsTreeItem, NS_ERROR_FAILURE); - //Get sHistory from nsIWebNavigation - webNav->GetSessionHistory(getter_AddRefs(sHistory)); - NS_ENSURE_TRUE(sHistory, NS_ERROR_FAILURE); + // Get the root DocShell from it + nsCOMPtr root; + dsTreeItem->GetSameTypeRootTreeItem(getter_AddRefs(root)); + NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); + + //QI root to nsIWebNavigation + nsCOMPtr webNav(do_QueryInterface(root)); + NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE); + + //Get SH from nsIWebNavigation + webNav->GetSessionHistory(getter_AddRefs(sHistory)); + NS_ENSURE_TRUE(sHistory, NS_ERROR_FAILURE); + + // QI SHistory to nsIWebNavigation + nsCOMPtr shWebnav(do_QueryInterface(sHistory)); + NS_ENSURE_TRUE(shWebnav, NS_ERROR_FAILURE); if (argc > 0) { if (JSVAL_IS_INT(argv[0])) {