Skip to content

Commit

Permalink
Add task source for media element
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Oct 10, 2018
1 parent 7a88a2e commit 7b3cf27
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
25 changes: 11 additions & 14 deletions components/script/dom/htmlmediaelement.rs
Expand Up @@ -278,7 +278,8 @@ impl HTMLMediaElement {
let state = self.ready_state.get();

let window = window_from_node(self);
let task_source = window.dom_manipulation_task_source();
// FIXME(nox): Why are errors silenced here?
let task_source = window.media_element_task_source();
if self.Paused() {
// Step 6.1.
self.paused.set(false);
Expand Down Expand Up @@ -356,9 +357,7 @@ impl HTMLMediaElement {
let window = window_from_node(self);
let this = Trusted::new(self);
let generation_id = self.generation_id.get();
// FIXME(nox): Why are errors silenced here?
// FIXME(nox): Media element event task source should be used here.
let _ = window.dom_manipulation_task_source().queue(
let _ = window.media_element_task_source().queue(
task!(internal_pause_steps: move || {
let this = this.root();
if generation_id != this.generation_id.get() {
Expand Down Expand Up @@ -400,8 +399,7 @@ impl HTMLMediaElement {
let this = Trusted::new(self);
let generation_id = self.generation_id.get();
// FIXME(nox): Why are errors silenced here?
// FIXME(nox): Media element event task source should be used here.
let _ = window.dom_manipulation_task_source().queue(
let _ = window.media_element_task_source().queue(
task!(notify_about_playing: move || {
let this = this.root();
if generation_id != this.generation_id.get() {
Expand Down Expand Up @@ -435,7 +433,7 @@ impl HTMLMediaElement {
}

let window = window_from_node(self);
let task_source = window.dom_manipulation_task_source();
let task_source = window.media_element_task_source();

// Step 1.
match (old_ready_state, ready_state) {
Expand Down Expand Up @@ -590,7 +588,7 @@ impl HTMLMediaElement {

// Step 8.
let window = window_from_node(self);
window.dom_manipulation_task_source().queue_simple_event(
window.media_element_task_source().queue_simple_event(
self.upcast(),
atom!("loadstart"),
&window,
Expand Down Expand Up @@ -667,7 +665,7 @@ impl HTMLMediaElement {

// Step 4.remote.1.2.
let window = window_from_node(self);
window.dom_manipulation_task_source().queue_simple_event(
window.media_element_task_source().queue_simple_event(
self.upcast(),
atom!("suspend"),
&window,
Expand All @@ -676,7 +674,7 @@ impl HTMLMediaElement {
// Step 4.remote.1.3.
let this = Trusted::new(self);
window
.dom_manipulation_task_source()
.media_element_task_source()
.queue(
task!(set_media_delay_load_event_flag_to_false: move || {
this.root().delay_load_event(false);
Expand Down Expand Up @@ -755,8 +753,7 @@ impl HTMLMediaElement {
let generation_id = self.generation_id.get();
self.take_pending_play_promises(Err(Error::NotSupported));
// FIXME(nox): Why are errors silenced here?
// FIXME(nox): Media element event task source should be used here.
let _ = window.dom_manipulation_task_source().queue(
let _ = window.media_element_task_source().queue(
task!(dedicated_media_source_failure_steps: move || {
let this = this.root();
if generation_id != this.generation_id.get() {
Expand Down Expand Up @@ -813,7 +810,7 @@ impl HTMLMediaElement {
}

let window = window_from_node(self);
let task_source = window.dom_manipulation_task_source();
let task_source = window.media_element_task_source();

// Step 5.
let network_state = self.network_state.get();
Expand Down Expand Up @@ -1291,7 +1288,7 @@ impl FetchResponseListener for HTMLMediaElementContext {
// => "If mode is remote" step 2
if time::get_time() > self.next_progress_event {
let window = window_from_node(&*elem);
window.dom_manipulation_task_source().queue_simple_event(
window.media_element_task_source().queue_simple_event(
elem.upcast(),
atom!("progress"),
&window,
Expand Down
9 changes: 9 additions & 0 deletions components/script/dom/window.rs
Expand Up @@ -123,6 +123,7 @@ use task_source::TaskSourceName;
use task_source::dom_manipulation::DOMManipulationTaskSource;
use task_source::file_reading::FileReadingTaskSource;
use task_source::history_traversal::HistoryTraversalTaskSource;
use task_source::media_element::MediaElementTaskSource;
use task_source::networking::NetworkingTaskSource;
use task_source::performance_timeline::PerformanceTimelineTaskSource;
use task_source::remote_event::RemoteEventTaskSource;
Expand Down Expand Up @@ -175,6 +176,8 @@ pub struct Window {
#[ignore_malloc_size_of = "task sources are hard"]
dom_manipulation_task_source: DOMManipulationTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
media_element_task_source: MediaElementTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
user_interaction_task_source: UserInteractionTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
networking_task_source: NetworkingTaskSource,
Expand Down Expand Up @@ -359,6 +362,10 @@ impl Window {
self.dom_manipulation_task_source.clone()
}

pub fn media_element_task_source(&self) -> MediaElementTaskSource {
self.media_element_task_source.clone()
}

pub fn user_interaction_task_source(&self) -> UserInteractionTaskSource {
self.user_interaction_task_source.clone()
}
Expand Down Expand Up @@ -2061,6 +2068,7 @@ impl Window {
runtime: Rc<Runtime>,
script_chan: MainThreadScriptChan,
dom_manipulation_task_source: DOMManipulationTaskSource,
media_element_task_source: MediaElementTaskSource,
user_interaction_task_source: UserInteractionTaskSource,
networking_task_source: NetworkingTaskSource,
history_traversal_task_source: HistoryTraversalTaskSource,
Expand Down Expand Up @@ -2116,6 +2124,7 @@ impl Window {
),
script_chan,
dom_manipulation_task_source,
media_element_task_source,
user_interaction_task_source,
networking_task_source,
history_traversal_task_source,
Expand Down
9 changes: 9 additions & 0 deletions components/script/script_thread.rs
Expand Up @@ -121,6 +121,7 @@ use task_source::TaskSourceName;
use task_source::dom_manipulation::DOMManipulationTaskSource;
use task_source::file_reading::FileReadingTaskSource;
use task_source::history_traversal::HistoryTraversalTaskSource;
use task_source::media_element::MediaElementTaskSource;
use task_source::networking::NetworkingTaskSource;
use task_source::performance_timeline::PerformanceTimelineTaskSource;
use task_source::remote_event::RemoteEventTaskSource;
Expand Down Expand Up @@ -502,6 +503,8 @@ pub struct ScriptThread {

dom_manipulation_task_sender: Sender<MainThreadScriptMsg>,

media_element_task_sender: Sender<MainThreadScriptMsg>,

user_interaction_task_sender: Sender<MainThreadScriptMsg>,

networking_task_sender: Box<ScriptChan>,
Expand Down Expand Up @@ -1018,6 +1021,7 @@ impl ScriptThread {

chan: MainThreadScriptChan(chan.clone()),
dom_manipulation_task_sender: chan.clone(),
media_element_task_sender: chan.clone(),
user_interaction_task_sender: chan.clone(),
networking_task_sender: boxed_script_sender.clone(),
file_reading_task_sender: boxed_script_sender.clone(),
Expand Down Expand Up @@ -2151,6 +2155,10 @@ impl ScriptThread {
DOMManipulationTaskSource(self.dom_manipulation_task_sender.clone(), pipeline_id)
}

pub fn media_element_task_source(&self, pipeline_id: PipelineId) -> MediaElementTaskSource {
MediaElementTaskSource(self.media_element_task_sender.clone(), pipeline_id)
}

pub fn performance_timeline_task_source(
&self,
pipeline_id: PipelineId,
Expand Down Expand Up @@ -2558,6 +2566,7 @@ impl ScriptThread {
self.js_runtime.clone(),
MainThreadScriptChan(sender.clone()),
self.dom_manipulation_task_source(incomplete.pipeline_id),
self.media_element_task_source(incomplete.pipeline_id),
self.user_interaction_task_source(incomplete.pipeline_id),
self.networking_task_source(incomplete.pipeline_id),
HistoryTraversalTaskSource(history_sender.clone()),
Expand Down
51 changes: 51 additions & 0 deletions components/script/task_source/media_element.rs
@@ -0,0 +1,51 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 dom::bindings::inheritance::Castable;
use dom::bindings::refcounted::Trusted;
use dom::event::SimpleEventTask;
use dom::eventtarget::EventTarget;
use dom::window::Window;
use msg::constellation_msg::PipelineId;
use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory};
use script_thread::MainThreadScriptMsg;
use servo_atoms::Atom;
use servo_channel::Sender;
use std::fmt;
use std::result::Result;
use task::{TaskCanceller, TaskOnce};
use task_source::{TaskSource, TaskSourceName};

#[derive(Clone, JSTraceable)]
pub struct MediaElementTaskSource(pub Sender<MainThreadScriptMsg>, pub PipelineId);

impl fmt::Debug for MediaElementTaskSource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MediaElementTaskSource(...)")
}
}

impl TaskSource for MediaElementTaskSource {
const NAME: TaskSourceName = TaskSourceName::MediaElement;

fn queue_with_canceller<T>(&self, task: T, canceller: &TaskCanceller) -> Result<(), ()>
where
T: TaskOnce + 'static,
{
let msg = MainThreadScriptMsg::Common(CommonScriptMsg::Task(
ScriptThreadEventCategory::ScriptEvent,
Box::new(canceller.wrap_task(task)),
Some(self.1),
MediaElementTaskSource::NAME,
));
self.0.send(msg).map_err(|_| ())
}
}

impl MediaElementTaskSource {
pub fn queue_simple_event(&self, target: &EventTarget, name: Atom, window: &Window) {
let target = Trusted::new(target);
let _ = self.queue(SimpleEventTask { target, name }, window.upcast());
}
}
2 changes: 2 additions & 0 deletions components/script/task_source/mod.rs
Expand Up @@ -5,6 +5,7 @@
pub mod dom_manipulation;
pub mod file_reading;
pub mod history_traversal;
pub mod media_element;
pub mod networking;
pub mod performance_timeline;
pub mod remote_event;
Expand All @@ -29,6 +30,7 @@ pub enum TaskSourceName {
PerformanceTimeline,
UserInteraction,
RemoteEvent,
MediaElement,
Websocket,
}

Expand Down

0 comments on commit 7b3cf27

Please sign in to comment.