Back to index

lightning-sunbird  0.9+nobinonly
nsGlobalHistory2Adapter.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 "nsGlobalHistory2Adapter.h"
00039 
00040 #include "nsDocShellCID.h"
00041 #include "nsServiceManagerUtils.h"
00042 #include "nsIComponentRegistrar.h"
00043 #include "nsGlobalHistoryAdapter.h"
00044 #include "nsIURI.h"
00045 #include "nsString.h"
00046 #include "nsNetUtil.h"
00047 
00048 // we should really have a nsIGenericFactory macro to make this a static
00049 // member function
00050 
00051 nsresult
00052 nsGlobalHistory2Adapter::Create(nsISupports *aOuter,
00053                                 REFNSIID aIID,
00054                                 void **aResult)
00055 {
00056   nsresult rv;
00057 
00058   if (aOuter) {
00059     rv = NS_ERROR_NO_AGGREGATION;
00060     return rv;
00061   }
00062 
00063   nsGlobalHistory2Adapter* adapter = new nsGlobalHistory2Adapter();
00064   if (!adapter) {
00065     rv = NS_ERROR_OUT_OF_MEMORY;
00066     return rv;
00067   }
00068 
00069   NS_ADDREF(adapter);
00070   rv = adapter->Init();
00071   if (NS_SUCCEEDED(rv)) {
00072     rv = adapter->QueryInterface(aIID, aResult);
00073   }
00074   NS_RELEASE(adapter);
00075 
00076   return rv;
00077 }
00078 
00079 nsresult
00080 nsGlobalHistory2Adapter::RegisterSelf(nsIComponentManager* aCompMgr,
00081                                       nsIFile* aPath,
00082                                       const char* aLoaderStr,
00083                                       const char* aType,
00084                                       const nsModuleComponentInfo *aInfo)
00085 {
00086   nsresult rv;
00087   PRBool registered;
00088   nsCOMPtr<nsIComponentRegistrar> compReg( do_QueryInterface(aCompMgr) );
00089   if (!compReg) {
00090     rv = NS_ERROR_UNEXPECTED;
00091     return rv;
00092   }
00093 
00094   rv = compReg->IsContractIDRegistered(NS_GLOBALHISTORY_CONTRACTID, &registered);
00095   if (NS_FAILED(rv)) return rv;
00096 
00097   // If the embedder has already registered the contractID, we don't want to
00098   // register ourself. Ideally the component manager would handle this for us.
00099   if (registered) {
00100     rv = NS_OK;
00101     return rv;
00102   }
00103 
00104   return compReg->RegisterFactoryLocation(GetCID(),
00105                                           "nsGlobalHistory2Adapter",
00106                                           NS_GLOBALHISTORY_CONTRACTID,
00107                                           aPath, aLoaderStr, aType);
00108 }                                     
00109 
00110 nsGlobalHistory2Adapter::nsGlobalHistory2Adapter()
00111 { }
00112 
00113 nsGlobalHistory2Adapter::~nsGlobalHistory2Adapter()
00114 { }
00115 
00116 nsresult
00117 nsGlobalHistory2Adapter::Init()
00118 {
00119   nsresult rv;
00120 
00121   nsCOMPtr<nsIComponentRegistrar> compReg;
00122   rv = NS_GetComponentRegistrar(getter_AddRefs(compReg));
00123   NS_ENSURE_SUCCESS(rv, rv);
00124 
00125   nsCID *cid;
00126   rv = compReg->ContractIDToCID(NS_GLOBALHISTORY2_CONTRACTID, &cid);
00127   if (NS_FAILED(rv)) {
00128     rv = NS_ERROR_FACTORY_NOT_REGISTERED;
00129     return rv;
00130   }
00131 
00132   if (cid->Equals(nsGlobalHistoryAdapter::GetCID())) {
00133     rv = NS_ERROR_FACTORY_NOT_REGISTERED;
00134     return rv;
00135   }
00136 
00137   NS_WARNING("Using nsIGlobalHistory->nsIGlobalHistory2 adapter.");
00138   mHistory = do_GetService(NS_GLOBALHISTORY2_CONTRACTID, &rv);
00139   return rv;
00140 }
00141 
00142 NS_IMPL_ISUPPORTS1(nsGlobalHistory2Adapter, nsIGlobalHistory)
00143 
00144 NS_IMETHODIMP
00145 nsGlobalHistory2Adapter::AddPage(const char* aURI)
00146 {
00147   NS_ENSURE_ARG_POINTER(aURI);
00148   NS_ENSURE_TRUE(*aURI, NS_ERROR_ILLEGAL_VALUE);
00149 
00150   nsresult rv;
00151 
00152   nsCOMPtr<nsIURI> uri;
00153   rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(aURI));
00154 
00155   if (NS_SUCCEEDED(rv)) {
00156     rv = mHistory->AddURI(uri, PR_FALSE, PR_FALSE, nsnull);
00157   }
00158 
00159   return rv;
00160 }
00161 
00162 NS_IMETHODIMP
00163 nsGlobalHistory2Adapter::IsVisited(const char* aURI, PRBool* aRetval)
00164 {
00165   NS_ENSURE_ARG_POINTER(aURI);
00166   nsresult rv;
00167 
00168   nsCOMPtr<nsIURI> uri;
00169   rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(aURI));
00170 
00171   if (NS_SUCCEEDED(rv)) {
00172     rv = mHistory->IsVisited(uri, aRetval);
00173   }
00174 
00175   return rv;
00176 }