1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
7 #ifndef WIDGET_GTK_MPRIS_SERVICE_HANDLER_H_
8 #define WIDGET_GTK_MPRIS_SERVICE_HANDLER_H_
11 #include "mozilla/dom/FetchImageHelper.h"
12 #include "mozilla/dom/MediaControlKeySource.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/UniquePtr.h"
16 #include "nsMimeTypes.h"
19 #define DBUS_MPRIS_SERVICE_NAME "org.mpris.MediaPlayer2.firefox"
20 #define DBUS_MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
21 #define DBUS_MPRIS_INTERFACE "org.mpris.MediaPlayer2"
22 #define DBUS_MPRIS_PLAYER_INTERFACE "org.mpris.MediaPlayer2.Player"
23 #define DBUS_MPRIS_TRACK_PATH "/org/mpris/MediaPlayer2/firefox"
29 * This class implements the "MPRIS" D-Bus Service
30 * (https://specifications.freedesktop.org/mpris-spec/2.2),
31 * which is used to communicate with the Desktop Environment about the
32 * Multimedia playing in Gecko.
33 * Note that this interface requires many methods which may not be supported by
34 * Gecko, the interface
35 * however provides CanXYZ properties for these methods, so the method is
36 * defined but won't be executed.
38 * Also note that the following defines are for parts that the MPRIS Spec
39 * defines optional. The code won't
40 * compile with any of the defines set, yet, as those aren't implemented yet and
41 * probably never will be of
42 * use for gecko. For sake of completeness, they have been added until the
43 * decision about their implementation
46 * The constexpr'ed methods are capabilities of the user agent known at compile
47 * time, e.g. we decided at
48 * compile time whether we ever want to support closing the user agent via MPRIS
49 * (Quit() and CanQuit()).
51 * Other properties like CanPlay() might depend on the runtime state (is there
52 * media available for playback?)
53 * and thus aren't a constexpr but merely a const method.
55 class MPRISServiceHandler final
: public dom::MediaControlKeySource
{
56 NS_INLINE_DECL_REFCOUNTING(MPRISServiceHandler
, override
)
58 // Note that this constructor does NOT initialize the MPRIS Service but only
59 // this class. The method Open() is responsible for registering and MAY FAIL.
61 MPRISServiceHandler();
63 void Close() override
;
64 bool IsOpened() const override
;
66 // From the EventSource.
67 void SetPlaybackState(dom::MediaSessionPlaybackState aState
) override
;
69 // GetPlaybackState returns dom::PlaybackState. GetPlaybackStatus returns this
70 // state converted into d-bus variants.
71 GVariant
* GetPlaybackStatus() const;
73 const char* Identity() const;
74 const char* DesktopEntry() const;
75 bool PressKey(dom::MediaControlKey aKey
) const;
77 void SetMediaMetadata(const dom::MediaMetadataBase
& aMetadata
) override
;
78 GVariant
* GetMetadataAsGVariant() const;
80 void SetSupportedMediaKeys(const MediaKeysArray
& aSupportedKeys
) override
;
82 bool IsMediaKeySupported(dom::MediaControlKey aKey
) const;
85 ~MPRISServiceHandler();
87 // Note: Registration Ids for the D-Bus start with 1, so a value of 0
88 // indicates an error (or an object which wasn't initialized yet)
90 // a handle to our bus registration/ownership
92 // This is for the interface org.mpris.MediaPlayer2
93 guint mRootRegistrationId
= 0;
94 // This is for the interface org.mpris.MediaPlayer2.Player
95 guint mPlayerRegistrationId
= 0;
96 RefPtr
<GDBusNodeInfo
> mIntrospectionData
;
97 GDBusConnection
* mConnection
= nullptr;
98 bool mInitialized
= false;
99 nsAutoCString mIdentity
;
100 nsAutoCString mDesktopEntry
;
102 // The image format used in MPRIS is based on the mMimeType here. Although
103 // IMAGE_JPEG or IMAGE_BMP are valid types as well but a png image with
104 // transparent background will be converted into a jpeg/bmp file with a
105 // colored background IMAGE_PNG format seems to be the best choice for now.
106 nsCString mMimeType
{IMAGE_PNG
};
108 // A bitmask indicating what keys are enabled
109 uint32_t mSupportedKeys
= 0;
111 class MPRISMetadata
: public dom::MediaMetadataBase
{
113 MPRISMetadata() = default;
114 ~MPRISMetadata() = default;
116 void UpdateFromMetadataBase(const dom::MediaMetadataBase
& aMetadata
) {
117 mTitle
= aMetadata
.mTitle
;
118 mArtist
= aMetadata
.mArtist
;
119 mAlbum
= aMetadata
.mAlbum
;
120 mArtwork
= aMetadata
.mArtwork
;
123 UpdateFromMetadataBase(MediaMetadataBase::EmptyData());
129 MPRISMetadata mMPRISMetadata
;
131 // The saved image file fetched from the URL
132 nsCOMPtr
<nsIFile
> mLocalImageFile
;
133 nsCOMPtr
<nsIFile
> mLocalImageFolder
;
135 UniquePtr
<dom::FetchImageHelper
> mImageFetcher
;
136 MozPromiseRequestHolder
<dom::ImagePromise
> mImageFetchRequest
;
138 nsString mFetchingUrl
;
139 nsString mCurrentImageUrl
;
141 size_t mNextImageIndex
= 0;
143 // Load the image at index aIndex of the metadta's artwork to MPRIS
145 void LoadImageAtIndex(const size_t aIndex
);
146 bool SetImageToDisplay(const char* aImageData
, uint32_t aDataSize
);
148 bool RenewLocalImageFile(const char* aImageData
, uint32_t aDataSize
);
149 bool InitLocalImageFile();
150 bool InitLocalImageFolder();
151 void RemoveAllLocalImages();
152 bool LocalImageFolderExists();
154 // Queries nsAppInfo to get the branded browser name and vendor
157 // non-public API, called from events
158 void OnNameAcquired(GDBusConnection
* aConnection
, const gchar
* aName
);
159 void OnNameLost(GDBusConnection
* aConnection
, const gchar
* aName
);
160 void OnBusAcquired(GDBusConnection
* aConnection
, const gchar
* aName
);
162 static void OnNameAcquiredStatic(GDBusConnection
* aConnection
,
163 const gchar
* aName
, gpointer aUserData
);
164 static void OnNameLostStatic(GDBusConnection
* aConnection
, const gchar
* aName
,
166 static void OnBusAcquiredStatic(GDBusConnection
* aConnection
,
167 const gchar
* aName
, gpointer aUserData
);
169 void EmitEvent(dom::MediaControlKey aKey
) const;
171 bool EmitMetadataChanged() const;
173 void SetMediaMetadataInternal(const dom::MediaMetadataBase
& aMetadata
,
174 bool aClearArtUrl
= true);
176 bool EmitSupportedKeyChanged(dom::MediaControlKey aKey
,
177 bool aSupported
) const;
179 bool EmitPropertiesChangedSignal(GVariant
* aParameters
) const;
181 void ClearMetadata();
184 } // namespace widget
185 } // namespace mozilla
187 #endif // WIDGET_GTK_MPRIS_SERVICE_HANDLER_H_