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 #include "content/child/notifications/pending_notifications_tracker.h" | 5 #include "content/child/notifications/pending_notifications_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/thread_task_runner_handle.h" | 9 #include "base/callback.h" |
10 #include "content/child/notifications/notification_image_loader.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "content/child/notifications/pending_notification.h" |
11 #include "content/public/common/notification_resources.h" | 12 #include "content/public/common/notification_resources.h" |
12 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onData.h" | |
13 #include "third_party/skia/include/core/SkBitmap.h" | |
14 | 13 |
15 namespace content { | 14 namespace content { |
16 | 15 |
17 // Stores the information associated with a pending notification. | |
18 struct PendingNotificationsTracker::PendingNotification { | |
19 PendingNotification( | |
20 const scoped_refptr<NotificationImageLoader>& image_loader, | |
21 const NotificationResourcesFetchedCallback& callback) | |
22 : image_loader(image_loader), callback(callback) {} | |
23 | |
24 scoped_refptr<NotificationImageLoader> image_loader; | |
25 NotificationResourcesFetchedCallback callback; | |
26 }; | |
27 | |
28 PendingNotificationsTracker::PendingNotificationsTracker( | 16 PendingNotificationsTracker::PendingNotificationsTracker( |
29 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 17 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) |
30 : main_thread_task_runner_(main_thread_task_runner), weak_factory_(this) {} | 18 : next_notification_id_(0), main_task_runner_(main_task_runner) {} |
31 | 19 |
32 PendingNotificationsTracker::~PendingNotificationsTracker() {} | 20 PendingNotificationsTracker::~PendingNotificationsTracker() {} |
33 | 21 |
34 void PendingNotificationsTracker::FetchPageNotificationResources( | 22 void PendingNotificationsTracker::FetchResources( |
35 const blink::WebNotificationData& notification_data, | 23 const blink::WebNotificationData& notification_data, |
36 blink::WebNotificationDelegate* delegate, | 24 blink::WebNotificationDelegate* delegate, |
37 const NotificationResourcesFetchedCallback& callback) { | 25 const ResourcesCallback& resources_callback) { |
38 delegate_to_pending_id_map_[delegate] = FetchNotificationResources( | 26 int32_t notification_id = next_notification_id_++; |
39 notification_data, callback, | 27 |
40 new NotificationImageLoader( | 28 if (delegate) |
41 base::Bind(&PendingNotificationsTracker::DidFetchPageNotification, | 29 delegate_to_pending_id_map_[delegate] = notification_id; |
42 weak_factory_.GetWeakPtr(), delegate), | 30 |
43 base::ThreadTaskRunnerHandle::Get())); | 31 base::Closure fetches_finished_callback = base::Bind( |
| 32 &PendingNotificationsTracker::FetchesFinished, |
| 33 base::Unretained(this) /* The pending notifications are owned by this. */, |
| 34 delegate, notification_id, resources_callback); |
| 35 |
| 36 scoped_ptr<PendingNotification> pending_notification( |
| 37 new PendingNotification(main_task_runner_)); |
| 38 pending_notification->FetchResources(notification_data, |
| 39 fetches_finished_callback); |
| 40 |
| 41 pending_notifications_.AddWithID(pending_notification.release(), |
| 42 notification_id); |
44 } | 43 } |
45 | 44 |
46 void PendingNotificationsTracker::FetchPersistentNotificationResources( | 45 bool PendingNotificationsTracker::CancelResourceFetches( |
47 const blink::WebNotificationData& notification_data, | 46 blink::WebNotificationDelegate* delegate) { |
48 const NotificationResourcesFetchedCallback& callback) { | 47 DCHECK(delegate); |
49 FetchNotificationResources( | |
50 notification_data, callback, | |
51 new NotificationImageLoader( | |
52 base::Bind( | |
53 &PendingNotificationsTracker::DidFetchPersistentNotification, | |
54 weak_factory_.GetWeakPtr()), | |
55 base::ThreadTaskRunnerHandle::Get())); | |
56 } | |
57 | 48 |
58 bool PendingNotificationsTracker::CancelPageNotificationFetches( | |
59 blink::WebNotificationDelegate* delegate) { | |
60 auto iter = delegate_to_pending_id_map_.find(delegate); | 49 auto iter = delegate_to_pending_id_map_.find(delegate); |
61 if (iter == delegate_to_pending_id_map_.end()) | 50 if (iter == delegate_to_pending_id_map_.end()) |
62 return false; | 51 return false; |
63 | 52 |
64 pending_notifications_.Remove(iter->second); | 53 pending_notifications_.Remove(iter->second); |
65 delegate_to_pending_id_map_.erase(iter); | 54 delegate_to_pending_id_map_.erase(iter); |
66 | 55 |
67 return true; | 56 return true; |
68 } | 57 } |
69 | 58 |
70 void PendingNotificationsTracker::DidFetchPageNotification( | 59 void PendingNotificationsTracker::FetchesFinished( |
71 blink::WebNotificationDelegate* delegate, | 60 blink::WebNotificationDelegate* delegate, |
72 int notification_id, | 61 int32_t notification_id, |
73 const SkBitmap& icon) { | 62 const ResourcesCallback& resources_callback) { |
74 PendingNotification* pending_notification = | 63 PendingNotification* pending_notification = |
75 pending_notifications_.Lookup(notification_id); | 64 pending_notifications_.Lookup(notification_id); |
76 DCHECK(pending_notification); | 65 DCHECK(pending_notification); |
77 | 66 |
78 NotificationResources notification_resources; | 67 resources_callback.Run(pending_notification->GetResources()); |
79 notification_resources.notification_icon = icon; | |
80 pending_notification->callback.Run(notification_resources); | |
81 | 68 |
82 delegate_to_pending_id_map_.erase(delegate); | 69 if (delegate) |
| 70 delegate_to_pending_id_map_.erase(delegate); |
83 pending_notifications_.Remove(notification_id); | 71 pending_notifications_.Remove(notification_id); |
84 } | 72 } |
85 | 73 |
86 void PendingNotificationsTracker::DidFetchPersistentNotification( | |
87 int notification_id, const SkBitmap& icon) { | |
88 PendingNotification* pending_notification = | |
89 pending_notifications_.Lookup(notification_id); | |
90 DCHECK(pending_notification); | |
91 | |
92 NotificationResources notification_resources; | |
93 notification_resources.notification_icon = icon; | |
94 pending_notification->callback.Run(notification_resources); | |
95 | |
96 pending_notifications_.Remove(notification_id); | |
97 } | |
98 | |
99 int PendingNotificationsTracker::FetchNotificationResources( | |
100 const blink::WebNotificationData& notification_data, | |
101 const NotificationResourcesFetchedCallback& callback, | |
102 const scoped_refptr<NotificationImageLoader>& image_loader) { | |
103 int notification_id = pending_notifications_.Add( | |
104 new PendingNotification(image_loader, callback)); | |
105 | |
106 main_thread_task_runner_->PostTask( | |
107 FROM_HERE, | |
108 base::Bind(&NotificationImageLoader::StartOnMainThread, image_loader, | |
109 notification_id, GURL(notification_data.icon))); | |
110 | |
111 return notification_id; | |
112 } | |
113 | |
114 } // namespace content | 74 } // namespace content |
OLD | NEW |