servo: Merge #12635 - Extracted common parts of the starting content process (from nc4rrillo:refactor-spawn-multiprocess); r=Manishearth

<!-- Please describe your changes on the following line: -->

---
I implemented a trait for both `process::Command` and `sandbox::Command` in order to constrain my generic `setup_common` method.

<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #12622 (github issue number if applicable).

<!-- Either: -->
- [X] These changes do not require tests because we are extracting common functionality across two code branches into a method

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: 94e6f59174f7102d37eae7107e6bd502a9b0ebe1
This commit is contained in:
nc4rrillo 2016-07-29 06:43:19 -05:00
parent e8a467a45c
commit a8e51ccb9a

View File

@ -28,6 +28,8 @@ use script_traits::{ConstellationControlMsg, InitialScriptState, MozBrowserEvent
use script_traits::{LayoutControlMsg, LayoutMsg, NewLayoutInfo, ScriptMsg, SWManagerMsg, SWManagerSenders}; use script_traits::{LayoutControlMsg, LayoutMsg, NewLayoutInfo, ScriptMsg, SWManagerMsg, SWManagerSenders};
use script_traits::{ScriptThreadFactory, TimerEventRequest, WindowSizeData}; use script_traits::{ScriptThreadFactory, TimerEventRequest, WindowSizeData};
use std::collections::HashMap; use std::collections::HashMap;
use std::env;
use std::ffi::OsStr;
use std::io::Error as IOError; use std::io::Error as IOError;
use std::process; use std::process;
use std::sync::mpsc::{Sender, channel}; use std::sync::mpsc::{Sender, channel};
@ -485,7 +487,18 @@ impl UnprivilegedPipelineContent {
use gaol::sandbox::{self, Sandbox, SandboxMethods}; use gaol::sandbox::{self, Sandbox, SandboxMethods};
use ipc_channel::ipc::IpcOneShotServer; use ipc_channel::ipc::IpcOneShotServer;
use sandboxing::content_process_sandbox_profile; use sandboxing::content_process_sandbox_profile;
use std::env;
impl CommandMethods for sandbox::Command {
fn arg<T>(&mut self, arg: T)
where T: AsRef<OsStr> {
self.arg(arg);
}
fn env<T, U>(&mut self, key: T, val: U)
where T: AsRef<OsStr>, U: AsRef<OsStr> {
self.env(key, val);
}
}
// Note that this function can panic, due to process creation, // Note that this function can panic, due to process creation,
// avoiding this panic would require a mechanism for dealing // avoiding this panic would require a mechanism for dealing
@ -497,15 +510,7 @@ impl UnprivilegedPipelineContent {
// If there is a sandbox, use the `gaol` API to create the child process. // If there is a sandbox, use the `gaol` API to create the child process.
let child_process = if opts::get().sandbox { let child_process = if opts::get().sandbox {
let mut command = sandbox::Command::me().expect("Failed to get current sandbox."); let mut command = sandbox::Command::me().expect("Failed to get current sandbox.");
command.arg("--content-process").arg(token); self.setup_common(&mut command, token);
if let Ok(value) = env::var("RUST_BACKTRACE") {
command.env("RUST_BACKTRACE", value);
}
if let Ok(value) = env::var("RUST_LOG") {
command.env("RUST_LOG", value);
}
let profile = content_process_sandbox_profile(); let profile = content_process_sandbox_profile();
ChildProcess::Sandboxed(Sandbox::new(profile).start(&mut command) ChildProcess::Sandboxed(Sandbox::new(profile).start(&mut command)
@ -514,16 +519,7 @@ impl UnprivilegedPipelineContent {
let path_to_self = env::current_exe() let path_to_self = env::current_exe()
.expect("Failed to get current executor."); .expect("Failed to get current executor.");
let mut child_process = process::Command::new(path_to_self); let mut child_process = process::Command::new(path_to_self);
child_process.arg("--content-process"); self.setup_common(&mut child_process, token);
child_process.arg(token);
if let Ok(value) = env::var("RUST_BACKTRACE") {
child_process.env("RUST_BACKTRACE", value);
}
if let Ok(value) = env::var("RUST_LOG") {
child_process.env("RUST_LOG", value);
}
ChildProcess::Unsandboxed(child_process.spawn() ChildProcess::Unsandboxed(child_process.spawn()
.expect("Failed to start unsandboxed child process!")) .expect("Failed to start unsandboxed child process!"))
@ -541,6 +537,19 @@ impl UnprivilegedPipelineContent {
process::exit(1); process::exit(1);
} }
fn setup_common<C: CommandMethods>(&self, command: &mut C, token: String) {
C::arg(command, "--content-process");
C::arg(command, token);
if let Ok(value) = env::var("RUST_BACKTRACE") {
C::env(command, "RUST_BACKTRACE", value);
}
if let Ok(value) = env::var("RUST_LOG") {
C::env(command, "RUST_LOG", value);
}
}
pub fn constellation_chan(&self) -> IpcSender<ScriptMsg> { pub fn constellation_chan(&self) -> IpcSender<ScriptMsg> {
self.constellation_chan.clone() self.constellation_chan.clone()
} }
@ -560,3 +569,23 @@ impl UnprivilegedPipelineContent {
} }
} }
} }
trait CommandMethods {
fn arg<T>(&mut self, arg: T)
where T: AsRef<OsStr>;
fn env<T, U>(&mut self, key: T, val: U)
where T: AsRef<OsStr>, U: AsRef<OsStr>;
}
impl CommandMethods for process::Command {
fn arg<T>(&mut self, arg: T)
where T: AsRef<OsStr> {
self.arg(arg);
}
fn env<T, U>(&mut self, key: T, val: U)
where T: AsRef<OsStr>, U: AsRef<OsStr> {
self.env(key, val);
}
}