Bug 1591564 - Run rustfmt on FOGotype r=janerik

Depends on D59531

Differential Revision: https://phabricator.services.mozilla.com/D59532

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Chris H-C 2020-01-10 16:49:39 +00:00
Родитель b38f3b9ecb
Коммит 891ffc198f
1 изменённых файлов: 134 добавлений и 111 удалений

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

@ -5,23 +5,22 @@
#[macro_use]
extern crate cstr;
use glean_preview::{ClientInfoMetrics, Configuration};
use glean_preview::metrics::PingType;
use glean_preview::{ClientInfoMetrics, Configuration};
use log::error;
use nserror::{nsresult, NS_ERROR_FAILURE, NS_OK};
use nsstring::{nsAString, nsString};
use std::{thread, time};
use std::ffi::{CString};
use std::ffi::CString;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Error, Write};
use std::path::{Path, PathBuf};
use std::thread::JoinHandle;
use std::{thread, time};
use xpcom::interfaces::{nsIFile, nsIProcess};
use xpcom::RefPtr;
#[no_mangle]
pub unsafe extern "C" fn fog_init(data_dir: &nsAString, pingsender_path: &nsAString) -> nsresult {
let upload_enabled = static_prefs::pref!("datareporting.healthreport.uploadEnabled");
let cfg = Configuration {
@ -35,7 +34,7 @@ pub unsafe extern "C" fn fog_init(data_dir: &nsAString, pingsender_path: &nsAStr
// TODO: Build our own ClientInfoMetrics instead of using unknown().
if glean_preview::initialize(cfg, ClientInfoMetrics::unknown()).is_err() {
return NS_ERROR_FAILURE
return NS_ERROR_FAILURE;
}
let mut data_path = PathBuf::from(data_dir.to_string());
@ -44,22 +43,28 @@ pub unsafe extern "C" fn fog_init(data_dir: &nsAString, pingsender_path: &nsAStr
// We ignore the returned JoinHandle for the nonce.
// The detached thread will live until this process (the main process) dies.
if prototype_ping_init(data_path, pingsender_path).is_err() {
return NS_ERROR_FAILURE
return NS_ERROR_FAILURE;
}
NS_OK
}
fn prototype_ping_init(ping_dir: PathBuf, pingsender_path: &nsAString) -> Result<JoinHandle<()>, Error> {
fn prototype_ping_init(
ping_dir: PathBuf,
pingsender_path: &nsAString,
) -> Result<JoinHandle<()>, Error> {
let pingsender_path = pingsender_path.to_string();
thread::Builder::new().name("fogotype_ping".to_owned()).spawn(move || {
thread::Builder::new()
.name("fogotype_ping".to_owned())
.spawn(move || {
let prototype_ping = PingType::new("prototype", true, true);
glean_preview::register_ping_type(&prototype_ping);
let an_hour = time::Duration::from_secs(60 * 60);
loop {
thread::sleep(an_hour);
let upload_enabled = static_prefs::pref!("datareporting.healthreport.uploadEnabled");
let upload_enabled =
static_prefs::pref!("datareporting.healthreport.uploadEnabled");
glean_preview::set_upload_enabled(upload_enabled);
if !upload_enabled {
continue;
@ -72,7 +77,10 @@ fn prototype_ping_init(ping_dir: PathBuf, pingsender_path: &nsAString) -> Result
})
}
fn send_all_pings(ping_dir: &Path, pingsender_path: &str) -> Result<(), Box<dyn std::error::Error>> {
fn send_all_pings(
ping_dir: &Path,
pingsender_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
assert!(ping_dir.is_dir());
// This will be a multi-step process:
// 1. Ensure we have an (empty) subdirectory in ping_dir called "telemetry" we can work within.
@ -104,31 +112,46 @@ fn send_all_pings(ping_dir: &Path, pingsender_path: &str) -> Result<(), Box<dyn
continue;
}
let telemetry_ping_path = telemetry_dir.join(path.file_name().ok_or("ping dir file name invalid")?);
let telemetry_ping_path =
telemetry_dir.join(path.file_name().ok_or("ping dir file name invalid")?);
let mut telemetry_ping_file = File::create(&telemetry_ping_path)?;
write!(telemetry_ping_file, "{}", lines[1])?;
fs::remove_file(path)?;
let pingsender_file: RefPtr<nsIFile> = xpcom::create_instance(&cstr!("@mozilla.org/file/local;1")).ok_or("couldn't create nsIFile")?;
let process: RefPtr<nsIProcess> = xpcom::create_instance(&cstr!("@mozilla.org/process/util;1")).ok_or("couldn't create nsIProcess")?;
let pingsender_file: RefPtr<nsIFile> =
xpcom::create_instance(&cstr!("@mozilla.org/file/local;1"))
.ok_or("couldn't create nsIFile")?;
let process: RefPtr<nsIProcess> =
xpcom::create_instance(&cstr!("@mozilla.org/process/util;1"))
.ok_or("couldn't create nsIProcess")?;
unsafe {
pingsender_file.InitWithPath(&*nsString::from(pingsender_path) as &nsAString).to_result()?;
pingsender_file
.InitWithPath(&*nsString::from(pingsender_path) as &nsAString)
.to_result()?;
process.Init(&*pingsender_file).to_result()?;
process.SetStartHidden(true).to_result()?;
process.SetNoShell(true).to_result()?;
};
let server_url = CString::new(format!("https://incoming.telemetry.mozilla.org{}", lines[0]))?;
let telemetry_ping_path_cstr = CString::new(telemetry_ping_path.to_str().expect("non-unicode ping path character"))?;
let mut args = [
server_url.as_ptr(),
telemetry_ping_path_cstr.as_ptr(),
];
let server_url = CString::new(format!(
"https://incoming.telemetry.mozilla.org{}",
lines[0]
))?;
let telemetry_ping_path_cstr = CString::new(
telemetry_ping_path
.to_str()
.expect("non-unicode ping path character"),
)?;
let mut args = [server_url.as_ptr(), telemetry_ping_path_cstr.as_ptr()];
let args_length = 2;
// Block while running the process.
// We should feel free to do this because we're on our own thread.
// Also, if we run async, nsIProcess tries to get the ObserverService on this thread, and asserts.
unsafe { process.Run(true /* blocking */, args.as_mut_ptr(), args_length).to_result()?; };
unsafe {
process
.Run(true /* blocking */, args.as_mut_ptr(), args_length)
.to_result()?;
};
}
Ok(())
}