Use associated types to improve LayoutThreadFactory and ScriptThreadFactory.

This commit is contained in:
Ms2ger 2016-05-24 17:50:06 +02:00
parent cd1396fa9a
commit 49d244d39c
8 changed files with 47 additions and 78 deletions

View file

@ -16,8 +16,7 @@ use msg::constellation_msg::{PanicMsg, PipelineId, WindowSizeData};
use net_traits::image_cache_thread::ImageCacheThread;
use profile_traits::mem::ReportsChan;
use script_traits::{ConstellationControlMsg, LayoutControlMsg, LayoutMsg as ConstellationMsg};
use script_traits::{OpaqueScriptLayoutChannel, UntrustedNodeAddress};
use std::any::Any;
use script_traits::{UntrustedNodeAddress};
use std::sync::Arc;
use std::sync::mpsc::{Receiver, Sender, channel};
use string_cache::Atom;
@ -230,36 +229,11 @@ impl LayoutChan {
}
}
/// A trait to manage opaque references to script<->layout channels without needing
/// to expose the message type to crates that don't need to know about them.
pub trait ScriptLayoutChan {
fn new(sender: Sender<Msg>, receiver: Receiver<Msg>) -> Self;
fn sender(&self) -> Sender<Msg>;
fn receiver(self) -> Receiver<Msg>;
}
impl ScriptLayoutChan for OpaqueScriptLayoutChannel {
fn new(sender: Sender<Msg>, receiver: Receiver<Msg>) -> OpaqueScriptLayoutChannel {
let inner = (box sender as Box<Any + Send>, box receiver as Box<Any + Send>);
OpaqueScriptLayoutChannel(inner)
}
fn sender(&self) -> Sender<Msg> {
let &OpaqueScriptLayoutChannel((ref sender, _)) = self;
(*sender.downcast_ref::<Sender<Msg>>().unwrap()).clone()
}
fn receiver(self) -> Receiver<Msg> {
let OpaqueScriptLayoutChannel((_, receiver)) = self;
*receiver.downcast::<Receiver<Msg>>().unwrap()
}
}
pub struct NewLayoutThreadInfo {
pub id: PipelineId,
pub url: Url,
pub is_parent: bool,
pub layout_pair: OpaqueScriptLayoutChannel,
pub layout_pair: (Sender<Msg>, Receiver<Msg>),
pub pipeline_port: IpcReceiver<LayoutControlMsg>,
pub constellation_chan: IpcSender<ConstellationMsg>,
pub panic_chan: IpcSender<PanicMsg>,

View file

@ -58,7 +58,7 @@ use js::jsapi::{JSContext, JS_SetWrapObjectCallbacks, JSTracer, SetWindowProxyCl
use js::jsval::UndefinedValue;
use js::rust::Runtime;
use layout_interface::{ReflowQueryType};
use layout_interface::{self, LayoutChan, NewLayoutThreadInfo, ScriptLayoutChan};
use layout_interface::{self, LayoutChan, NewLayoutThreadInfo};
use mem::heap_size_of_self_and_children;
use msg::constellation_msg::{LoadData, PanicMsg, PipelineId, PipelineNamespace};
use msg::constellation_msg::{SubpageId, WindowSizeData, WindowSizeType};
@ -80,10 +80,9 @@ use script_traits::CompositorEvent::{KeyEvent, MouseButtonEvent, MouseMoveEvent,
use script_traits::CompositorEvent::{TouchEvent, TouchpadPressureEvent};
use script_traits::{CompositorEvent, ConstellationControlMsg, EventResult};
use script_traits::{InitialScriptState, MouseButton, MouseEventType, MozBrowserEvent, NewLayoutInfo};
use script_traits::{LayoutMsg, OpaqueScriptLayoutChannel, ScriptMsg as ConstellationMsg};
use script_traits::{LayoutMsg, ScriptMsg as ConstellationMsg};
use script_traits::{ScriptThreadFactory, ScriptToCompositorMsg, TimerEvent, TimerEventRequest, TimerSource};
use script_traits::{TouchEventType, TouchId};
use std::any::Any;
use std::borrow::ToOwned;
use std::cell::{Cell, RefCell};
use std::collections::HashSet;
@ -428,22 +427,16 @@ impl<'a> Drop for ScriptMemoryFailsafe<'a> {
}
impl ScriptThreadFactory for ScriptThread {
fn create_layout_channel() -> OpaqueScriptLayoutChannel {
let (chan, port) = channel();
ScriptLayoutChan::new(chan, port)
}
fn clone_layout_channel(pair: &OpaqueScriptLayoutChannel)
-> Box<Any + Send> {
box pair.sender() as Box<Any + Send>
}
type Message = layout_interface::Msg;
fn create(state: InitialScriptState,
layout_chan: &OpaqueScriptLayoutChannel,
load_data: LoadData) {
load_data: LoadData)
-> (Sender<layout_interface::Msg>, Receiver<layout_interface::Msg>) {
let panic_chan = state.panic_chan.clone();
let (script_chan, script_port) = channel();
let layout_chan = LayoutChan(layout_chan.sender());
let (sender, receiver) = channel();
let layout_chan = LayoutChan(sender.clone());
let pipeline_id = state.id;
thread::spawn_named_with_send_on_panic(format!("ScriptThread {:?}", state.id),
thread_state::SCRIPT,
@ -479,6 +472,8 @@ impl ScriptThreadFactory for ScriptThread {
// This must always be the very last operation performed before the thread completes
failsafe.neuter();
}, Some(pipeline_id), panic_chan);
(sender, receiver)
}
}
@ -1103,9 +1098,8 @@ impl ScriptThread {
content_process_shutdown_chan,
} = new_layout_info;
let layout_pair = ScriptThread::create_layout_channel();
let layout_chan = LayoutChan(*ScriptThread::clone_layout_channel(
&layout_pair).downcast::<Sender<layout_interface::Msg>>().unwrap());
let layout_pair = channel();
let layout_chan = LayoutChan(layout_pair.0.clone());
let layout_creation_info = NewLayoutThreadInfo {
id: new_pipeline_id,