Simplify AsyncResponseListener implementations.

This commit is contained in:
Eli Friedman 2015-10-15 16:52:09 -07:00
parent c1aff0b678
commit 827f2b873c
3 changed files with 23 additions and 25 deletions

View file

@ -23,7 +23,6 @@ use network_listener::{NetworkListener, PreInvoke};
use script_task::ScriptChan;
use std::ascii::AsciiExt;
use std::borrow::ToOwned;
use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use time::{self, Timespec, now};
use unicase::UniCase;
@ -106,7 +105,7 @@ impl CORSRequest {
script_chan: Box<ScriptChan + Send>) {
struct CORSContext {
listener: Box<AsyncCORSResponseListener + Send>,
response: RefCell<Option<CORSResponse>>,
response: Option<CORSResponse>,
}
// This is shoe-horning the CORSReponse stuff into the rest of the async network
@ -119,7 +118,7 @@ impl CORSRequest {
}
fn response_complete(&mut self, _status: Result<(), String>) {
let response = self.response.borrow_mut().take().unwrap();
let response = self.response.take().unwrap();
self.listener.response_available(response);
}
}
@ -127,7 +126,7 @@ impl CORSRequest {
let context = CORSContext {
listener: listener,
response: RefCell::new(None),
response: None,
};
let listener = NetworkListener {
context: Arc::new(Mutex::new(context)),
@ -141,7 +140,7 @@ impl CORSRequest {
let response = req.http_fetch();
let mut context = listener.context.lock();
let context = context.as_mut().unwrap();
*context.response.borrow_mut() = Some(response);
context.response = Some(response);
listener.notify(ResponseAction::ResponseComplete(Ok(())));
});
}

View file

@ -38,7 +38,7 @@ use network_listener::{NetworkListener, PreInvoke};
use script_task::ScriptTaskEventCategory::ScriptEvent;
use script_task::{CommonScriptMsg, Runnable, ScriptChan};
use std::ascii::AsciiExt;
use std::cell::{Cell, RefCell};
use std::cell::Cell;
use std::mem;
use std::sync::{Arc, Mutex};
use url::{Url, UrlParser};
@ -128,9 +128,9 @@ struct ScriptContext {
/// The element that initiated the request.
elem: Trusted<HTMLScriptElement>,
/// The response body received to date.
data: RefCell<Vec<u8>>,
data: Vec<u8>,
/// The response metadata received to date.
metadata: RefCell<Option<Metadata>>,
metadata: Option<Metadata>,
/// Whether the owning document's parser should resume once the response completes.
resume_on_completion: bool,
/// The initial URL requested.
@ -139,18 +139,18 @@ struct ScriptContext {
impl AsyncResponseListener for ScriptContext {
fn headers_available(&mut self, metadata: Metadata) {
*self.metadata.borrow_mut() = Some(metadata);
self.metadata = Some(metadata);
}
fn data_available(&mut self, payload: Vec<u8>) {
let mut payload = payload;
self.data.borrow_mut().append(&mut payload);
self.data.append(&mut payload);
}
fn response_complete(&mut self, status: Result<(), String>) {
let load = status.map(|_| {
let data = mem::replace(&mut *self.data.borrow_mut(), vec!());
let metadata = self.metadata.borrow_mut().take().unwrap();
let data = mem::replace(&mut self.data, vec!());
let metadata = self.metadata.take().unwrap();
(metadata, data)
});
let elem = self.elem.root();
@ -283,8 +283,8 @@ impl HTMLScriptElement {
let context = Arc::new(Mutex::new(ScriptContext {
elem: elem,
data: RefCell::new(vec!()),
metadata: RefCell::new(None),
data: vec!(),
metadata: None,
resume_on_completion: self.parser_inserted.get(),
url: url.clone(),
}));

View file

@ -31,7 +31,7 @@ use net_traits::{AsyncResponseListener, Metadata};
use network_listener::PreInvoke;
use parse::Parser;
use script_task::{ScriptChan, ScriptTask};
use std::cell::{Cell, RefCell};
use std::cell::Cell;
use std::default::Default;
use url::Url;
@ -69,9 +69,9 @@ pub type Tokenizer = tokenizer::Tokenizer<TreeBuilder<JS<Node>, Sink>>;
/// The context required for asynchronously fetching a document and parsing it progressively.
pub struct ParserContext {
/// The parser that initiated the request.
parser: RefCell<Option<Trusted<ServoHTMLParser>>>,
parser: Option<Trusted<ServoHTMLParser>>,
/// Is this document a synthesized document for a single image?
is_image_document: Cell<bool>,
is_image_document: bool,
/// The pipeline associated with this document.
id: PipelineId,
/// The subpage associated with this document.
@ -86,8 +86,8 @@ impl ParserContext {
pub fn new(id: PipelineId, subpage: Option<SubpageId>, script_chan: Box<ScriptChan + Send>,
url: Url) -> ParserContext {
ParserContext {
parser: RefCell::new(None),
is_image_document: Cell::new(false),
parser: None,
is_image_document: false,
id: id,
subpage: subpage,
script_chan: script_chan,
@ -109,12 +109,11 @@ impl AsyncResponseListener for ParserContext {
let parser = parser.r();
let win = parser.window();
*self.parser.borrow_mut() = Some(Trusted::new(win.r().get_cx(), parser,
self.script_chan.clone()));
self.parser = Some(Trusted::new(win.r().get_cx(), parser, self.script_chan.clone()));
match content_type {
Some(ContentType(Mime(TopLevel::Image, _, _))) => {
self.is_image_document.set(true);
self.is_image_document = true;
let page = format!("<html><body><img src='{}' /></body></html>",
self.url.serialize());
parser.pending_input.borrow_mut().push(page);
@ -138,10 +137,10 @@ impl AsyncResponseListener for ParserContext {
}
fn data_available(&mut self, payload: Vec<u8>) {
if !self.is_image_document.get() {
if !self.is_image_document {
// FIXME: use Vec<u8> (html5ever #34)
let data = UTF_8.decode(&payload, DecoderTrap::Replace).unwrap();
let parser = match self.parser.borrow().as_ref() {
let parser = match self.parser.as_ref() {
Some(parser) => parser.root(),
None => return,
};
@ -150,7 +149,7 @@ impl AsyncResponseListener for ParserContext {
}
fn response_complete(&mut self, status: Result<(), String>) {
let parser = match self.parser.borrow().as_ref() {
let parser = match self.parser.as_ref() {
Some(parser) => parser.root(),
None => return,
};