From c897f374736b8ba92b94cee0b0853937cf83e057 Mon Sep 17 00:00:00 2001 From: "bsmedberg%covad.net" Date: Wed, 13 Oct 2004 17:14:13 +0000 Subject: [PATCH] Bug 264093 - combine chrome registry and chrome protocol handler on one object r=darin --- chrome/src/nsChromeFactory.cpp | 10 ++- chrome/src/nsChromeProtocolHandler.cpp | 87 +++++++++----------------- chrome/src/nsChromeProtocolHandler.h | 65 ------------------- chrome/src/nsChromeRegistry.cpp | 36 ++++++----- chrome/src/nsChromeRegistry.h | 6 ++ 5 files changed, 60 insertions(+), 144 deletions(-) delete mode 100644 chrome/src/nsChromeProtocolHandler.h diff --git a/chrome/src/nsChromeFactory.cpp b/chrome/src/nsChromeFactory.cpp index 3f0f089406ec..742306efe445 100644 --- a/chrome/src/nsChromeFactory.cpp +++ b/chrome/src/nsChromeFactory.cpp @@ -44,11 +44,9 @@ #include "nsIChromeRegistry.h" #include "nscore.h" #include "rdf.h" -#include "nsChromeProtocolHandler.h" #include "nsChromeRegistry.h" NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsChromeRegistry, Init) -NS_GENERIC_FACTORY_CONSTRUCTOR(nsChromeProtocolHandler) // The list of components we register static const nsModuleComponentInfo components[] = @@ -59,10 +57,10 @@ static const nsModuleComponentInfo components[] = nsChromeRegistryConstructor }, - { "Chrome Protocol Handler", - NS_CHROMEPROTOCOLHANDLER_CID, - NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "chrome", - nsChromeProtocolHandlerConstructor + { "Chrome Registry", + NS_CHROMEREGISTRY_CID, + NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "chrome", + nsChromeRegistryConstructor } }; diff --git a/chrome/src/nsChromeProtocolHandler.cpp b/chrome/src/nsChromeProtocolHandler.cpp index 0ed910d7086f..e7cdb40986c9 100644 --- a/chrome/src/nsChromeProtocolHandler.cpp +++ b/chrome/src/nsChromeProtocolHandler.cpp @@ -21,6 +21,7 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): + * Benjamin Smedberg * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -42,13 +43,12 @@ */ -#include "nsChromeProtocolHandler.h" +#include "nsChromeRegistry.h" #include "nsCOMPtr.h" #include "nsContentCID.h" #include "nsCRT.h" #include "nsEventQueueUtils.h" #include "nsIChannel.h" -#include "nsIChromeRegistry.h" #include "nsIComponentManager.h" #include "nsIEventQueue.h" #include "nsIEventQueueService.h" @@ -80,9 +80,6 @@ static NS_DEFINE_CID(kXULPrototypeCacheCID, NS_XULPROTOTYPECACHE_CID); #endif -// This comes from nsChromeRegistry.cpp -extern nsIChromeRegistry* gChromeRegistry; - //---------------------------------------------------------------------- // // A channel that's used for loading cached chrome documents. Since a @@ -422,29 +419,25 @@ nsCachedChromeChannel::DestroyLoadEvent(PLEvent* aEvent) delete aEvent; } -//////////////////////////////////////////////////////////////////////////////// - -NS_IMPL_THREADSAFE_ISUPPORTS2(nsChromeProtocolHandler, nsIProtocolHandler, nsISupportsWeakReference) - //////////////////////////////////////////////////////////////////////////////// // nsIProtocolHandler methods: NS_IMETHODIMP -nsChromeProtocolHandler::GetScheme(nsACString &result) +nsChromeRegistry::GetScheme(nsACString &result) { result.AssignLiteral("chrome"); return NS_OK; } NS_IMETHODIMP -nsChromeProtocolHandler::GetDefaultPort(PRInt32 *result) +nsChromeRegistry::GetDefaultPort(PRInt32 *result) { *result = -1; // no port for chrome: URLs return NS_OK; } NS_IMETHODIMP -nsChromeProtocolHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval) +nsChromeRegistry::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval) { // don't override anything. *_retval = PR_FALSE; @@ -452,17 +445,17 @@ nsChromeProtocolHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_re } NS_IMETHODIMP -nsChromeProtocolHandler::GetProtocolFlags(PRUint32 *result) +nsChromeRegistry::GetProtocolFlags(PRUint32 *result) { *result = URI_STD; return NS_OK; } NS_IMETHODIMP -nsChromeProtocolHandler::NewURI(const nsACString &aSpec, - const char *aCharset, - nsIURI *aBaseURI, - nsIURI **result) +nsChromeRegistry::NewURI(const nsACString &aSpec, + const char *aCharset, + nsIURI *aBaseURI, + nsIURI **result) { NS_PRECONDITION(result, "Null out param"); @@ -489,20 +482,7 @@ nsChromeProtocolHandler::NewURI(const nsACString &aSpec, // "chrome://navigator/content/" and "chrome://navigator/content" // and "chrome://navigator/content/navigator.xul". - // Try the global cache first. - nsCOMPtr reg = gChromeRegistry; - - // If that fails, the service has not been instantiated yet; let's - // do that now. - if (!reg) { - reg = do_GetService(NS_CHROMEREGISTRY_CONTRACTID, &rv); - if (NS_FAILED(rv)) - return rv; - } - - NS_ASSERTION(reg, "Must have a chrome registry by now"); - - rv = reg->Canonify(uri); + rv = Canonify(uri); if (NS_FAILED(rv)) return rv; @@ -512,8 +492,8 @@ nsChromeProtocolHandler::NewURI(const nsACString &aSpec, } NS_IMETHODIMP -nsChromeProtocolHandler::NewChannel(nsIURI* aURI, - nsIChannel* *aResult) +nsChromeRegistry::NewChannel(nsIURI* aURI, + nsIChannel* *aResult) { NS_ENSURE_ARG_POINTER(aURI); NS_PRECONDITION(aResult, "Null out param"); @@ -521,20 +501,16 @@ nsChromeProtocolHandler::NewChannel(nsIURI* aURI, #ifdef DEBUG // Check that the uri we got is already canonified nsresult debug_rv; - nsCOMPtr debugReg(do_GetService(NS_CHROMEREGISTRY_CONTRACTID, &debug_rv)); + nsCOMPtr debugClone; + debug_rv = aURI->Clone(getter_AddRefs(debugClone)); if (NS_SUCCEEDED(debug_rv)) { - nsCOMPtr debugClone; - debug_rv = aURI->Clone(getter_AddRefs(debugClone)); + debug_rv = Canonify(debugClone); if (NS_SUCCEEDED(debug_rv)) { - debug_rv = debugReg->Canonify(debugClone); + PRBool same; + debug_rv = aURI->Equals(debugClone, &same); if (NS_SUCCEEDED(debug_rv)) { - PRBool same; - debug_rv = aURI->Equals(debugClone, &same); - if (NS_SUCCEEDED(debug_rv)) { - NS_ASSERTION(same, "Non-canonified chrome uri passed to nsChromeProtocolHandler::NewChannel!"); - } + NS_ASSERTION(same, "Non-canonified chrome uri passed to nsChromeProtocolHandler::NewChannel!"); } - } } #endif @@ -547,7 +523,7 @@ nsChromeProtocolHandler::NewChannel(nsIURI* aURI, // document in the cache. nsCOMPtr cache = do_GetService(kXULPrototypeCacheCID, &rv); - if (NS_FAILED(rv)) return rv; + NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr proto; cache->GetPrototype(aURI, getter_AddRefs(proto)); @@ -572,8 +548,7 @@ nsChromeProtocolHandler::NewChannel(nsIURI* aURI, // ...in which case, we'll create a dummy stream that'll just // load the thing. result = new nsCachedChromeChannel(aURI); - if (! result) - return NS_ERROR_OUT_OF_MEMORY; + NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY); } else #endif @@ -584,14 +559,8 @@ nsChromeProtocolHandler::NewChannel(nsIURI* aURI, //aURI->GetSpec(getter_Copies(oldSpec)); //printf("*************************** %s\n", (const char*)oldSpec); - nsCOMPtr reg = gChromeRegistry; - if (!reg) { - reg = do_GetService(NS_CHROMEREGISTRY_CONTRACTID, &rv); - if (NS_FAILED(rv)) return rv; - } - nsCAutoString spec; - rv = reg->ConvertChromeURL(aURI, spec); + rv = ConvertChromeURL(aURI, spec); if (NS_FAILED(rv)) { #ifdef DEBUG aURI->GetSpec(spec); @@ -601,14 +570,14 @@ nsChromeProtocolHandler::NewChannel(nsIURI* aURI, } nsCOMPtr ioServ(do_GetIOService(&rv)); - if (NS_FAILED(rv)) return rv; + NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr chromeURI; rv = ioServ->NewURI(spec, nsnull, nsnull, getter_AddRefs(chromeURI)); - if (NS_FAILED(rv)) return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = ioServ->NewChannelFromURI(chromeURI, getter_AddRefs(result)); - if (NS_FAILED(rv)) return rv; + NS_ENSURE_SUCCESS(rv, rv); // XXX Will be removed someday when we handle remote chrome. nsCOMPtr fileChan @@ -640,7 +609,7 @@ nsChromeProtocolHandler::NewChannel(nsIURI* aURI, // Make sure that the channel remembers where it was // originally loaded from. rv = result->SetOriginalURI(aURI); - if (NS_FAILED(rv)) return rv; + NS_ENSURE_SUCCESS(rv, rv); // Get a system principal for xul files and set the owner // property of the result @@ -653,11 +622,11 @@ nsChromeProtocolHandler::NewChannel(nsIURI* aURI, { nsCOMPtr securityManager = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); - if (NS_FAILED(rv)) return rv; + NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr principal; rv = securityManager->GetSystemPrincipal(getter_AddRefs(principal)); - if (NS_FAILED(rv)) return rv; + NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr owner = do_QueryInterface(principal); result->SetOwner(owner); diff --git a/chrome/src/nsChromeProtocolHandler.h b/chrome/src/nsChromeProtocolHandler.h deleted file mode 100644 index 6ae1a4d79cc9..000000000000 --- a/chrome/src/nsChromeProtocolHandler.h +++ /dev/null @@ -1,65 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Netscape Public License - * Version 1.1 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is mozilla.org code. - * - * The Initial Developer of the Original Code is - * Netscape Communications Corporation. - * Portions created by the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef nsChromeProtocolHandler_h___ -#define nsChromeProtocolHandler_h___ - -#include "nsIProtocolHandler.h" -#include "nsWeakReference.h" - -#define NS_CHROMEPROTOCOLHANDLER_CID \ -{ /* 61ba33c0-3031-11d3-8cd0-0060b0fc14a3 */ \ - 0x61ba33c0, \ - 0x3031, \ - 0x11d3, \ - {0x8c, 0xd0, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \ -} - -class nsChromeProtocolHandler : public nsIProtocolHandler, public nsSupportsWeakReference -{ -public: - NS_DECL_ISUPPORTS - - // nsIProtocolHandler methods: - NS_DECL_NSIPROTOCOLHANDLER - - // nsChromeProtocolHandler methods: - nsChromeProtocolHandler() {} - ~nsChromeProtocolHandler() {} -}; - -#endif /* nsChromeProtocolHandler_h___ */ diff --git a/chrome/src/nsChromeRegistry.cpp b/chrome/src/nsChromeRegistry.cpp index c23b8fda555e..69c8fb8bf3ee 100644 --- a/chrome/src/nsChromeRegistry.cpp +++ b/chrome/src/nsChromeRegistry.cpp @@ -122,8 +122,6 @@ static NS_DEFINE_CID(kRDFContainerUtilsCID, NS_RDFCONTAINERUTILS_CID); static NS_DEFINE_CID(kCSSLoaderCID, NS_CSS_LOADER_CID); static NS_DEFINE_CID(kLookAndFeelCID, NS_LOOKANDFEEL_CID); -nsIChromeRegistry* gChromeRegistry = nsnull; - #define CHROME_URI "http://www.mozilla.org/rdf/chrome#" DEFINE_RDF_VOCAB(CHROME_URI, CHROME, baseURL); @@ -175,8 +173,6 @@ DatasourceEnumerator(nsHashKey *aKey, void *aData, void* closure) nsChromeRegistry::~nsChromeRegistry() { - gChromeRegistry = nsnull; - if (mDataSourceTable) { mDataSourceTable->Enumerate(DatasourceEnumerator, mChromeDataSource); delete mDataSourceTable; @@ -194,10 +190,11 @@ nsChromeRegistry::~nsChromeRegistry() } -NS_IMPL_THREADSAFE_ISUPPORTS5(nsChromeRegistry, +NS_IMPL_THREADSAFE_ISUPPORTS6(nsChromeRegistry, nsIChromeRegistry, nsIXULChromeRegistry, nsIXULOverlayProvider, + nsIProtocolHandler, nsIObserver, nsISupportsWeakReference) @@ -248,8 +245,6 @@ nsChromeRegistry::Init() if (!mSelectedLocales.Init()) return NS_ERROR_FAILURE; if (!mSelectedSkins.Init()) return NS_ERROR_FAILURE; - gChromeRegistry = this; - nsresult rv; rv = nsServiceManager::GetService(kRDFServiceCID, NS_GET_IID(nsIRDFService), @@ -364,8 +359,6 @@ nsChromeRegistry::Init() } } - CheckForNewChrome(); - return NS_OK; } @@ -2603,7 +2596,11 @@ nsresult nsChromeRegistry::LoadInstallDataSource() NS_ENSURE_SUCCESS(rv, rv); mInstallInitialized = PR_TRUE; - return AddToCompositeDataSource(PR_FALSE); + rv = AddToCompositeDataSource(PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); + + (void) RealCheckForNewChrome(); + return NS_OK; } nsresult nsChromeRegistry::LoadProfileDataSource() @@ -2615,7 +2612,9 @@ nsresult nsChromeRegistry::LoadProfileDataSource() mProfileInitialized = mInstallInitialized = PR_TRUE; mChromeDataSource = nsnull; rv = AddToCompositeDataSource(PR_TRUE); - if (NS_FAILED(rv)) return rv; + NS_ENSURE_SUCCESS(rv, rv); + + (void) RealCheckForNewChrome(); } nsCOMPtr pref (do_GetService(NS_PREFSERVICE_CONTRACTID)); @@ -2696,9 +2695,18 @@ nsChromeRegistry::CheckForNewChrome() { nsresult rv; - rv = LoadInstallDataSource(); - if (NS_FAILED(rv)) - return rv; + if (mInstallInitialized) { + return RealCheckForNewChrome(); + } + + // LoadInstallDataSource calls RealCheckForNewChrome internally + return LoadInstallDataSource(); +} + +nsresult +nsChromeRegistry::RealCheckForNewChrome() +{ + nsresult rv; nsCOMPtr dirSvc = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv); diff --git a/chrome/src/nsChromeRegistry.h b/chrome/src/nsChromeRegistry.h index 222477cdab1b..1320112bc1ee 100644 --- a/chrome/src/nsChromeRegistry.h +++ b/chrome/src/nsChromeRegistry.h @@ -53,6 +53,7 @@ class nsIProperties; #include "nsIChromeRegistry.h" #include "nsIXULOverlayProvider.h" +#include "nsIProtocolHandler.h" #include "nsIRDFCompositeDataSource.h" #include "nsIObserver.h" #include "nsWeakReference.h" @@ -68,6 +69,7 @@ class nsIProperties; class nsChromeRegistry : public nsIXULChromeRegistry, public nsIXULOverlayProvider, + public nsIProtocolHandler, public nsIObserver, public nsSupportsWeakReference { @@ -79,6 +81,9 @@ public: NS_DECL_NSIXULCHROMEREGISTRY NS_DECL_NSIXULOVERLAYPROVIDER + // nsIProtocolHandler + NS_DECL_NSIPROTOCOLHANDLER + NS_DECL_NSIOBSERVER // nsChromeRegistry methods: @@ -167,6 +172,7 @@ private: nsresult UninstallFromDynamicDataSource(const nsACString& aPackageName, PRBool aIsOverlay, PRBool aUseProfile); + NS_HIDDEN_(nsresult) RealCheckForNewChrome(); NS_HIDDEN_(nsresult) ProcessNewChromeFile(nsILocalFile *aListFile); NS_HIDDEN_(nsresult) ProcessNewChromeBuffer(char *aBuffer, PRInt32 aLength);