remote_command_center_delegate_cocoa.mm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/system_media_controls/mac/remote_command_center_delegate_cocoa.h"
  5. #import <MediaPlayer/MediaPlayer.h>
  6. #include "base/time/time.h"
  7. #include "components/system_media_controls/mac/remote_command_center_delegate.h"
  8. @interface RemoteCommandCenterDelegateCocoa ()
  9. - (void)setCommand:(MPRemoteCommand*)command enabled:(bool)enabled;
  10. - (void)enableCommand:(MPRemoteCommand*)command;
  11. - (void)disableCommand:(MPRemoteCommand*)command;
  12. @end
  13. @implementation RemoteCommandCenterDelegateCocoa
  14. - (instancetype)initWithDelegate:
  15. (system_media_controls::internal::RemoteCommandCenterDelegate*)delegate {
  16. if (self = [super init]) {
  17. _delegate = delegate;
  18. // Initialize all commands as disabled.
  19. MPRemoteCommandCenter* commandCenter =
  20. [MPRemoteCommandCenter sharedCommandCenter];
  21. commandCenter.pauseCommand.enabled = NO;
  22. commandCenter.playCommand.enabled = NO;
  23. commandCenter.stopCommand.enabled = NO;
  24. commandCenter.togglePlayPauseCommand.enabled = NO;
  25. commandCenter.nextTrackCommand.enabled = NO;
  26. commandCenter.previousTrackCommand.enabled = NO;
  27. commandCenter.changeRepeatModeCommand.enabled = NO;
  28. commandCenter.changeShuffleModeCommand.enabled = NO;
  29. commandCenter.changePlaybackRateCommand.enabled = NO;
  30. commandCenter.seekBackwardCommand.enabled = NO;
  31. commandCenter.seekForwardCommand.enabled = NO;
  32. commandCenter.skipBackwardCommand.enabled = NO;
  33. commandCenter.skipForwardCommand.enabled = NO;
  34. commandCenter.changePlaybackPositionCommand.enabled = NO;
  35. commandCenter.ratingCommand.enabled = NO;
  36. commandCenter.likeCommand.enabled = NO;
  37. commandCenter.dislikeCommand.enabled = NO;
  38. commandCenter.bookmarkCommand.enabled = NO;
  39. commandCenter.enableLanguageOptionCommand.enabled = NO;
  40. commandCenter.disableLanguageOptionCommand.enabled = NO;
  41. }
  42. return self;
  43. }
  44. - (MPRemoteCommandHandlerStatus)onCommand:(MPRemoteCommandEvent*)event {
  45. MPRemoteCommandCenter* commandCenter =
  46. [MPRemoteCommandCenter sharedCommandCenter];
  47. if (event.command == commandCenter.pauseCommand) {
  48. _delegate->OnPause();
  49. } else if (event.command == commandCenter.playCommand) {
  50. _delegate->OnPlay();
  51. } else if (event.command == commandCenter.stopCommand) {
  52. _delegate->OnStop();
  53. } else if (event.command == commandCenter.togglePlayPauseCommand) {
  54. _delegate->OnPlayPause();
  55. } else if (event.command == commandCenter.nextTrackCommand) {
  56. _delegate->OnNext();
  57. } else if (event.command == commandCenter.previousTrackCommand) {
  58. _delegate->OnPrevious();
  59. } else if (event.command == commandCenter.changePlaybackPositionCommand) {
  60. MPChangePlaybackPositionCommandEvent* changePlaybackPositionCommandEvent =
  61. (MPChangePlaybackPositionCommandEvent*)event;
  62. _delegate->OnSeekTo(
  63. base::Seconds(changePlaybackPositionCommandEvent.positionTime));
  64. }
  65. return MPRemoteCommandHandlerStatusSuccess;
  66. }
  67. - (void)setCanPlay:(bool)can_play {
  68. MPRemoteCommandCenter* commandCenter =
  69. [MPRemoteCommandCenter sharedCommandCenter];
  70. [self setCommand:commandCenter.playCommand enabled:can_play];
  71. }
  72. - (void)setCanPause:(bool)can_pause {
  73. MPRemoteCommandCenter* commandCenter =
  74. [MPRemoteCommandCenter sharedCommandCenter];
  75. [self setCommand:commandCenter.pauseCommand enabled:can_pause];
  76. }
  77. - (void)setCanStop:(bool)can_stop {
  78. MPRemoteCommandCenter* commandCenter =
  79. [MPRemoteCommandCenter sharedCommandCenter];
  80. [self setCommand:commandCenter.stopCommand enabled:can_stop];
  81. }
  82. - (void)setCanPlayPause:(bool)can_playpause {
  83. MPRemoteCommandCenter* commandCenter =
  84. [MPRemoteCommandCenter sharedCommandCenter];
  85. [self setCommand:commandCenter.togglePlayPauseCommand enabled:can_playpause];
  86. }
  87. - (void)setCanGoNextTrack:(bool)can_go_next_track {
  88. MPRemoteCommandCenter* commandCenter =
  89. [MPRemoteCommandCenter sharedCommandCenter];
  90. [self setCommand:commandCenter.nextTrackCommand enabled:can_go_next_track];
  91. }
  92. - (void)setCanGoPreviousTrack:(bool)can_go_prev_track {
  93. MPRemoteCommandCenter* commandCenter =
  94. [MPRemoteCommandCenter sharedCommandCenter];
  95. [self setCommand:commandCenter.previousTrackCommand
  96. enabled:can_go_prev_track];
  97. }
  98. - (void)setCanSeekTo:(bool)can_seek_to {
  99. MPRemoteCommandCenter* commandCenter =
  100. [MPRemoteCommandCenter sharedCommandCenter];
  101. [self setCommand:commandCenter.changePlaybackPositionCommand
  102. enabled:can_seek_to];
  103. }
  104. - (void)setCommand:(MPRemoteCommand*)command enabled:(bool)enabled {
  105. if (enabled) {
  106. [self enableCommand:command];
  107. } else {
  108. [self disableCommand:command];
  109. }
  110. }
  111. - (void)enableCommand:(MPRemoteCommand*)command {
  112. command.enabled = YES;
  113. [command addTarget:self action:@selector(onCommand:)];
  114. }
  115. - (void)disableCommand:(MPRemoteCommand*)command {
  116. command.enabled = NO;
  117. [command removeTarget:self];
  118. }
  119. @end