diff --git a/xpcom/build/Services.py b/xpcom/build/Services.py deleted file mode 100644 index ca2db5b1a254..000000000000 --- a/xpcom/build/Services.py +++ /dev/null @@ -1,138 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -# NOTE: Although this generates headers and code for C++, using Services.h -# is deprecated in favour of Components.h. - - -services = [] - - -def service(name, iface, contractid): - """Define a convenient service getter""" - services.append((name, iface, contractid)) - - -# The `name` parameter is derived from the `iface` by removing the `nsI` -# prefix. (This often matches the `contractid`, but not always.) -service("ObserverService", "nsIObserverService", "@mozilla.org/observer-service;1") - -# The definition file needs access to the definitions of the particular -# interfaces. If you add a new interface here, make sure the necessary includes -# are also listed in the following code snippet. -CPP_INCLUDES = """ -#include "mozilla/Likely.h" -#include "mozilla/Services.h" -#include "nsComponentManager.h" -#include "nsIObserverService.h" -#include "nsXPCOMPrivate.h" -""" - - -##### -# Codegen Logic -# -# The following code consumes the data listed above to generate the files -# Services.h, and Services.cpp which provide access to these service getters in -# C++ code. - - -def services_h(output): - output.write( - """\ -/* THIS FILE IS GENERATED BY Services.py - DO NOT EDIT */ - -#ifndef mozilla_Services_h -#define mozilla_Services_h - -#include "nscore.h" -#include "nsCOMPtr.h" -""" - ) - - for name, iface, contractid in services: - # Write out a forward declaration for the type in question - segs = iface.split("::") - for namespace in segs[:-1]: - output.write("namespace %s {\n" % namespace) - output.write("class %s;\n" % segs[-1]) - for namespace in reversed(segs[:-1]): - output.write("} // namespace %s\n" % namespace) - - # Write out the C-style function signature, and the C++ wrapper - output.write( - """ -#ifdef MOZILLA_INTERNAL_API -namespace mozilla { -namespace services { -/** - * Fetch a cached instance of the %(name)s. - * This function will return nullptr during XPCOM shutdown. - * Prefer using static components to this method. - * WARNING: This method is _not_ threadsafe! - */ -already_AddRefed<%(type)s> Get%(name)s(); -} // namespace services -} // namespace mozilla -#endif // defined(MOZILLA_INTERNAL_API) -""" - % { - "name": name, - "type": iface, - } - ) - - output.write("#endif // !defined(mozilla_Services_h)\n") - - -def services_cpp(output): - output.write( - """\ -/* THIS FILE IS GENERATED BY Services.py - DO NOT EDIT */ -""" - ) - output.write(CPP_INCLUDES) - - for name, iface, contractid in services: - output.write( - """ -static %(type)s* g%(name)s = nullptr; - -namespace mozilla { -namespace services { -already_AddRefed<%(type)s> Get%(name)s() -{ - if (MOZ_UNLIKELY(gXPCOMShuttingDown)) { - return nullptr; - } - if (!g%(name)s) { - nsCOMPtr<%(type)s> os = do_GetService("%(contractid)s"); - os.swap(g%(name)s); - } - return do_AddRef(g%(name)s); -} -} -} -""" - % { - "name": name, - "type": iface, - "contractid": contractid, - } - ) - - output.write( - """ -/** - * Clears service cache, sets gXPCOMShuttingDown - */ -void -mozilla::services::Shutdown() -{ - gXPCOMShuttingDown = true; -""" - ) - for name, iface, contractid in services: - output.write(" NS_IF_RELEASE(g%s);\n" % name) - output.write("}\n") diff --git a/xpcom/build/moz.build b/xpcom/build/moz.build index c1858d81208b..727068c451dc 100644 --- a/xpcom/build/moz.build +++ b/xpcom/build/moz.build @@ -13,7 +13,6 @@ EXPORTS += [ EXPORTS.mozilla += [ "!GeckoProcessTypes.h", - "!Services.h", "FileLocation.h", "IOInterposer.h", "LateWriteChecks.h", @@ -62,15 +61,11 @@ UNIFIED_SOURCES += [ "XPCOMInit.cpp", ] -SOURCES += ["!Services.cpp"] - if CONFIG["OS_ARCH"] != "WINNT": SOURCES += [ "NSPRInterposer.cpp", ] -GeneratedFile("Services.cpp", script="Services.py", entry_point="services_cpp") -GeneratedFile("Services.h", script="Services.py", entry_point="services_h") GeneratedFile( "GeckoProcessTypes.h", script="gen_process_types.py", diff --git a/xpcom/components/Services.cpp b/xpcom/components/Services.cpp new file mode 100644 index 000000000000..f189b010c5e8 --- /dev/null +++ b/xpcom/components/Services.cpp @@ -0,0 +1,37 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +/* THIS FILE IS GENERATED BY Services.py - DO NOT EDIT */ + +#include "mozilla/Services.h" + +#include "mozilla/Likely.h" +#include "nsComponentManager.h" +#include "nsIObserverService.h" +#include "nsXPCOMPrivate.h" + +static nsIObserverService* gObserverService = nullptr; + +namespace mozilla { +namespace services { +already_AddRefed GetObserverService() { + if (MOZ_UNLIKELY(gXPCOMShuttingDown)) { + return nullptr; + } + if (!gObserverService) { + nsCOMPtr os = + do_GetService("@mozilla.org/observer-service;1"); + os.swap(gObserverService); + } + return do_AddRef(gObserverService); +} +} // namespace services +} // namespace mozilla + +/** + * Clears service cache, sets gXPCOMShuttingDown + */ +void mozilla::services::Shutdown() { + gXPCOMShuttingDown = true; + NS_IF_RELEASE(gObserverService); +} diff --git a/xpcom/components/Services.h b/xpcom/components/Services.h new file mode 100644 index 000000000000..8efab14e691d --- /dev/null +++ b/xpcom/components/Services.h @@ -0,0 +1,26 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +/* THIS FILE IS GENERATED BY Services.py - DO NOT EDIT */ + +#ifndef mozilla_Services_h +#define mozilla_Services_h + +#include "nsCOMPtr.h" +#include "nscore.h" +class nsIObserverService; + +#ifdef MOZILLA_INTERNAL_API +namespace mozilla { +namespace services { +/** + * Fetch a cached instance of the ObserverService. + * This function will return nullptr during XPCOM shutdown. + * Prefer using static components to this method. + * WARNING: This method is _not_ threadsafe! + */ +already_AddRefed GetObserverService(); +} // namespace services +} // namespace mozilla +#endif // defined(MOZILLA_INTERNAL_API) +#endif // !defined(mozilla_Services_h) diff --git a/xpcom/components/moz.build b/xpcom/components/moz.build index d588a42da6f9..5bf71a5600b7 100644 --- a/xpcom/components/moz.build +++ b/xpcom/components/moz.build @@ -24,6 +24,7 @@ EXPORTS.mozilla += [ "GenericFactory.h", "Module.h", "ModuleUtils.h", + "Services.h", ] XPCOM_MANIFESTS += [ @@ -52,6 +53,7 @@ UNIFIED_SOURCES += [ "nsCategoryManager.cpp", "nsComponentManager.cpp", "nsComponentManagerUtils.cpp", + "Services.cpp", ] SOURCES += [