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.
5 #ifndef ASH_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_
6 #define ASH_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_
11 #include "ash/ash_export.h"
12 #include "ash/session/session_observer.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "chromeos/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
17 #include "mojo/public/cpp/bindings/binding.h"
19 namespace message_center {
22 } // namespace message_center
24 namespace service_manager {
26 } // namespace service_manager
30 // Presents notifications necessary for MultiDevice setup flow. It observes the
31 // MultiDeviceSetup mojo service to show a notification when
32 // (1) a potential host is found for someone who has not gone through the setup
34 // (2) the host has switched for someone who has, or
35 // (3) a new Chromebook has been added to an account for someone who has.
37 // The behavior caused by clicking a notification depends its content as
39 // (1) triggers the setup UI to appear to prompt setup flow and
40 // (2) & (3) open the Connected Devices subpage in Settings.
42 // Note that if one notification is showing and another one is triggered, the
43 // old text is replaced (if it's different) and the notification pops up again.
44 class ASH_EXPORT MultiDeviceNotificationPresenter
45 : public chromeos::multidevice_setup::mojom::AccountStatusChangeDelegate,
46 public SessionObserver {
48 MultiDeviceNotificationPresenter(
49 message_center::MessageCenter* message_center,
50 service_manager::Connector* connector);
51 ~MultiDeviceNotificationPresenter() override;
53 // Removes the notification created by NotifyPotentialHostExists() or does
54 // nothing if that notification is not currently displayed.
55 void RemoveMultiDeviceSetupNotification();
58 // multidevice_setup::mojom::AccountStatusChangeDelegate:
59 void OnPotentialHostExistsForNewUser() override;
60 void OnNoLongerNewUser() override;
61 void OnConnectedHostSwitchedForExistingUser(
62 const std::string& new_host_device_name) override;
63 void OnNewChromebookAddedForExistingUser(
64 const std::string& new_host_device_name) override;
67 void OnUserSessionAdded(const AccountId& account_id) override;
68 void OnSessionStateChanged(session_manager::SessionState state) override;
71 friend class MultiDeviceNotificationPresenterTest;
73 // MultiDevice setup notification ID.
74 static const char kNotificationId[];
76 // These methods are delegated to a nested class to make them easier to stub
77 // in unit tests. This way they can all be stubbed simultaneously by building
78 // a test delegate class deriving from OpenUiDelegate.
79 class OpenUiDelegate {
81 virtual ~OpenUiDelegate();
82 virtual void OpenMultiDeviceSetupUi();
83 virtual void OpenConnectedDevicesSettings();
86 // Represents each possible MultiDevice setup notification that the setup flow
87 // can show with a "none" option for the general state with no notification
90 kNoNotificationVisible,
91 kNewUserNotificationVisible,
92 kExistingUserHostSwitchedNotificationVisible,
93 kExistingUserNewChromebookNotificationVisible
96 // Reflects MultiDeviceSetupNotification enum in enums.xml. Do not
98 enum NotificationType {
99 kNotificationTypeNewUserPotentialHostExists = 0,
100 kNotificationTypeExistingUserHostSwitched = 1,
101 kNotificationTypeExistingUserNewChromebookAdded = 2,
105 static NotificationType GetMetricValueForNotification(
106 Status notification_status);
108 static std::string GetNotificationDescriptionForLogging(
109 Status notification_status);
111 void ObserveMultiDeviceSetupIfPossible();
112 void OnNotificationClicked();
113 void ShowNotification(const Status notification_status,
114 const base::string16& title,
115 const base::string16& message);
116 std::unique_ptr<message_center::Notification> CreateNotification(
117 const base::string16& title,
118 const base::string16& message);
120 void FlushForTesting();
122 message_center::MessageCenter* message_center_;
123 service_manager::Connector* connector_;
125 // Notification currently showing or
126 // Status::kNoNotificationVisible if there isn't one.
127 Status notification_status_ = Status::kNoNotificationVisible;
129 chromeos::multidevice_setup::mojom::MultiDeviceSetupPtr
130 multidevice_setup_ptr_;
131 mojo::Binding<chromeos::multidevice_setup::mojom::AccountStatusChangeDelegate>
134 std::unique_ptr<OpenUiDelegate> open_ui_delegate_;
135 base::WeakPtrFactory<MultiDeviceNotificationPresenter> weak_ptr_factory_;
137 DISALLOW_COPY_AND_ASSIGN(MultiDeviceNotificationPresenter);
142 #endif // ASH_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_