diff options
Diffstat (limited to 'components/net_traits/lib.rs')
-rw-r--r-- | components/net_traits/lib.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/components/net_traits/lib.rs b/components/net_traits/lib.rs index 127c85f13b0..4760797cc2f 100644 --- a/components/net_traits/lib.rs +++ b/components/net_traits/lib.rs @@ -187,6 +187,13 @@ pub trait FetchTaskTarget { fn process_response_eof(&mut self, response: &response::Response); } +pub trait FetchResponseListener { + fn process_request_body(&mut self); + fn process_request_eof(&mut self); + fn process_response(&mut self, metadata: Result<Metadata, NetworkError>); + fn process_response_eof(&mut self, response: Result<Vec<u8>, NetworkError>); +} + impl FetchTaskTarget for IpcSender<FetchResponseMsg> { fn process_request_body(&mut self, _: &request::Request) { let _ = self.send(FetchResponseMsg::ProcessRequestBody); @@ -217,6 +224,10 @@ impl FetchTaskTarget for IpcSender<FetchResponseMsg> { } } +pub trait Action<Listener> { + fn process(self, listener: &mut Listener); +} + /// A listener for asynchronous network events. Cancelling the underlying request is unsupported. pub trait AsyncResponseListener { /// The response headers for a request have been received. @@ -241,9 +252,9 @@ pub enum ResponseAction { ResponseComplete(Result<(), NetworkError>) } -impl ResponseAction { +impl<T: AsyncResponseListener> Action<T> for ResponseAction { /// Execute the default action on a provided listener. - pub fn process(self, listener: &mut AsyncResponseListener) { + fn process(self, listener: &mut T) { match self { ResponseAction::HeadersAvailable(m) => listener.headers_available(m), ResponseAction::DataAvailable(d) => listener.data_available(d), @@ -252,6 +263,18 @@ impl ResponseAction { } } +impl<T: FetchResponseListener> Action<T> for FetchResponseMsg { + /// Execute the default action on a provided listener. + fn process(self, listener: &mut T) { + match self { + FetchResponseMsg::ProcessRequestBody => listener.process_request_body(), + FetchResponseMsg::ProcessRequestEOF => listener.process_request_eof(), + FetchResponseMsg::ProcessResponse(meta) => listener.process_response(meta), + FetchResponseMsg::ProcessResponseEOF(data) => listener.process_response_eof(data), + } + } +} + /// A target for async networking events. Commonly used to dispatch a runnable event to another /// thread storing the wrapped closure for later execution. #[derive(Deserialize, Serialize)] |