Back to index

lightning-sunbird  0.9+nobinonly
nsGlobalHistoryAdapter.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; 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 gecko code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Benjamin Smedberg <bsmedberg@covad.net>
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsGlobalHistoryAdapter.h"
00039 
00040 #include "nsDocShellCID.h"
00041 #include "nsServiceManagerUtils.h"
00042 #include "nsIComponentRegistrar.h"
00043 #include "nsGlobalHistory2Adapter.h"
00044 #include "nsIURI.h"
00045 #include "nsString.h"
00046 
00047 nsresult
00048 nsGlobalHistoryAdapter::Create(nsISupports *aOuter,
00049                                REFNSIID aIID,
00050                                void **aResult)
00051 {
00052   nsresult rv;
00053 
00054   if (aOuter) {
00055     rv = NS_ERROR_NO_AGGREGATION;
00056     return rv;
00057   }
00058 
00059   nsGlobalHistoryAdapter* adapter = new nsGlobalHistoryAdapter();
00060   if (!adapter) {
00061     rv = NS_ERROR_OUT_OF_MEMORY;
00062     return rv;
00063   }
00064 
00065   NS_ADDREF(adapter);
00066   rv = adapter->Init();
00067   if (NS_SUCCEEDED(rv)) {
00068     rv = adapter->QueryInterface(aIID, aResult);
00069   }
00070   NS_RELEASE(adapter);
00071 
00072   return rv;
00073 }
00074 
00075 nsresult
00076 nsGlobalHistoryAdapter::RegisterSelf(nsIComponentManager* aCompMgr,
00077                                      nsIFile* aPath,
00078                                      const char* aLoaderStr,
00079                                      const char* aType,
00080                                      const nsModuleComponentInfo *aInfo)
00081 {
00082   nsresult rv;
00083   PRBool registered;
00084   nsCOMPtr<nsIComponentRegistrar> compReg( do_QueryInterface(aCompMgr) );
00085   if (!compReg) {
00086     rv = NS_ERROR_UNEXPECTED;
00087     return rv;
00088   }
00089 
00090   rv = compReg->IsContractIDRegistered(NS_GLOBALHISTORY2_CONTRACTID, &registered);
00091   if (NS_FAILED(rv)) return rv;
00092 
00093   // If the embedder has already registered the contractID, we don't want to
00094   // register ourself. Ideally the component manager would handle this for us.
00095   if (registered) {
00096     rv = NS_OK;
00097     return rv;
00098   }
00099 
00100   return compReg->RegisterFactoryLocation(GetCID(),
00101                                           "nsGlobalHistoryAdapter",
00102                                           NS_GLOBALHISTORY2_CONTRACTID,
00103                                           aPath, aLoaderStr, aType);
00104 }                                     
00105 
00106 nsGlobalHistoryAdapter::nsGlobalHistoryAdapter()
00107 { }
00108 
00109 nsGlobalHistoryAdapter::~nsGlobalHistoryAdapter()
00110 { }
00111 
00112 nsresult
00113 nsGlobalHistoryAdapter::Init()
00114 {
00115   nsresult rv;
00116 
00117   nsCOMPtr<nsIComponentRegistrar> compReg;
00118   rv = NS_GetComponentRegistrar(getter_AddRefs(compReg));
00119   NS_ENSURE_SUCCESS(rv, rv);
00120 
00121   nsCID *cid;
00122   rv = compReg->ContractIDToCID(NS_GLOBALHISTORY_CONTRACTID, &cid);
00123   if (NS_FAILED(rv)) {
00124     rv = NS_ERROR_FACTORY_NOT_REGISTERED;
00125     return rv;
00126   }
00127 
00128   if (cid->Equals(nsGlobalHistory2Adapter::GetCID())) {
00129     rv = NS_ERROR_FACTORY_NOT_REGISTERED;
00130     return rv;
00131   }
00132 
00133   NS_WARNING("Using nsIGlobalHistory2->nsIGlobalHistory adapter.");
00134   mHistory = do_GetService(NS_GLOBALHISTORY_CONTRACTID, &rv);
00135   return rv;
00136 }
00137 
00138 NS_IMPL_ISUPPORTS1(nsGlobalHistoryAdapter, nsIGlobalHistory2)
00139 
00140 NS_IMETHODIMP
00141 nsGlobalHistoryAdapter::AddURI(nsIURI* aURI, PRBool aRedirect,
00142                                PRBool aToplevel, nsIURI* aReferrer)
00143 {
00144   NS_ENSURE_ARG_POINTER(aURI);
00145   nsresult rv;
00146 
00147   // The model is really if we don't know differently then add which basically
00148   // means we are supposed to try all the things we know not to allow in and
00149   // then if we don't bail go on and allow it in.  But here lets compare
00150   // against the most common case we know to allow in and go on and say yes
00151   // to it.
00152 
00153   PRBool isHTTP = PR_FALSE;
00154   PRBool isHTTPS = PR_FALSE;
00155 
00156   NS_ENSURE_SUCCESS(rv = aURI->SchemeIs("http", &isHTTP), rv);
00157   NS_ENSURE_SUCCESS(rv = aURI->SchemeIs("https", &isHTTPS), rv);
00158 
00159   if (!isHTTP && !isHTTPS) {
00160     PRBool isAbout, isImap, isNews, isMailbox, isViewSource, isChrome, isData;
00161 
00162     rv = aURI->SchemeIs("about", &isAbout);
00163     rv |= aURI->SchemeIs("imap", &isImap);
00164     rv |= aURI->SchemeIs("news", &isNews);
00165     rv |= aURI->SchemeIs("mailbox", &isMailbox);
00166     rv |= aURI->SchemeIs("view-source", &isViewSource);
00167     rv |= aURI->SchemeIs("chrome", &isChrome);
00168     rv |= aURI->SchemeIs("data", &isData);
00169     NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
00170 
00171     if (isAbout || isImap || isNews || isMailbox || isViewSource || isChrome || isData) {
00172       return NS_OK;
00173     }
00174   }
00175 
00176   nsCAutoString spec;
00177   rv = aURI->GetSpec(spec);
00178   NS_ENSURE_SUCCESS(rv, rv);
00179 
00180   return mHistory->AddPage(spec.get());
00181 }
00182 
00183 NS_IMETHODIMP
00184 nsGlobalHistoryAdapter::IsVisited(nsIURI* aURI, PRBool* aRetval)
00185 {
00186   NS_ENSURE_ARG_POINTER(aURI);
00187 
00188   nsCAutoString spec;
00189   nsresult rv = aURI->GetSpec(spec);
00190   NS_ENSURE_SUCCESS(rv, rv);
00191 
00192   return mHistory->IsVisited(spec.get(), aRetval);
00193 }
00194 
00195 NS_IMETHODIMP
00196 nsGlobalHistoryAdapter::SetPageTitle(nsIURI* aURI, const nsAString& aTitle)
00197 {
00198   return NS_ERROR_NOT_IMPLEMENTED;
00199 }