mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Auto merge of #6944 - r0e:testing, r=jdm
Fix for issue #6768. Refactor ReadData and BlobBody Fix for issue #6768. Merge common fields of ReadData and BlobBody to avoid passing redundant information to functions. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/6944) <!-- Reviewable:end -->
This commit is contained in:
commit
ac533b1466
1 changed files with 29 additions and 49 deletions
|
@ -37,38 +37,17 @@ pub enum FileReaderFunction {
|
||||||
|
|
||||||
pub type TrustedFileReader = Trusted<FileReader>;
|
pub type TrustedFileReader = Trusted<FileReader>;
|
||||||
|
|
||||||
pub struct ReadData {
|
|
||||||
pub bytes: Receiver<Vec<u8>>,
|
|
||||||
pub blobtype: DOMString,
|
|
||||||
pub label: Option<DOMString>,
|
|
||||||
pub function: FileReaderFunction
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ReadData {
|
|
||||||
pub fn new(bytes: Receiver<Vec<u8>>, blobtype: DOMString,
|
|
||||||
label: Option<DOMString>, function: FileReaderFunction) -> ReadData {
|
|
||||||
ReadData {
|
|
||||||
bytes: bytes,
|
|
||||||
blobtype: blobtype,
|
|
||||||
label: label,
|
|
||||||
function: function,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct BlobBody {
|
pub struct ReadMetaData {
|
||||||
pub bytes: Vec<u8>,
|
|
||||||
pub blobtype: DOMString,
|
pub blobtype: DOMString,
|
||||||
pub label: Option<DOMString>,
|
pub label: Option<DOMString>,
|
||||||
pub function: FileReaderFunction
|
pub function: FileReaderFunction
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlobBody {
|
impl ReadMetaData {
|
||||||
pub fn new(bytes: Vec<u8>, blobtype: DOMString,
|
pub fn new(blobtype: DOMString,
|
||||||
label: Option<DOMString>, function: FileReaderFunction) -> BlobBody {
|
label: Option<DOMString>, function: FileReaderFunction) -> ReadMetaData {
|
||||||
BlobBody {
|
ReadMetaData {
|
||||||
bytes: bytes,
|
|
||||||
blobtype: blobtype,
|
blobtype: blobtype,
|
||||||
label: label,
|
label: label,
|
||||||
function: function,
|
function: function,
|
||||||
|
@ -181,7 +160,8 @@ impl FileReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/FileAPI/#dfn-readAsText
|
// https://w3c.github.io/FileAPI/#dfn-readAsText
|
||||||
pub fn process_read_eof(filereader: TrustedFileReader, gen_id: GenerationId, blob_body: BlobBody) {
|
pub fn process_read_eof(filereader: TrustedFileReader, gen_id: GenerationId,
|
||||||
|
data: ReadMetaData, blob_contents: Vec<u8>) {
|
||||||
let fr = filereader.root();
|
let fr = filereader.root();
|
||||||
|
|
||||||
macro_rules! return_on_abort(
|
macro_rules! return_on_abort(
|
||||||
|
@ -196,11 +176,11 @@ impl FileReader {
|
||||||
// Step 8.1
|
// Step 8.1
|
||||||
fr.change_ready_state(FileReaderReadyState::Done);
|
fr.change_ready_state(FileReaderReadyState::Done);
|
||||||
// Step 8.2
|
// Step 8.2
|
||||||
let output = match blob_body.function {
|
let output = match data.function {
|
||||||
FileReaderFunction::ReadAsDataUrl =>
|
FileReaderFunction::ReadAsDataUrl =>
|
||||||
FileReader::perform_readasdataurl(blob_body),
|
FileReader::perform_readasdataurl(data, blob_contents),
|
||||||
FileReaderFunction::ReadAsText =>
|
FileReaderFunction::ReadAsText =>
|
||||||
FileReader::perform_readastext(blob_body),
|
FileReader::perform_readastext(data, blob_contents),
|
||||||
};
|
};
|
||||||
|
|
||||||
*fr.result.borrow_mut() = Some(output);
|
*fr.result.borrow_mut() = Some(output);
|
||||||
|
@ -218,12 +198,12 @@ impl FileReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/FileAPI/#dfn-readAsText
|
// https://w3c.github.io/FileAPI/#dfn-readAsText
|
||||||
fn perform_readastext(blob_body: BlobBody)
|
fn perform_readastext(data: ReadMetaData, blob_contents: Vec<u8>)
|
||||||
-> DOMString {
|
-> DOMString {
|
||||||
|
|
||||||
let blob_label = &blob_body.label;
|
let blob_label = &data.label;
|
||||||
let blob_type = &blob_body.blobtype;
|
let blob_type = &data.blobtype;
|
||||||
let blob_bytes = &blob_body.bytes[..];
|
let blob_bytes = &blob_contents[..];
|
||||||
|
|
||||||
//https://w3c.github.io/FileAPI/#encoding-determination
|
//https://w3c.github.io/FileAPI/#encoding-determination
|
||||||
// Steps 1 & 2 & 3
|
// Steps 1 & 2 & 3
|
||||||
|
@ -251,7 +231,7 @@ impl FileReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
//https://w3c.github.io/FileAPI/#dfn-readAsDataURL
|
//https://w3c.github.io/FileAPI/#dfn-readAsDataURL
|
||||||
fn perform_readasdataurl(blob_body: BlobBody)
|
fn perform_readasdataurl(data: ReadMetaData, blob_contents: Vec<u8>)
|
||||||
-> DOMString {
|
-> DOMString {
|
||||||
let config = Config {
|
let config = Config {
|
||||||
char_set: CharacterSet::UrlSafe,
|
char_set: CharacterSet::UrlSafe,
|
||||||
|
@ -259,12 +239,12 @@ impl FileReader {
|
||||||
pad: true,
|
pad: true,
|
||||||
line_length: None
|
line_length: None
|
||||||
};
|
};
|
||||||
let base64 = blob_body.bytes.to_base64(config);
|
let base64 = blob_contents.to_base64(config);
|
||||||
|
|
||||||
let output = if blob_body.blobtype.is_empty() {
|
let output = if data.blobtype.is_empty() {
|
||||||
format!("data:base64,{}", base64)
|
format!("data:base64,{}", base64)
|
||||||
} else {
|
} else {
|
||||||
format!("data:{};base64,{}", blob_body.blobtype, base64)
|
format!("data:{};base64,{}", data.blobtype, base64)
|
||||||
};
|
};
|
||||||
|
|
||||||
output
|
output
|
||||||
|
@ -375,7 +355,7 @@ impl<'a> PrivateFileReaderHelpers for &'a FileReader {
|
||||||
blob.read_out_buffer(send);
|
blob.read_out_buffer(send);
|
||||||
let type_ = blob.Type();
|
let type_ = blob.Type();
|
||||||
|
|
||||||
let load_data = ReadData::new(bytes, type_, label, function);
|
let load_data = ReadMetaData::new(type_, label, function);
|
||||||
|
|
||||||
let fr = Trusted::new(global.get_cx(), self, global.script_chan());
|
let fr = Trusted::new(global.get_cx(), self, global.script_chan());
|
||||||
let gen_id = self.generation_id.get();
|
let gen_id = self.generation_id.get();
|
||||||
|
@ -383,7 +363,7 @@ impl<'a> PrivateFileReaderHelpers for &'a FileReader {
|
||||||
let script_chan = global.script_chan();
|
let script_chan = global.script_chan();
|
||||||
|
|
||||||
spawn_named("file reader async operation".to_owned(), move || {
|
spawn_named("file reader async operation".to_owned(), move || {
|
||||||
perform_annotated_read_operation(gen_id, load_data, fr, script_chan)
|
perform_annotated_read_operation(gen_id, load_data, bytes, fr, script_chan)
|
||||||
});
|
});
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -398,7 +378,7 @@ pub enum FileReaderEvent {
|
||||||
ProcessRead(TrustedFileReader, GenerationId),
|
ProcessRead(TrustedFileReader, GenerationId),
|
||||||
ProcessReadData(TrustedFileReader, GenerationId, DOMString),
|
ProcessReadData(TrustedFileReader, GenerationId, DOMString),
|
||||||
ProcessReadError(TrustedFileReader, GenerationId, DOMErrorName),
|
ProcessReadError(TrustedFileReader, GenerationId, DOMErrorName),
|
||||||
ProcessReadEOF(TrustedFileReader, GenerationId, BlobBody)
|
ProcessReadEOF(TrustedFileReader, GenerationId, ReadMetaData, Vec<u8>)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Runnable for FileReaderEvent {
|
impl Runnable for FileReaderEvent {
|
||||||
|
@ -414,15 +394,15 @@ impl Runnable for FileReaderEvent {
|
||||||
FileReaderEvent::ProcessReadError(filereader, gen_id, error) => {
|
FileReaderEvent::ProcessReadError(filereader, gen_id, error) => {
|
||||||
FileReader::process_read_error(filereader, gen_id, error);
|
FileReader::process_read_error(filereader, gen_id, error);
|
||||||
},
|
},
|
||||||
FileReaderEvent::ProcessReadEOF(filereader, gen_id, blob_body) => {
|
FileReaderEvent::ProcessReadEOF(filereader, gen_id, data, blob_contents) => {
|
||||||
FileReader::process_read_eof(filereader, gen_id, blob_body);
|
FileReader::process_read_eof(filereader, gen_id, data, blob_contents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/FileAPI/#task-read-operation
|
// https://w3c.github.io/FileAPI/#task-read-operation
|
||||||
fn perform_annotated_read_operation(gen_id: GenerationId, read_data: ReadData,
|
fn perform_annotated_read_operation(gen_id: GenerationId, data: ReadMetaData, blob_contents: Receiver<Vec<u8>>,
|
||||||
filereader: TrustedFileReader, script_chan: Box<ScriptChan + Send>) {
|
filereader: TrustedFileReader, script_chan: Box<ScriptChan + Send>) {
|
||||||
let chan = &script_chan;
|
let chan = &script_chan;
|
||||||
// Step 4
|
// Step 4
|
||||||
|
@ -433,7 +413,7 @@ fn perform_annotated_read_operation(gen_id: GenerationId, read_data: ReadData,
|
||||||
gen_id, DOMString::new());
|
gen_id, DOMString::new());
|
||||||
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
|
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
|
||||||
|
|
||||||
let bytes = match read_data.bytes.recv() {
|
let bytes = match blob_contents.recv() {
|
||||||
Ok(bytes) => bytes,
|
Ok(bytes) => bytes,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let task = box FileReaderEvent::ProcessReadError(filereader,
|
let task = box FileReaderEvent::ProcessReadError(filereader,
|
||||||
|
@ -443,11 +423,11 @@ fn perform_annotated_read_operation(gen_id: GenerationId, read_data: ReadData,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let blobtype = read_data.blobtype.clone();
|
let blobtype = data.blobtype.clone();
|
||||||
let label = read_data.label.clone();
|
let label = data.label.clone();
|
||||||
|
|
||||||
let blob_body = BlobBody::new(bytes, blobtype, label, read_data.function);
|
let read_meta_data = ReadMetaData::new(blobtype, label, data.function);
|
||||||
|
|
||||||
let task = box FileReaderEvent::ProcessReadEOF(filereader, gen_id, blob_body);
|
let task = box FileReaderEvent::ProcessReadEOF(filereader, gen_id, read_meta_data, bytes);
|
||||||
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
|
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue