Bug 1658075 - part1 : move testing functions to new namespace 'MediaControlService'. r=smaug,chunmin

Create a new namespace `MediaControlService` to use those helper functions which is used to get the main controller related status.

Then, move those functions from `ChromeUtils` to `MediaControlService`, which give us two benefit. The first is that we can remove redudant test-only enum `MediaSessionPlaybackTestState`, the second is a pref-required work for bug1656398, to fix the build order error when exposing `MediaSessionPlaybackState` in the media controller's webidl.

Differential Revision: https://phabricator.services.mozilla.com/D86620
This commit is contained in:
alwu 2020-08-14 01:32:10 +00:00
Родитель 324562e846
Коммит 923ecc4e6a
7 изменённых файлов: 62 добавлений и 105 удалений

Просмотреть файл

@ -29,8 +29,6 @@
#include "mozilla/dom/InProcessParent.h"
#include "mozilla/dom/InProcessChild.h"
#include "mozilla/dom/JSActorService.h"
#include "mozilla/dom/MediaControlUtils.h"
#include "mozilla/dom/MediaControlService.h"
#include "mozilla/dom/MediaMetadata.h"
#include "mozilla/dom/MediaSessionBinding.h"
#include "mozilla/dom/Performance.h"
@ -1278,15 +1276,6 @@ void ChromeUtils::PrivateNoteIntentionalCrash(const GlobalObject& aGlobal,
aError.Throw(NS_ERROR_NOT_IMPLEMENTED);
}
/* static */
void ChromeUtils::GenerateMediaControlKey(const GlobalObject& aGlobal,
MediaControlKey aKey) {
RefPtr<MediaControlService> service = MediaControlService::GetService();
if (service) {
service->GenerateTestMediaControlKey(aKey);
}
}
/* static */
nsIDOMProcessChild* ChromeUtils::GetDomProcessChild(const GlobalObject&) {
return nsIDOMProcessChild::GetSingleton();
@ -1311,44 +1300,5 @@ void ChromeUtils::GetAllDOMProcesses(
}
}
/* static */
void ChromeUtils::GetCurrentActiveMediaMetadata(const GlobalObject& aGlobal,
MediaMetadataInit& aMetadata) {
if (RefPtr<MediaControlService> service = MediaControlService::GetService()) {
MediaMetadataBase metadata = service->GetMainControllerMediaMetadata();
aMetadata.mTitle = metadata.mTitle;
aMetadata.mArtist = metadata.mArtist;
aMetadata.mAlbum = metadata.mAlbum;
for (const auto& artwork : metadata.mArtwork) {
// If OOM happens resulting in not able to append the element, then we
// would get incorrect result and fail on test, so we don't need to throw
// an error explicitly.
if (MediaImage* image = aMetadata.mArtwork.AppendElement(fallible)) {
image->mSrc = artwork.mSrc;
image->mSizes = artwork.mSizes;
image->mType = artwork.mType;
}
}
}
}
/* static */
MediaSessionPlaybackTestState ChromeUtils::GetCurrentMediaSessionPlaybackState(
GlobalObject& aGlobal) {
static_assert(int(MediaSessionPlaybackState::None) ==
int(MediaSessionPlaybackTestState::Stopped) &&
int(MediaSessionPlaybackState::Paused) ==
int(MediaSessionPlaybackTestState::Paused) &&
int(MediaSessionPlaybackState::Playing) ==
int(MediaSessionPlaybackTestState::Playing) &&
MediaSessionPlaybackStateValues::Count ==
MediaSessionPlaybackTestStateValues::Count);
if (RefPtr<MediaControlService> service = MediaControlService::GetService()) {
return ConvertToMediaSessionPlaybackTestState(
service->GetMainControllerPlaybackState());
}
return MediaSessionPlaybackTestState::Stopped;
}
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -216,22 +216,11 @@ class ChromeUtils {
static void PrivateNoteIntentionalCrash(const GlobalObject& aGlobal,
ErrorResult& aError);
static void GenerateMediaControlKey(const GlobalObject& aGlobal,
MediaControlKey aKey);
static nsIDOMProcessChild* GetDomProcessChild(const GlobalObject&);
static void GetAllDOMProcesses(
GlobalObject& aGlobal, nsTArray<RefPtr<nsIDOMProcessParent>>& aParents,
ErrorResult& aRv);
// This function would only be used for testing.
static void GetCurrentActiveMediaMetadata(const GlobalObject& aGlobal,
MediaMetadataInit& aMetadata);
// This function would only be used for testing.
static MediaSessionPlaybackTestState GetCurrentMediaSessionPlaybackState(
GlobalObject& aGlobal);
};
} // namespace dom

Просмотреть файл

@ -473,10 +473,6 @@ partial namespace ChromeUtils {
[ChromeOnly, Throws]
void privateNoteIntentionalCrash();
// This is used to generate fake media control keys event in testing.
[ChromeOnly]
void generateMediaControlKey(MediaControlKey aKey);
/**
* nsIDOMProcessChild for the current process.
*/
@ -491,16 +487,6 @@ partial namespace ChromeUtils {
*/
[Throws, ChromeOnly]
sequence<nsIDOMProcessParent> getAllDOMProcesses();
// This is used to get the media metadata from the current main controller in
// testing.
[ChromeOnly]
MediaMetadataInit getCurrentActiveMediaMetadata();
// This is used to get the actual media playback state from the current main
// controller in testing.
[ChromeOnly]
MediaSessionPlaybackTestState getCurrentMediaSessionPlaybackState();
};
/*
@ -742,20 +728,3 @@ enum PopupBlockerState {
"openAbused",
"openOverridden",
};
// Keep this in sync with MediaSessionPlaybackState in MediaSession.webidl!
// The reason we create an new enum MediaSessionPlaybackTestState is because of
// building issue. If we use MediaSessionPlaybackState directly, then the
// codegen would automatically add an header include of MediaSessionBinding.h
// in the ChromeUtilsBinding.h, which would cause the complier complaining about
// lots of undeclared identifier. In addition, there is an universal definition
// for 'None' on X11, and it conflicts with the 'None' in the enum of
// MediaSessionPlaybackState, which causes a build fail. As this state is
// defined in the ChromeUtilsBinding.h, which is automatically generated by the
// codegen, we can't include 'X11UndefineNone.h' to workaround this problem, so
// we have to change 'None' to 'Stopped'.
enum MediaSessionPlaybackTestState {
"stopped",
"paused",
"playing",
};

Просмотреть файл

@ -45,3 +45,17 @@ interface MediaController : EventTarget {
// eg. play(), pause().
void seekTo(double seekTime, optional boolean fastSeek = false);
};
[ChromeOnly,Exposed=Window,HeaderFile="mozilla/dom/MediaControlService.h"]
namespace MediaControlService {
// This is used to generate fake media control keys event in testing.
void generateMediaControlKey(MediaControlKey aKey);
// This is used to get the media metadata from the current main controller in
// testing.
MediaMetadataInit getCurrentActiveMediaMetadata();
// This is used to get the actual media playback state from the current main
// controller in testing.
MediaSessionPlaybackState getCurrentMediaSessionPlaybackState();
};

Просмотреть файл

@ -48,6 +48,46 @@ RefPtr<MediaControlService> MediaControlService::GetService() {
return service;
}
/* static */
void MediaControlService::GenerateMediaControlKey(const GlobalObject& global,
MediaControlKey aKey) {
RefPtr<MediaControlService> service = MediaControlService::GetService();
if (service) {
service->GenerateTestMediaControlKey(aKey);
}
}
/* static */
void MediaControlService::GetCurrentActiveMediaMetadata(
const GlobalObject& aGlobal, MediaMetadataInit& aMetadata) {
if (RefPtr<MediaControlService> service = MediaControlService::GetService()) {
MediaMetadataBase metadata = service->GetMainControllerMediaMetadata();
aMetadata.mTitle = metadata.mTitle;
aMetadata.mArtist = metadata.mArtist;
aMetadata.mAlbum = metadata.mAlbum;
for (const auto& artwork : metadata.mArtwork) {
// If OOM happens resulting in not able to append the element, then we
// would get incorrect result and fail on test, so we don't need to throw
// an error explicitly.
if (MediaImage* image = aMetadata.mArtwork.AppendElement(fallible)) {
image->mSrc = artwork.mSrc;
image->mSizes = artwork.mSizes;
image->mType = artwork.mType;
}
}
}
}
/* static */
MediaSessionPlaybackState
MediaControlService::GetCurrentMediaSessionPlaybackState(
GlobalObject& aGlobal) {
if (RefPtr<MediaControlService> service = MediaControlService::GetService()) {
return service->GetMainControllerPlaybackState();
}
return MediaSessionPlaybackState::None;
}
NS_INTERFACE_MAP_BEGIN(MediaControlService)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsIObserver)

Просмотреть файл

@ -36,6 +36,14 @@ class MediaControlService final : public nsIObserver {
static RefPtr<MediaControlService> GetService();
// Currently these following static methods are only being used in testing.
static void GenerateMediaControlKey(const GlobalObject& global,
MediaControlKey aKey);
static void GetCurrentActiveMediaMetadata(const GlobalObject& aGlobal,
MediaMetadataInit& aMetadata);
static MediaSessionPlaybackState GetCurrentMediaSessionPlaybackState(
GlobalObject& aGlobal);
AudioFocusManager& GetAudioFocusManager() { return mAudioFocusManager; }
MediaControlKeySource* GetMediaControlKeySource() {
return mMediaControlKeyManager;

Просмотреть файл

@ -99,19 +99,6 @@ inline MediaControlKey ConvertMediaSessionActionToControlKey(
}
}
inline MediaSessionPlaybackTestState ConvertToMediaSessionPlaybackTestState(
MediaSessionPlaybackState aState) {
switch (aState) {
case MediaSessionPlaybackState::Playing:
return MediaSessionPlaybackTestState::Playing;
case MediaSessionPlaybackState::Paused:
return MediaSessionPlaybackTestState::Paused;
default:
MOZ_ASSERT(aState == MediaSessionPlaybackState::None);
return MediaSessionPlaybackTestState::Stopped;
}
}
inline MediaSessionAction ConvertToMediaSessionAction(uint8_t aActionValue) {
MOZ_DIAGNOSTIC_ASSERT(aActionValue < uint8_t(MediaSessionAction::EndGuard_));
return static_cast<MediaSessionAction>(aActionValue);