File: system_media_controls_mac.h

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (127 lines) | stat: -rw-r--r-- 5,452 bytes parent folder | download | duplicates (4)
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
// 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.

#ifndef COMPONENTS_SYSTEM_MEDIA_CONTROLS_MAC_SYSTEM_MEDIA_CONTROLS_MAC_H_
#define COMPONENTS_SYSTEM_MEDIA_CONTROLS_MAC_SYSTEM_MEDIA_CONTROLS_MAC_H_

#include <memory>

#include "base/observer_list.h"
#include "components/remote_cocoa/browser/application_host.h"
#include "components/remote_cocoa/common/application.mojom.h"
#include "components/system_media_controls/mac/now_playing_info_center_delegate.h"
#include "components/system_media_controls/mac/remote_cocoa/system_media_controls.mojom.h"
#include "components/system_media_controls/mac/remote_command_center_delegate.h"
#include "components/system_media_controls/system_media_controls.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace media_session {
struct MediaPosition;
}  // namespace media_session
class SkBitmap;

namespace system_media_controls {
class SystemMediaControlsBridge;
class SystemMediaControlsObserver;

namespace internal {

// Interfaces with Mac OS's MPNowPlayingInfoCenter and related MediaPlayer API.
// The combination of those two form the full SystemMediaControls API.
class SystemMediaControlsMac
    : public system_media_controls::SystemMediaControls,
      public mojom::SystemMediaControlsObserver,
      public remote_cocoa::ApplicationHost::Observer {
 public:
  explicit SystemMediaControlsMac(
      remote_cocoa::ApplicationHost* application_host);
  SystemMediaControlsMac(const SystemMediaControlsMac&) = delete;
  SystemMediaControlsMac& operator=(const SystemMediaControlsMac&) = delete;
  ~SystemMediaControlsMac() override;

  // system_media_controls::SystemMediaControls implementation.
  void AddObserver(
      system_media_controls::SystemMediaControlsObserver* observer) override;
  void RemoveObserver(
      system_media_controls::SystemMediaControlsObserver* observer) override;
  void SetEnabled(bool enabled) override {}
  void SetIsNextEnabled(bool value) override;
  void SetIsPreviousEnabled(bool value) override;
  void SetIsPlayPauseEnabled(bool value) override;
  void SetIsStopEnabled(bool value) override;
  void SetIsSeekToEnabled(bool value) override;
  void SetPlaybackStatus(PlaybackStatus status) override;
  void SetTitle(const std::u16string& title) override;
  void SetArtist(const std::u16string& artist) override;
  void SetAlbum(const std::u16string& album) override;
  void SetThumbnail(const SkBitmap& bitmap) override;
  void SetPosition(const media_session::MediaPosition& position) override;
  void ClearThumbnail() override {}
  void ClearMetadata() override;
  void UpdateDisplay() override {}
  bool GetVisibilityForTesting() const override;
  void SetOnBridgeCreatedCallbackForTesting(
      base::RepeatingCallback<void()>) override;

  // system_media_controls::mojom::SystemMediaControlsObserver:
  // Uses bridge_receiver_ to receive messages from AppShim.
  // Also notifies everyone in `observers_`
  void OnNext() override;
  void OnPrevious() override;
  void OnPause() override;
  void OnPlayPause() override;
  void OnStop() override;
  void OnPlay() override;
  void OnSeekTo(base::TimeDelta seek_time) override;
  void OnBridgeCreatedForTesting() override;
  void OnMetadataClearedForTesting() override;

  // remote_cocoa::ApplicationHost::Observer:
  void OnApplicationHostDestroying(
      remote_cocoa::ApplicationHost* host) override;

 private:
  // If needed, rebinds `bridge_remote_` and `bridge_receiver_` to the correct
  // out of process SystemMediaControlsBridge. This is needed because there can
  // be multiple of the same PWA open/playing audio, and each has its own
  // SystemMediaControlsMac. However, there is only one app shim process,
  // meaning only one out of process SystemMediaControlsBridge.
  // When the user switches between the duplicated PWAs, we need to rebind
  // the SystemMediaControlsBridge to the correct SystemMediaControlsMac.
  // In the case that this function rebinds, it first makes a ClearMetadata
  // call to ensure that there's no mixing of metadata across the different
  // mojo connections.
  void MaybeRebindToBridge();

  // The Application that `this` is connected to in the app shim.
  raw_ptr<remote_cocoa::ApplicationHost> application_host_;

  // Receives updates from macOS via a SystemMediaControlsBridge.
  mojo::Receiver<mojom::SystemMediaControlsObserver> bridge_receiver_{this};

  // Sends information and updates to macOS via a SystemMediaControlsBridge.
  mojo::Remote<mojom::SystemMediaControls> bridge_remote_;

  // The above Remote/Receiver will speak to `in_proc_bridge_` for the browser's
  // system media controls connection.
  std::unique_ptr<SystemMediaControlsBridge> in_proc_bridge_;

  // People who need to know when macOS did something -
  // MediaKeysListenerManagerImpl mainly.
  base::ObserverList<system_media_controls::SystemMediaControlsObserver>
      observers_;

  // True if we've received OnApplicationHostDestroying. Prevents a crash
  // when you cmd+Q quit a PWA.
  bool is_app_shim_closing_ = false;

  // Notifies tests upon successful creation of a SystemMediaControlsBridge.
  base::RepeatingCallback<void()> on_bridge_created_callback_for_testing_;
};

}  // namespace internal
}  // namespace system_media_controls

#endif  // COMPONENTS_SYSTEM_MEDIA_CONTROLS_MAC_SYSTEM_MEDIA_CONTROLS_MAC_H_