OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/child/notifications/pending_notifications_tracker.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/callback.h" | |
12 #include "content/child/notifications/pending_notification.h" | |
13 #include "content/public/common/notification_resources.h" | |
14 | |
15 namespace content { | |
16 | |
17 PendingNotificationsTracker::PendingNotificationsTracker( | |
18 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) | |
19 : next_notification_id_(0), main_task_runner_(main_task_runner) {} | |
20 | |
21 PendingNotificationsTracker::~PendingNotificationsTracker() {} | |
22 | |
23 void PendingNotificationsTracker::FetchResources( | |
24 const blink::WebNotificationData& notification_data, | |
25 blink::WebNotificationDelegate* delegate, | |
26 const ResourcesCallback& resources_callback) { | |
27 int32_t notification_id = next_notification_id_++; | |
28 | |
29 if (delegate) | |
30 delegate_to_pending_id_map_[delegate] = notification_id; | |
31 | |
32 base::Closure fetches_finished_callback = base::Bind( | |
33 &PendingNotificationsTracker::FetchesFinished, | |
34 base::Unretained(this) /* The pending notifications are owned by this. */, | |
35 delegate, notification_id, resources_callback); | |
36 | |
37 std::unique_ptr<PendingNotification> pending_notification( | |
38 new PendingNotification(main_task_runner_)); | |
39 pending_notification->FetchResources(notification_data, | |
40 fetches_finished_callback); | |
41 | |
42 pending_notifications_.AddWithID(pending_notification.release(), | |
43 notification_id); | |
44 } | |
45 | |
46 bool PendingNotificationsTracker::CancelResourceFetches( | |
47 blink::WebNotificationDelegate* delegate) { | |
48 DCHECK(delegate); | |
49 | |
50 auto iter = delegate_to_pending_id_map_.find(delegate); | |
51 if (iter == delegate_to_pending_id_map_.end()) | |
52 return false; | |
53 | |
54 pending_notifications_.Remove(iter->second); | |
55 delegate_to_pending_id_map_.erase(iter); | |
56 | |
57 return true; | |
58 } | |
59 | |
60 void PendingNotificationsTracker::FetchesFinished( | |
61 blink::WebNotificationDelegate* delegate, | |
62 int32_t notification_id, | |
63 const ResourcesCallback& resources_callback) { | |
64 PendingNotification* pending_notification = | |
65 pending_notifications_.Lookup(notification_id); | |
66 DCHECK(pending_notification); | |
67 | |
68 resources_callback.Run(pending_notification->GetResources()); | |
69 | |
70 if (delegate) | |
71 delegate_to_pending_id_map_.erase(delegate); | |
72 pending_notifications_.Remove(notification_id); | |
73 } | |
74 | |
75 } // namespace content | |
OLD | NEW |