compositing: Split Servo up into multiple sandboxed processes.

Multiprocess mode is enabled with the `-M` switch, and sandboxing is
enabled with the `-S` switch.
This commit is contained in:
Patrick Walton 2015-07-24 17:00:10 -07:00 committed by Josh Matthews
parent ff4171170d
commit 1c130819ca
33 changed files with 688 additions and 265 deletions

View file

@ -14,6 +14,7 @@ use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use ipc_channel::router::ROUTER;
use layers::geometry::DevicePixel;
use layout_traits::{LayoutControlChan, LayoutTaskFactory};
use msg::compositor_msg::ScriptToCompositorMsg;
use msg::constellation_msg::ScriptMsg as ConstellationMsg;
use msg::constellation_msg::{ConstellationChan, Failure, FrameId, PipelineId, SubpageId};
use msg::constellation_msg::{LoadData, MozBrowserEvent, WindowSizeData};
@ -26,7 +27,6 @@ use profile_traits::time;
use script_traits::{ConstellationControlMsg, InitialScriptState};
use script_traits::{LayoutControlMsg, NewLayoutInfo, ScriptTaskFactory};
use script_traits::{TimerEventRequest};
use std::any::Any;
use std::mem;
use std::sync::mpsc::{Receiver, Sender, channel};
use std::thread;
@ -34,20 +34,21 @@ use url::Url;
use util;
use util::geometry::{PagePx, ViewportPx};
use util::ipc::OptionalIpcSender;
use util::opts::{self, Opts};
use util::prefs;
/// A uniquely-identifiable pipeline of script task, layout task, and paint task.
pub struct Pipeline {
pub id: PipelineId,
pub parent_info: Option<(PipelineId, SubpageId)>,
pub script_chan: Sender<ConstellationControlMsg>,
pub script_chan: IpcSender<ConstellationControlMsg>,
/// A channel to layout, for performing reflows and shutdown.
pub layout_chan: LayoutControlChan,
/// A channel to the compositor.
pub compositor_proxy: Box<CompositorProxy + 'static + Send>,
pub chrome_to_paint_chan: Sender<ChromeToPaintMsg>,
pub layout_shutdown_port: Receiver<()>,
pub paint_shutdown_port: Receiver<()>,
pub layout_shutdown_port: IpcReceiver<()>,
pub paint_shutdown_port: IpcReceiver<()>,
/// URL corresponding to the most recently-loaded page.
pub url: Url,
/// The title of the most recently-loaded page.
@ -63,7 +64,7 @@ pub struct Pipeline {
#[derive(Clone)]
pub struct CompositionPipeline {
pub id: PipelineId,
pub script_chan: Sender<ConstellationControlMsg>,
pub script_chan: IpcSender<ConstellationControlMsg>,
pub layout_chan: LayoutControlChan,
pub chrome_to_paint_chan: Sender<ChromeToPaintMsg>,
}
@ -104,7 +105,7 @@ pub struct InitialPipelineState {
pub device_pixel_ratio: ScaleFactor<ViewportPx, DevicePixel, f32>,
/// A channel to the script thread, if applicable. If this is `Some`,
/// then `parent_info` must also be `Some`.
pub script_chan: Option<Sender<ConstellationControlMsg>>,
pub script_chan: Option<IpcSender<ConstellationControlMsg>>,
/// Information about the page to load.
pub load_data: LoadData,
/// The ID of the pipeline namespace for this script thread.
@ -115,13 +116,14 @@ impl Pipeline {
/// Starts a paint task, layout task, and possibly a script task.
/// Returns the channels wrapped in a struct.
pub fn create<LTF, STF>(state: InitialPipelineState)
-> (Pipeline, PipelineContent)
-> (Pipeline, UnprivilegedPipelineContent, PrivilegedPipelineContent)
where LTF: LayoutTaskFactory, STF: ScriptTaskFactory {
let (layout_to_paint_chan, layout_to_paint_port) = util::ipc::optional_ipc_channel();
let (chrome_to_paint_chan, chrome_to_paint_port) = channel();
let (paint_shutdown_chan, paint_shutdown_port) = channel();
let (layout_shutdown_chan, layout_shutdown_port) = channel();
let (paint_shutdown_chan, paint_shutdown_port) = ipc::channel().unwrap();
let (layout_shutdown_chan, layout_shutdown_port) = ipc::channel().unwrap();
let (pipeline_chan, pipeline_port) = ipc::channel().unwrap();
let (script_to_compositor_chan, script_to_compositor_port) = ipc::channel().unwrap();
let mut pipeline_port = Some(pipeline_port);
let failure = Failure {
@ -148,6 +150,9 @@ impl Pipeline {
script_to_devtools_chan
});
let (layout_content_process_shutdown_chan, layout_content_process_shutdown_port) =
ipc::channel().unwrap();
let (script_chan, script_port) = match state.script_chan {
Some(script_chan) => {
let (containing_pipeline_id, subpage_id) =
@ -157,10 +162,11 @@ impl Pipeline {
new_pipeline_id: state.id,
subpage_id: subpage_id,
load_data: state.load_data.clone(),
paint_chan: box layout_to_paint_chan.clone() as Box<Any + Send>,
paint_chan: layout_to_paint_chan.clone().to_opaque(),
failure: failure,
pipeline_port: mem::replace(&mut pipeline_port, None).unwrap(),
layout_shutdown_chan: layout_shutdown_chan.clone(),
content_process_shutdown_chan: layout_content_process_shutdown_chan.clone(),
};
script_chan.send(ConstellationControlMsg::AttachLayout(new_layout_info))
@ -168,11 +174,14 @@ impl Pipeline {
(script_chan, None)
}
None => {
let (script_chan, script_port) = channel();
let (script_chan, script_port) = ipc::channel().unwrap();
(script_chan, Some(script_port))
}
};
let (script_content_process_shutdown_chan, script_content_process_shutdown_port) =
ipc::channel().unwrap();
let pipeline = Pipeline::new(state.id,
state.parent_info,
script_chan.clone(),
@ -184,45 +193,63 @@ impl Pipeline {
state.load_data.url.clone(),
state.window_size);
let pipeline_content = PipelineContent {
let unprivileged_pipeline_content = UnprivilegedPipelineContent {
id: state.id,
parent_info: state.parent_info,
constellation_chan: state.constellation_chan,
constellation_chan: state.constellation_chan.clone(),
scheduler_chan: state.scheduler_chan,
compositor_proxy: state.compositor_proxy,
devtools_chan: script_to_devtools_chan,
image_cache_task: state.image_cache_task,
font_cache_task: state.font_cache_task,
font_cache_task: state.font_cache_task.clone(),
resource_task: state.resource_task,
storage_task: state.storage_task,
time_profiler_chan: state.time_profiler_chan,
mem_profiler_chan: state.mem_profiler_chan,
time_profiler_chan: state.time_profiler_chan.clone(),
mem_profiler_chan: state.mem_profiler_chan.clone(),
window_size: window_size,
script_chan: script_chan,
load_data: state.load_data,
load_data: state.load_data.clone(),
failure: failure,
script_port: script_port,
opts: (*opts::get()).clone(),
layout_to_paint_chan: layout_to_paint_chan,
chrome_to_paint_chan: chrome_to_paint_chan,
layout_to_paint_port: Some(layout_to_paint_port),
chrome_to_paint_port: Some(chrome_to_paint_port),
pipeline_port: pipeline_port,
paint_shutdown_chan: paint_shutdown_chan,
layout_shutdown_chan: layout_shutdown_chan,
paint_shutdown_chan: paint_shutdown_chan.clone(),
script_to_compositor_chan: script_to_compositor_chan,
pipeline_namespace_id: state.pipeline_namespace_id,
layout_content_process_shutdown_chan: layout_content_process_shutdown_chan,
layout_content_process_shutdown_port: layout_content_process_shutdown_port,
script_content_process_shutdown_chan: script_content_process_shutdown_chan,
script_content_process_shutdown_port: script_content_process_shutdown_port,
};
(pipeline, pipeline_content)
let privileged_pipeline_content = PrivilegedPipelineContent {
id: state.id,
constellation_chan: state.constellation_chan,
compositor_proxy: state.compositor_proxy,
font_cache_task: state.font_cache_task,
time_profiler_chan: state.time_profiler_chan,
mem_profiler_chan: state.mem_profiler_chan,
load_data: state.load_data,
failure: failure,
layout_to_paint_port: Some(layout_to_paint_port),
chrome_to_paint_chan: chrome_to_paint_chan,
chrome_to_paint_port: Some(chrome_to_paint_port),
paint_shutdown_chan: paint_shutdown_chan,
script_to_compositor_port: Some(script_to_compositor_port),
};
(pipeline, unprivileged_pipeline_content, privileged_pipeline_content)
}
pub fn new(id: PipelineId,
parent_info: Option<(PipelineId, SubpageId)>,
script_chan: Sender<ConstellationControlMsg>,
script_chan: IpcSender<ConstellationControlMsg>,
layout_chan: LayoutControlChan,
compositor_proxy: Box<CompositorProxy + 'static + Send>,
chrome_to_paint_chan: Sender<ChromeToPaintMsg>,
layout_shutdown_port: Receiver<()>,
paint_shutdown_port: Receiver<()>,
layout_shutdown_port: IpcReceiver<()>,
paint_shutdown_port: IpcReceiver<()>,
url: Url,
size: Option<TypedSize2D<PagePx, f32>>)
-> Pipeline {
@ -315,13 +342,14 @@ impl Pipeline {
}
}
pub struct PipelineContent {
#[derive(Deserialize, Serialize)]
pub struct UnprivilegedPipelineContent {
id: PipelineId,
parent_info: Option<(PipelineId, SubpageId)>,
constellation_chan: ConstellationChan<ConstellationMsg>,
scheduler_chan: IpcSender<TimerEventRequest>,
compositor_proxy: Box<CompositorProxy + Send + 'static>,
devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
script_to_compositor_chan: IpcSender<ScriptToCompositorMsg>,
image_cache_task: ImageCacheTask,
font_cache_task: FontCacheTask,
resource_task: ResourceTask,
@ -329,39 +357,31 @@ pub struct PipelineContent {
time_profiler_chan: time::ProfilerChan,
mem_profiler_chan: profile_mem::ProfilerChan,
window_size: Option<WindowSizeData>,
script_chan: Sender<ConstellationControlMsg>,
script_chan: IpcSender<ConstellationControlMsg>,
load_data: LoadData,
failure: Failure,
script_port: Option<Receiver<ConstellationControlMsg>>,
script_port: Option<IpcReceiver<ConstellationControlMsg>>,
layout_to_paint_chan: OptionalIpcSender<LayoutToPaintMsg>,
chrome_to_paint_chan: Sender<ChromeToPaintMsg>,
layout_to_paint_port: Option<Receiver<LayoutToPaintMsg>>,
chrome_to_paint_port: Option<Receiver<ChromeToPaintMsg>>,
paint_shutdown_chan: Sender<()>,
opts: Opts,
paint_shutdown_chan: IpcSender<()>,
pipeline_port: Option<IpcReceiver<LayoutControlMsg>>,
layout_shutdown_chan: Sender<()>,
pipeline_namespace_id: PipelineNamespaceId,
layout_shutdown_chan: IpcSender<()>,
layout_content_process_shutdown_chan: IpcSender<()>,
layout_content_process_shutdown_port: IpcReceiver<()>,
script_content_process_shutdown_chan: IpcSender<()>,
script_content_process_shutdown_port: IpcReceiver<()>,
}
impl PipelineContent {
pub fn start_all<LTF, STF>(mut self) where LTF: LayoutTaskFactory, STF: ScriptTaskFactory {
impl UnprivilegedPipelineContent {
pub fn start_all<LTF, STF>(mut self, wait_for_completion: bool)
where LTF: LayoutTaskFactory, STF: ScriptTaskFactory {
let layout_pair = ScriptTaskFactory::create_layout_channel(None::<&mut STF>);
let (script_to_compositor_chan, script_to_compositor_port) = ipc::channel().unwrap();
self.start_paint_task();
let compositor_proxy_for_script_listener_thread =
self.compositor_proxy.clone_compositor_proxy();
thread::spawn(move || {
compositor_task::run_script_listener_thread(
compositor_proxy_for_script_listener_thread,
script_to_compositor_port)
});
ScriptTaskFactory::create(None::<&mut STF>, InitialScriptState {
id: self.id,
parent_info: self.parent_info,
compositor: script_to_compositor_chan,
compositor: self.script_to_compositor_chan.clone(),
control_chan: self.script_chan.clone(),
control_port: mem::replace(&mut self.script_port, None).unwrap(),
constellation_chan: self.constellation_chan.clone(),
@ -375,6 +395,7 @@ impl PipelineContent {
devtools_chan: self.devtools_chan,
window_size: self.window_size,
pipeline_namespace_id: self.pipeline_namespace_id,
content_process_shutdown_chan: self.script_content_process_shutdown_chan.clone(),
}, &layout_pair, self.load_data.clone());
LayoutTaskFactory::create(None::<&mut LTF>,
@ -391,7 +412,49 @@ impl PipelineContent {
self.font_cache_task,
self.time_profiler_chan,
self.mem_profiler_chan,
self.layout_shutdown_chan);
self.layout_shutdown_chan,
self.layout_content_process_shutdown_chan.clone());
if wait_for_completion {
self.script_content_process_shutdown_port.recv().unwrap();
self.layout_content_process_shutdown_port.recv().unwrap();
}
}
pub fn opts(&self) -> Opts {
self.opts.clone()
}
}
pub struct PrivilegedPipelineContent {
id: PipelineId,
constellation_chan: ConstellationChan<ConstellationMsg>,
compositor_proxy: Box<CompositorProxy + Send + 'static>,
script_to_compositor_port: Option<IpcReceiver<ScriptToCompositorMsg>>,
font_cache_task: FontCacheTask,
time_profiler_chan: time::ProfilerChan,
mem_profiler_chan: profile_mem::ProfilerChan,
load_data: LoadData,
failure: Failure,
layout_to_paint_port: Option<Receiver<LayoutToPaintMsg>>,
chrome_to_paint_chan: Sender<ChromeToPaintMsg>,
chrome_to_paint_port: Option<Receiver<ChromeToPaintMsg>>,
paint_shutdown_chan: IpcSender<()>,
}
impl PrivilegedPipelineContent {
pub fn start_all(&mut self) {
self.start_paint_task();
let compositor_proxy_for_script_listener_thread =
self.compositor_proxy.clone_compositor_proxy();
let script_to_compositor_port =
mem::replace(&mut self.script_to_compositor_port, None).unwrap();
thread::spawn(move || {
compositor_task::run_script_listener_thread(
compositor_proxy_for_script_listener_thread,
script_to_compositor_port)
});
}
pub fn start_paint_task(&mut self) {