adding interface for custom responses

This commit is contained in:
Rahul Sharma 2016-05-01 01:11:15 +05:30
parent bcea0ada27
commit 3766cd1673
17 changed files with 663 additions and 146 deletions

View file

@ -76,6 +76,30 @@ pub enum LoadContext {
CacheManifest,
}
#[derive(Clone, Debug, Deserialize, Serialize, HeapSizeOf)]
pub struct CustomResponse {
#[ignore_heap_size_of = "Defined in hyper"]
pub headers: Headers,
#[ignore_heap_size_of = "Defined in hyper"]
pub raw_status: RawStatus,
pub body: Vec<u8>
}
impl CustomResponse {
pub fn new(headers: Headers, raw_status: RawStatus, body: Vec<u8>) -> CustomResponse {
CustomResponse { headers: headers, raw_status: raw_status, body: body }
}
}
pub type CustomResponseSender = IpcSender<Option<CustomResponse>>;
#[derive(Clone, Deserialize, Serialize, HeapSizeOf)]
pub enum RequestSource {
Window(#[ignore_heap_size_of = "Defined in ipc-channel"] IpcSender<CustomResponseSender>),
Worker(#[ignore_heap_size_of = "Defined in ipc-channel"] IpcSender<CustomResponseSender>),
None
}
#[derive(Clone, Deserialize, Serialize, HeapSizeOf)]
pub struct LoadData {
pub url: Url,
@ -96,15 +120,14 @@ pub struct LoadData {
/// The policy and referring URL for the originator of this request
pub referrer_policy: Option<ReferrerPolicy>,
pub referrer_url: Option<Url>,
pub source: RequestSource,
}
impl LoadData {
pub fn new(context: LoadContext,
url: Url,
id: Option<PipelineId>,
referrer_policy: Option<ReferrerPolicy>,
referrer_url: Option<Url>) -> LoadData {
load_origin: &LoadOrigin) -> LoadData {
LoadData {
url: url,
method: Method::Get,
@ -112,15 +135,23 @@ impl LoadData {
preserved_headers: Headers::new(),
data: None,
cors: None,
pipeline_id: id,
pipeline_id: load_origin.pipeline_id(),
credentials_flag: true,
context: context,
referrer_policy: referrer_policy,
referrer_url: referrer_url
referrer_policy: load_origin.referrer_policy(),
referrer_url: load_origin.referrer_url(),
source: load_origin.request_source()
}
}
}
pub trait LoadOrigin {
fn referrer_url(&self) -> Option<Url>;
fn referrer_policy(&self) -> Option<ReferrerPolicy>;
fn request_source(&self) -> RequestSource;
fn pipeline_id(&self) -> Option<PipelineId>;
}
/// Interface for observing the final response for an asynchronous fetch operation.
pub trait AsyncFetchListener {
fn response_available(&self, response: response::Response);
@ -304,6 +335,7 @@ pub struct PendingAsyncLoad {
context: LoadContext,
referrer_policy: Option<ReferrerPolicy>,
referrer_url: Option<Url>,
source: RequestSource
}
struct PendingLoadGuard {
@ -324,13 +356,29 @@ impl Drop for PendingLoadGuard {
}
}
impl LoadOrigin for PendingAsyncLoad {
fn referrer_url(&self) -> Option<Url> {
self.referrer_url.clone()
}
fn referrer_policy(&self) -> Option<ReferrerPolicy> {
self.referrer_policy.clone()
}
fn request_source(&self) -> RequestSource {
self.source.clone()
}
fn pipeline_id(&self) -> Option<PipelineId> {
self.pipeline
}
}
impl PendingAsyncLoad {
pub fn new(context: LoadContext,
core_resource_thread: CoreResourceThread,
url: Url,
pipeline: Option<PipelineId>,
referrer_policy: Option<ReferrerPolicy>,
referrer_url: Option<Url>)
referrer_url: Option<Url>,
source: RequestSource)
-> PendingAsyncLoad {
PendingAsyncLoad {
core_resource_thread: core_resource_thread,
@ -339,14 +387,18 @@ impl PendingAsyncLoad {
guard: PendingLoadGuard { loaded: false, },
context: context,
referrer_policy: referrer_policy,
referrer_url: referrer_url
referrer_url: referrer_url,
source: source
}
}
/// Initiate the network request associated with this pending load, using the provided target.
pub fn load_async(mut self, listener: AsyncResponseTarget) {
self.guard.neuter();
let load_data = LoadData::new(self.context, self.url, self.pipeline, self.referrer_policy, self.referrer_url);
let load_data = LoadData::new(self.context.clone(),
self.url.clone(),
&self);
let consumer = LoadConsumer::Listener(listener);
self.core_resource_thread.send(CoreResourceMsg::Load(load_data, consumer, None)).unwrap();
}
@ -460,11 +512,11 @@ pub enum ProgressMsg {
pub fn load_whole_resource(context: LoadContext,
core_resource_thread: &CoreResourceThread,
url: Url,
pipeline_id: Option<PipelineId>)
load_origin: &LoadOrigin)
-> Result<(Metadata, Vec<u8>), NetworkError> {
let (start_chan, start_port) = ipc::channel().unwrap();
core_resource_thread.send(CoreResourceMsg::Load(LoadData::new(context, url, pipeline_id, None, None),
LoadConsumer::Channel(start_chan), None)).unwrap();
let load_data = LoadData::new(context, url, load_origin);
core_resource_thread.send(CoreResourceMsg::Load(load_data, LoadConsumer::Channel(start_chan), None)).unwrap();
let response = start_port.recv().unwrap();
let mut buf = vec!();