mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Fix for issue #6768.
Merged common fields of BlobBody and ReadData into a new struct to avoid passing redundant information
This commit is contained in:
parent
c2497fcd49
commit
3e2e9b8715
1 changed files with 29 additions and 49 deletions
|
@ -37,38 +37,17 @@ pub enum FileReaderFunction {
|
|||
|
||||
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)]
|
||||
pub struct BlobBody {
|
||||
pub bytes: Vec<u8>,
|
||||
pub struct ReadMetaData {
|
||||
pub blobtype: DOMString,
|
||||
pub label: Option<DOMString>,
|
||||
pub function: FileReaderFunction
|
||||
}
|
||||
|
||||
impl BlobBody {
|
||||
pub fn new(bytes: Vec<u8>, blobtype: DOMString,
|
||||
label: Option<DOMString>, function: FileReaderFunction) -> BlobBody {
|
||||
BlobBody {
|
||||
bytes: bytes,
|
||||
impl ReadMetaData {
|
||||
pub fn new(blobtype: DOMString,
|
||||
label: Option<DOMString>, function: FileReaderFunction) -> ReadMetaData {
|
||||
ReadMetaData {
|
||||
blobtype: blobtype,
|
||||
label: label,
|
||||
function: function,
|
||||
|
@ -181,7 +160,8 @@ impl FileReader {
|
|||
}
|
||||
|
||||
// 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();
|
||||
|
||||
macro_rules! return_on_abort(
|
||||
|
@ -196,11 +176,11 @@ impl FileReader {
|
|||
// Step 8.1
|
||||
fr.change_ready_state(FileReaderReadyState::Done);
|
||||
// Step 8.2
|
||||
let output = match blob_body.function {
|
||||
let output = match data.function {
|
||||
FileReaderFunction::ReadAsDataUrl =>
|
||||
FileReader::perform_readasdataurl(blob_body),
|
||||
FileReader::perform_readasdataurl(data, blob_contents),
|
||||
FileReaderFunction::ReadAsText =>
|
||||
FileReader::perform_readastext(blob_body),
|
||||
FileReader::perform_readastext(data, blob_contents),
|
||||
};
|
||||
|
||||
*fr.result.borrow_mut() = Some(output);
|
||||
|
@ -218,12 +198,12 @@ impl FileReader {
|
|||
}
|
||||
|
||||
// https://w3c.github.io/FileAPI/#dfn-readAsText
|
||||
fn perform_readastext(blob_body: BlobBody)
|
||||
fn perform_readastext(data: ReadMetaData, blob_contents: Vec<u8>)
|
||||
-> DOMString {
|
||||
|
||||
let blob_label = &blob_body.label;
|
||||
let blob_type = &blob_body.blobtype;
|
||||
let blob_bytes = &blob_body.bytes[..];
|
||||
let blob_label = &data.label;
|
||||
let blob_type = &data.blobtype;
|
||||
let blob_bytes = &blob_contents[..];
|
||||
|
||||
//https://w3c.github.io/FileAPI/#encoding-determination
|
||||
// Steps 1 & 2 & 3
|
||||
|
@ -251,7 +231,7 @@ impl FileReader {
|
|||
}
|
||||
|
||||
//https://w3c.github.io/FileAPI/#dfn-readAsDataURL
|
||||
fn perform_readasdataurl(blob_body: BlobBody)
|
||||
fn perform_readasdataurl(data: ReadMetaData, blob_contents: Vec<u8>)
|
||||
-> DOMString {
|
||||
let config = Config {
|
||||
char_set: CharacterSet::UrlSafe,
|
||||
|
@ -259,12 +239,12 @@ impl FileReader {
|
|||
pad: true,
|
||||
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)
|
||||
} else {
|
||||
format!("data:{};base64,{}", blob_body.blobtype, base64)
|
||||
format!("data:{};base64,{}", data.blobtype, base64)
|
||||
};
|
||||
|
||||
output
|
||||
|
@ -375,7 +355,7 @@ impl<'a> PrivateFileReaderHelpers for &'a FileReader {
|
|||
blob.read_out_buffer(send);
|
||||
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 gen_id = self.generation_id.get();
|
||||
|
@ -383,7 +363,7 @@ impl<'a> PrivateFileReaderHelpers for &'a FileReader {
|
|||
let script_chan = global.script_chan();
|
||||
|
||||
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(())
|
||||
}
|
||||
|
@ -398,7 +378,7 @@ pub enum FileReaderEvent {
|
|||
ProcessRead(TrustedFileReader, GenerationId),
|
||||
ProcessReadData(TrustedFileReader, GenerationId, DOMString),
|
||||
ProcessReadError(TrustedFileReader, GenerationId, DOMErrorName),
|
||||
ProcessReadEOF(TrustedFileReader, GenerationId, BlobBody)
|
||||
ProcessReadEOF(TrustedFileReader, GenerationId, ReadMetaData, Vec<u8>)
|
||||
}
|
||||
|
||||
impl Runnable for FileReaderEvent {
|
||||
|
@ -414,15 +394,15 @@ impl Runnable for FileReaderEvent {
|
|||
FileReaderEvent::ProcessReadError(filereader, gen_id, error) => {
|
||||
FileReader::process_read_error(filereader, gen_id, error);
|
||||
},
|
||||
FileReaderEvent::ProcessReadEOF(filereader, gen_id, blob_body) => {
|
||||
FileReader::process_read_eof(filereader, gen_id, blob_body);
|
||||
FileReaderEvent::ProcessReadEOF(filereader, gen_id, data, blob_contents) => {
|
||||
FileReader::process_read_eof(filereader, gen_id, data, blob_contents);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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>) {
|
||||
let chan = &script_chan;
|
||||
// Step 4
|
||||
|
@ -433,7 +413,7 @@ fn perform_annotated_read_operation(gen_id: GenerationId, read_data: ReadData,
|
|||
gen_id, DOMString::new());
|
||||
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
|
||||
|
||||
let bytes = match read_data.bytes.recv() {
|
||||
let bytes = match blob_contents.recv() {
|
||||
Ok(bytes) => bytes,
|
||||
Err(_) => {
|
||||
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 label = read_data.label.clone();
|
||||
let blobtype = data.blobtype.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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue