Skip to content

Commit

Permalink
Implement file reading task source
Browse files Browse the repository at this point in the history
And remove superfluous FileReaderEvent enum
  • Loading branch information
KiChjang authored and jdm committed Jul 14, 2016
1 parent 2aef518 commit aa5f34f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 60 deletions.
5 changes: 3 additions & 2 deletions components/script/dom/bindings/global.rs
Expand Up @@ -26,6 +26,7 @@ use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort};
use script_thread::{MainThreadScriptChan, ScriptThread, RunnableWrapper};
use script_traits::{MsDuration, ScriptMsg as ConstellationMsg, TimerEventRequest};
use task_source::dom_manipulation::DOMManipulationTaskSource;
use task_source::file_reading::FileReadingTaskSource;
use timers::{OneshotTimerCallback, OneshotTimerHandle};
use url::Url;

Expand Down Expand Up @@ -219,10 +220,10 @@ impl<'a> GlobalRef<'a> {

/// `ScriptChan` used to send messages to the event loop of this global's
/// thread.
pub fn file_reading_task_source(&self) -> Box<ScriptChan + Send> {
pub fn file_reading_task_source(&self) -> FileReadingTaskSource {
match *self {
GlobalRef::Window(ref window) => window.file_reading_task_source(),
GlobalRef::Worker(ref worker) => worker.script_chan(),
GlobalRef::Worker(ref worker) => worker.file_reading_task_source(),
}
}

Expand Down
62 changes: 18 additions & 44 deletions components/script/dom/filereader.rs
Expand Up @@ -23,12 +23,12 @@ use encoding::label::encoding_from_whatwg_label;
use encoding::types::{DecoderTrap, EncodingRef};
use hyper::mime::{Attr, Mime};
use rustc_serialize::base64::{CharacterSet, Config, Newline, ToBase64};
use script_runtime::ScriptThreadEventCategory::FileRead;
use script_runtime::{ScriptChan, CommonScriptMsg};
use script_thread::Runnable;
use script_thread::RunnableWrapper;
use std::cell::Cell;
use std::sync::Arc;
use string_cache::Atom;
use task_source::TaskSource;
use task_source::file_reading::{FileReadingTaskSource, FileReadingRunnable, FileReadingTask};
use util::thread::spawn_named;

#[derive(PartialEq, Clone, Copy, JSTraceable, HeapSizeOf)]
Expand Down Expand Up @@ -358,10 +358,11 @@ impl FileReader {
let fr = Trusted::new(self);
let gen_id = self.generation_id.get();

let script_chan = self.global().r().file_reading_task_source();
let wrapper = self.global().r().get_runnable_wrapper();
let task_source = self.global().r().file_reading_task_source();

spawn_named("file reader async operation".to_owned(), move || {
perform_annotated_read_operation(gen_id, load_data, blob_contents, fr, script_chan)
perform_annotated_read_operation(gen_id, load_data, blob_contents, fr, task_source, wrapper)
});
Ok(())
}
Expand All @@ -371,47 +372,20 @@ impl FileReader {
}
}

#[derive(Clone)]
pub enum FileReaderEvent {
ProcessRead(TrustedFileReader, GenerationId),
ProcessReadData(TrustedFileReader, GenerationId),
ProcessReadError(TrustedFileReader, GenerationId, DOMErrorName),
ProcessReadEOF(TrustedFileReader, GenerationId, ReadMetaData, Arc<Vec<u8>>)
}

impl Runnable for FileReaderEvent {
fn name(&self) -> &'static str { "FileReaderEvent" }

fn handler(self: Box<FileReaderEvent>) {
let file_reader_event = *self;
match file_reader_event {
FileReaderEvent::ProcessRead(filereader, gen_id) => {
FileReader::process_read(filereader, gen_id);
},
FileReaderEvent::ProcessReadData(filereader, gen_id) => {
FileReader::process_read_data(filereader, gen_id);
},
FileReaderEvent::ProcessReadError(filereader, gen_id, error) => {
FileReader::process_read_error(filereader, gen_id, error);
},
FileReaderEvent::ProcessReadEOF(filereader, gen_id, data, blob_contents) => {
FileReader::process_read_eof(filereader, gen_id, data, blob_contents);
}
}
}
}

// https://w3c.github.io/FileAPI/#thread-read-operation
fn perform_annotated_read_operation(gen_id: GenerationId, data: ReadMetaData, blob_contents: Arc<Vec<u8>>,
filereader: TrustedFileReader, script_chan: Box<ScriptChan + Send>) {
let chan = &script_chan;
fn perform_annotated_read_operation(gen_id: GenerationId,
data: ReadMetaData,
blob_contents: Arc<Vec<u8>>,
filereader: TrustedFileReader,
task_source: FileReadingTaskSource,
wrapper: RunnableWrapper) {
// Step 4
let thread = box FileReaderEvent::ProcessRead(filereader.clone(), gen_id);
chan.send(CommonScriptMsg::RunnableMsg(FileRead, thread)).unwrap();
let task = FileReadingRunnable::new(FileReadingTask::ProcessRead(filereader.clone(), gen_id));
task_source.queue_with_wrapper(task, &wrapper).unwrap();

let thread = box FileReaderEvent::ProcessReadData(filereader.clone(), gen_id);
chan.send(CommonScriptMsg::RunnableMsg(FileRead, thread)).unwrap();
let task = FileReadingRunnable::new(FileReadingTask::ProcessReadData(filereader.clone(), gen_id));
task_source.queue_with_wrapper(task, &wrapper).unwrap();

let thread = box FileReaderEvent::ProcessReadEOF(filereader, gen_id, data, blob_contents);
chan.send(CommonScriptMsg::RunnableMsg(FileRead, thread)).unwrap();
let task = FileReadingRunnable::new(FileReadingTask::ProcessReadEOF(filereader, gen_id, data, blob_contents));
task_source.queue_with_wrapper(task, &wrapper).unwrap();
}
2 changes: 1 addition & 1 deletion components/script/dom/window.rs
Expand Up @@ -302,7 +302,7 @@ impl Window {
self.history_traversal_task_source.clone()
}

pub fn file_reading_task_source(&self) -> Box<ScriptChan + Send> {
pub fn file_reading_task_source(&self) -> FileReadingTaskSource {
self.file_reading_task_source.clone()
}

Expand Down
5 changes: 5 additions & 0 deletions components/script/dom/workerglobalscope.rs
Expand Up @@ -39,6 +39,7 @@ use std::rc::Rc;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Receiver;
use task_source::file_reading::FileReadingTaskSource;
use timers::{IsInterval, OneshotTimerCallback, OneshotTimerHandle, OneshotTimers, TimerCallback};
use url::Url;

Expand Down Expand Up @@ -460,6 +461,10 @@ impl WorkerGlobalScope {
}
}

pub fn file_reading_task_source(&self) -> FileReadingTaskSource {
FileReadingTaskSource(self.script_chan())
}

pub fn pipeline(&self) -> PipelineId {
let dedicated = self.downcast::<DedicatedWorkerGlobalScope>();
let service_worker = self.downcast::<ServiceWorkerGlobalScope>();
Expand Down
9 changes: 5 additions & 4 deletions components/script/script_thread.rs
Expand Up @@ -575,6 +575,8 @@ impl ScriptThread {
// Ask the router to proxy IPC messages from the control port to us.
let control_port = ROUTER.route_ipc_receiver_to_new_mpsc_receiver(state.control_port);

let boxed_script_sender = MainThreadScriptChan(chan.clone()).clone();

ScriptThread {
browsing_context: MutNullableHeap::new(None),
incomplete_loads: DOMRefCell::new(vec!()),
Expand All @@ -595,8 +597,8 @@ impl ScriptThread {
dom_manipulation_task_source: DOMManipulationTaskSource(chan.clone()),
user_interaction_task_source: UserInteractionTaskSource(chan.clone()),
networking_task_source: NetworkingTaskSource(chan.clone()),
history_traversal_task_source: HistoryTraversalTaskSource(chan.clone()),
file_reading_task_source: FileReadingTaskSource(chan),
history_traversal_task_source: HistoryTraversalTaskSource(chan),
file_reading_task_source: FileReadingTaskSource(boxed_script_sender),

control_chan: state.control_chan,
control_port: control_port,
Expand Down Expand Up @@ -1585,7 +1587,6 @@ impl ScriptThread {
let UserInteractionTaskSource(ref user_sender) = self.user_interaction_task_source;
let NetworkingTaskSource(ref network_sender) = self.networking_task_source;
let HistoryTraversalTaskSource(ref history_sender) = self.history_traversal_task_source;
let FileReadingTaskSource(ref file_sender) = self.file_reading_task_source;

let (ipc_timer_event_chan, ipc_timer_event_port) = ipc::channel().unwrap();
ROUTER.route_ipc_receiver_to_mpsc_sender(ipc_timer_event_port,
Expand All @@ -1598,7 +1599,7 @@ impl ScriptThread {
UserInteractionTaskSource(user_sender.clone()),
NetworkingTaskSource(network_sender.clone()),
HistoryTraversalTaskSource(history_sender.clone()),
FileReadingTaskSource(file_sender.clone()),
self.file_reading_task_source.clone(),
self.image_cache_channel.clone(),
self.custom_message_chan.clone(),
self.image_cache_thread.clone(),
Expand Down
71 changes: 62 additions & 9 deletions components/script/task_source/file_reading.rs
Expand Up @@ -2,19 +2,72 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use script_runtime::{CommonScriptMsg, ScriptChan};
use script_thread::MainThreadScriptMsg;
use std::sync::mpsc::Sender;
use dom::domexception::DOMErrorName;
use dom::filereader::{FileReader, TrustedFileReader, GenerationId, ReadMetaData};
use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory, ScriptChan};
use script_thread::{Runnable, RunnableWrapper};
use std::sync::Arc;
use task_source::TaskSource;

#[derive(JSTraceable)]
pub struct FileReadingTaskSource(pub Sender<MainThreadScriptMsg>);
pub struct FileReadingTaskSource(pub Box<ScriptChan + Send + 'static>);

impl ScriptChan for FileReadingTaskSource {
fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> {
self.0.send(MainThreadScriptMsg::Common(msg)).map_err(|_| ())
impl Clone for FileReadingTaskSource {
fn clone(&self) -> FileReadingTaskSource {
FileReadingTaskSource(self.0.clone())
}
}

impl TaskSource for FileReadingTaskSource {
fn queue_with_wrapper<T>(&self,
msg: Box<T>,
wrapper: &RunnableWrapper)
-> Result<(), ()>
where T: Runnable + Send + 'static {
self.0.send(CommonScriptMsg::RunnableMsg(ScriptThreadEventCategory::FileRead,
wrapper.wrap_runnable(msg)))
}
}

pub struct FileReadingRunnable {
task: FileReadingTask,
}

impl FileReadingRunnable {
pub fn new(task: FileReadingTask) -> Box<FileReadingRunnable> {
box FileReadingRunnable {
task: task
}
}
}

impl Runnable for FileReadingRunnable {
fn handler(self: Box<FileReadingRunnable>) {
self.task.handle_task();
}
}

#[allow(dead_code)]
pub enum FileReadingTask {
ProcessRead(TrustedFileReader, GenerationId),
ProcessReadData(TrustedFileReader, GenerationId),
ProcessReadError(TrustedFileReader, GenerationId, DOMErrorName),
ProcessReadEOF(TrustedFileReader, GenerationId, ReadMetaData, Arc<Vec<u8>>),
}

impl FileReadingTask {
pub fn handle_task(self) {
use self::FileReadingTask::*;

fn clone(&self) -> Box<ScriptChan + Send> {
box FileReadingTaskSource((&self.0).clone())
match self {
ProcessRead(reader, gen_id) =>
FileReader::process_read(reader, gen_id),
ProcessReadData(reader, gen_id) =>
FileReader::process_read_data(reader, gen_id),
ProcessReadError(reader, gen_id, error) =>
FileReader::process_read_error(reader, gen_id, error),
ProcessReadEOF(reader, gen_id, metadata, blob_contents) =>
FileReader::process_read_eof(reader, gen_id, metadata, blob_contents),
}
}
}

0 comments on commit aa5f34f

Please sign in to comment.