tray_action.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2017 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 "ash/tray_action/tray_action.h"
  5. #include <utility>
  6. #include "ash/lock_screen_action/lock_screen_note_display_state_handler.h"
  7. #include "ash/tray_action/tray_action_observer.h"
  8. #include "base/bind.h"
  9. #include "base/callback.h"
  10. #include "base/check.h"
  11. #include "ui/events/devices/stylus_state.h"
  12. namespace ash {
  13. TrayAction::TrayAction(BacklightsForcedOffSetter* backlights_forced_off_setter)
  14. : backlights_forced_off_setter_(backlights_forced_off_setter) {
  15. stylus_observation_.Observe(ui::DeviceDataManager::GetInstance());
  16. }
  17. TrayAction::~TrayAction() = default;
  18. void TrayAction::AddObserver(TrayActionObserver* observer) {
  19. observers_.AddObserver(observer);
  20. }
  21. void TrayAction::RemoveObserver(TrayActionObserver* observer) {
  22. observers_.RemoveObserver(observer);
  23. }
  24. void TrayAction::BindReceiver(
  25. mojo::PendingReceiver<mojom::TrayAction> receiver) {
  26. receivers_.Add(this, std::move(receiver));
  27. }
  28. mojom::TrayActionState TrayAction::GetLockScreenNoteState() const {
  29. if (!tray_action_client_)
  30. return mojom::TrayActionState::kNotAvailable;
  31. return lock_screen_note_state_;
  32. }
  33. bool TrayAction::IsLockScreenNoteActive() const {
  34. return GetLockScreenNoteState() == mojom::TrayActionState::kActive;
  35. }
  36. void TrayAction::SetClient(
  37. mojo::PendingRemote<mojom::TrayActionClient> tray_action_client,
  38. mojom::TrayActionState lock_screen_note_state) {
  39. mojom::TrayActionState old_lock_screen_note_state = GetLockScreenNoteState();
  40. if (tray_action_client) {
  41. tray_action_client_.Bind(std::move(tray_action_client));
  42. // Makes sure the state is updated in case the connection is lost.
  43. tray_action_client_.set_disconnect_handler(base::BindOnce(
  44. &TrayAction::SetClient, base::Unretained(this), mojo::NullRemote(),
  45. mojom::TrayActionState::kNotAvailable));
  46. lock_screen_note_state_ = lock_screen_note_state;
  47. lock_screen_note_display_state_handler_ =
  48. std::make_unique<LockScreenNoteDisplayStateHandler>(
  49. backlights_forced_off_setter_);
  50. } else {
  51. tray_action_client_.reset();
  52. lock_screen_note_display_state_handler_.reset();
  53. }
  54. // Setting action handler value can change effective state - notify observers
  55. // if that was the case.
  56. if (GetLockScreenNoteState() != old_lock_screen_note_state)
  57. NotifyLockScreenNoteStateChanged();
  58. }
  59. void TrayAction::UpdateLockScreenNoteState(mojom::TrayActionState state) {
  60. if (state == lock_screen_note_state_)
  61. return;
  62. lock_screen_note_state_ = state;
  63. if (lock_screen_note_state_ == mojom::TrayActionState::kNotAvailable)
  64. lock_screen_note_display_state_handler_->Reset();
  65. // If the client is not set, the effective state has not changed, so no need
  66. // to notify observers of a state change.
  67. if (tray_action_client_)
  68. NotifyLockScreenNoteStateChanged();
  69. }
  70. void TrayAction::RequestNewLockScreenNote(mojom::LockScreenNoteOrigin origin) {
  71. if (GetLockScreenNoteState() != mojom::TrayActionState::kAvailable)
  72. return;
  73. // An action state can be kAvailable only if |tray_action_client_| is set.
  74. DCHECK(tray_action_client_);
  75. tray_action_client_->RequestNewLockScreenNote(origin);
  76. }
  77. void TrayAction::CloseLockScreenNote(mojom::CloseLockScreenNoteReason reason) {
  78. if (tray_action_client_)
  79. tray_action_client_->CloseLockScreenNote(reason);
  80. }
  81. void TrayAction::OnStylusStateChanged(ui::StylusState state) {
  82. if (state == ui::StylusState::REMOVED)
  83. lock_screen_note_display_state_handler_->AttemptNoteLaunchForStylusEject();
  84. }
  85. void TrayAction::FlushMojoForTesting() {
  86. if (tray_action_client_)
  87. tray_action_client_.FlushForTesting();
  88. }
  89. void TrayAction::NotifyLockScreenNoteStateChanged() {
  90. for (auto& observer : observers_)
  91. observer.OnLockScreenNoteStateChanged(GetLockScreenNoteState());
  92. }
  93. } // namespace ash