system_media_controls_linux.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifndef COMPONENTS_SYSTEM_MEDIA_CONTROLS_LINUX_SYSTEM_MEDIA_CONTROLS_LINUX_H_
  5. #define COMPONENTS_SYSTEM_MEDIA_CONTROLS_LINUX_SYSTEM_MEDIA_CONTROLS_LINUX_H_
  6. #include <string>
  7. #include "base/component_export.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/observer_list.h"
  11. #include "base/timer/timer.h"
  12. #include "components/dbus/properties/types.h"
  13. #include "components/system_media_controls/system_media_controls.h"
  14. #include "dbus/bus.h"
  15. #include "dbus/exported_object.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. class DbusProperties;
  18. namespace dbus {
  19. class MethodCall;
  20. } // namespace dbus
  21. namespace system_media_controls {
  22. class SystemMediaControlsObserver;
  23. namespace internal {
  24. COMPONENT_EXPORT(SYSTEM_MEDIA_CONTROLS)
  25. extern const char kMprisAPIServiceNameFormatString[];
  26. COMPONENT_EXPORT(SYSTEM_MEDIA_CONTROLS) extern const char kMprisAPIObjectPath[];
  27. COMPONENT_EXPORT(SYSTEM_MEDIA_CONTROLS)
  28. extern const char kMprisAPIInterfaceName[];
  29. COMPONENT_EXPORT(SYSTEM_MEDIA_CONTROLS)
  30. extern const char kMprisAPIPlayerInterfaceName[];
  31. COMPONENT_EXPORT(SYSTEM_MEDIA_CONTROLS)
  32. extern const char kMprisAPISignalSeeked[];
  33. // A D-Bus service conforming to the MPRIS spec:
  34. // https://specifications.freedesktop.org/mpris-spec/latest/
  35. class COMPONENT_EXPORT(SYSTEM_MEDIA_CONTROLS) SystemMediaControlsLinux
  36. : public SystemMediaControls {
  37. public:
  38. explicit SystemMediaControlsLinux(const std::string& product_name);
  39. SystemMediaControlsLinux(const SystemMediaControlsLinux&) = delete;
  40. SystemMediaControlsLinux& operator=(const SystemMediaControlsLinux&) = delete;
  41. ~SystemMediaControlsLinux() override;
  42. // Starts the DBus service.
  43. void StartService();
  44. // SystemMediaControls implementation.
  45. void AddObserver(SystemMediaControlsObserver* observer) override;
  46. void RemoveObserver(SystemMediaControlsObserver* observer) override;
  47. void SetEnabled(bool enabled) override {}
  48. void SetIsNextEnabled(bool value) override;
  49. void SetIsPreviousEnabled(bool value) override;
  50. void SetIsPlayPauseEnabled(bool value) override;
  51. void SetIsStopEnabled(bool value) override {}
  52. void SetIsSeekToEnabled(bool value) override;
  53. void SetPlaybackStatus(PlaybackStatus value) override;
  54. void SetID(const std::string* value) override;
  55. void SetTitle(const std::u16string& value) override;
  56. void SetArtist(const std::u16string& value) override;
  57. void SetAlbum(const std::u16string& value) override;
  58. void SetThumbnail(const SkBitmap& bitmap) override {}
  59. void SetPosition(const media_session::MediaPosition& position) override;
  60. void ClearThumbnail() override {}
  61. void ClearMetadata() override;
  62. void UpdateDisplay() override {}
  63. // Returns the generated service name.
  64. std::string GetServiceName() const;
  65. // Used for testing with a mock DBus Bus.
  66. void SetBusForTesting(scoped_refptr<dbus::Bus> bus) { bus_ = bus; }
  67. private:
  68. void InitializeProperties();
  69. void InitializeDbusInterface();
  70. void OnExported(const std::string& interface_name,
  71. const std::string& method_name,
  72. bool success);
  73. void OnInitialized(bool success);
  74. void OnOwnership(const std::string& service_name, bool success);
  75. // org.mpris.MediaPlayer2.Player interface.
  76. void Next(dbus::MethodCall* method_call,
  77. dbus::ExportedObject::ResponseSender response_sender);
  78. void Previous(dbus::MethodCall* method_call,
  79. dbus::ExportedObject::ResponseSender response_sender);
  80. void Pause(dbus::MethodCall* method_call,
  81. dbus::ExportedObject::ResponseSender response_sender);
  82. void PlayPause(dbus::MethodCall* method_call,
  83. dbus::ExportedObject::ResponseSender response_sender);
  84. void Stop(dbus::MethodCall* method_call,
  85. dbus::ExportedObject::ResponseSender response_sender);
  86. void Play(dbus::MethodCall* method_call,
  87. dbus::ExportedObject::ResponseSender response_sender);
  88. void Seek(dbus::MethodCall* method_call,
  89. dbus::ExportedObject::ResponseSender response_sender);
  90. void SetPositionMpris(dbus::MethodCall* method_call,
  91. dbus::ExportedObject::ResponseSender response_sender);
  92. // Used for API methods we don't support.
  93. void DoNothing(dbus::MethodCall* method_call,
  94. dbus::ExportedObject::ResponseSender response_sender);
  95. // Sets a value on the Metadata property map and sends a PropertiesChanged
  96. // signal if necessary.
  97. void SetMetadataPropertyInternal(const std::string& property_name,
  98. DbusVariant&& new_value);
  99. void ClearTrackId();
  100. void ClearPosition();
  101. // Updates MPRIS with our current position.
  102. void UpdatePosition(bool emit_signal);
  103. void StartPositionUpdateTimer();
  104. void StopPositionUpdateTimer();
  105. absl::optional<media_session::MediaPosition> position_;
  106. base::RepeatingTimer position_update_timer_;
  107. bool playing_ = false;
  108. const std::string product_name_;
  109. std::unique_ptr<DbusProperties> properties_;
  110. scoped_refptr<dbus::Bus> bus_;
  111. raw_ptr<dbus::ExportedObject> exported_object_;
  112. // The generated service name given to |bus_| when requesting ownership.
  113. const std::string service_name_;
  114. base::RepeatingCallback<void(bool)> barrier_;
  115. // True if we have started creating the DBus service.
  116. bool started_ = false;
  117. // True if we have finished creating the DBus service and received ownership.
  118. bool service_ready_ = false;
  119. base::ObserverList<SystemMediaControlsObserver> observers_;
  120. };
  121. } // namespace internal
  122. } // namespace system_media_controls
  123. #endif // COMPONENTS_SYSTEM_MEDIA_CONTROLS_LINUX_SYSTEM_MEDIA_CONTROLS_LINUX_H_