Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / ash / multi_device_setup / multi_device_notification_presenter.h
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
5 #ifndef ASH_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_
6 #define ASH_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_
7
8 #include <memory>
9 #include <string>
10
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"
18
19 namespace message_center {
20 class MessageCenter;
21 class Notification;
22 }  // namespace message_center
23
24 namespace service_manager {
25 class Connector;
26 }  // namespace service_manager
27
28 namespace ash {
29
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
33 //     flow before,
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.
36 //
37 // The behavior caused by clicking a notification depends its content as
38 // described above:
39 // (1) triggers the setup UI to appear to prompt setup flow and
40 // (2) & (3) open the Connected Devices subpage in Settings.
41 //
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 {
47  public:
48   MultiDeviceNotificationPresenter(
49       message_center::MessageCenter* message_center,
50       service_manager::Connector* connector);
51   ~MultiDeviceNotificationPresenter() override;
52
53   // Removes the notification created by NotifyPotentialHostExists() or does
54   // nothing if that notification is not currently displayed.
55   void RemoveMultiDeviceSetupNotification();
56
57  protected:
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;
65
66   // SessionObserver:
67   void OnUserSessionAdded(const AccountId& account_id) override;
68   void OnSessionStateChanged(session_manager::SessionState state) override;
69
70  private:
71   friend class MultiDeviceNotificationPresenterTest;
72
73   // MultiDevice setup notification ID.
74   static const char kNotificationId[];
75
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 {
80    public:
81     virtual ~OpenUiDelegate();
82     virtual void OpenMultiDeviceSetupUi();
83     virtual void OpenConnectedDevicesSettings();
84   };
85
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
88   // present.
89   enum class Status {
90     kNoNotificationVisible,
91     kNewUserNotificationVisible,
92     kExistingUserHostSwitchedNotificationVisible,
93     kExistingUserNewChromebookNotificationVisible
94   };
95
96   // Reflects MultiDeviceSetupNotification enum in enums.xml. Do not
97   // rearrange.
98   enum NotificationType {
99     kNotificationTypeNewUserPotentialHostExists = 0,
100     kNotificationTypeExistingUserHostSwitched = 1,
101     kNotificationTypeExistingUserNewChromebookAdded = 2,
102     kNotificationTypeMax
103   };
104
105   static NotificationType GetMetricValueForNotification(
106       Status notification_status);
107
108   static std::string GetNotificationDescriptionForLogging(
109       Status notification_status);
110
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);
119
120   void FlushForTesting();
121
122   message_center::MessageCenter* message_center_;
123   service_manager::Connector* connector_;
124
125   // Notification currently showing or
126   // Status::kNoNotificationVisible if there isn't one.
127   Status notification_status_ = Status::kNoNotificationVisible;
128
129   chromeos::multidevice_setup::mojom::MultiDeviceSetupPtr
130       multidevice_setup_ptr_;
131   mojo::Binding<chromeos::multidevice_setup::mojom::AccountStatusChangeDelegate>
132       binding_;
133
134   std::unique_ptr<OpenUiDelegate> open_ui_delegate_;
135   base::WeakPtrFactory<MultiDeviceNotificationPresenter> weak_ptr_factory_;
136
137   DISALLOW_COPY_AND_ASSIGN(MultiDeviceNotificationPresenter);
138 };
139
140 }  // namespace ash
141
142 #endif  // ASH_MULTI_DEVICE_SETUP_MULTI_DEVICE_NOTIFICATION_PRESENTER_H_