mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
MediaSession registration
This commit is contained in:
parent
1ab65005ae
commit
f8246801ba
2 changed files with 53 additions and 7 deletions
|
@ -8,11 +8,13 @@ use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionAc
|
|||
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionActionHandler;
|
||||
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionPlaybackState;
|
||||
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
|
||||
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
|
||||
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
|
||||
use crate::dom::mediametadata::MediaMetadata;
|
||||
use crate::dom::window::Window;
|
||||
use crate::script_thread::ScriptThread;
|
||||
use dom_struct::dom_struct;
|
||||
use msg::constellation_msg::PipelineId;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[dom_struct]
|
||||
|
@ -25,18 +27,24 @@ pub struct MediaSession {
|
|||
}
|
||||
|
||||
impl MediaSession {
|
||||
fn new_inherited() -> MediaSession {
|
||||
MediaSession {
|
||||
#[allow(unrooted_must_root)]
|
||||
fn new_inherited(pipeline_id: PipelineId) -> MediaSession {
|
||||
let media_session = MediaSession {
|
||||
reflector_: Reflector::new(),
|
||||
metadata: Default::default(),
|
||||
playback_state: DomRefCell::new(MediaSessionPlaybackState::None),
|
||||
}
|
||||
};
|
||||
ScriptThread::register_media_session(&media_session, pipeline_id);
|
||||
media_session
|
||||
}
|
||||
|
||||
pub fn new(global: &Window) -> DomRoot<MediaSession> {
|
||||
pub fn new(window: &Window) -> DomRoot<MediaSession> {
|
||||
let pipeline_id = window
|
||||
.pipeline_id()
|
||||
.expect("Cannot create MediaSession without a PipelineId");
|
||||
reflect_dom_object(
|
||||
Box::new(MediaSession::new_inherited()),
|
||||
global,
|
||||
Box::new(MediaSession::new_inherited(pipeline_id)),
|
||||
window,
|
||||
MediaSessionBinding::Wrap,
|
||||
)
|
||||
}
|
||||
|
@ -71,3 +79,14 @@ impl MediaSessionMethods for MediaSession {
|
|||
) {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for MediaSession {
|
||||
fn drop(&mut self) {
|
||||
let global = self.global();
|
||||
let pipeline_id = global
|
||||
.as_window()
|
||||
.pipeline_id()
|
||||
.expect("No PipelineId while dropping MediaSession");
|
||||
ScriptThread::remove_media_session(pipeline_id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,6 +51,7 @@ use crate::dom::event::{Event, EventBubbles, EventCancelable};
|
|||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::dom::htmlanchorelement::HTMLAnchorElement;
|
||||
use crate::dom::htmliframeelement::{HTMLIFrameElement, NavigationType};
|
||||
use crate::dom::mediasession::MediaSession;
|
||||
use crate::dom::mutationobserver::MutationObserver;
|
||||
use crate::dom::node::{
|
||||
from_untrusted_node_address, window_from_node, Node, NodeDamage, ShadowIncluding,
|
||||
|
@ -696,6 +697,11 @@ pub struct ScriptThread {
|
|||
|
||||
/// Code is running as a consequence of a user interaction
|
||||
is_user_interacting: Cell<bool>,
|
||||
|
||||
/// The MediaSessions registered for this Pipeline, if any.
|
||||
/// There can only be one active MediaSession. The constellation
|
||||
/// has the PipelineId of the active MediaSession, if any.
|
||||
media_sessions: DomRefCell<HashMap<PipelineId, Dom<MediaSession>>>,
|
||||
}
|
||||
|
||||
/// In the event of thread panic, all data on the stack runs its destructor. However, there
|
||||
|
@ -1365,6 +1371,7 @@ impl ScriptThread {
|
|||
|
||||
node_ids: Default::default(),
|
||||
is_user_interacting: Cell::new(false),
|
||||
media_sessions: DomRefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3948,6 +3955,26 @@ impl ScriptThread {
|
|||
globals,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn register_media_session(media_session: &MediaSession, pipeline_id: PipelineId) {
|
||||
SCRIPT_THREAD_ROOT.with(|root| {
|
||||
let script_thread = unsafe { &*root.get().unwrap() };
|
||||
script_thread
|
||||
.media_sessions
|
||||
.borrow_mut()
|
||||
.insert(pipeline_id, Dom::from_ref(media_session));
|
||||
})
|
||||
}
|
||||
|
||||
pub fn remove_media_session(pipeline_id: PipelineId) {
|
||||
SCRIPT_THREAD_ROOT.with(|root| {
|
||||
let script_thread = unsafe { &*root.get().unwrap() };
|
||||
script_thread
|
||||
.media_sessions
|
||||
.borrow_mut()
|
||||
.remove(&pipeline_id);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ScriptThread {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue