microphone_mute_notification_controller.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2021 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 ASH_SYSTEM_MICROPHONE_MUTE_MICROPHONE_MUTE_NOTIFICATION_CONTROLLER_H_
  5. #define ASH_SYSTEM_MICROPHONE_MUTE_MICROPHONE_MUTE_NOTIFICATION_CONTROLLER_H_
  6. #include <string>
  7. #include "ash/ash_export.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/scoped_observation.h"
  10. #include "chromeos/ash/components/audio/cras_audio_handler.h"
  11. #include "ui/message_center/public/cpp/notification_types.h"
  12. namespace message_center {
  13. class Notification;
  14. }
  15. namespace ash {
  16. // Controller class to manage microphone mute notifications. This
  17. // notification shows up when the user launches an app that uses the microphone
  18. // while the microphone is muted.
  19. class ASH_EXPORT MicrophoneMuteNotificationController
  20. : public ash::CrasAudioHandler::AudioObserver {
  21. public:
  22. MicrophoneMuteNotificationController();
  23. MicrophoneMuteNotificationController(
  24. const MicrophoneMuteNotificationController&) = delete;
  25. MicrophoneMuteNotificationController& operator=(
  26. const MicrophoneMuteNotificationController&) = delete;
  27. ~MicrophoneMuteNotificationController() override;
  28. // Shows the microphone muted notification if it needs to be shown.
  29. // |priority| - The priority with which the notification should be shown.
  30. // |recreate| - Whether the notification should be recreated if it's already
  31. // shown.
  32. void MaybeShowNotification(message_center::NotificationPriority priority,
  33. bool recreate);
  34. // ash::CrasAudioHandler::AudioObserver:
  35. void OnInputMuteChanged(bool mute_on) override;
  36. void OnInputMutedByMicrophoneMuteSwitchChanged(bool muted) override;
  37. void OnNumberOfInputStreamsWithPermissionChanged() override;
  38. private:
  39. friend class MicrophoneMuteNotificationControllerTest;
  40. // Creates a notification for telling the user they're attempting to use the
  41. // mic while the mic is muted.
  42. std::unique_ptr<message_center::Notification>
  43. GenerateMicrophoneMuteNotification(
  44. const absl::optional<std::u16string>& app_name,
  45. message_center::NotificationPriority priority);
  46. // Mic mute notification title.
  47. std::u16string GetNotificationTitle(
  48. const absl::optional<std::u16string>& app_name) const;
  49. // Mic mute notification body.
  50. std::u16string GetNotificationMessage() const;
  51. // Takes down the mic mute notification.
  52. void RemoveMicrophoneMuteNotification();
  53. // Returns number if we have any active input stream with permission, of any
  54. // client type. See
  55. // ash::CrasAudioClient::NumberOfInputStreamsWithPermissionChanged() for more
  56. // details.
  57. int CountActiveInputStreams();
  58. static const char kNotificationId[];
  59. // Whether the microphone is muted.
  60. bool mic_mute_on_ = false;
  61. // Whether the microphone is muted using a microphone mute switch.
  62. bool mic_muted_by_mute_switch_ = false;
  63. // The number of currently active audio input steams.
  64. int input_stream_count_ = 0;
  65. // Set when a microphone mute notification is shown. Contains the notification
  66. // priority used for the notification.
  67. absl::optional<message_center::NotificationPriority>
  68. current_notification_priority_;
  69. base::ScopedObservation<ash::CrasAudioHandler,
  70. AudioObserver,
  71. &ash::CrasAudioHandler::AddAudioObserver,
  72. &ash::CrasAudioHandler::RemoveAudioObserver>
  73. audio_observation_{this};
  74. base::WeakPtrFactory<MicrophoneMuteNotificationController> weak_ptr_factory_{
  75. this};
  76. };
  77. } // namespace ash
  78. #endif // ASH_SYSTEM_MICROPHONE_MUTE_MICROPHONE_MUTE_NOTIFICATION_CONTROLLER_H_