multi_device_notification_presenter.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2018 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_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_
  5. #define ASH_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/ash_export.h"
  9. #include "ash/public/cpp/session/session_observer.h"
  10. #include "ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
  11. #include "base/auto_reset.h"
  12. #include "base/callback.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "mojo/public/cpp/bindings/receiver.h"
  15. #include "mojo/public/cpp/bindings/remote.h"
  16. #include "ui/message_center/message_center_observer.h"
  17. namespace message_center {
  18. class MessageCenter;
  19. class Notification;
  20. class RichNotificationData;
  21. } // namespace message_center
  22. namespace ash {
  23. // Presents notifications necessary for MultiDevice setup flow. It observes the
  24. // MultiDeviceSetup mojo service to show a notification when
  25. // (1) a potential host is found for someone who has not gone through the setup
  26. // flow before,
  27. // (2) the host has switched for someone who has, or
  28. // (3) a new Chromebook has been added to an account for someone who has.
  29. //
  30. // The behavior caused by clicking a notification depends its content as
  31. // described above:
  32. // (1) triggers the setup UI to appear to prompt setup flow and
  33. // (2) & (3) open the Connected Devices subpage in Settings.
  34. //
  35. // Note that if one notification is showing and another one is triggered, the
  36. // old text is replaced (if it's different) and the notification pops up again.
  37. class ASH_EXPORT MultiDeviceNotificationPresenter
  38. : public multidevice_setup::mojom::AccountStatusChangeDelegate,
  39. public SessionObserver,
  40. public message_center::MessageCenterObserver {
  41. public:
  42. explicit MultiDeviceNotificationPresenter(
  43. message_center::MessageCenter* message_center);
  44. MultiDeviceNotificationPresenter(const MultiDeviceNotificationPresenter&) =
  45. delete;
  46. MultiDeviceNotificationPresenter& operator=(
  47. const MultiDeviceNotificationPresenter&) = delete;
  48. ~MultiDeviceNotificationPresenter() override;
  49. // Disables notifications for tests.
  50. static std::unique_ptr<base::AutoReset<bool>>
  51. DisableNotificationsForTesting();
  52. // Removes the notification created by NotifyPotentialHostExists() or does
  53. // nothing if that notification is not currently displayed.
  54. void RemoveMultiDeviceSetupNotification();
  55. protected:
  56. // multidevice_setup::mojom::AccountStatusChangeDelegate:
  57. void OnPotentialHostExistsForNewUser() override;
  58. void OnNoLongerNewUser() override;
  59. void OnConnectedHostSwitchedForExistingUser(
  60. const std::string& new_host_device_name) override;
  61. void OnNewChromebookAddedForExistingUser(
  62. const std::string& new_host_device_name) override;
  63. void OnBecameEligibleForWifiSync() override;
  64. // SessionObserver:
  65. void OnUserSessionAdded(const AccountId& account_id) override;
  66. void OnSessionStateChanged(session_manager::SessionState state) override;
  67. // message_center::MessageCenterObserver
  68. void OnNotificationRemoved(const std::string& notification_id,
  69. bool by_user) override;
  70. void OnNotificationClicked(
  71. const std::string& notification_id,
  72. const absl::optional<int>& button_index,
  73. const absl::optional<std::u16string>& reply) override;
  74. private:
  75. friend class MultiDeviceNotificationPresenterTest;
  76. // MultiDevice setup notification ID.
  77. static const char kSetupNotificationId[];
  78. static const char kWifiSyncNotificationId[];
  79. // Represents each possible MultiDevice setup notification that the setup flow
  80. // can show with a "none" option for the general state with no notification
  81. // present.
  82. enum class Status {
  83. kNoNotificationVisible,
  84. kNewUserNotificationVisible,
  85. kExistingUserHostSwitchedNotificationVisible,
  86. kExistingUserNewChromebookNotificationVisible
  87. };
  88. // Reflects MultiDeviceSetupNotification enum in enums.xml. Do not
  89. // rearrange.
  90. enum class NotificationType {
  91. kNewUserPotentialHostExists = 0,
  92. kExistingUserHostSwitched = 1,
  93. kExistingUserNewChromebookAdded = 2,
  94. // This is a legacy error case that is not expected to occur.
  95. kErrorUnknown = 3,
  96. kWifiSyncAnnouncement = 4,
  97. kMaxValue = kWifiSyncAnnouncement
  98. };
  99. static NotificationType GetMetricValueForNotification(
  100. Status notification_status);
  101. static std::string GetNotificationDescriptionForLogging(
  102. Status notification_status);
  103. void ObserveMultiDeviceSetupIfPossible();
  104. void ShowSetupNotification(const Status notification_status,
  105. const std::u16string& title,
  106. const std::u16string& message);
  107. void ShowNotification(const std::string& id,
  108. const std::u16string& title,
  109. const std::u16string& message,
  110. message_center::RichNotificationData optional_fields);
  111. void FlushForTesting();
  112. message_center::MessageCenter* message_center_;
  113. // Notification currently showing or
  114. // Status::kNoNotificationVisible if there isn't one.
  115. Status notification_status_ = Status::kNoNotificationVisible;
  116. mojo::Remote<multidevice_setup::mojom::MultiDeviceSetup>
  117. multidevice_setup_remote_;
  118. mojo::Receiver<multidevice_setup::mojom::AccountStatusChangeDelegate>
  119. receiver_{this};
  120. base::WeakPtrFactory<MultiDeviceNotificationPresenter> weak_ptr_factory_{
  121. this};
  122. };
  123. } // namespace ash
  124. #endif // ASH_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_