From 023d663a32bab7a4c9b3b21c4a45c0b8b8a84f0a Mon Sep 17 00:00:00 2001 From: Taras Glek Date: Fri, 2 Apr 2010 11:38:25 -0700 Subject: [PATCH] Bug 516085 - C++ easy access for common global services r=bsmedberg --- xpcom/build/Makefile.in | 3 + xpcom/build/ServiceList.h | 2 + xpcom/build/Services.cpp | 82 +++++++++++++++++++++++++ xpcom/build/Services.h | 59 ++++++++++++++++++ xpcom/build/nsXPCOMPrivate.h | 13 ++++ xpcom/build/nsXPComInit.cpp | 3 +- xpcom/components/nsComponentManager.cpp | 3 - 7 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 xpcom/build/ServiceList.h create mode 100644 xpcom/build/Services.cpp create mode 100644 xpcom/build/Services.h diff --git a/xpcom/build/Makefile.in b/xpcom/build/Makefile.in index dbc6cb8e148e..0bbc85bde485 100644 --- a/xpcom/build/Makefile.in +++ b/xpcom/build/Makefile.in @@ -68,6 +68,7 @@ CPPSRCS = \ $(XPCOM_GLUENS_SRC_LCPPSRCS) \ nsXPComInit.cpp \ nsXPCOMStrings.cpp \ + Services.cpp \ $(NULL) ifndef MOZ_ENABLE_LIBXUL @@ -125,6 +126,8 @@ EXPORTS = \ EXPORTS_mozilla = \ XPCOM.h \ + Services.h \ + ServiceList.h \ $(NULL) # Force use of PIC diff --git a/xpcom/build/ServiceList.h b/xpcom/build/ServiceList.h new file mode 100644 index 000000000000..c7c714d4c289 --- /dev/null +++ b/xpcom/build/ServiceList.h @@ -0,0 +1,2 @@ +MOZ_SERVICE(IOService, nsIIOService, "@mozilla.org/network/io-service;1") +MOZ_SERVICE(ObserverService, nsIObserverService, "@mozilla.org/observer-service;1") diff --git a/xpcom/build/Services.cpp b/xpcom/build/Services.cpp new file mode 100644 index 000000000000..c9f5dcc1b05c --- /dev/null +++ b/xpcom/build/Services.cpp @@ -0,0 +1,82 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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 code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Taras Glek + * + * 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 MPL, 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 MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "mozilla/Services.h" +#include "nsComponentManager.h" +#include "nsIIOService.h" +#include "nsIDirectoryService.h" +#include "nsIChromeRegistry.h" +#include "nsIObserverService.h" +#include "nsNetCID.h" +#include "nsObserverService.h" +#include "nsXPCOMPrivate.h" + +using namespace mozilla::services; + +/* + * Define a global variable and a getter for every service in ServiceList. + * eg. gIOService and GetIOService() + */ +#define MOZ_SERVICE(NAME, TYPE, CONTRACT_ID) \ + static TYPE* g##NAME = nsnull; \ + \ + NS_COM already_AddRefed \ + mozilla::services::Get##NAME() \ + { \ + if (!g##NAME) { \ + nsCOMPtr os = do_GetService(CONTRACT_ID); \ + g##NAME = os.forget().get(); \ + } \ + NS_IF_ADDREF(g##NAME); \ + return g##NAME; \ + } + +#include "ServiceList.h" +#undef MOZ_SERVICE + +/** + * Clears service cache, sets gXPCOMShuttingDown + */ +void +mozilla::services::Shutdown() +{ + gXPCOMShuttingDown = PR_TRUE; +#define MOZ_SERVICE(NAME, TYPE, CONTRACT_ID) NS_IF_RELEASE(g##NAME); +#include "ServiceList.h" +#undef MOZ_SERVICE +} diff --git a/xpcom/build/Services.h b/xpcom/build/Services.h new file mode 100644 index 000000000000..e4d1f70b1f22 --- /dev/null +++ b/xpcom/build/Services.h @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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 code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Taras Glek + * + * 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 MPL, 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 MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef mozilla_Services_h +#define mozilla_Services_h + +#include "nscore.h" +#include "nsCOMPtr.h" + +#define MOZ_SERVICE(NAME, TYPE, SERVICE_CID) class TYPE; +#include "ServiceList.h" +#undef MOZ_SERVICE + +namespace mozilla { +namespace services { + +#define MOZ_SERVICE(NAME, TYPE, SERVICE_CID) NS_COM already_AddRefed Get##NAME(); +#include "ServiceList.h" +#undef MOZ_SERVICE + +} // namespace services +} // namespace mozilla + +#endif diff --git a/xpcom/build/nsXPCOMPrivate.h b/xpcom/build/nsXPCOMPrivate.h index d6675c64bf0e..72d58edaf24f 100644 --- a/xpcom/build/nsXPCOMPrivate.h +++ b/xpcom/build/nsXPCOMPrivate.h @@ -311,4 +311,17 @@ void LogTerm(); #endif #endif +extern PRBool gXPCOMShuttingDown; + +namespace mozilla { +namespace services { + +/** + * Clears service cache, sets gXPCOMShuttingDown + */ +void Shutdown(); + +} // namespace services +} // namespace mozilla + #endif diff --git a/xpcom/build/nsXPComInit.cpp b/xpcom/build/nsXPComInit.cpp index 65b5a6836a6b..13f4177302ea 100644 --- a/xpcom/build/nsXPComInit.cpp +++ b/xpcom/build/nsXPComInit.cpp @@ -142,6 +142,7 @@ NS_DECL_CLASSINFO(nsStringInputStream) #include "nsMemoryReporterManager.h" #include +#include "mozilla/Services.h" #ifdef MOZ_IPC #include "base/at_exit.h" @@ -802,7 +803,7 @@ ShutdownXPCOM(nsIServiceManager* servMgr) // XPCOM is officially in shutdown mode NOW // Set this only after the observers have been notified as this // will cause servicemanager to become inaccessible. - gXPCOMShuttingDown = PR_TRUE; + mozilla::services::Shutdown(); #ifdef DEBUG_dougt fprintf(stderr, "* * * * XPCOM shutdown. Access will be denied * * * * \n"); diff --git a/xpcom/components/nsComponentManager.cpp b/xpcom/components/nsComponentManager.cpp index 9496d857fb53..029ce2ecf792 100644 --- a/xpcom/components/nsComponentManager.cpp +++ b/xpcom/components/nsComponentManager.cpp @@ -154,9 +154,6 @@ NS_DEFINE_CID(kCategoryManagerCID, NS_CATEGORYMANAGER_CID); #define UID_STRING_LENGTH 39 -// Set to true from NS_ShutdownXPCOM. -extern PRBool gXPCOMShuttingDown; - static void GetIDString(const nsID& aCID, char buf[UID_STRING_LENGTH]) { PR_snprintf(buf, UID_STRING_LENGTH, gIDFormat,