1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 use dom::domexception::DOMErrorName; 6 use dom::filereader::{FileReader, TrustedFileReader, GenerationId, ReadMetaData}; 7 use msg::constellation_msg::PipelineId; 8 use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory, ScriptChan}; 9 use std::sync::Arc; 10 use task::{TaskCanceller, TaskOnce}; 11 use task_source::TaskSource; 12 13 #[derive(JSTraceable)] 14 pub struct FileReadingTaskSource(pub Box<ScriptChan + Send + 'static>, pub PipelineId); 15 16 impl Clone for FileReadingTaskSource { clone(&self) -> FileReadingTaskSource17 fn clone(&self) -> FileReadingTaskSource { 18 FileReadingTaskSource(self.0.clone(), self.1.clone()) 19 } 20 } 21 22 impl TaskSource for FileReadingTaskSource { queue_with_canceller<T>( &self, task: T, canceller: &TaskCanceller, ) -> Result<(), ()> where T: TaskOnce + 'static,23 fn queue_with_canceller<T>( 24 &self, 25 task: T, 26 canceller: &TaskCanceller, 27 ) -> Result<(), ()> 28 where 29 T: TaskOnce + 'static, 30 { 31 self.0.send(CommonScriptMsg::Task( 32 ScriptThreadEventCategory::FileRead, 33 Box::new(canceller.wrap_task(task)), 34 Some(self.1), 35 )) 36 } 37 } 38 39 impl TaskOnce for FileReadingTask { run_once(self)40 fn run_once(self) { 41 self.handle_task(); 42 } 43 } 44 45 #[allow(dead_code)] 46 pub enum FileReadingTask { 47 ProcessRead(TrustedFileReader, GenerationId), 48 ProcessReadData(TrustedFileReader, GenerationId), 49 ProcessReadError(TrustedFileReader, GenerationId, DOMErrorName), 50 ProcessReadEOF(TrustedFileReader, GenerationId, ReadMetaData, Arc<Vec<u8>>), 51 } 52 53 impl FileReadingTask { handle_task(self)54 pub fn handle_task(self) { 55 use self::FileReadingTask::*; 56 57 match self { 58 ProcessRead(reader, gen_id) => 59 FileReader::process_read(reader, gen_id), 60 ProcessReadData(reader, gen_id) => 61 FileReader::process_read_data(reader, gen_id), 62 ProcessReadError(reader, gen_id, error) => 63 FileReader::process_read_error(reader, gen_id, error), 64 ProcessReadEOF(reader, gen_id, metadata, blob_contents) => 65 FileReader::process_read_eof(reader, gen_id, metadata, blob_contents), 66 } 67 } 68 } 69