Skip to main content

script/dom/media/
mediametadata.rs

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 https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::cell::DomRefCell;
9use script_bindings::domstring::TracedDOMString;
10use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto};
11
12use crate::dom::bindings::codegen::Bindings::MediaMetadataBinding::{
13    MediaMetadataInit, MediaMetadataMethods,
14};
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::root::{DomRoot, MutNullableDom};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::media::mediasession::MediaSession;
19use crate::dom::window::Window;
20
21#[dom_struct]
22pub(crate) struct MediaMetadata {
23    reflector_: Reflector,
24    session: MutNullableDom<MediaSession>,
25    title: DomRefCell<TracedDOMString>,
26    artist: DomRefCell<TracedDOMString>,
27    album: DomRefCell<TracedDOMString>,
28}
29
30impl MediaMetadata {
31    fn new_inherited(init: &MediaMetadataInit) -> MediaMetadata {
32        MediaMetadata {
33            reflector_: Reflector::new(),
34            session: Default::default(),
35            title: DomRefCell::new(init.title.clone().traced()),
36            artist: DomRefCell::new(init.artist.clone().traced()),
37            album: DomRefCell::new(init.album.clone().traced()),
38        }
39    }
40
41    pub(crate) fn new(
42        cx: &mut JSContext,
43        global: &Window,
44        init: &MediaMetadataInit,
45    ) -> DomRoot<MediaMetadata> {
46        Self::new_with_proto(cx, global, None, init)
47    }
48
49    fn new_with_proto(
50        cx: &mut JSContext,
51        global: &Window,
52        proto: Option<HandleObject>,
53        init: &MediaMetadataInit,
54    ) -> DomRoot<MediaMetadata> {
55        reflect_dom_object_with_proto(
56            cx,
57            Box::new(MediaMetadata::new_inherited(init)),
58            global,
59            proto,
60        )
61    }
62
63    fn queue_update_metadata_algorithm(&self) {
64        if self.session.get().is_none() {}
65    }
66
67    pub(crate) fn set_session(&self, session: &MediaSession) {
68        self.session.set(Some(session));
69    }
70}
71
72impl MediaMetadataMethods<crate::DomTypeHolder> for MediaMetadata {
73    /// <https://w3c.github.io/mediasession/#dom-mediametadata-mediametadata>
74    fn Constructor(
75        cx: &mut JSContext,
76        window: &Window,
77        proto: Option<HandleObject>,
78        init: &MediaMetadataInit,
79    ) -> Fallible<DomRoot<MediaMetadata>> {
80        Ok(MediaMetadata::new_with_proto(cx, window, proto, init))
81    }
82
83    /// <https://w3c.github.io/mediasession/#dom-mediametadata-title>
84    fn Title(&self) -> DOMString {
85        self.title.borrow().clone().root()
86    }
87
88    /// <https://w3c.github.io/mediasession/#dom-mediametadata-title>
89    fn SetTitle(&self, value: DOMString) {
90        *self.title.borrow_mut() = value.traced();
91        self.queue_update_metadata_algorithm();
92    }
93
94    /// <https://w3c.github.io/mediasession/#dom-mediametadata-artist>
95    fn Artist(&self) -> DOMString {
96        self.artist.borrow().clone().root()
97    }
98
99    /// <https://w3c.github.io/mediasession/#dom-mediametadata-artist>
100    fn SetArtist(&self, value: DOMString) {
101        *self.artist.borrow_mut() = value.traced();
102        self.queue_update_metadata_algorithm();
103    }
104
105    /// <https://w3c.github.io/mediasession/#dom-mediametadata-album>
106    fn Album(&self) -> DOMString {
107        self.album.borrow().clone().root()
108    }
109
110    /// <https://w3c.github.io/mediasession/#dom-mediametadata-album>
111    fn SetAlbum(&self, value: DOMString) {
112        *self.album.borrow_mut() = value.traced();
113        self.queue_update_metadata_algorithm();
114    }
115}