1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_MediaMetadata_h
8 #define mozilla_dom_MediaMetadata_h
9 
10 #include "js/TypeDecls.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/dom/BindingDeclarations.h"
13 #include "mozilla/dom/MediaSessionBinding.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsWrapperCache.h"
16 
17 class nsIGlobalObject;
18 
19 namespace mozilla {
20 class ErrorResult;
21 
22 namespace dom {
23 
24 class MediaMetadataBase {
25  public:
26   MediaMetadataBase() = default;
MediaMetadataBase(const nsString & aTitle,const nsString & aArtist,const nsString & aAlbum)27   MediaMetadataBase(const nsString& aTitle, const nsString& aArtist,
28                     const nsString& aAlbum)
29       : mTitle(aTitle), mArtist(aArtist), mAlbum(aAlbum) {}
30 
EmptyData()31   static MediaMetadataBase EmptyData() { return MediaMetadataBase(); }
32 
33   nsString mTitle;
34   nsString mArtist;
35   nsString mAlbum;
36   CopyableTArray<MediaImage> mArtwork;
37 };
38 
39 class MediaMetadata final : public nsISupports,
40                             public nsWrapperCache,
41                             private MediaMetadataBase {
42  public:
43   // Ref counting and cycle collection
44   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
45   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MediaMetadata)
46 
47   // WebIDL methods
48   nsIGlobalObject* GetParentObject() const;
49 
50   JSObject* WrapObject(JSContext* aCx,
51                        JS::Handle<JSObject*> aGivenProto) override;
52 
53   static already_AddRefed<MediaMetadata> Constructor(
54       const GlobalObject& aGlobal, const MediaMetadataInit& aInit,
55       ErrorResult& aRv);
56 
57   void GetTitle(nsString& aRetVal) const;
58 
59   void SetTitle(const nsAString& aTitle);
60 
61   void GetArtist(nsString& aRetVal) const;
62 
63   void SetArtist(const nsAString& aArtist);
64 
65   void GetAlbum(nsString& aRetVal) const;
66 
67   void SetAlbum(const nsAString& aAlbum);
68 
69   void GetArtwork(JSContext* aCx, nsTArray<JSObject*>& aRetVal,
70                   ErrorResult& aRv) const;
71 
72   void SetArtwork(JSContext* aCx, const Sequence<JSObject*>& aArtwork,
73                   ErrorResult& aRv);
74 
75   // This would expose MediaMetadataBase's members as public, so use this method
76   // carefully. Now we only use this when we want to update the metadata to the
77   // media session controller in the chrome process.
AsMetadataBase()78   MediaMetadataBase* AsMetadataBase() { return this; }
79 
80  private:
81   MediaMetadata(nsIGlobalObject* aParent, const nsString& aTitle,
82                 const nsString& aArtist, const nsString& aAlbum);
83 
84   ~MediaMetadata() = default;
85 
86   // Perform `convert artwork algorithm`. Set `mArtwork` to a converted
87   // `aArtwork` if the conversion works, otherwise throw a type error in `aRv`.
88   void SetArtworkInternal(const Sequence<MediaImage>& aArtwork,
89                           ErrorResult& aRv);
90 
91   nsCOMPtr<nsIGlobalObject> mParent;
92 };
93 
94 }  // namespace dom
95 }  // namespace mozilla
96 
97 #endif  // mozilla_dom_MediaMetadata_h
98