Add StreamConsumer wrapper and methods to response

This commit is contained in:
Ridhim Rastogi 2019-11-30 00:54:11 -05:00 committed by Josh Matthews
parent 87c1019c5d
commit a5469f8710
6 changed files with 51 additions and 90 deletions

View file

@ -40,6 +40,7 @@ use crate::dom::document::PendingRestyle;
use crate::dom::htmlimageelement::SourceSet; use crate::dom::htmlimageelement::SourceSet;
use crate::dom::htmlmediaelement::{HTMLMediaElementFetchContext, MediaFrameRenderer}; use crate::dom::htmlmediaelement::{HTMLMediaElementFetchContext, MediaFrameRenderer};
use crate::dom::identityhub::Identities; use crate::dom::identityhub::Identities;
use crate::script_runtime::StreamConsumer;
use crate::task::TaskBox; use crate::task::TaskBox;
use app_units::Au; use app_units::Au;
use canvas_traits::canvas::{ use canvas_traits::canvas::{
@ -543,6 +544,7 @@ unsafe_no_jsmanaged_fields!(Arc<Mutex<dyn AudioRenderer>>);
unsafe_no_jsmanaged_fields!(MediaSessionActionType); unsafe_no_jsmanaged_fields!(MediaSessionActionType);
unsafe_no_jsmanaged_fields!(MediaMetadata); unsafe_no_jsmanaged_fields!(MediaMetadata);
unsafe_no_jsmanaged_fields!(WebrenderIpcSender); unsafe_no_jsmanaged_fields!(WebrenderIpcSender);
unsafe_no_jsmanaged_fields!(StreamConsumer);
unsafe impl<'a> JSTraceable for &'a str { unsafe impl<'a> JSTraceable for &'a str {
#[inline] #[inline]

View file

@ -19,6 +19,7 @@ use crate::dom::headers::{is_obs_text, is_vchar};
use crate::dom::headers::{Guard, Headers}; use crate::dom::headers::{Guard, Headers};
use crate::dom::promise::Promise; use crate::dom::promise::Promise;
use crate::dom::xmlhttprequest::Extractable; use crate::dom::xmlhttprequest::Extractable;
use crate::script_runtime::StreamConsumer;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use http::header::HeaderMap as HyperHeaders; use http::header::HeaderMap as HyperHeaders;
use hyper::StatusCode; use hyper::StatusCode;
@ -48,6 +49,8 @@ pub struct Response {
body: DomRefCell<NetTraitsResponseBody>, body: DomRefCell<NetTraitsResponseBody>,
#[ignore_malloc_size_of = "Rc"] #[ignore_malloc_size_of = "Rc"]
body_promise: DomRefCell<Option<(Rc<Promise>, BodyType)>>, body_promise: DomRefCell<Option<(Rc<Promise>, BodyType)>>,
#[ignore_malloc_size_of = "StreamConsumer"]
stream_consumer: DomRefCell<Option<StreamConsumer>>,
} }
impl Response { impl Response {
@ -64,6 +67,7 @@ impl Response {
url_list: DomRefCell::new(vec![]), url_list: DomRefCell::new(vec![]),
body: DomRefCell::new(NetTraitsResponseBody::Empty), body: DomRefCell::new(NetTraitsResponseBody::Empty),
body_promise: DomRefCell::new(None), body_promise: DomRefCell::new(None),
stream_consumer: DomRefCell::new(None),
} }
} }
@ -441,11 +445,24 @@ impl Response {
} }
} }
pub fn set_stream_consumer(&self, sc: Option<StreamConsumer>) {
*self.stream_consumer.borrow_mut() = sc;
}
pub fn stream_chunk(&self, stream: &[u8]) {
if let Some(stream_consumer) = self.stream_consumer.borrow_mut().as_ref() {
stream_consumer.consume_chunk(stream);
}
}
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
pub fn finish(&self, body: Vec<u8>) { pub fn finish(&self, body: Vec<u8>) {
*self.body.borrow_mut() = NetTraitsResponseBody::Done(body); *self.body.borrow_mut() = NetTraitsResponseBody::Done(body);
if let Some((p, body_type)) = self.body_promise.borrow_mut().take() { if let Some((p, body_type)) = self.body_promise.borrow_mut().take() {
consume_body_with_promise(self, body_type, &p); consume_body_with_promise(self, body_type, &p);
} }
if let Some(stream_consumer) = self.stream_consumer.borrow_mut().take() {
stream_consumer.stream_end();
}
} }
} }

View file

@ -260,6 +260,7 @@ impl FetchResponseListener for FetchContext {
} }
fn process_response_chunk(&mut self, mut chunk: Vec<u8>) { fn process_response_chunk(&mut self, mut chunk: Vec<u8>) {
self.response_object.root().stream_chunk(chunk.as_slice());
self.body.append(&mut chunk); self.body.append(&mut chunk);
} }

View file

@ -853,26 +853,30 @@ pub struct StreamConsumer(*mut JSStreamConsumer);
#[allow(unsafe_code)] #[allow(unsafe_code)]
impl StreamConsumer { impl StreamConsumer {
fn consume_chunk(&self, stream: &[u8]) -> bool { pub fn consume_chunk(&self, stream: &[u8]) -> bool {
unsafe { unsafe {
let stream_ptr = stream.as_ptr(); let stream_ptr = stream.as_ptr();
return StreamConsumerConsumeChunk(self.0, stream_ptr, stream.len()); return StreamConsumerConsumeChunk(self.0, stream_ptr, stream.len());
} }
} }
fn stream_end(&self) { pub fn stream_end(&self) {
unsafe { unsafe {
StreamConsumerStreamEnd(self.0); StreamConsumerStreamEnd(self.0);
} }
} }
fn stream_error(&self, error_code: usize) { pub fn stream_error(&self, error_code: usize) {
unsafe { unsafe {
StreamConsumerStreamError(self.0, error_code); StreamConsumerStreamError(self.0, error_code);
} }
} }
fn note_response_urls(&self, maybe_url: Option<String>, maybe_source_map_url: Option<String>) { pub fn note_response_urls(
&self,
maybe_url: Option<String>,
maybe_source_map_url: Option<String>,
) {
unsafe { unsafe {
let maybe_url = maybe_url.map(|url| CString::new(url).unwrap()); let maybe_url = maybe_url.map(|url| CString::new(url).unwrap());
let maybe_source_map_url = maybe_source_map_url.map(|url| CString::new(url).unwrap()); let maybe_source_map_url = maybe_source_map_url.map(|url| CString::new(url).unwrap());
@ -965,6 +969,7 @@ unsafe extern "C" fn consume_stream(
); );
return false; return false;
} }
unwrapped_source.set_stream_consumer(Some(StreamConsumer(_consumer)));
} else { } else {
//Step 3 Upon rejection of source, return with reason. //Step 3 Upon rejection of source, return with reason.
throw_dom_exception( throw_dom_exception(

View file

@ -1,22 +1,12 @@
[contenttype.any.worker.html] [contenttype.any.worker.html]
expected: TIMEOUT
[Response with Content-Type "application/wasm": compileStreaming]
expected: TIMEOUT
[Response with Content-Type "APPLICATION/wasm": compileStreaming]
expected: NOTRUN
[Response with Content-Type "APPLICATION/wasm": instantiateStreaming] [Response with Content-Type "APPLICATION/wasm": instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with Content-Type "APPLICATION/WASM": instantiateStreaming] [Response with Content-Type "APPLICATION/WASM": instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with Content-Type "application/wasm": instantiateStreaming] [Response with Content-Type "application/wasm": instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with Content-Type "APPLICATION/WASM": compileStreaming]
expected: NOTRUN
[contenttype.any.sharedworker.html] [contenttype.any.sharedworker.html]
@ -26,24 +16,14 @@
[contenttype.any.html] [contenttype.any.html]
expected: TIMEOUT
[Response with Content-Type "application/wasm": compileStreaming]
expected: TIMEOUT
[Response with Content-Type "APPLICATION/wasm": compileStreaming]
expected: NOTRUN
[Response with Content-Type "APPLICATION/wasm": instantiateStreaming] [Response with Content-Type "APPLICATION/wasm": instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with Content-Type "APPLICATION/WASM": instantiateStreaming] [Response with Content-Type "APPLICATION/WASM": instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with Content-Type "application/wasm": instantiateStreaming] [Response with Content-Type "application/wasm": instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with Content-Type "APPLICATION/WASM": compileStreaming]
expected: NOTRUN
[contenttype.any.serviceworker.html] [contenttype.any.serviceworker.html]

View file

@ -1,49 +1,27 @@
[status.any.html] [status.any.html]
expected: TIMEOUT
[Response with status 300: instantiateStreaming] [Response with status 300: instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with status 500: instantiateStreaming] [Response with status 500: instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with status 700: instantiateStreaming]
expected: NOTRUN
[Response with status 700: compileStreaming]
expected: NOTRUN
[Response with status 600: instantiateStreaming]
expected: NOTRUN
[Response with status 0: instantiateStreaming]
expected: NOTRUN
[Response with status 404: compileStreaming] [Response with status 404: compileStreaming]
expected: NOTRUN expected: FAIL
[Response with status 500: compileStreaming] [Response with status 500: compileStreaming]
expected: NOTRUN expected: FAIL
[Response with status 600: compileStreaming]
expected: NOTRUN
[Response with status 300: compileStreaming] [Response with status 300: compileStreaming]
expected: TIMEOUT expected: FAIL
[Response with status 400: compileStreaming] [Response with status 400: compileStreaming]
expected: NOTRUN expected: FAIL
[Response with status 400: instantiateStreaming] [Response with status 400: instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with status 999: instantiateStreaming]
expected: NOTRUN
[Response with status 999: compileStreaming]
expected: NOTRUN
[Response with status 404: instantiateStreaming] [Response with status 404: instantiateStreaming]
expected: NOTRUN expected: FAIL
[status.any.serviceworker.html] [status.any.serviceworker.html]
@ -59,49 +37,27 @@
[status.any.worker.html] [status.any.worker.html]
expected: TIMEOUT
[Response with status 300: instantiateStreaming] [Response with status 300: instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with status 500: instantiateStreaming] [Response with status 500: instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with status 700: instantiateStreaming]
expected: NOTRUN
[Response with status 700: compileStreaming]
expected: NOTRUN
[Response with status 600: instantiateStreaming]
expected: NOTRUN
[Response with status 0: instantiateStreaming]
expected: NOTRUN
[Response with status 404: compileStreaming] [Response with status 404: compileStreaming]
expected: NOTRUN expected: FAIL
[Response with status 500: compileStreaming] [Response with status 500: compileStreaming]
expected: NOTRUN expected: FAIL
[Response with status 600: compileStreaming]
expected: NOTRUN
[Response with status 300: compileStreaming] [Response with status 300: compileStreaming]
expected: TIMEOUT expected: FAIL
[Response with status 400: compileStreaming] [Response with status 400: compileStreaming]
expected: NOTRUN expected: FAIL
[Response with status 400: instantiateStreaming] [Response with status 400: instantiateStreaming]
expected: NOTRUN expected: FAIL
[Response with status 999: instantiateStreaming]
expected: NOTRUN
[Response with status 999: compileStreaming]
expected: NOTRUN
[Response with status 404: instantiateStreaming] [Response with status 404: instantiateStreaming]
expected: NOTRUN expected: FAIL