mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Auto merge of #12448 - jdm:file-reading-task-source-2, r=KiChjang
Implement file reading task source Implement the task source API for the File Reader task source, enabling using task sources from non-main threads. --- - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix (partially) #7959 (github issue number if applicable). - [X] These changes do not require tests because they're refactoring existing code <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12448) <!-- Reviewable:end -->
This commit is contained in:
commit
48a912f57e
14 changed files with 155 additions and 84 deletions
|
@ -23,9 +23,10 @@ use net_traits::filemanager_thread::FileManagerThreadMsg;
|
|||
use net_traits::{ResourceThreads, CoreResourceThread, RequestSource, IpcSend};
|
||||
use profile_traits::{mem, time};
|
||||
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort};
|
||||
use script_thread::{MainThreadScriptChan, ScriptThread};
|
||||
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;
|
||||
|
||||
|
@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -297,6 +298,15 @@ impl<'a> GlobalRef<'a> {
|
|||
GlobalRef::Worker(ref worker) => worker.panic_chan(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a wrapper for runnables to ensure they are cancelled if the global
|
||||
/// is being destroyed.
|
||||
pub fn get_runnable_wrapper(&self) -> RunnableWrapper {
|
||||
match *self {
|
||||
GlobalRef::Window(ref window) => window.get_runnable_wrapper(),
|
||||
GlobalRef::Worker(ref worker) => worker.get_runnable_wrapper(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GlobalRoot {
|
||||
|
|
|
@ -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)]
|
||||
|
@ -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(())
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
use dom::attr::Attr;
|
||||
use dom::bindings::codegen::Bindings::HTMLDetailsElementBinding;
|
||||
use dom::bindings::codegen::Bindings::HTMLDetailsElementBinding::HTMLDetailsElementMethods;
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::bindings::js::Root;
|
||||
use dom::bindings::refcounted::Trusted;
|
||||
|
@ -78,7 +79,7 @@ impl VirtualMethods for HTMLDetailsElement {
|
|||
element: details,
|
||||
toggle_number: counter
|
||||
};
|
||||
let _ = task_source.queue(runnable, window.r());
|
||||
let _ = task_source.queue(runnable, GlobalRef::Window(&window));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ use dom::bindings::codegen::Bindings::HTMLFormElementBinding::HTMLFormElementMet
|
|||
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
|
||||
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
|
||||
use dom::bindings::conversions::DerivedFrom;
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
|
||||
use dom::bindings::js::{JS, MutNullableHeap, Root};
|
||||
use dom::bindings::refcounted::Trusted;
|
||||
|
@ -484,7 +485,7 @@ impl HTMLFormElement {
|
|||
};
|
||||
|
||||
// Step 3
|
||||
window.dom_manipulation_task_source().queue(nav, window).unwrap();
|
||||
window.dom_manipulation_task_source().queue(nav, GlobalRef::Window(window)).unwrap();
|
||||
}
|
||||
|
||||
/// Interactively validate the constraints of form elements
|
||||
|
|
|
@ -184,7 +184,7 @@ impl HTMLImageElement {
|
|||
src: src.into(),
|
||||
};
|
||||
let task = window.dom_manipulation_task_source();
|
||||
let _ = task.queue(runnable, window);
|
||||
let _ = task.queue(runnable, GlobalRef::Window(window));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -241,7 +241,7 @@ impl HTMLMediaElement {
|
|||
elem: Trusted::new(self),
|
||||
};
|
||||
let win = window_from_node(self);
|
||||
let _ = win.dom_manipulation_task_source().queue(task, win.r());
|
||||
let _ = win.dom_manipulation_task_source().queue(task, GlobalRef::Window(&win));
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#internal-pause-steps step 2.2
|
||||
|
@ -265,13 +265,13 @@ impl HTMLMediaElement {
|
|||
elem: Trusted::new(self),
|
||||
};
|
||||
let win = window_from_node(self);
|
||||
let _ = win.dom_manipulation_task_source().queue(task, win.r());
|
||||
let _ = win.dom_manipulation_task_source().queue(task, GlobalRef::Window(&win));
|
||||
}
|
||||
|
||||
fn queue_fire_simple_event(&self, type_: &'static str) {
|
||||
let win = window_from_node(self);
|
||||
let task = box FireSimpleEventTask::new(self, type_);
|
||||
let _ = win.dom_manipulation_task_source().queue(task, win.r());
|
||||
let _ = win.dom_manipulation_task_source().queue(task, GlobalRef::Window(&win));
|
||||
}
|
||||
|
||||
fn fire_simple_event(&self, type_: &str) {
|
||||
|
@ -498,7 +498,8 @@ impl HTMLMediaElement {
|
|||
|
||||
fn queue_dedicated_media_source_failure_steps(&self) {
|
||||
let window = window_from_node(self);
|
||||
let _ = window.dom_manipulation_task_source().queue(box DedicatedMediaSourceFailureTask::new(self), window.r());
|
||||
let _ = window.dom_manipulation_task_source().queue(box DedicatedMediaSourceFailureTask::new(self),
|
||||
GlobalRef::Window(&window));
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dedicated-media-source-failure-steps
|
||||
|
|
|
@ -161,7 +161,8 @@ impl Storage {
|
|||
let window = global_ref.as_window();
|
||||
let task_source = window.dom_manipulation_task_source();
|
||||
let trusted_storage = Trusted::new(self);
|
||||
task_source.queue(box StorageEventRunnable::new(trusted_storage, key, old_value, new_value), window).unwrap();
|
||||
task_source.queue(box StorageEventRunnable::new(trusted_storage, key, old_value, new_value),
|
||||
global_ref).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@ use net_traits::{LoadContext, ResourceThreads, load_whole_resource};
|
|||
use net_traits::{RequestSource, LoadOrigin, CustomResponseSender, IpcSend};
|
||||
use profile_traits::{mem, time};
|
||||
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort, maybe_take_panic_result};
|
||||
use script_thread::RunnableWrapper;
|
||||
use script_traits::ScriptMsg as ConstellationMsg;
|
||||
use script_traits::{MsDuration, TimerEvent, TimerEventId, TimerEventRequest, TimerSource};
|
||||
use std::cell::Cell;
|
||||
|
@ -38,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;
|
||||
|
||||
|
@ -268,6 +270,12 @@ impl WorkerGlobalScope {
|
|||
pub fn panic_chan(&self) -> &IpcSender<PanicMsg> {
|
||||
&self.panic_chan
|
||||
}
|
||||
|
||||
pub fn get_runnable_wrapper(&self) -> RunnableWrapper {
|
||||
RunnableWrapper {
|
||||
cancelled: self.closing.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LoadOrigin for WorkerGlobalScope {
|
||||
|
@ -453,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>();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue