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