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.
5 #include "content/child/notifications/pending_notifications_tracker.h"
8 #include "base/location.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "content/child/notifications/notification_image_loader.h"
11 #include "content/child/notifications/notification_manager.h"
12 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificationData.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
17 // Stores the information associated with a pending notification.
18 struct PendingNotificationsTracker::PendingNotification
{
20 const scoped_refptr
<NotificationImageLoader
>& image_loader
,
21 const NotificationResourcesFetchedCallback
& callback
)
22 : image_loader(image_loader
),
25 scoped_refptr
<NotificationImageLoader
> image_loader
;
26 NotificationResourcesFetchedCallback callback
;
29 PendingNotificationsTracker::PendingNotificationsTracker(
30 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
)
31 : main_thread_task_runner_(main_thread_task_runner
),
32 weak_factory_(this) {}
34 PendingNotificationsTracker::~PendingNotificationsTracker() {}
36 void PendingNotificationsTracker::FetchPageNotificationResources(
37 const blink::WebNotificationData
& notification_data
,
38 blink::WebNotificationDelegate
* delegate
,
39 const NotificationResourcesFetchedCallback
& callback
) {
40 delegate_to_pending_id_map_
[delegate
] = FetchNotificationResources(
43 new NotificationImageLoader(
45 &PendingNotificationsTracker::DidFetchPageNotification
,
46 weak_factory_
.GetWeakPtr(), delegate
),
47 base::ThreadTaskRunnerHandle::Get()));
50 void PendingNotificationsTracker::FetchPersistentNotificationResources(
51 const blink::WebNotificationData
& notification_data
,
52 const NotificationResourcesFetchedCallback
& callback
) {
53 FetchNotificationResources(
56 new NotificationImageLoader(
58 &PendingNotificationsTracker::DidFetchPersistentNotification
,
59 weak_factory_
.GetWeakPtr()),
60 base::ThreadTaskRunnerHandle::Get()));
63 bool PendingNotificationsTracker::CancelPageNotificationFetches(
64 blink::WebNotificationDelegate
* delegate
) {
65 auto iter
= delegate_to_pending_id_map_
.find(delegate
);
66 if (iter
== delegate_to_pending_id_map_
.end())
69 pending_notifications_
.Remove(iter
->second
);
70 delegate_to_pending_id_map_
.erase(iter
);
75 void PendingNotificationsTracker::DidFetchPageNotification(
76 blink::WebNotificationDelegate
* delegate
,
78 const SkBitmap
& icon
) {
79 PendingNotification
* pending_notification
=
80 pending_notifications_
.Lookup(notification_id
);
81 DCHECK(pending_notification
);
83 pending_notification
->callback
.Run(icon
);
85 delegate_to_pending_id_map_
.erase(delegate
);
86 pending_notifications_
.Remove(notification_id
);
89 void PendingNotificationsTracker::DidFetchPersistentNotification(
90 int notification_id
, const SkBitmap
& icon
) {
91 PendingNotification
* pending_notification
=
92 pending_notifications_
.Lookup(notification_id
);
93 DCHECK(pending_notification
);
95 pending_notification
->callback
.Run(icon
);
97 pending_notifications_
.Remove(notification_id
);
100 int PendingNotificationsTracker::FetchNotificationResources(
101 const blink::WebNotificationData
& notification_data
,
102 const NotificationResourcesFetchedCallback
& callback
,
103 const scoped_refptr
<NotificationImageLoader
>& image_loader
) {
104 int notification_id
= pending_notifications_
.Add(
105 new PendingNotification(image_loader
, callback
));
107 main_thread_task_runner_
->PostTask(
108 FROM_HERE
, base::Bind(&NotificationImageLoader::StartOnMainThread
,
109 image_loader
, notification_id
,
110 GURL(notification_data
.icon
.spec())));
112 return notification_id
;
115 } // namespace content