Bug 1789902 - Part 2: Use XPCOM static components instead of Services in Rust, r=xpcom-reviewers,necko-reviewers,barret,valentin

Differential Revision: https://phabricator.services.mozilla.com/D156891
This commit is contained in:
Nika Layzell 2022-09-13 13:47:13 +00:00
Родитель 2d7346701a
Коммит 3d9a6d0374
12 изменённых файлов: 43 добавлений и 25 удалений

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

@ -50,7 +50,7 @@ impl<T: XpCom + 'static> Iterator for IterSimpleEnumerator<T> {
}
fn process_type() -> u32 {
if let Some(appinfo) = xpcom::services::get_XULRuntime() {
if let Ok(appinfo) = xpcom::components::XULRuntime::service::<nsIXULRuntime>() {
let mut process_type = nsIXULRuntime::PROCESS_TYPE_DEFAULT;
if unsafe { appinfo.GetProcessType(&mut process_type).succeeded() } {
return process_type;

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

@ -12,6 +12,7 @@ UnloadFunc = 'mozilla::UnloadPrefsModule'
Classes = [
{
'name': 'Preferences',
'js_name': 'prefs',
'cid': '{91ca2441-050f-4f7c-9df8-75b40ea40156}',
'contract_ids': ['@mozilla.org/preferences-service;1'],

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

@ -14,7 +14,7 @@ extern crate nserror;
use nserror::*;
extern crate xpcom;
use xpcom::interfaces::nsrefcnt;
use xpcom::interfaces::{mozIThirdPartyUtil, nsrefcnt};
use xpcom::{AtomicRefcnt, RefCounted, RefPtr};
extern crate uuid;
@ -316,7 +316,9 @@ pub extern "C" fn mozurl_origin(url: &MozURL, origin: &mut nsACString) {
fn get_base_domain(url: &MozURL) -> Result<Option<String>, nsresult> {
match url.scheme() {
"ftp" | "http" | "https" | "moz-extension" | "resource" => {
let third_party_util = xpcom::services::get_ThirdPartyUtil().unwrap();
let third_party_util: RefPtr<mozIThirdPartyUtil> =
xpcom::components::ThirdPartyUtil::service()
.map_err(|_| NS_ERROR_ILLEGAL_DURING_SHUTDOWN)?;
let scheme = nsCString::from(url.scheme());

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

@ -53,7 +53,7 @@ use thin_vec::ThinVec;
use xpcom::interfaces::{
nsICRLiteCoverage, nsICRLiteTimestamp, nsICertInfo, nsICertStorage, nsICertStorageCallback,
nsIFile, nsIHandleReportCallback, nsIIssuerAndSerialRevocationState, nsIMemoryReporter,
nsIMemoryReporterManager, nsIRevocationState, nsISerialEventTarget,
nsIMemoryReporterManager, nsIProperties, nsIRevocationState, nsISerialEventTarget,
nsISubjectAndPubKeyRevocationState, nsISupports,
};
use xpcom::{nsIID, GetterAddrefs, RefPtr, ThreadBoundRefPtr, XpCom};
@ -1083,7 +1083,8 @@ impl EncodedSecurityState {
}
fn get_path_from_directory_service(key: &str) -> Result<PathBuf, nserror::nsresult> {
let directory_service = xpcom::services::get_DirectoryService().ok_or(NS_ERROR_FAILURE)?;
let directory_service: RefPtr<nsIProperties> =
xpcom::components::Directory::service().map_err(|_| NS_ERROR_FAILURE)?;
let cs_key = CString::new(key).map_err(|_| NS_ERROR_FAILURE)?;
let mut requested_dir = GetterAddrefs::<nsIFile>::new();

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

@ -25,7 +25,7 @@ use std::{
},
};
use xpcom::{
interfaces::{mozIAppServicesLogger, mozIServicesLogSink, nsISupports},
interfaces::{mozIAppServicesLogger, mozIServicesLogSink, nsIObserverService, nsISupports},
RefPtr,
};
@ -85,7 +85,7 @@ fn ensure_observing_shutdown() {
if SHUTDOWN_OBSERVED.load(Ordering::Relaxed) {
return;
}
if let Some(service) = xpcom::services::get_ObserverService() {
if let Ok(service) = xpcom::components::Observer::service::<nsIObserverService>() {
let observer = ShutdownObserver::allocate(InitShutdownObserver {});
let rv = unsafe {
service.AddObserver(observer.coerce(), cstr!("xpcom-shutdown").as_ptr(), false)
@ -110,7 +110,7 @@ impl ShutdownObserver {
_data: *const u16,
) -> Result<(), nsresult> {
LOGGERS_BY_TARGET.write().unwrap().clear();
if let Some(service) = xpcom::services::get_ObserverService() {
if let Ok(service) = xpcom::components::Observer::service::<nsIObserverService>() {
// Ignore errors, since we're already shutting down.
let _ = unsafe { service.RemoveObserver(self.coerce(), topic) };
}

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

@ -13,8 +13,8 @@ use nserror::{nsresult, NS_ERROR_FAILURE};
use nsstring::{nsACString, nsCString, nsString};
#[cfg(not(target_os = "android"))]
use xpcom::interfaces::mozIViaduct;
use xpcom::interfaces::{nsIFile, nsIXULAppInfo};
use xpcom::XpCom;
use xpcom::interfaces::{nsIFile, nsIPrefService, nsIProperties, nsIXULAppInfo, nsIXULRuntime};
use xpcom::{RefPtr, XpCom};
use glean::{ClientInfoMetrics, Configuration};
@ -233,8 +233,8 @@ fn setup_viaduct() {
/// Construct and return the data_path from the profile dir, or return an error.
fn get_data_path() -> Result<String, nsresult> {
let dir_svc = match xpcom::services::get_DirectoryService() {
Some(ds) => ds,
let dir_svc: RefPtr<nsIProperties> = match xpcom::components::Directory::service() {
Ok(ds) => ds,
_ => return Err(NS_ERROR_FAILURE),
};
let mut profile_dir = xpcom::GetterAddrefs::<nsIFile>::new();
@ -262,9 +262,11 @@ fn get_data_path() -> Result<String, nsresult> {
/// build_id ad app_version will be "unknown".
/// Other problems result in an error being returned instead.
fn get_app_info() -> Result<(String, String, String), nsresult> {
let xul = xpcom::services::get_XULRuntime().ok_or(NS_ERROR_FAILURE)?;
let xul: RefPtr<nsIXULRuntime> =
xpcom::components::XULRuntime::service().map_err(|_| NS_ERROR_FAILURE)?;
let pref_service = xpcom::services::get_PrefService().ok_or(NS_ERROR_FAILURE)?;
let pref_service: RefPtr<nsIPrefService> =
xpcom::components::Preferences::service().map_err(|_| NS_ERROR_FAILURE)?;
let branch = xpcom::getter_addrefs(|p| {
// Safe because:
// * `null` is explicitly allowed per documentation

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

@ -10,7 +10,7 @@ use nserror::{nsresult, NS_ERROR_FAILURE, NS_OK};
use nsstring::{nsACString, nsCStr};
use xpcom::{
interfaces::{nsIPrefBranch, nsISupports},
RefPtr, XpCom,
RefPtr,
};
/// Whether the current value of the localhost testing pref is permitting
@ -36,9 +36,8 @@ impl UploadPrefObserver {
// * We control all input to `AddObserverImpl`
unsafe {
let pref_obs = Self::allocate(InitUploadPrefObserver {});
let pref_service = xpcom::services::get_PrefService().ok_or(NS_ERROR_FAILURE)?;
let pref_branch: RefPtr<nsIPrefBranch> =
(*pref_service).query_interface().ok_or(NS_ERROR_FAILURE)?;
xpcom::components::Preferences::service().map_err(|_| NS_ERROR_FAILURE)?;
let pref_nscstr =
&nsCStr::from("datareporting.healthreport.uploadEnabled") as &nsACString;
(*pref_branch)

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

@ -11,7 +11,10 @@ use std::sync::{
use std::time::{Duration, Instant};
use nserror::{nsresult, NS_ERROR_FAILURE, NS_OK};
use xpcom::interfaces::nsISupports;
use xpcom::{
interfaces::{nsIObserverService, nsISupports},
RefPtr,
};
// Partially cargo-culted from UploadPrefObserver.
#[xpcom(implement(nsIObserver), atomic)]
@ -42,7 +45,8 @@ impl UserActivityObserver {
last_edge: RwLock::new(Instant::now()),
was_active: AtomicBool::new(false),
});
let obs_service = xpcom::services::get_ObserverService().ok_or(NS_ERROR_FAILURE)?;
let obs_service: RefPtr<nsIObserverService> =
xpcom::components::Observer::service().map_err(|_| NS_ERROR_FAILURE)?;
let rv = obs_service.AddObserver(
activity_obs.coerce(),
cstr!("user-interaction-active").as_ptr(),

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

@ -19,7 +19,10 @@ use std::{
str,
sync::{Arc, Mutex, RwLock},
};
use xpcom::{interfaces::nsIFile, XpCom};
use xpcom::{
interfaces::{nsIFile, nsIObserverService, nsIProperties, nsIXULRuntime},
RefPtr, XpCom,
};
type Manager = rkv::Manager<SafeModeEnvironment>;
type Rkv = rkv::Rkv<SafeModeEnvironment>;
@ -79,7 +82,8 @@ fn get_profile_dir() -> XULStoreResult<PathBuf> {
// a `*mut *mut libc::c_void` in Rust, whereas getter_addrefs() expects
// a closure with a `*mut *const T` parameter.
let dir_svc = xpcom::services::get_DirectoryService().ok_or(XULStoreError::Unavailable)?;
let dir_svc: RefPtr<nsIProperties> =
xpcom::components::Directory::service().map_err(|_| XULStoreError::Unavailable)?;
let mut profile_dir = xpcom::GetterAddrefs::<nsIFile>::new();
unsafe {
dir_svc
@ -130,7 +134,8 @@ fn observe_profile_change() {
// But we use a closure returning a result to enable use of the ? operator.
(|| -> XULStoreResult<()> {
// Observe profile changes so we can update this directory accordingly.
let obs_svc = xpcom::services::get_ObserverService().ok_or(XULStoreError::Unavailable)?;
let obs_svc: RefPtr<nsIObserverService> =
xpcom::components::Observer::service().map_err(|_| XULStoreError::Unavailable)?;
let observer = ProfileChangeObserver::new();
unsafe {
obs_svc
@ -147,7 +152,8 @@ fn observe_profile_change() {
}
fn in_safe_mode() -> XULStoreResult<bool> {
let xul_runtime = xpcom::services::get_XULRuntime().ok_or(XULStoreError::Unavailable)?;
let xul_runtime: RefPtr<nsIXULRuntime> =
xpcom::components::XULRuntime::service().map_err(|_| XULStoreError::Unavailable)?;
let mut in_safe_mode = false;
unsafe {
xul_runtime.GetInSafeMode(&mut in_safe_mode).to_result()?;

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

@ -11,6 +11,7 @@ else:
Classes = [
{
'name': 'XULRuntime',
'js_name': 'appinfo',
'cid': '{95d89e3e-a169-41a3-8e56-719978e15b12}',
'contract_ids': [

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

@ -12,6 +12,7 @@ Classes = [
'headers': ['nsArray.h'],
},
{
'name': 'Observer',
'js_name': 'obs',
'cid': '{d07f5195-e3d1-11d2-8acd-00105a1b8860}',
'contract_ids': ['@mozilla.org/observer-service;1'],

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

@ -13,11 +13,12 @@ use nserror::{nsresult, NS_OK};
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::ptr;
use xpcom::interfaces;
use xpcom::{interfaces, RefPtr};
#[no_mangle]
pub unsafe extern "C" fn Rust_ObserveFromRust() -> *const interfaces::nsIObserverService {
let obssvc = xpcom::services::get_ObserverService().unwrap();
let obssvc: RefPtr<interfaces::nsIObserverService> =
xpcom::components::Observer::service().unwrap();
// Define an observer
#[xpcom(implement(nsIObserver), nonatomic)]