2018-01-05 01:36:53 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
services = []
|
2018-05-26 07:29:52 +03:00
|
|
|
|
|
|
|
|
2018-01-05 01:36:53 +03:00
|
|
|
def service(name, iface, contractid):
|
|
|
|
"""Define a convenient service getter"""
|
|
|
|
services.append((name, iface, contractid))
|
|
|
|
|
2018-05-26 07:29:52 +03:00
|
|
|
|
2018-01-05 01:36:53 +03:00
|
|
|
service('ChromeRegistryService', 'nsIChromeRegistry',
|
|
|
|
"@mozilla.org/chrome/chrome-registry;1")
|
|
|
|
service('ToolkitChromeRegistryService', 'nsIToolkitChromeRegistry',
|
|
|
|
"@mozilla.org/chrome/chrome-registry;1")
|
|
|
|
service('XULChromeRegistryService', 'nsIXULChromeRegistry',
|
|
|
|
"@mozilla.org/chrome/chrome-registry;1")
|
2019-03-20 20:00:47 +03:00
|
|
|
service('DirectoryService', 'nsIProperties',
|
|
|
|
"@mozilla.org/file/directory_service;1"),
|
2018-01-05 01:36:53 +03:00
|
|
|
service('IOService', 'nsIIOService',
|
|
|
|
"@mozilla.org/network/io-service;1")
|
|
|
|
service('ObserverService', 'nsIObserverService',
|
|
|
|
"@mozilla.org/observer-service;1")
|
|
|
|
service('StringBundleService', 'nsIStringBundleService',
|
|
|
|
"@mozilla.org/intl/stringbundle;1")
|
|
|
|
service('PermissionManager', 'nsIPermissionManager',
|
|
|
|
"@mozilla.org/permissionmanager;1")
|
2019-03-20 20:00:47 +03:00
|
|
|
service('PreferencesService', 'nsIPrefService',
|
|
|
|
"@mozilla.org/preferences-service;1")
|
2018-01-05 01:36:53 +03:00
|
|
|
service('ServiceWorkerManager', 'nsIServiceWorkerManager',
|
|
|
|
"@mozilla.org/serviceworkers/manager;1")
|
|
|
|
service('AsyncShutdown', 'nsIAsyncShutdownService',
|
|
|
|
"@mozilla.org/async-shutdown-service;1")
|
|
|
|
service('UUIDGenerator', 'nsIUUIDGenerator',
|
|
|
|
"@mozilla.org/uuid-generator;1")
|
|
|
|
service('GfxInfo', 'nsIGfxInfo',
|
|
|
|
"@mozilla.org/gfx/info;1")
|
|
|
|
service('SocketTransportService', 'nsISocketTransportService',
|
|
|
|
"@mozilla.org/network/socket-transport-service;1")
|
|
|
|
service('StreamTransportService', 'nsIStreamTransportService',
|
|
|
|
"@mozilla.org/network/stream-transport-service;1")
|
|
|
|
service('CacheStorageService', 'nsICacheStorageService',
|
|
|
|
"@mozilla.org/netwerk/cache-storage-service;1")
|
|
|
|
service('URIClassifier', 'nsIURIClassifier',
|
|
|
|
"@mozilla.org/uriclassifierservice")
|
|
|
|
service('ActivityDistributor', 'nsIHttpActivityDistributor',
|
|
|
|
"@mozilla.org/network/http-activity-distributor;1")
|
|
|
|
service('HistoryService', 'mozilla::IHistory',
|
|
|
|
"@mozilla.org/browser/history;1")
|
2018-06-20 20:38:22 +03:00
|
|
|
service('ThirdPartyUtil', 'mozIThirdPartyUtil',
|
|
|
|
"@mozilla.org/thirdpartyutil;1")
|
2018-10-20 00:47:43 +03:00
|
|
|
service('URIFixup', 'nsIURIFixup',
|
|
|
|
"@mozilla.org/docshell/urifixup;1")
|
2018-01-05 01:36:53 +03:00
|
|
|
|
|
|
|
# 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"
|
2018-06-20 20:38:22 +03:00
|
|
|
#include "mozIThirdPartyUtil.h"
|
2018-01-05 01:36:53 +03:00
|
|
|
#include "nsComponentManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsObserverService.h"
|
|
|
|
#include "nsXPCOMPrivate.h"
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
#include "nsIChromeRegistry.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIToolkitChromeRegistry.h"
|
|
|
|
#include "IHistory.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
2019-03-20 20:00:47 +03:00
|
|
|
#include "nsIPrefService.h"
|
2018-01-05 01:36:53 +03:00
|
|
|
#include "nsIServiceWorkerManager.h"
|
|
|
|
#include "nsICacheStorageService.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsIURIClassifier.h"
|
|
|
|
#include "nsIHttpActivityObserver.h"
|
|
|
|
#include "nsIAsyncShutdown.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nsIGfxInfo.h"
|
2018-10-20 00:47:43 +03:00
|
|
|
#include "nsIURIFixup.h"
|
2018-01-05 01:36:53 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
#####
|
|
|
|
# Codegen Logic
|
|
|
|
#
|
|
|
|
# The following code consumes the data listed above to generate the files
|
|
|
|
# Services.h, Services.cpp, and services.rs which provide access to these
|
|
|
|
# service getters in both rust and C++ code.
|
|
|
|
#
|
|
|
|
# XXX(nika): would it be a good idea to unify Services.jsm into here too?
|
|
|
|
|
2018-05-26 07:29:52 +03:00
|
|
|
|
2018-01-05 01:36:53 +03:00
|
|
|
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
|
|
|
|
extern "C" {
|
|
|
|
/**
|
2018-11-15 02:54:36 +03:00
|
|
|
* NOTE: Don't call this method directly, instead call mozilla::services::Get%(name)s.
|
2018-01-05 01:36:53 +03:00
|
|
|
* It is used to expose XPCOM services to rust code. The return value is already addrefed.
|
|
|
|
*/
|
|
|
|
%(type)s* XPCOMService_Get%(name)s();
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace services {
|
|
|
|
/**
|
|
|
|
* Fetch a cached instance of the %(name)s.
|
|
|
|
* This function will return nullptr during XPCOM shutdown.
|
|
|
|
*/
|
|
|
|
inline already_AddRefed<%(type)s>
|
|
|
|
Get%(name)s()
|
|
|
|
{
|
|
|
|
return already_AddRefed<%(type)s>(XPCOMService_Get%(name)s());
|
|
|
|
}
|
|
|
|
} // namespace services
|
|
|
|
} // namespace mozilla
|
|
|
|
#endif // defined(MOZILLA_INTERNAL_API)
|
|
|
|
""" % {
|
2018-05-26 07:29:52 +03:00
|
|
|
'name': name,
|
|
|
|
'type': iface,
|
|
|
|
})
|
2018-01-05 01:36:53 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
/**
|
2018-11-15 02:54:36 +03:00
|
|
|
* NOTE: Don't call this method directly, instead call `mozilla::services::Get%(name)s`.
|
2018-01-05 01:36:53 +03:00
|
|
|
* This method is extern "C" to expose XPCOM services to rust code.
|
|
|
|
* The return value is already addrefed.
|
|
|
|
*/
|
|
|
|
%(type)s*
|
|
|
|
XPCOMService_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).take();
|
|
|
|
}
|
|
|
|
} // extern "C"
|
|
|
|
""" % {
|
2018-05-26 07:29:52 +03:00
|
|
|
'name': name,
|
|
|
|
'type': iface,
|
|
|
|
'contractid': contractid,
|
|
|
|
})
|
2018-01-05 01:36:53 +03:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
def services_rs(output):
|
|
|
|
output.write("""\
|
|
|
|
/* THIS FILE IS GENERATED BY Services.py - DO NOT EDIT */
|
|
|
|
|
|
|
|
use RefPtr;
|
|
|
|
""")
|
|
|
|
|
|
|
|
for (name, iface, _) in services:
|
|
|
|
# NOTE: We can't support namespaced interfaces in rust code, so we have to ignore them.
|
|
|
|
if "::" in iface:
|
|
|
|
continue
|
|
|
|
|
|
|
|
output.write("""
|
|
|
|
/// Fetches a cached reference to the `%(name)s`.
|
|
|
|
/// This function will return `None` during XPCOM shutdown.
|
|
|
|
pub fn get_%(name)s() -> Option<RefPtr<::interfaces::%(type)s>> {
|
|
|
|
extern "C" {
|
|
|
|
fn XPCOMService_Get%(name)s() -> *mut ::interfaces::%(type)s;
|
|
|
|
}
|
|
|
|
unsafe { RefPtr::from_raw_dont_addref(XPCOMService_Get%(name)s()) }
|
|
|
|
}
|
|
|
|
""" % {
|
2018-05-26 07:29:52 +03:00
|
|
|
'name': name,
|
|
|
|
'type': iface,
|
|
|
|
})
|