Bug 1631196 - Implement MPRIS Raise event by focusing the window playing media. r=alwu

Implement MPRIS Raise event by focusing the window playing media.

Differential Revision: https://phabricator.services.mozilla.com/D71450
This commit is contained in:
Tom Schuster 2020-04-23 19:32:34 +00:00
Родитель c4b2d338a2
Коммит 7abdca1793
8 изменённых файлов: 32 добавлений и 5 удалений

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

@ -714,6 +714,7 @@ enum PopupBlockerState {
// Keep this in sync with MediaControlKeysEvent in MediaControlKeysEvent.h!
enum MediaControlKeysTestEvent {
"focus",
"play",
"pause",
"playPause",

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

@ -38,6 +38,9 @@ void MediaControlKeysHandler::OnKeyPressed(MediaControlKeysEvent aKeyEvent) {
}
switch (aKeyEvent) {
case MediaControlKeysEvent::eFocus:
controller->Focus();
return;
case MediaControlKeysEvent::ePlay:
controller->Play();
return;

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

@ -27,6 +27,7 @@ enum class MediaControlKeysEvent : uint32_t {
eNextTrack,
eSeekBackward,
eSeekForward,
eFocus,
// Keep this the last element, or you have to modify the serialized structure
// in `MediaControlIPC.h` when you use other element as the last one.
eStop,

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

@ -36,6 +36,12 @@ MediaController::~MediaController() {
}
};
void MediaController::Focus() {
LOG("Focus");
UpdateMediaControlKeysEventToContentMediaIfNeeded(
MediaControlKeysEvent::eFocus);
}
void MediaController::Play() {
LOG("Play");
SetGuessedPlayState(MediaSessionPlaybackState::Playing);

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

@ -52,6 +52,8 @@ class MediaController final
explicit MediaController(uint64_t aContextId);
// Focus the window currently playing media.
void Focus();
void Play();
void Pause();
void Stop();

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

@ -7,6 +7,7 @@
#include "MediaControlUtils.h"
#include "mozilla/dom/MediaSession.h"
#include "mozilla/dom/Navigator.h"
#include "nsFocusManager.h"
// avoid redefined macro in unified build
#undef LOG
@ -33,6 +34,14 @@ MediaSession* PlaybackController::GetMediaSession() {
: nullptr;
}
void PlaybackController::Focus() {
// Focus is not part of the MediaSession standard, so always use the
// default behavior and focus the window currently playing media.
if (RefPtr<nsPIDOMWindowOuter> win = mBC->GetDOMWindow()) {
nsFocusManager::FocusWindow(win, CallerType::System);
}
}
void PlaybackController::Play() {
const MediaSessionAction action = MediaSessionAction::Play;
RefPtr<MediaSession> session = GetMediaSession();
@ -126,6 +135,9 @@ void MediaActionHandler::HandleMediaControlKeysEvent(
BrowsingContext* aContext, MediaControlKeysEvent aEvent) {
PlaybackController controller(aContext);
switch (aEvent) {
case MediaControlKeysEvent::eFocus:
controller.Focus();
return;
case MediaControlKeysEvent::ePlay:
controller.Play();
return;

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

@ -38,6 +38,7 @@ class MOZ_STACK_CLASS PlaybackController {
explicit PlaybackController(BrowsingContext* aContext);
~PlaybackController() = default;
void Focus();
void Play();
void Pause();
void SeekBackward();

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

@ -500,11 +500,7 @@ GVariant* MPRISServiceHandler::SupportedMimeTypes() {
return g_variant_builder_end(&builder);
}
constexpr bool MPRISServiceHandler::CanRaise() { return false; }
void MPRISServiceHandler::Raise() {
MOZ_ASSERT_UNREACHABLE("CanRaise is false, this method is not implemented");
}
constexpr bool MPRISServiceHandler::CanRaise() { return true; }
constexpr bool MPRISServiceHandler::CanQuit() { return false; }
@ -631,6 +627,11 @@ void MPRISServiceHandler::EmitEvent(mozilla::dom::MediaControlKeysEvent event) {
}
}
void MPRISServiceHandler::Raise() {
LOG("Raise");
EmitEvent(mozilla::dom::MediaControlKeysEvent::eFocus);
}
void MPRISServiceHandler::Next() {
LOG("Next");
EmitEvent(mozilla::dom::MediaControlKeysEvent::eNextTrack);