mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Start switching net/ to use abstractions over channels to allow introducing non-channel communication in the future.
This commit is contained in:
parent
7fddf4aa13
commit
1644436557
13 changed files with 108 additions and 79 deletions
|
@ -51,11 +51,10 @@ pub struct LoadData {
|
|||
pub preserved_headers: Headers,
|
||||
pub data: Option<Vec<u8>>,
|
||||
pub cors: Option<ResourceCORSData>,
|
||||
pub consumer: Sender<LoadResponse>,
|
||||
}
|
||||
|
||||
impl LoadData {
|
||||
pub fn new(url: Url, consumer: Sender<LoadResponse>) -> LoadData {
|
||||
pub fn new(url: Url) -> LoadData {
|
||||
LoadData {
|
||||
url: url,
|
||||
method: Method::Get,
|
||||
|
@ -63,17 +62,31 @@ impl LoadData {
|
|||
preserved_headers: Headers::new(),
|
||||
data: None,
|
||||
cors: None,
|
||||
consumer: consumer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AsyncResponseListener {
|
||||
fn headers_available(&self, metadata: Metadata);
|
||||
fn data_available(&self, payload: Vec<u8>);
|
||||
fn response_complete(&self, status: Result<(), String>);
|
||||
}
|
||||
|
||||
pub trait AsyncResponseTarget {
|
||||
fn get_listener(&self) -> &AsyncResponseListener;
|
||||
}
|
||||
|
||||
pub enum LoadConsumer {
|
||||
Channel(Sender<LoadResponse>),
|
||||
Listener(Box<AsyncResponseTarget + Send>),
|
||||
}
|
||||
|
||||
/// Handle to a resource task
|
||||
pub type ResourceTask = Sender<ControlMsg>;
|
||||
|
||||
pub enum ControlMsg {
|
||||
/// Request the data associated with a particular URL
|
||||
Load(LoadData),
|
||||
Load(LoadData, LoadConsumer),
|
||||
/// Store a set of cookies for a given originating URL
|
||||
SetCookiesForUrl(Url, String, CookieSource),
|
||||
/// Retrieve the stored cookies for a given URL
|
||||
|
@ -159,6 +172,20 @@ pub enum CookieSource {
|
|||
NonHTTP,
|
||||
}
|
||||
|
||||
pub enum ResponseSenders {
|
||||
Channel(Sender<LoadResponse>),
|
||||
Listener(Box<AsyncResponseTarget + Send>),
|
||||
}
|
||||
|
||||
impl ResponseSenders {
|
||||
pub fn from_consumer(consumer: LoadConsumer) -> ResponseSenders {
|
||||
match consumer {
|
||||
LoadConsumer::Channel(c) => ResponseSenders::Channel(c),
|
||||
LoadConsumer::Listener(l) => ResponseSenders::Listener(l),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Messages sent in response to a `Load` message
|
||||
#[derive(PartialEq,Debug)]
|
||||
pub enum ProgressMsg {
|
||||
|
@ -172,7 +199,7 @@ pub enum ProgressMsg {
|
|||
pub fn load_whole_resource(resource_task: &ResourceTask, url: Url)
|
||||
-> Result<(Metadata, Vec<u8>), String> {
|
||||
let (start_chan, start_port) = channel();
|
||||
resource_task.send(ControlMsg::Load(LoadData::new(url, start_chan))).unwrap();
|
||||
resource_task.send(ControlMsg::Load(LoadData::new(url), LoadConsumer::Channel(start_chan))).unwrap();
|
||||
let response = start_port.recv().unwrap();
|
||||
|
||||
let mut buf = vec!();
|
||||
|
@ -188,7 +215,7 @@ pub fn load_whole_resource(resource_task: &ResourceTask, url: Url)
|
|||
/// Load a URL asynchronously and iterate over chunks of bytes from the response.
|
||||
pub fn load_bytes_iter(resource_task: &ResourceTask, url: Url) -> (Metadata, ProgressMsgPortIterator) {
|
||||
let (input_chan, input_port) = channel();
|
||||
resource_task.send(ControlMsg::Load(LoadData::new(url, input_chan))).unwrap();
|
||||
resource_task.send(ControlMsg::Load(LoadData::new(url), LoadConsumer::Channel(input_chan))).unwrap();
|
||||
|
||||
let response = input_port.recv().unwrap();
|
||||
let iter = ProgressMsgPortIterator { progress_port: response.progress_port };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue