Bug 1943650 - Command-line --help output misformatted after --dbus-service. r=emilio
[gecko.git] / dom / media / mediacontrol / MediaControlUtils.h
blobb0940269ef997b41d457fcf61409b9ec05afc201
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_
8 #define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_
10 #include "MediaController.h"
11 #include "imgIEncoder.h"
12 #include "imgITools.h"
13 #include "mozilla/Logging.h"
14 #include "mozilla/dom/ChromeUtilsBinding.h"
15 #include "mozilla/dom/MediaControllerBinding.h"
16 #include "nsReadableUtils.h"
17 #include "nsServiceManagerUtils.h"
19 extern mozilla::LazyLogModule gMediaControlLog;
21 namespace mozilla::dom {
23 inline const char* ToMediaControlKeyStr(const Maybe<MediaControlKey>& aKey) {
24 if (aKey.isNothing()) {
25 MOZ_ASSERT_UNREACHABLE("Invalid action.");
26 return "Unknown";
28 return GetEnumString(aKey.value()).get();
31 inline MediaControlKey ConvertMediaSessionActionToControlKey(
32 MediaSessionAction aAction) {
33 switch (aAction) {
34 case MediaSessionAction::Play:
35 return MediaControlKey::Play;
36 case MediaSessionAction::Pause:
37 return MediaControlKey::Pause;
38 case MediaSessionAction::Seekbackward:
39 return MediaControlKey::Seekbackward;
40 case MediaSessionAction::Seekforward:
41 return MediaControlKey::Seekforward;
42 case MediaSessionAction::Previoustrack:
43 return MediaControlKey::Previoustrack;
44 case MediaSessionAction::Nexttrack:
45 return MediaControlKey::Nexttrack;
46 case MediaSessionAction::Skipad:
47 return MediaControlKey::Skipad;
48 case MediaSessionAction::Seekto:
49 return MediaControlKey::Seekto;
50 default:
51 MOZ_ASSERT(aAction == MediaSessionAction::Stop);
52 return MediaControlKey::Stop;
56 inline const char* ToMediaAudibleStateStr(MediaAudibleState aState) {
57 switch (aState) {
58 case MediaAudibleState::eInaudible:
59 return "inaudible";
60 case MediaAudibleState::eAudible:
61 return "audible";
62 default:
63 MOZ_ASSERT_UNREACHABLE("Invalid audible state.");
64 return "Unknown";
68 inline const char* ToMediaSessionPlaybackStateStr(
69 const MediaSessionPlaybackState& aState) {
70 switch (aState) {
71 case MediaSessionPlaybackState::None:
72 return "none";
73 case MediaSessionPlaybackState::Paused:
74 return "paused";
75 case MediaSessionPlaybackState::Playing:
76 return "playing";
77 default:
78 MOZ_ASSERT_UNREACHABLE("Invalid MediaSessionPlaybackState.");
79 return "Unknown";
83 BrowsingContext* GetAliveTopBrowsingContext(BrowsingContext* aBC);
85 inline bool IsImageIn(const nsTArray<MediaImage>& aArtwork,
86 const nsAString& aImageUrl) {
87 for (const MediaImage& image : aArtwork) {
88 if (image.mSrc == aImageUrl) {
89 return true;
92 return false;
95 // The image buffer would be allocated in aStream whose size is aSize and the
96 // buffer head is aBuffer
97 inline nsresult GetEncodedImageBuffer(imgIContainer* aImage,
98 const nsACString& aMimeType,
99 nsIInputStream** aStream, uint32_t* aSize,
100 char** aBuffer) {
101 MOZ_ASSERT(aImage);
103 nsCOMPtr<imgITools> imgTools = do_GetService("@mozilla.org/image/tools;1");
104 if (!imgTools) {
105 return NS_ERROR_FAILURE;
108 nsCOMPtr<nsIInputStream> inputStream;
109 nsresult rv = imgTools->EncodeImage(aImage, aMimeType, u""_ns,
110 getter_AddRefs(inputStream));
111 if (NS_FAILED(rv)) {
112 return rv;
115 if (!inputStream) {
116 return NS_ERROR_FAILURE;
119 nsCOMPtr<imgIEncoder> encoder = do_QueryInterface(inputStream);
120 if (!encoder) {
121 return NS_ERROR_FAILURE;
124 rv = encoder->GetImageBufferUsed(aSize);
125 if (NS_FAILED(rv)) {
126 return rv;
129 rv = encoder->GetImageBuffer(aBuffer);
130 if (NS_FAILED(rv)) {
131 return rv;
134 encoder.forget(aStream);
135 return NS_OK;
138 inline bool IsValidImageUrl(const nsAString& aUrl) {
139 return StringBeginsWith(aUrl, u"http://"_ns) ||
140 StringBeginsWith(aUrl, u"https://"_ns);
143 inline uint32_t GetMediaKeyMask(mozilla::dom::MediaControlKey aKey) {
144 return 1 << static_cast<uint8_t>(aKey);
147 } // namespace mozilla::dom
149 #endif // DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_