Bug 1293362 - Part 7: Expose the nsComponentManager static interfaces to rust code, r=froydnj

MozReview-Commit-ID: D4kvNFgmIpH
This commit is contained in:
Nika Layzell 2018-01-04 17:37:39 -05:00
parent 3db03efd58
commit 98ea82060d
3 changed files with 131 additions and 0 deletions

View File

@ -2001,3 +2001,27 @@ XRE_AddJarManifestLocation(NSLocationType aType, nsIFile* aLocation)
return NS_OK;
}
// Expose some important global interfaces to rust for the rust xpcom API. These
// methods return a non-owning reference to the component manager, which should
// live for the lifetime of XPCOM.
extern "C" {
const nsIComponentManager*
Gecko_GetComponentManager()
{
return nsComponentManagerImpl::gComponentManager;
}
const nsIServiceManager*
Gecko_GetServiceManager()
{
return nsComponentManagerImpl::gComponentManager;
}
const nsIComponentRegistrar*
Gecko_GetComponentRegistrar()
{
return nsComponentManagerImpl::gComponentManager;
}
}

View File

@ -29,6 +29,9 @@ pub use base::*;
mod refptr;
pub use refptr::*;
mod statics;
pub use statics::*;
// XPCOM interface definitions.
pub mod interfaces;

View File

@ -0,0 +1,104 @@
/* 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/. */
use std::ffi::CStr;
use std::ptr;
use nserror::NsresultExt;
use {
RefPtr,
GetterAddrefs,
XpCom,
};
use interfaces::{
nsIComponentManager,
nsIServiceManager,
nsIComponentRegistrar,
};
macro_rules! try_opt {
($e: expr) => {
match $e {
Some(x) => x,
None => return None,
}
}
}
/// Get a reference to the global `nsIComponentManager`.
///
/// Can return `None` during shutdown.
#[inline]
pub fn component_manager() -> Option<RefPtr<nsIComponentManager>> {
unsafe {
RefPtr::from_raw(Gecko_GetComponentManager())
}
}
/// Get a reference to the global `nsIServiceManager`.
///
/// Can return `None` during shutdown.
#[inline]
pub fn service_manager() -> Option<RefPtr<nsIServiceManager>> {
unsafe {
RefPtr::from_raw(Gecko_GetServiceManager())
}
}
/// Get a reference to the global `nsIComponentRegistrar`
///
/// Can return `None` during shutdown.
#[inline]
pub fn component_registrar() -> Option<RefPtr<nsIComponentRegistrar>> {
unsafe {
RefPtr::from_raw(Gecko_GetComponentRegistrar())
}
}
/// Helper for calling `nsIComponentManager::CreateInstanceByContractID` on the
/// global `nsIComponentRegistrar`.
///
/// This method is similar to `do_CreateInstance` in C++.
#[inline]
pub fn create_instance<T: XpCom>(id: &CStr) -> Option<RefPtr<T>> {
unsafe {
let mut ga = GetterAddrefs::<T>::new();
if try_opt!(component_manager()).CreateInstanceByContractID(
id.as_ptr(),
ptr::null(),
&T::IID,
ga.void_ptr(),
).succeeded() {
ga.refptr()
} else {
None
}
}
}
/// Helper for calling `nsIServiceManager::GetServiceByContractID` on the global
/// `nsIServiceManager`.
///
/// This method is similar to `do_GetService` in C++.
#[inline]
pub fn get_service<T: XpCom>(id: &CStr) -> Option<RefPtr<T>> {
unsafe {
let mut ga = GetterAddrefs::<T>::new();
if try_opt!(service_manager()).GetServiceByContractID(
id.as_ptr(),
&T::IID,
ga.void_ptr()
).succeeded() {
ga.refptr()
} else {
None
}
}
}
extern "C" {
fn Gecko_GetComponentManager() -> *const nsIComponentManager;
fn Gecko_GetServiceManager() -> *const nsIServiceManager;
fn Gecko_GetComponentRegistrar() -> *const nsIComponentRegistrar;
}