1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/system_media_controls/mac/remote_command_center_delegate.h"
#include "base/time/time.h"
#include "components/system_media_controls/mac/remote_command_center_delegate_cocoa.h"
#include "components/system_media_controls/system_media_controls_observer.h"
namespace system_media_controls::internal {
RemoteCommandCenterDelegate::RemoteCommandCenterDelegate() {
remote_command_center_delegate_cocoa_ =
[[RemoteCommandCenterDelegateCocoa alloc] initWithDelegate:this];
}
RemoteCommandCenterDelegate::~RemoteCommandCenterDelegate() {
// Ensure that we unregister from all commands.
SetIsNextEnabled(false);
SetIsPreviousEnabled(false);
SetIsPlayPauseEnabled(false);
SetIsStopEnabled(false);
SetIsSeekToEnabled(false);
}
void RemoteCommandCenterDelegate::SetIsNextEnabled(bool value) {
if (!ShouldSetCommandEnabled(Command::kNextTrack, value))
return;
[remote_command_center_delegate_cocoa_ setCanGoNextTrack:value];
}
void RemoteCommandCenterDelegate::SetIsPreviousEnabled(bool value) {
if (!ShouldSetCommandEnabled(Command::kPreviousTrack, value))
return;
[remote_command_center_delegate_cocoa_ setCanGoPreviousTrack:value];
}
void RemoteCommandCenterDelegate::SetIsPlayPauseEnabled(bool value) {
if (!ShouldSetCommandEnabled(Command::kPlayPause, value))
return;
[remote_command_center_delegate_cocoa_ setCanPlay:value];
[remote_command_center_delegate_cocoa_ setCanPause:value];
[remote_command_center_delegate_cocoa_ setCanPlayPause:value];
}
void RemoteCommandCenterDelegate::SetIsStopEnabled(bool value) {
if (!ShouldSetCommandEnabled(Command::kStop, value))
return;
[remote_command_center_delegate_cocoa_ setCanStop:value];
}
void RemoteCommandCenterDelegate::SetIsSeekToEnabled(bool value) {
if (!ShouldSetCommandEnabled(Command::kSeekTo, value))
return;
[remote_command_center_delegate_cocoa_ setCanSeekTo:value];
}
void RemoteCommandCenterDelegate::OnNext() {
DCHECK(enabled_commands_.contains(Command::kNextTrack));
observer_remote_->OnNext();
}
void RemoteCommandCenterDelegate::OnPrevious() {
DCHECK(enabled_commands_.contains(Command::kPreviousTrack));
observer_remote_->OnPrevious();
}
void RemoteCommandCenterDelegate::OnPlay() {
DCHECK(enabled_commands_.contains(Command::kPlayPause));
observer_remote_->OnPlay();
}
void RemoteCommandCenterDelegate::OnPause() {
DCHECK(enabled_commands_.contains(Command::kPlayPause));
observer_remote_->OnPause();
}
void RemoteCommandCenterDelegate::OnPlayPause() {
DCHECK(enabled_commands_.contains(Command::kPlayPause));
observer_remote_->OnPlayPause();
}
void RemoteCommandCenterDelegate::OnStop() {
DCHECK(enabled_commands_.contains(Command::kStop));
observer_remote_->OnStop();
}
void RemoteCommandCenterDelegate::OnSeekTo(const base::TimeDelta& time) {
DCHECK(enabled_commands_.contains(Command::kSeekTo));
observer_remote_->OnSeekTo(time);
}
void RemoteCommandCenterDelegate::BindObserverRemote(
mojo::PendingRemote<mojom::SystemMediaControlsObserver> remote) {
if (observer_remote_.is_bound()) {
observer_remote_.reset();
}
observer_remote_.Bind(std::move(remote));
DCHECK(observer_remote_.is_bound());
DCHECK(observer_remote_.is_connected());
}
bool RemoteCommandCenterDelegate::ShouldSetCommandEnabled(Command command,
bool will_enable) {
if (will_enable == enabled_commands_.contains(command))
return false;
if (will_enable)
enabled_commands_.insert(command);
else
enabled_commands_.erase(command);
return true;
}
void RemoteCommandCenterDelegate::OnBridgeCreatedForTesting() {
observer_remote_->OnBridgeCreatedForTesting();
}
void RemoteCommandCenterDelegate::OnMetadataClearedForTesting() {
observer_remote_->OnMetadataClearedForTesting();
}
} // namespace system_media_controls::internal
|