Back to index

lightning-sunbird  0.9+nobinonly
nsHistory.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Travis Bogard <travis@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "nscore.h"
00041 #include "nsHistory.h"
00042 #include "nsIDOMWindowInternal.h"
00043 #include "nsPIDOMWindow.h"
00044 #include "nsIScriptGlobalObject.h"
00045 #include "nsIDOMDocument.h"
00046 #include "nsIDocument.h"
00047 #include "nsIPresShell.h"
00048 #include "nsPresContext.h"
00049 #include "nsIDocShell.h"
00050 #include "nsIDocShellTreeItem.h"
00051 #include "nsIWebNavigation.h"
00052 #include "nsIHistoryEntry.h"
00053 #include "nsIURI.h"
00054 #include "nsIServiceManager.h"
00055 #include "nsIInterfaceRequestorUtils.h"
00056 #include "nsXPIDLString.h"
00057 #include "nsReadableUtils.h"
00058 #include "nsDOMClassInfo.h"
00059 #include "nsContentUtils.h"
00060 
00061 //
00062 //  History class implementation 
00063 //
00064 nsHistory::nsHistory(nsIDocShell* aDocShell) : mDocShell(aDocShell)
00065 {
00066 }
00067 
00068 nsHistory::~nsHistory()
00069 {
00070 }
00071 
00072 
00073 // QueryInterface implementation for nsHistory
00074 NS_INTERFACE_MAP_BEGIN(nsHistory)
00075   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMHistory)
00076   NS_INTERFACE_MAP_ENTRY(nsIDOMHistory)
00077   NS_INTERFACE_MAP_ENTRY(nsIDOMNSHistory)
00078   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(History)
00079 NS_INTERFACE_MAP_END
00080 
00081 
00082 NS_IMPL_ADDREF(nsHistory)
00083 NS_IMPL_RELEASE(nsHistory)
00084 
00085 
00086 void
00087 nsHistory::SetDocShell(nsIDocShell *aDocShell)
00088 {
00089   mDocShell = aDocShell; // Weak Reference
00090 }
00091 
00092 NS_IMETHODIMP
00093 nsHistory::GetLength(PRInt32* aLength)
00094 {
00095   nsCOMPtr<nsISHistory>   sHistory;
00096 
00097   // Get session History from docshell
00098   GetSessionHistoryFromDocShell(mDocShell, getter_AddRefs(sHistory));
00099   NS_ENSURE_TRUE(sHistory, NS_ERROR_FAILURE);
00100   return sHistory->GetCount(aLength);
00101 }
00102 
00103 NS_IMETHODIMP
00104 nsHistory::GetCurrent(nsAString& aCurrent)
00105 {
00106   PRInt32 curIndex=0;
00107   nsCAutoString curURL;
00108   nsCOMPtr<nsISHistory> sHistory;
00109 
00110   // Get SessionHistory from docshell
00111   GetSessionHistoryFromDocShell(mDocShell, getter_AddRefs(sHistory));
00112   NS_ENSURE_TRUE(sHistory, NS_ERROR_FAILURE);
00113 
00114   // Get the current index at session History
00115   sHistory->GetIndex(&curIndex);
00116   nsCOMPtr<nsIHistoryEntry> curEntry;
00117   nsCOMPtr<nsIURI>     uri;
00118 
00119   // Get the SH entry for the current index
00120   sHistory->GetEntryAtIndex(curIndex, PR_FALSE, getter_AddRefs(curEntry));
00121   NS_ENSURE_TRUE(curEntry, NS_ERROR_FAILURE);
00122 
00123   // Get the URI for the current entry
00124   curEntry->GetURI(getter_AddRefs(uri));
00125   NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
00126   uri->GetSpec(curURL);
00127   CopyUTF8toUTF16(curURL, aCurrent);
00128 
00129   return NS_OK;
00130 }
00131 
00132 NS_IMETHODIMP
00133 nsHistory::GetPrevious(nsAString& aPrevious)
00134 {
00135   PRInt32 curIndex;
00136   nsCAutoString prevURL;
00137   nsCOMPtr<nsISHistory>  sHistory;
00138 
00139   // Get session History from docshell
00140   GetSessionHistoryFromDocShell(mDocShell, getter_AddRefs(sHistory));
00141   NS_ENSURE_TRUE(sHistory, NS_ERROR_FAILURE);
00142 
00143   // Get the current index at session History
00144   sHistory->GetIndex(&curIndex);
00145   nsCOMPtr<nsIHistoryEntry> prevEntry;
00146   nsCOMPtr<nsIURI>     uri;
00147 
00148   // Get the previous SH entry
00149   sHistory->GetEntryAtIndex((curIndex-1), PR_FALSE, getter_AddRefs(prevEntry));
00150   NS_ENSURE_TRUE(prevEntry, NS_ERROR_FAILURE);
00151 
00152   // Get the URI for the previous entry
00153   prevEntry->GetURI(getter_AddRefs(uri));
00154   NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
00155   uri->GetSpec(prevURL);
00156   CopyUTF8toUTF16(prevURL, aPrevious);
00157 
00158   return NS_OK;
00159 }
00160 
00161 NS_IMETHODIMP
00162 nsHistory::GetNext(nsAString& aNext)
00163 {
00164   PRInt32 curIndex;
00165   nsCAutoString nextURL;
00166   nsCOMPtr<nsISHistory>  sHistory;
00167 
00168   // Get session History from docshell
00169   GetSessionHistoryFromDocShell(mDocShell, getter_AddRefs(sHistory));
00170   NS_ENSURE_TRUE(sHistory, NS_ERROR_FAILURE);
00171 
00172   // Get the current index at session History
00173   sHistory->GetIndex(&curIndex);
00174   nsCOMPtr<nsIHistoryEntry> nextEntry;
00175   nsCOMPtr<nsIURI>     uri;
00176 
00177   // Get the next SH entry
00178   sHistory->GetEntryAtIndex((curIndex+1), PR_FALSE, getter_AddRefs(nextEntry));
00179   NS_ENSURE_TRUE(nextEntry, NS_ERROR_FAILURE);
00180 
00181   // Get the URI for the next entry
00182   nextEntry->GetURI(getter_AddRefs(uri));
00183   NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
00184   uri->GetSpec(nextURL); 
00185   CopyUTF8toUTF16(nextURL, aNext);
00186 
00187   return NS_OK;
00188 }
00189 
00190 NS_IMETHODIMP
00191 nsHistory::Back()
00192 {
00193   nsCOMPtr<nsISHistory>  sHistory;
00194 
00195   GetSessionHistoryFromDocShell(mDocShell, getter_AddRefs(sHistory));
00196   NS_ENSURE_TRUE(sHistory, NS_ERROR_FAILURE);
00197 
00198   //QI SHistory to WebNavigation
00199   nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(sHistory));
00200   NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
00201   webNav->GoBack();
00202 
00203   return NS_OK;
00204 }
00205 
00206 NS_IMETHODIMP
00207 nsHistory::Forward()
00208 {
00209   nsCOMPtr<nsISHistory>  sHistory;
00210 
00211   GetSessionHistoryFromDocShell(mDocShell, getter_AddRefs(sHistory));
00212   NS_ENSURE_TRUE(sHistory, NS_ERROR_FAILURE);
00213 
00214   //QI SHistory to WebNavigation
00215   nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(sHistory));
00216   NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
00217   webNav->GoForward();
00218 
00219   return NS_OK;
00220 }
00221 
00222 NS_IMETHODIMP
00223 nsHistory::Go(PRInt32 aDelta)
00224 {
00225   nsCOMPtr<nsISHistory> session_history;
00226 
00227   GetSessionHistoryFromDocShell(mDocShell, getter_AddRefs(session_history));
00228   NS_ENSURE_TRUE(session_history, NS_ERROR_FAILURE);
00229 
00230   // QI SHistory to nsIWebNavigation
00231   nsCOMPtr<nsIWebNavigation> webnav(do_QueryInterface(session_history));
00232   NS_ENSURE_TRUE(webnav, NS_ERROR_FAILURE);
00233 
00234   PRInt32 curIndex=-1;
00235   PRInt32 len = 0;
00236   nsresult rv = session_history->GetIndex(&curIndex);  
00237   rv = session_history->GetCount(&len);
00238   
00239   PRInt32 index = curIndex + aDelta;
00240   if (index > -1  &&  index < len)
00241     webnav->GotoIndex(index);
00242   // We always want to return a NS_OK, since returning errors 
00243   // from GotoIndex() can lead to exceptions and a possible leak
00244   // of history length
00245   return NS_OK;
00246 }
00247 
00248 NS_IMETHODIMP
00249 nsHistory::Go()
00250 {
00251   nsCOMPtr<nsIXPCNativeCallContext> ncc;
00252   nsresult rv = nsContentUtils::XPConnect()->
00253     GetCurrentNativeCallContext(getter_AddRefs(ncc));
00254   NS_ENSURE_SUCCESS(rv, rv);
00255 
00256   if (!ncc)
00257     return NS_ERROR_NOT_AVAILABLE;
00258 
00259   PRUint32 argc;
00260   ncc->GetArgc(&argc);
00261 
00262   PRInt32 delta = 0;
00263 
00264   if (argc > 0) {
00265     jsval *argv = nsnull;
00266 
00267     ncc->GetArgvPtr(&argv);
00268     NS_ENSURE_TRUE(argv, NS_ERROR_UNEXPECTED);
00269 
00270     if (!JSVAL_IS_INT(argv[0])) {
00271       // Not an index, don't do anything.
00272 
00273       return NS_OK;
00274     }
00275 
00276     delta = JSVAL_TO_INT(argv[0]);
00277   }
00278 
00279   if (delta == 0) {
00280     nsCOMPtr<nsPIDOMWindow> window(do_GetInterface(mDocShell));
00281 
00282     if (window && window->IsHandlingResizeEvent()) {
00283       // history.go(0) (aka location.reload()) was called on a window
00284       // that is handling a resize event. Sites do this since Netscape
00285       // 4.x needed it, but we don't, and it's a horrible experience
00286       // for nothing.  In stead of reloading the page, just clear
00287       // style data and reflow the page since some sites may use this
00288       // trick to work around gecko reflow bugs, and this should have
00289       // the same effect.
00290 
00291       nsCOMPtr<nsIDocument> doc =
00292         do_QueryInterface(window->GetExtantDocument());
00293 
00294       nsIPresShell *shell;
00295       nsPresContext *pcx;
00296       if (doc && (shell = doc->GetShellAt(0)) &&
00297           (pcx = shell->GetPresContext())) {
00298         pcx->ClearStyleDataAndReflow();
00299       }
00300 
00301       return NS_OK;
00302     }
00303   }
00304 
00305   return Go(delta);
00306 }
00307 
00308 NS_IMETHODIMP
00309 nsHistory::Item(PRUint32 aIndex, nsAString& aReturn)
00310 {
00311   aReturn.Truncate();
00312 
00313   nsresult rv = NS_OK;
00314   nsCOMPtr<nsISHistory>  session_history;
00315 
00316   GetSessionHistoryFromDocShell(mDocShell, getter_AddRefs(session_history));
00317   NS_ENSURE_TRUE(session_history, NS_ERROR_FAILURE);
00318 
00319        nsCOMPtr<nsIHistoryEntry> sh_entry;
00320        nsCOMPtr<nsIURI> uri;
00321 
00322   rv = session_history->GetEntryAtIndex(aIndex, PR_FALSE,
00323                                         getter_AddRefs(sh_entry));
00324 
00325   if (sh_entry) {
00326     rv = sh_entry->GetURI(getter_AddRefs(uri));
00327   }
00328 
00329   if (uri) {
00330     nsCAutoString urlCString;
00331     rv = uri->GetSpec(urlCString);
00332 
00333     CopyUTF8toUTF16(urlCString, aReturn);
00334   }
00335 
00336   return rv;
00337 }
00338 
00339 nsresult
00340 nsHistory::GetSessionHistoryFromDocShell(nsIDocShell * aDocShell, 
00341                                          nsISHistory ** aReturn)
00342 {
00343 
00344   NS_ENSURE_TRUE(aDocShell, NS_ERROR_FAILURE);
00345   /* The docshell we have may or may not be
00346    * the root docshell. So, get a handle to
00347    * SH from the root docshell
00348    */
00349   
00350   // QI mDocShell to nsIDocShellTreeItem
00351   nsCOMPtr<nsIDocShellTreeItem> dsTreeItem(do_QueryInterface(mDocShell));
00352   NS_ENSURE_TRUE(dsTreeItem, NS_ERROR_FAILURE);
00353 
00354   // Get the root DocShell from it
00355   nsCOMPtr<nsIDocShellTreeItem> root;
00356   dsTreeItem->GetSameTypeRootTreeItem(getter_AddRefs(root));
00357   NS_ENSURE_TRUE(root, NS_ERROR_FAILURE);
00358   
00359   //QI root to nsIWebNavigation
00360   nsCOMPtr<nsIWebNavigation>   webNav(do_QueryInterface(root));
00361   NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
00362 
00363   //Get  SH from nsIWebNavigation
00364   return webNav->GetSessionHistory(aReturn);
00365   
00366 }
00367