OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_ | 5 #ifndef CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_ |
6 #define CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_ | 6 #define CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
| 10 #include "base/callback_forward.h" |
10 #include "base/id_map.h" | 11 #include "base/id_map.h" |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "content/common/content_export.h" |
14 #include "base/memory/weak_ptr.h" | |
15 | |
16 class SkBitmap; | |
17 | 15 |
18 namespace base { | 16 namespace base { |
19 class SingleThreadTaskRunner; | 17 class SingleThreadTaskRunner; |
20 } | 18 } |
21 | 19 |
22 namespace blink { | 20 namespace blink { |
23 struct WebNotificationData; | 21 struct WebNotificationData; |
24 class WebNotificationDelegate; | 22 class WebNotificationDelegate; |
25 } | 23 } |
26 | 24 |
27 namespace content { | 25 namespace content { |
28 | 26 |
29 class NotificationImageLoader; | 27 class PendingNotification; |
| 28 class PendingNotificationsTrackerTest; |
30 struct NotificationResources; | 29 struct NotificationResources; |
31 | 30 |
32 // Type definition for the callback signature which is to be invoked when the | 31 // Tracks all pending Web Notifications as PendingNotification instances. The |
33 // resources associated with a notification have been fetched. | 32 // pending notifications (and their associated data) are stored in this class. |
34 using NotificationResourcesFetchedCallback = | |
35 base::Callback<void(const NotificationResources&)>; | |
36 | |
37 // Tracks all aspects of all pending Web Notifications. Most notably, it's in | |
38 // charge of ensuring that all resource fetches associated with the notification | |
39 // are completed as expected. The data associated with the notifications is | |
40 // stored in this class, to maintain thread integrity of their members. | |
41 // | |
42 // The pending notification tracker is owned by the NotificationManager, and | 33 // The pending notification tracker is owned by the NotificationManager, and |
43 // lives on the thread that manager has been associated with. | 34 // lives on the thread that manager has been associated with. |
44 class PendingNotificationsTracker { | 35 class CONTENT_EXPORT PendingNotificationsTracker { |
45 public: | 36 public: |
46 explicit PendingNotificationsTracker( | 37 explicit PendingNotificationsTracker( |
47 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner); | 38 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner); |
48 ~PendingNotificationsTracker(); | 39 ~PendingNotificationsTracker(); |
49 | 40 |
50 // Adds a page notification to the tracker. Resource fetches for the | 41 // Type definition for the callback signature which is to be invoked when the |
51 // notification will be started on asynchronously the main thread. | 42 // resources associated with a notification have been fetched. |
52 void FetchPageNotificationResources( | 43 using ResourcesCallback = base::Callback<void(const NotificationResources&)>; |
53 const blink::WebNotificationData& notification_data, | |
54 blink::WebNotificationDelegate* delegate, | |
55 const NotificationResourcesFetchedCallback& callback); | |
56 | 44 |
57 // Adds a persistent notification to the tracker. Resource fetches for the | 45 // Adds a pending notification for which to fetch resources. The |delegate| |
58 // notification will be started asynchronously on the main thread. | 46 // may be null, but non-null values can be used to cancel the fetches. |
59 void FetchPersistentNotificationResources( | 47 // Resource fetches will be started asynchronously on the main thread. |
60 const blink::WebNotificationData& notification_data, | 48 void FetchResources(const blink::WebNotificationData& notification_data, |
61 const NotificationResourcesFetchedCallback& callback); | 49 blink::WebNotificationDelegate* delegate, |
| 50 const ResourcesCallback& resources_callback); |
62 | 51 |
63 // Cancels all pending and in-fligth fetches for the page notification | 52 // Cancels all pending and in-flight fetches for the notification identified |
64 // identified by |delegate|. Returns if the notification was cancelled. | 53 // by |delegate|, which may not be null. Returns whether the notification was |
65 bool CancelPageNotificationFetches(blink::WebNotificationDelegate* delegate); | 54 // cancelled. |
| 55 bool CancelResourceFetches(blink::WebNotificationDelegate* delegate); |
66 | 56 |
67 private: | 57 private: |
68 // To be called on the worker thread when the pending page notification | 58 friend class PendingNotificationsTrackerTest; |
69 // identified by |notification_id| has finished fetching the icon. | |
70 void DidFetchPageNotification(blink::WebNotificationDelegate* delegate, | |
71 int notification_id, | |
72 const SkBitmap& icon); | |
73 | 59 |
74 // To be called on the worker thread when the pending persistent notification | 60 // To be called on the worker thread when the pending notification |
75 // identified by |notification_id| has finished fetching the icon. | 61 // identified by |notification_id| has finished fetching the resources. The |
76 void DidFetchPersistentNotification(int notification_id, | 62 // |delegate| may be null. |
77 const SkBitmap& icon); | 63 void FetchesFinished(blink::WebNotificationDelegate* delegate, |
| 64 int notification_id, |
| 65 const ResourcesCallback& resources_callback); |
78 | 66 |
79 // Common code for starting to fetch resources associated with any kind of | 67 // Used to generate ids for tracking pending notifications. |
80 // notification. Will return the id of the pending notification as allocated | 68 int32_t next_notification_id_; |
81 // in the |pending_notifications_| map. | |
82 int FetchNotificationResources( | |
83 const blink::WebNotificationData& notification_data, | |
84 const NotificationResourcesFetchedCallback& callback, | |
85 const scoped_refptr<NotificationImageLoader>& image_loader); | |
86 | 69 |
87 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 70 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
88 | 71 |
89 struct PendingNotification; | 72 // The notifications whose resources are still being fetched. |
90 | |
91 // List of the notifications whose resources are still being fetched. | |
92 IDMap<PendingNotification, IDMapOwnPointer> pending_notifications_; | 73 IDMap<PendingNotification, IDMapOwnPointer> pending_notifications_; |
93 | 74 |
94 // In order to be able to cancel pending page notifications by delegate, store | 75 // In order to be able to cancel pending page notifications by delegate, store |
95 // a mapping of the delegate to the pending notification id as well. | 76 // a mapping of the delegate to the pending notification id as well. |
96 std::map<blink::WebNotificationDelegate*, int> delegate_to_pending_id_map_; | 77 std::map<blink::WebNotificationDelegate*, int> delegate_to_pending_id_map_; |
97 | 78 |
98 base::WeakPtrFactory<PendingNotificationsTracker> weak_factory_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(PendingNotificationsTracker); | 79 DISALLOW_COPY_AND_ASSIGN(PendingNotificationsTracker); |
101 }; | 80 }; |
102 | 81 |
103 } // namespace content | 82 } // namespace content |
104 | 83 |
105 #endif // CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_ | 84 #endif // CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_ |
OLD | NEW |