Bug 516085 - C++ easy access for common global services r=bsmedberg

This commit is contained in:
Taras Glek 2010-04-02 11:38:25 -07:00
Родитель 364c02a5c9
Коммит 023d663a32
7 изменённых файлов: 161 добавлений и 4 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -0,0 +1,2 @@
MOZ_SERVICE(IOService, nsIIOService, "@mozilla.org/network/io-service;1")
MOZ_SERVICE(ObserverService, nsIObserverService, "@mozilla.org/observer-service;1")

82
xpcom/build/Services.cpp Normal file
Просмотреть файл

@ -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 <tglek@mozilla.com>
*
* 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<TYPE> \
mozilla::services::Get##NAME() \
{ \
if (!g##NAME) { \
nsCOMPtr<TYPE> 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
}

59
xpcom/build/Services.h Normal file
Просмотреть файл

@ -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 <tglek@mozilla.com>
*
* 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<TYPE> Get##NAME();
#include "ServiceList.h"
#undef MOZ_SERVICE
} // namespace services
} // namespace mozilla
#endif

Просмотреть файл

@ -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

Просмотреть файл

@ -142,6 +142,7 @@ NS_DECL_CLASSINFO(nsStringInputStream)
#include "nsMemoryReporterManager.h"
#include <locale.h>
#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");

Просмотреть файл

@ -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,