1 // Copyright 2016 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_PUBLIC_CPP_EXTERNAL_ARC_MESSAGE_CENTER_ARC_NOTIFICATION_MANAGER_H_
6 #define ASH_PUBLIC_CPP_EXTERNAL_ARC_MESSAGE_CENTER_ARC_NOTIFICATION_MANAGER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <unordered_map>
11 
12 #include "ash/public/cpp/message_center/arc_notification_manager_base.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list_types.h"
15 #include "components/account_id/account_id.h"
16 #include "components/arc/mojom/notifications.mojom.h"
17 #include "components/arc/session/connection_holder.h"
18 #include "components/arc/session/connection_observer.h"
19 #include "mojo/public/cpp/bindings/pending_remote.h"
20 #include "ui/message_center/message_center.h"
21 
22 namespace ash {
23 
24 class ArcNotificationItem;
25 class ArcNotificationManagerDelegate;
26 
27 class ArcNotificationManager
28     : public arc::ConnectionObserver<arc::mojom::NotificationsInstance>,
29       public arc::mojom::NotificationsHost,
30       public ArcNotificationManagerBase {
31  public:
32   // Sets the factory function to create ARC notification views. Exposed for
33   // testing.
34   static void SetCustomNotificationViewFactory();
35 
36   ArcNotificationManager();
37   ~ArcNotificationManager() override;
38 
39   void SetInstance(
40       mojo::PendingRemote<arc::mojom::NotificationsInstance> instance_remote);
41 
42   arc::ConnectionHolder<arc::mojom::NotificationsInstance,
43                         arc::mojom::NotificationsHost>*
44   GetConnectionHolderForTest();
45 
46   // ConnectionObserver<arc::mojom::NotificationsInstance> implementation:
47   void OnConnectionReady() override;
48   void OnConnectionClosed() override;
49 
50   // arc::mojom::NotificationsHost implementation:
51   void OnNotificationPosted(arc::mojom::ArcNotificationDataPtr data) override;
52   void OnNotificationUpdated(arc::mojom::ArcNotificationDataPtr data) override;
53   void OnNotificationRemoved(const std::string& key) override;
54   void OpenMessageCenter() override;
55   void CloseMessageCenter() override;
56   void OnDoNotDisturbStatusUpdated(
57       arc::mojom::ArcDoNotDisturbStatusPtr status) override;
58   void OnLockScreenSettingUpdated(
59       arc::mojom::ArcLockScreenNotificationSettingPtr setting) override;
60   void ProcessUserAction(
61       arc::mojom::ArcNotificationUserActionDataPtr data) override;
62 
63   // Methods called from ArcNotificationItem:
64   void SendNotificationRemovedFromChrome(const std::string& key);
65   void SendNotificationClickedOnChrome(const std::string& key);
66   void SendNotificationActivatedInChrome(const std::string& key,
67                                          bool activated);
68   void CreateNotificationWindow(const std::string& key);
69   void CloseNotificationWindow(const std::string& key);
70   void OpenNotificationSettings(const std::string& key);
71   void OpenNotificationSnoozeSettings(const std::string& key);
72   bool IsOpeningSettingsSupported() const;
73   void SendNotificationToggleExpansionOnChrome(const std::string& key);
74   void SetDoNotDisturbStatusOnAndroid(bool enabled);
75   void CancelPress(const std::string& key);
76   void SetNotificationConfiguration();
77 
78   // Methods called from |visibility_manager_|:
79   void OnMessageCenterVisibilityChanged(
80       arc::mojom::MessageCenterVisibility visibility);
81 
82   // ArcNotificationManagerBase implementation:
83   void AddObserver(Observer* observer) override;
84   void RemoveObserver(Observer* observer) override;
85   void Init(std::unique_ptr<ArcNotificationManagerDelegate> delegate,
86             const AccountId& profile_id,
87             message_center::MessageCenter* message_center) override;
88 
89  private:
90   // Helper class to own MojoChannel and ConnectionHolder.
91   class InstanceOwner;
92 
93   bool ShouldIgnoreNotification(arc::mojom::ArcNotificationData* data);
94 
95   void PerformUserAction(uint32_t id, bool open_message_center);
96   void CancelUserAction(uint32_t id);
97 
98   std::unique_ptr<ArcNotificationManagerDelegate> delegate_;
99   AccountId main_profile_id_;
100   message_center::MessageCenter* message_center_ = nullptr;
101   std::unique_ptr<message_center::MessageCenterObserver>
102       do_not_disturb_manager_;
103   std::unique_ptr<message_center::MessageCenterObserver> visibility_manager_;
104 
105   using ItemMap =
106       std::unordered_map<std::string, std::unique_ptr<ArcNotificationItem>>;
107   ItemMap items_;
108 
109   bool ready_ = false;
110 
111   // If any remote input is focused, its key is stored. Otherwise, empty.
112   std::string previously_focused_notification_key_;
113 
114   std::unique_ptr<InstanceOwner> instance_owner_;
115 
116   base::ObserverList<Observer> observers_;
117 
118   base::WeakPtrFactory<ArcNotificationManager> weak_ptr_factory_{this};
119 
120   DISALLOW_COPY_AND_ASSIGN(ArcNotificationManager);
121 };
122 
123 }  // namespace ash
124 
125 #endif  // ASH_PUBLIC_CPP_EXTERNAL_ARC_MESSAGE_CENTER_ARC_NOTIFICATION_MANAGER_H_
126