From 78c8b4232fd10d2180d138f14260e322ac8f0826 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 21 Nov 2017 15:31:26 -0800 Subject: [PATCH] Use FetchCanceller in XHR --- components/script/dom/xmlhttprequest.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/components/script/dom/xmlhttprequest.rs b/components/script/dom/xmlhttprequest.rs index f1ae76ad087e..e0f29383230d 100644 --- a/components/script/dom/xmlhttprequest.rs +++ b/components/script/dom/xmlhttprequest.rs @@ -38,6 +38,7 @@ use dom::xmlhttprequestupload::XMLHttpRequestUpload; use dom_struct::dom_struct; use encoding_rs::{Encoding, UTF_8}; use euclid::Length; +use fetch::FetchCanceller; use html5ever::serialize; use html5ever::serialize::SerializeOpts; use hyper::header::{ContentLength, ContentType, ContentEncoding}; @@ -154,8 +155,7 @@ pub struct XMLHttpRequest { response_status: Cell>, referrer_url: Option, referrer_policy: Option, - #[ignore_malloc_size_of = "channels are hard"] - cancellation_chan: DomRefCell>>, + canceller: DomRefCell, } impl XMLHttpRequest { @@ -200,7 +200,7 @@ impl XMLHttpRequest { response_status: Cell::new(Ok(())), referrer_url: referrer_url, referrer_policy: referrer_policy, - cancellation_chan: DomRefCell::new(None), + canceller: DomRefCell::new(Default::default()), } } pub fn new(global: &GlobalScope) -> DomRoot { @@ -978,6 +978,7 @@ impl XMLHttpRequest { self.sync.get()); self.cancel_timeout(); + self.canceller.borrow_mut().ignore(); // Part of step 11, send() (processing response end of file) // XXXManishearth handle errors, if any (substep 2) @@ -994,6 +995,7 @@ impl XMLHttpRequest { }, XHRProgress::Errored(_, e) => { self.cancel_timeout(); + self.canceller.borrow_mut().ignore(); self.discard_subsequent_responses(); self.send_flag.set(false); @@ -1023,12 +1025,7 @@ impl XMLHttpRequest { } fn terminate_ongoing_fetch(&self) { - if let Some(ref cancel_chan) = *self.cancellation_chan.borrow() { - // The receiver will be destroyed if the request has already completed; - // so we throw away the error. Cancellation is a courtesy call, - // we don't actually care if the other side heard. - let _ = cancel_chan.send(()); - } + self.canceller.borrow_mut().cancel(); let GenerationId(prev_id) = self.generation_id.get(); self.generation_id.set(GenerationId(prev_id + 1)); self.response_status.set(Ok(())); @@ -1322,8 +1319,7 @@ impl XMLHttpRequest { (global.networking_task_source(), None) }; - let (cancel_sender, cancel_receiver) = ipc::channel().unwrap(); - *self.cancellation_chan.borrow_mut() = Some(cancel_sender); + let cancel_receiver = self.canceller.borrow_mut().initialize(); XMLHttpRequest::initiate_async_xhr(context.clone(), task_source, global, init, cancel_receiver);