From 7064f6232714c762fa2b8d8a20987d8ce7724b99 Mon Sep 17 00:00:00 2001 From: "cbiesinger%web.de" Date: Sun, 19 Sep 2004 20:50:32 +0000 Subject: [PATCH] Bug 128398 store referrer in history patch by James Andrewartha r=biesi sr=neil git-svn-id: svn://10.0.0.236/trunk@162533 18797224-902f-48f8-a5cc-f745e15eee43 --- .../browser/base/content/contentAreaUtils.js | 2 +- mozilla/docshell/base/nsDocShell.cpp | 18 ++++++-- mozilla/docshell/base/nsDocShell.h | 2 +- .../docshell/base/nsGlobalHistory2Adapter.cpp | 2 +- .../docshell/base/nsGlobalHistoryAdapter.cpp | 2 +- mozilla/docshell/base/nsIGlobalHistory2.idl | 8 ++-- .../history/src/nsGlobalHistory.cpp | 44 +++++++++++++++---- .../components/history/src/nsGlobalHistory.h | 4 +- .../history/src/nsGlobalHistory.cpp | 30 ++++++++++--- .../components/history/src/nsGlobalHistory.h | 2 + 10 files changed, 88 insertions(+), 26 deletions(-) diff --git a/mozilla/browser/base/content/contentAreaUtils.js b/mozilla/browser/base/content/contentAreaUtils.js index f61ea60b4a7..99f6d7de59b 100644 --- a/mozilla/browser/base/content/contentAreaUtils.js +++ b/mozilla/browser/base/content/contentAreaUtils.js @@ -68,7 +68,7 @@ function markLinkVisited(href, linkNode) var uri = makeURL(href); if (!globalHistory.isVisited(uri)) { - globalHistory.addURI(uri, false, true); + globalHistory.addURI(uri, false, true, null); var oldHref = linkNode.getAttribute("href"); if (typeof oldHref == "string") { // Use setAttribute instead of direct assignment. diff --git a/mozilla/docshell/base/nsDocShell.cpp b/mozilla/docshell/base/nsDocShell.cpp index 8be60bcf3e5..21cc300b74c 100644 --- a/mozilla/docshell/base/nsDocShell.cpp +++ b/mozilla/docshell/base/nsDocShell.cpp @@ -4272,11 +4272,16 @@ nsDocShell::OnStateChange(nsIWebProgress * aProgress, nsIRequest * aRequest, if (channel) { // Get the uri from the channel nsCOMPtr uri; + nsCOMPtr referrer; channel->GetURI(getter_AddRefs(uri)); + // Get the referrer uri from the channel + nsCOMPtr httpchannel(do_QueryInterface(aRequest)); + if (httpchannel) + httpchannel->GetReferrer(getter_AddRefs(referrer)); // Add the original url to global History so that // visited url color changes happen. if (uri) - AddToGlobalHistory(uri, PR_TRUE); + AddToGlobalHistory(uri, PR_TRUE, referrer); } // channel } // aProgress } @@ -6138,7 +6143,12 @@ nsDocShell::OnNewURI(nsIURI * aURI, nsIChannel * aChannel, } // Update Global history - AddToGlobalHistory(aURI, PR_FALSE); + // Get the referrer uri from the channel + nsCOMPtr referrer; + nsCOMPtr httpchannel(do_QueryInterface(aChannel)); + if (httpchannel) + httpchannel->GetReferrer(getter_AddRefs(referrer)); + AddToGlobalHistory(aURI, PR_FALSE, referrer); } // If this was a history load, update the index in @@ -6637,7 +6647,7 @@ NS_IMETHODIMP nsDocShell::MakeEditable(PRBool inWaitForUriLoad) } nsresult -nsDocShell::AddToGlobalHistory(nsIURI * aURI, PRBool aRedirect) +nsDocShell::AddToGlobalHistory(nsIURI * aURI, PRBool aRedirect, nsIURI * aReferrer) { if (mItemType != typeContent) return NS_OK; @@ -6645,7 +6655,7 @@ nsDocShell::AddToGlobalHistory(nsIURI * aURI, PRBool aRedirect) if (!mGlobalHistory) return NS_OK; - return mGlobalHistory->AddURI(aURI, aRedirect, !IsFrame()); + return mGlobalHistory->AddURI(aURI, aRedirect, !IsFrame(), aReferrer); } //***************************************************************************** diff --git a/mozilla/docshell/base/nsDocShell.h b/mozilla/docshell/base/nsDocShell.h index 9271c6f4c6c..f6f8ce0e34b 100644 --- a/mozilla/docshell/base/nsDocShell.h +++ b/mozilla/docshell/base/nsDocShell.h @@ -283,7 +283,7 @@ protected: PRBool ShouldDiscardLayoutState(nsIHttpChannel * aChannel); // Global History - nsresult AddToGlobalHistory(nsIURI * aURI, PRBool aRedirect); + nsresult AddToGlobalHistory(nsIURI * aURI, PRBool aRedirect, nsIURI * aReferrer); // Helper Routines NS_IMETHOD GetPromptAndStringBundle(nsIPrompt ** aPrompt, diff --git a/mozilla/docshell/base/nsGlobalHistory2Adapter.cpp b/mozilla/docshell/base/nsGlobalHistory2Adapter.cpp index d5159b4e47c..3beba6256d8 100644 --- a/mozilla/docshell/base/nsGlobalHistory2Adapter.cpp +++ b/mozilla/docshell/base/nsGlobalHistory2Adapter.cpp @@ -153,7 +153,7 @@ nsGlobalHistory2Adapter::AddPage(const char* aURI) rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(aURI)); if (NS_SUCCEEDED(rv)) { - rv = mHistory->AddURI(uri, PR_FALSE, PR_FALSE); + rv = mHistory->AddURI(uri, PR_FALSE, PR_FALSE, nsnull); } return rv; diff --git a/mozilla/docshell/base/nsGlobalHistoryAdapter.cpp b/mozilla/docshell/base/nsGlobalHistoryAdapter.cpp index b0c704bf76f..dc70c5e36da 100644 --- a/mozilla/docshell/base/nsGlobalHistoryAdapter.cpp +++ b/mozilla/docshell/base/nsGlobalHistoryAdapter.cpp @@ -139,7 +139,7 @@ NS_IMPL_ISUPPORTS1(nsGlobalHistoryAdapter, nsIGlobalHistory2) NS_IMETHODIMP nsGlobalHistoryAdapter::AddURI(nsIURI* aURI, PRBool aRedirect, - PRBool aToplevel) + PRBool aToplevel, nsIURI* aReferrer) { NS_ENSURE_ARG_POINTER(aURI); nsresult rv; diff --git a/mozilla/docshell/base/nsIGlobalHistory2.idl b/mozilla/docshell/base/nsIGlobalHistory2.idl index 541b09ae2a5..89847b9b398 100644 --- a/mozilla/docshell/base/nsIGlobalHistory2.idl +++ b/mozilla/docshell/base/nsIGlobalHistory2.idl @@ -45,7 +45,7 @@ #include "nsISupports.idl" interface nsIURI; -[scriptable, uuid(c4d143b7-22d7-4760-b9a0-ac942016f189)] +[scriptable, uuid(cf777d42-1270-4b34-be7b-2931c93feda5)] interface nsIGlobalHistory2 : nsISupports { /** @@ -56,12 +56,14 @@ interface nsIGlobalHistory2 : nsISupports * this is 'true' for the original URI which is * redirected. * @param aToplevel whether the URI is loaded in a top-level window + * @param aReferrer the URI of the referring page * * @note Docshell will not filter out URI schemes like chrome: data: * about: and view-source:. Embedders should consider filtering out - * these schemes and others, e.g. mailbox: + * these schemes and others, e.g. mailbox: for the main URI and the + * referrer. */ - void addURI(in nsIURI aURI, in boolean aRedirect, in boolean aToplevel); + void addURI(in nsIURI aURI, in boolean aRedirect, in boolean aToplevel, in nsIURI aReferrer); /** * Checks to see whether the given URI is in history. diff --git a/mozilla/toolkit/components/history/src/nsGlobalHistory.cpp b/mozilla/toolkit/components/history/src/nsGlobalHistory.cpp index 80a828065ce..9ff922d09bf 100644 --- a/mozilla/toolkit/components/history/src/nsGlobalHistory.cpp +++ b/mozilla/toolkit/components/history/src/nsGlobalHistory.cpp @@ -601,16 +601,16 @@ NS_IMPL_ISUPPORTS7(nsGlobalHistory, // NS_IMETHODIMP -nsGlobalHistory::AddURI(nsIURI *aURI, PRBool aRedirect, PRBool aTopLevel) +nsGlobalHistory::AddURI(nsIURI *aURI, PRBool aRedirect, PRBool aTopLevel, nsIURI *aReferrer) { PRInt64 now = GetNow(); - return AddPageToDatabase(aURI, aRedirect, aTopLevel, now); + return AddPageToDatabase(aURI, aRedirect, aTopLevel, now, aReferrer); } nsresult nsGlobalHistory::AddPageToDatabase(nsIURI* aURI, PRBool aRedirect, PRBool aTopLevel, - PRInt64 aLastVisitDate) + PRInt64 aLastVisitDate, nsIURI *aReferrer) { nsresult rv; NS_ENSURE_ARG_POINTER(aURI); @@ -677,7 +677,7 @@ nsGlobalHistory::AddPageToDatabase(nsIURI* aURI, PRBool aRedirect, PRBool aTopLe // update the database, and get the old info back PRInt64 oldDate; PRInt32 oldCount; - rv = AddExistingPageToDatabase(row, aLastVisitDate, &oldDate, &oldCount); + rv = AddExistingPageToDatabase(row, aLastVisitDate, aReferrer, &oldDate, &oldCount); NS_ASSERTION(NS_SUCCEEDED(rv), "AddExistingPageToDatabase failed; see bug 88961"); if (NS_FAILED(rv)) return rv; @@ -687,7 +687,7 @@ nsGlobalHistory::AddPageToDatabase(nsIURI* aURI, PRBool aRedirect, PRBool aTopLe } else { rv = AddNewPageToDatabase(aURI, aLastVisitDate, aRedirect, - aTopLevel, getter_AddRefs(row)); + aTopLevel, aReferrer, getter_AddRefs(row)); NS_ASSERTION(NS_SUCCEEDED(rv), "AddNewPageToDatabase failed; see bug 88961"); if (NS_FAILED(rv)) return rv; @@ -723,16 +723,24 @@ nsGlobalHistory::AddPageToDatabase(nsIURI* aURI, PRBool aRedirect, PRBool aTopLe nsresult nsGlobalHistory::AddExistingPageToDatabase(nsIMdbRow *row, PRInt64 aDate, + nsIURI* aReferrer, PRInt64 *aOldDate, PRInt32 *aOldCount) { nsresult rv; + nsCAutoString oldReferrer; - // if the page was typed, unhide it now because it's nsCAutoString URISpec; rv = GetRowValue(row, kToken_URLColumn, URISpec); NS_ENSURE_SUCCESS(rv, rv); + nsCAutoString referrerSpec; + if (aReferrer) { + rv = aReferrer->GetSpec(referrerSpec); + NS_ENSURE_SUCCESS(rv, rv); + } + + // if the page was typed, unhide it now because it's // known to be valid if (HasCell(mEnv, row, kToken_TypedColumn)) row->CutColumn(mEnv, kToken_HiddenColumn); @@ -751,6 +759,13 @@ nsGlobalHistory::AddExistingPageToDatabase(nsIMdbRow *row, SetRowValue(row, kToken_LastVisitDateColumn, aDate); SetRowValue(row, kToken_VisitCountColumn, (*aOldCount) + 1); + if (aReferrer) { + rv = GetRowValue(row, kToken_ReferrerColumn, oldReferrer); + // No referrer? Now there is! + if ((NS_FAILED(rv) || oldReferrer.IsEmpty())) + SetRowValue(row, kToken_ReferrerColumn, referrerSpec.get()); + } + // Notify observers nsCOMPtr url; rv = gRDFService->GetResource(URISpec, getter_AddRefs(url)); @@ -789,6 +804,7 @@ nsGlobalHistory::AddNewPageToDatabase(nsIURI* aURI, PRInt64 aDate, PRBool aRedirect, PRBool aTopLevel, + nsIURI* aReferrer, nsIMdbRow **aResult) { mdb_err err; @@ -799,6 +815,12 @@ nsGlobalHistory::AddNewPageToDatabase(nsIURI* aURI, nsresult rv = aURI->GetSpec(URISpec); NS_ENSURE_SUCCESS(rv, rv); + nsCAutoString referrerSpec; + if (aReferrer) { + rv = aReferrer->GetSpec(referrerSpec); + NS_ENSURE_SUCCESS(rv, rv); + } + // Create a new row mdbOid rowId; rowId.mOid_Scope = kToken_HistoryRowScope; @@ -819,6 +841,10 @@ nsGlobalHistory::AddNewPageToDatabase(nsIURI* aURI, SetRowValue(row, kToken_LastVisitDateColumn, aDate); SetRowValue(row, kToken_FirstVisitDateColumn, aDate); + // Set the referrer if there is one. + if (aReferrer) + SetRowValue(row, kToken_ReferrerColumn, referrerSpec.get()); + nsCOMPtr uri; NS_NewURI(getter_AddRefs(uri), URISpec, nsnull, nsnull); nsCAutoString hostname; @@ -1060,7 +1086,7 @@ NS_IMETHODIMP nsGlobalHistory::AddPageWithDetails(nsIURI *aURI, const PRUnichar *aTitle, PRInt64 aLastVisitDate) { - nsresult rv = AddPageToDatabase(aURI, PR_FALSE, PR_TRUE, aLastVisitDate); + nsresult rv = AddPageToDatabase(aURI, PR_FALSE, PR_TRUE, aLastVisitDate, nsnull); if (NS_FAILED(rv)) return rv; return SetPageTitle(aURI, nsDependentString(aTitle)); @@ -1346,7 +1372,7 @@ nsGlobalHistory::HidePage(nsIURI *aURI) if (NS_FAILED(rv)) { // it hasn't been visited yet, but if one ever comes in, we need // to hide it when it is visited - rv = AddURI(aURI, PR_FALSE, PR_FALSE); + rv = AddURI(aURI, PR_FALSE, PR_FALSE, nsnull); if (NS_FAILED(rv)) return rv; rv = FindRow(kToken_URLColumn, URISpec.get(), getter_AddRefs(row)); @@ -1375,7 +1401,7 @@ nsGlobalHistory::MarkPageAsTyped(nsIURI *aURI) nsCOMPtr row; rv = FindRow(kToken_URLColumn, spec.get(), getter_AddRefs(row)); if (NS_FAILED(rv)) { - rv = AddNewPageToDatabase(aURI, GetNow(), PR_FALSE, PR_TRUE, getter_AddRefs(row)); + rv = AddNewPageToDatabase(aURI, GetNow(), PR_FALSE, PR_TRUE, nsnull, getter_AddRefs(row)); NS_ENSURE_SUCCESS(rv, rv); // We don't know if this is a valid URI yet. Hide it until it finishes diff --git a/mozilla/toolkit/components/history/src/nsGlobalHistory.h b/mozilla/toolkit/components/history/src/nsGlobalHistory.h index 6855a59259f..e64ecc432d7 100644 --- a/mozilla/toolkit/components/history/src/nsGlobalHistory.h +++ b/mozilla/toolkit/components/history/src/nsGlobalHistory.h @@ -302,15 +302,17 @@ protected: // AddPage-oriented stuff // nsresult AddPageToDatabase(nsIURI* aURI, PRBool aRedirect, PRBool aTopLevel, - PRInt64 aLastVisitDate); + PRInt64 aLastVisitDate, nsIURI * aReferrer); nsresult AddExistingPageToDatabase(nsIMdbRow *row, PRInt64 aDate, + nsIURI *aReferrer, PRInt64 *aOldDate, PRInt32 *aOldCount); nsresult AddNewPageToDatabase(nsIURI* aURI, PRInt64 aDate, PRBool aRedirect, PRBool aTopLevel, + nsIURI *aReferrer, nsIMdbRow **aResult); nsresult RemovePageInternal(const char *aSpec); diff --git a/mozilla/xpfe/components/history/src/nsGlobalHistory.cpp b/mozilla/xpfe/components/history/src/nsGlobalHistory.cpp index b9d98ed3e1d..932205e596a 100644 --- a/mozilla/xpfe/components/history/src/nsGlobalHistory.cpp +++ b/mozilla/xpfe/components/history/src/nsGlobalHistory.cpp @@ -587,7 +587,7 @@ NS_IMPL_ISUPPORTS7(nsGlobalHistory, NS_IMETHODIMP -nsGlobalHistory::AddURI(nsIURI *aURI, PRBool aRedirect, PRBool aTopLevel) +nsGlobalHistory::AddURI(nsIURI *aURI, PRBool aRedirect, PRBool aTopLevel, nsIURI *aReferrer) { nsresult rv; NS_ENSURE_ARG_POINTER(aURI); @@ -635,6 +635,12 @@ nsGlobalHistory::AddURI(nsIURI *aURI, PRBool aRedirect, PRBool aTopLevel) rv = aURI->GetSpec(URISpec); NS_ENSURE_SUCCESS(rv, rv); + nsCAutoString referrerSpec; + if (aReferrer) { + rv = aReferrer->GetSpec(referrerSpec); + NS_ENSURE_SUCCESS(rv, rv); + } + PRInt64 now = GetNow(); // For notifying observers, later... @@ -654,7 +660,7 @@ nsGlobalHistory::AddURI(nsIURI *aURI, PRBool aRedirect, PRBool aTopLevel) // update the database, and get the old info back PRInt64 oldDate; PRInt32 oldCount; - rv = AddExistingPageToDatabase(row, now, &oldDate, &oldCount); + rv = AddExistingPageToDatabase(row, now, referrerSpec.get(), &oldDate, &oldCount); NS_ASSERTION(NS_SUCCEEDED(rv), "AddExistingPageToDatabase failed; see bug 88961"); if (NS_FAILED(rv)) return rv; @@ -683,7 +689,7 @@ nsGlobalHistory::AddURI(nsIURI *aURI, PRBool aRedirect, PRBool aTopLevel) } else { - rv = AddNewPageToDatabase(URISpec.get(), now, getter_AddRefs(row)); + rv = AddNewPageToDatabase(URISpec.get(), now, referrerSpec.get(), getter_AddRefs(row)); NS_ASSERTION(NS_SUCCEEDED(rv), "AddNewPageToDatabase failed; see bug 88961"); if (NS_FAILED(rv)) return rv; @@ -739,10 +745,12 @@ nsGlobalHistory::AddURI(nsIURI *aURI, PRBool aRedirect, PRBool aTopLevel) nsresult nsGlobalHistory::AddExistingPageToDatabase(nsIMdbRow *row, PRInt64 aDate, + const char *aReferrer, PRInt64 *aOldDate, PRInt32 *aOldCount) { nsresult rv; + nsCAutoString oldReferrer; // if the page was typed, unhide it now because it's // known to be valid @@ -763,12 +771,20 @@ nsGlobalHistory::AddExistingPageToDatabase(nsIMdbRow *row, SetRowValue(row, kToken_LastVisitDateColumn, aDate); SetRowValue(row, kToken_VisitCountColumn, (*aOldCount) + 1); + if (aReferrer && *aReferrer) { + rv = GetRowValue(row, kToken_ReferrerColumn, oldReferrer); + // No referrer? Now there is! + if (NS_FAILED(rv) || oldReferrer.IsEmpty()) + SetRowValue(row, kToken_ReferrerColumn, aReferrer); + } + return NS_OK; } nsresult nsGlobalHistory::AddNewPageToDatabase(const char *aURL, PRInt64 aDate, + const char *aReferrer, nsIMdbRow **aResult) { mdb_err err; @@ -793,6 +809,10 @@ nsGlobalHistory::AddNewPageToDatabase(const char *aURL, SetRowValue(row, kToken_LastVisitDateColumn, aDate); SetRowValue(row, kToken_FirstVisitDateColumn, aDate); + // Set the referrer if there is one. + if (aReferrer && *aReferrer) + SetRowValue(row, kToken_ReferrerColumn, aReferrer); + nsCOMPtr uri; NS_NewURI(getter_AddRefs(uri), nsDependentCString(aURL), nsnull, nsnull); nsCAutoString hostname; @@ -1350,7 +1370,7 @@ nsGlobalHistory::HidePage(nsIURI *aURI) if (NS_FAILED(rv)) { // it hasn't been visited yet, but if one ever comes in, we need // to hide it when it is visited - rv = AddURI(aURI, PR_FALSE, PR_FALSE); + rv = AddURI(aURI, PR_FALSE, PR_FALSE, nsnull); if (NS_FAILED(rv)) return rv; rv = FindRow(kToken_URLColumn, URISpec.get(), getter_AddRefs(row)); @@ -1379,7 +1399,7 @@ nsGlobalHistory::MarkPageAsTyped(nsIURI *aURI) nsCOMPtr row; rv = FindRow(kToken_URLColumn, spec.get(), getter_AddRefs(row)); if (NS_FAILED(rv)) { - rv = AddNewPageToDatabase(spec.get(), GetNow(), getter_AddRefs(row)); + rv = AddNewPageToDatabase(spec.get(), GetNow(), nsnull, getter_AddRefs(row)); NS_ENSURE_SUCCESS(rv, rv); // We don't know if this is a valid URI yet. Hide it until it finishes diff --git a/mozilla/xpfe/components/history/src/nsGlobalHistory.h b/mozilla/xpfe/components/history/src/nsGlobalHistory.h index 62709556ee2..96f415ea90a 100644 --- a/mozilla/xpfe/components/history/src/nsGlobalHistory.h +++ b/mozilla/xpfe/components/history/src/nsGlobalHistory.h @@ -305,10 +305,12 @@ protected: // nsresult AddExistingPageToDatabase(nsIMdbRow *row, PRInt64 aDate, + const char *aReferrer, PRInt64 *aOldDate, PRInt32 *aOldCount); nsresult AddNewPageToDatabase(const char *aURL, PRInt64 aDate, + const char *aReferrer, nsIMdbRow **aResult); nsresult RemovePageInternal(const char *aSpec);