OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_ANDROID_H_ | 5 #ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_ANDROID_H_ |
6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_ANDROID_H_ | 6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_ANDROID_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 #include <map> | 9 #include <map> |
10 #include <set> | |
11 #include <string> | 10 #include <string> |
12 | 11 |
13 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
14 #include "chrome/browser/notifications/notification_ui_manager.h" | 13 #include "chrome/browser/notifications/notification_ui_manager.h" |
15 | 14 |
16 class ProfileNotification; | |
17 | |
18 // Implementation of the Notification UI Manager for Android, which defers to | 15 // Implementation of the Notification UI Manager for Android, which defers to |
19 // the Android framework for displaying notifications. | 16 // the Android framework for displaying notifications. |
20 // | 17 // |
21 // The Android notification manager only operates reliably on notifications | 18 // Android does not support getting the notifications currently shown by an |
22 // that were opened or interacted with since the last restart of Chrome. | 19 // app without very intrusive permissions, which means that it's not possible |
| 20 // to provide reliable implementations for methods which have to iterate over |
| 21 // the existing notifications. Because of this, these have not been implemented. |
| 22 // |
| 23 // The UI manager implementation *is* reliable for adding and canceling single |
| 24 // notifications based on their delegate id. Finally, events for persistent Web |
| 25 // Notifications will be forwarded directly to the associated event handlers, |
| 26 // as such notifications may outlive the browser process on Android. |
23 class NotificationUIManagerAndroid : public NotificationUIManager { | 27 class NotificationUIManagerAndroid : public NotificationUIManager { |
24 public: | 28 public: |
25 NotificationUIManagerAndroid(); | 29 NotificationUIManagerAndroid(); |
26 ~NotificationUIManagerAndroid() override; | 30 ~NotificationUIManagerAndroid() override; |
27 | 31 |
28 // Called by the Java implementation when the notification has been clicked. | 32 // Called by the Java implementation when the notification has been clicked. |
29 bool OnNotificationClicked(JNIEnv* env, | 33 bool OnNotificationClicked(JNIEnv* env, |
30 jobject java_object, | 34 jobject java_object, |
31 jlong persistent_notification_id, | 35 jlong persistent_notification_id, |
32 jstring java_origin, | 36 jstring java_origin, |
33 jstring java_tag); | 37 jstring java_tag); |
34 | 38 |
35 // Called by the Java implementation when the notification has been closed. | 39 // Called by the Java implementation when the notification has been closed. |
36 bool OnNotificationClosed(JNIEnv* env, | 40 bool OnNotificationClosed(JNIEnv* env, |
37 jobject java_object, | 41 jobject java_object, |
38 jlong persistent_notification_id, | 42 jlong persistent_notification_id, |
39 jstring java_origin, | 43 jstring java_origin, |
40 jstring java_tag); | 44 jstring java_tag); |
41 | 45 |
42 // NotificationUIManager implementation; | 46 // NotificationUIManager implementation. |
43 void Add(const Notification& notification, Profile* profile) override; | 47 void Add(const Notification& notification, Profile* profile) override; |
44 bool Update(const Notification& notification, | 48 bool Update(const Notification& notification, |
45 Profile* profile) override; | 49 Profile* profile) override; |
46 const Notification* FindById(const std::string& delegate_id, | 50 const Notification* FindById(const std::string& delegate_id, |
47 ProfileID profile_id) const override; | 51 ProfileID profile_id) const override; |
48 bool CancelById(const std::string& delegate_id, | 52 bool CancelById(const std::string& delegate_id, |
49 ProfileID profile_id) override; | 53 ProfileID profile_id) override; |
50 std::set<std::string> GetAllIdsByProfileAndSourceOrigin(Profile* profile, | 54 std::set<std::string> GetAllIdsByProfileAndSourceOrigin(Profile* profile, |
51 const GURL& source) | 55 const GURL& source) |
52 override; | 56 override; |
53 bool CancelAllBySourceOrigin(const GURL& source_origin) override; | 57 bool CancelAllBySourceOrigin(const GURL& source_origin) override; |
54 bool CancelAllByProfile(ProfileID profile_id) override; | 58 bool CancelAllByProfile(ProfileID profile_id) override; |
55 void CancelAll() override; | 59 void CancelAll() override; |
56 | 60 |
57 static bool RegisterNotificationUIManager(JNIEnv* env); | 61 static bool RegisterNotificationUIManager(JNIEnv* env); |
58 | 62 |
59 private: | 63 private: |
60 // Closes the Notification as displayed on the Android system. | |
61 void PlatformCloseNotification(const std::string& notification_id); | |
62 | |
63 // Adds |profile_notification| to a private local map. The map takes ownership | |
64 // of the notification object. | |
65 void AddProfileNotification(ProfileNotification* profile_notification); | |
66 | |
67 // Erases |profile_notification| from the private local map and deletes it. | |
68 // If |close| is true, also closes the notification. | |
69 void RemoveProfileNotification(ProfileNotification* profile_notification, | |
70 bool close); | |
71 | |
72 // Returns the ProfileNotification for the |id|, or NULL if no such | |
73 // notification is found. | |
74 ProfileNotification* FindProfileNotification(const std::string& id) const; | |
75 | |
76 // Returns the ProfileNotification for |profile| that has the same origin and | |
77 // tag as |notification|. Returns NULL if no valid match is found. | |
78 ProfileNotification* FindNotificationToReplace( | |
79 const Notification& notification, Profile* profile) const; | |
80 | |
81 // Map from a notification id to the associated ProfileNotification*. | |
82 std::map<std::string, ProfileNotification*> profile_notifications_; | |
83 | |
84 // Pair containing the information necessary in order to enable closing | 64 // Pair containing the information necessary in order to enable closing |
85 // notifications that were not created by this instance of the manager: the | 65 // notifications that were not created by this instance of the manager: the |
86 // notification's origin and tag. This list may not contain the notifications | 66 // notification's origin and tag. This list may not contain the notifications |
87 // that have not been interacted with since the last restart of Chrome. | 67 // that have not been interacted with since the last restart of Chrome. |
88 using RegeneratedNotificationInfo = std::pair<std::string, std::string>; | 68 using RegeneratedNotificationInfo = std::pair<std::string, std::string>; |
89 | 69 |
90 // Map from persistent notification id to RegeneratedNotificationInfo. | 70 // Mapping of a persistent notification id to renegerated notification info. |
| 71 // TODO(peter): Remove this map once notification delegate ids for Web |
| 72 // notifications are created by the content/ layer. |
91 std::map<int64_t, RegeneratedNotificationInfo> | 73 std::map<int64_t, RegeneratedNotificationInfo> |
92 regenerated_notification_infos_; | 74 regenerated_notification_infos_; |
93 | 75 |
94 base::android::ScopedJavaGlobalRef<jobject> java_object_; | 76 base::android::ScopedJavaGlobalRef<jobject> java_object_; |
95 | 77 |
96 DISALLOW_COPY_AND_ASSIGN(NotificationUIManagerAndroid); | 78 DISALLOW_COPY_AND_ASSIGN(NotificationUIManagerAndroid); |
97 }; | 79 }; |
98 | 80 |
99 #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_ANDROID_H_ | 81 #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_ANDROID_H_ |
OLD | NEW |