Make the fetch method only handle sync XHRs.

This commit is contained in:
Josh Matthews 2015-03-04 15:38:25 -05:00
parent 17a88f1f81
commit 2ee21ddbe7

View file

@ -77,24 +77,6 @@ enum XMLHttpRequestState {
Done = 4, Done = 4,
} }
struct XHRProgressHandler {
addr: TrustedXHRAddress,
progress: XHRProgress,
}
impl XHRProgressHandler {
fn new(addr: TrustedXHRAddress, progress: XHRProgress) -> XHRProgressHandler {
XHRProgressHandler { addr: addr, progress: progress }
}
}
impl Runnable for XHRProgressHandler {
fn handler(self: Box<XHRProgressHandler>) {
let this = *self;
XMLHttpRequest::handle_progress(this.addr, this.progress);
}
}
#[derive(PartialEq, Clone, Copy)] #[derive(PartialEq, Clone, Copy)]
#[jstraceable] #[jstraceable]
pub struct GenerationId(u32); pub struct GenerationId(u32);
@ -122,11 +104,6 @@ impl XHRProgress {
} }
} }
enum SyncOrAsync<'a> {
Sync(JSRef<'a, XMLHttpRequest>),
Async(TrustedXHRAddress, Box<ScriptChan+Send>)
}
enum TerminateReason { enum TerminateReason {
AbortedOrReopened, AbortedOrReopened,
TimedOut, TimedOut,
@ -222,8 +199,10 @@ impl XMLHttpRequest {
let cors_request = match cors_request { let cors_request = match cors_request {
Err(_) => { Err(_) => {
// Happens in case of cross-origin non-http URIs // Happens in case of cross-origin non-http URIs
//notify_error_and_return!(Network); let xhr = xhr.to_temporary().root();
return; //XXXjdm xhr.r().process_partial_response(XHRProgress::Errored(
xhr.r().generation_id.get(), Network));
return; //XXXjdm Err(Network)
} }
Ok(req) => req, Ok(req) => req,
}; };
@ -390,25 +369,14 @@ impl XMLHttpRequest {
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]
fn fetch(fetch_type: &SyncOrAsync, resource_task: ResourceTask, fn fetch(xhr: JSRef<XMLHttpRequest>, resource_task: ResourceTask,
mut load_data: LoadData, terminate_receiver: Receiver<TerminateReason>, mut load_data: LoadData, terminate_receiver: Receiver<TerminateReason>,
cors_request: Result<Option<CORSRequest>,()>, gen_id: GenerationId) cors_request: Result<Option<CORSRequest>,()>, gen_id: GenerationId)
-> ErrorResult { -> ErrorResult {
fn notify_partial_progress(fetch_type: &SyncOrAsync, msg: XHRProgress) {
match *fetch_type {
SyncOrAsync::Sync(xhr) => {
xhr.process_partial_response(msg);
},
SyncOrAsync::Async(ref addr, ref script_chan) => {
script_chan.send(ScriptMsg::RunnableMsg(box XHRProgressHandler::new(addr.clone(), msg))).unwrap();
}
}
}
macro_rules! notify_error_and_return( macro_rules! notify_error_and_return(
($err:expr) => ({ ($err:expr) => ({
notify_partial_progress(fetch_type, XHRProgress::Errored(gen_id, $err)); xhr.process_partial_response(XHRProgress::Errored(gen_id, $err));
return Err($err) return Err($err)
}); });
); );
@ -481,7 +449,7 @@ impl XMLHttpRequest {
_ => {} _ => {}
}; };
// XXXManishearth Clear cache entries in case of a network error // XXXManishearth Clear cache entries in case of a network error
notify_partial_progress(fetch_type, XHRProgress::HeadersReceived(gen_id, xhr.process_partial_response(XHRProgress::HeadersReceived(gen_id,
response.metadata.headers.clone(), response.metadata.status.clone())); response.metadata.headers.clone(), response.metadata.status.clone()));
progress_port = response.progress_port; progress_port = response.progress_port;
@ -506,11 +474,11 @@ impl XMLHttpRequest {
progress = progress_port.recv() => match progress.unwrap() { progress = progress_port.recv() => match progress.unwrap() {
Payload(data) => { Payload(data) => {
buf.push_all(data.as_slice()); buf.push_all(data.as_slice());
notify_partial_progress(fetch_type, xhr.process_partial_response(XHRProgress::Loading(
XHRProgress::Loading(gen_id, ByteString::new(buf.clone()))); gen_id, ByteString::new(buf.clone())));
}, },
Done(Ok(())) => { Done(Ok(())) => {
notify_partial_progress(fetch_type, XHRProgress::Done(gen_id)); xhr.process_partial_response(XHRProgress::Done(gen_id));
return Ok(()); return Ok(());
}, },
Done(Err(_)) => { Done(Err(_)) => {
@ -828,7 +796,7 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
let gen_id = self.generation_id.get(); let gen_id = self.generation_id.get();
if self.sync.get() { if self.sync.get() {
return XMLHttpRequest::fetch(&mut SyncOrAsync::Sync(self), resource_task, load_data, return XMLHttpRequest::fetch(self, resource_task, load_data,
terminate_receiver, cors_request, gen_id); terminate_receiver, cors_request, gen_id);
} else { } else {
self.fetch_time.set(time::now().to_timespec().sec); self.fetch_time.set(time::now().to_timespec().sec);