Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / notifications / pending_notifications_tracker.cc
blob69b5c8e90b7e56cd7856998f8d16a426dd6c24eb
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"
7 #include "base/bind.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/WebSerializedOrigin.h"
13 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificationData.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
16 namespace content {
18 // Stores the information associated with a pending notification.
19 struct PendingNotificationsTracker::PendingNotification {
20 PendingNotification(
21 const scoped_refptr<NotificationImageLoader>& image_loader,
22 const NotificationResourcesFetchedCallback& callback)
23 : image_loader(image_loader),
24 callback(callback) {}
26 scoped_refptr<NotificationImageLoader> image_loader;
27 NotificationResourcesFetchedCallback callback;
30 PendingNotificationsTracker::PendingNotificationsTracker(
31 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner)
32 : main_thread_task_runner_(main_thread_task_runner),
33 weak_factory_(this) {}
35 PendingNotificationsTracker::~PendingNotificationsTracker() {}
37 void PendingNotificationsTracker::FetchPageNotificationResources(
38 const blink::WebNotificationData& notification_data,
39 blink::WebNotificationDelegate* delegate,
40 const NotificationResourcesFetchedCallback& callback) {
41 delegate_to_pending_id_map_[delegate] = FetchNotificationResources(
42 notification_data,
43 callback,
44 new NotificationImageLoader(
45 base::Bind(
46 &PendingNotificationsTracker::DidFetchPageNotification,
47 weak_factory_.GetWeakPtr(), delegate),
48 base::ThreadTaskRunnerHandle::Get()));
51 void PendingNotificationsTracker::FetchPersistentNotificationResources(
52 const blink::WebNotificationData& notification_data,
53 const NotificationResourcesFetchedCallback& callback) {
54 FetchNotificationResources(
55 notification_data,
56 callback,
57 new NotificationImageLoader(
58 base::Bind(
59 &PendingNotificationsTracker::DidFetchPersistentNotification,
60 weak_factory_.GetWeakPtr()),
61 base::ThreadTaskRunnerHandle::Get()));
64 bool PendingNotificationsTracker::CancelPageNotificationFetches(
65 blink::WebNotificationDelegate* delegate) {
66 auto iter = delegate_to_pending_id_map_.find(delegate);
67 if (iter == delegate_to_pending_id_map_.end())
68 return false;
70 pending_notifications_.Remove(iter->second);
71 delegate_to_pending_id_map_.erase(iter);
73 return true;
76 void PendingNotificationsTracker::DidFetchPageNotification(
77 blink::WebNotificationDelegate* delegate,
78 int notification_id,
79 const SkBitmap& icon) {
80 PendingNotification* pending_notification =
81 pending_notifications_.Lookup(notification_id);
82 DCHECK(pending_notification);
84 pending_notification->callback.Run(icon);
86 delegate_to_pending_id_map_.erase(delegate);
87 pending_notifications_.Remove(notification_id);
90 void PendingNotificationsTracker::DidFetchPersistentNotification(
91 int notification_id, const SkBitmap& icon) {
92 PendingNotification* pending_notification =
93 pending_notifications_.Lookup(notification_id);
94 DCHECK(pending_notification);
96 pending_notification->callback.Run(icon);
98 pending_notifications_.Remove(notification_id);
101 int PendingNotificationsTracker::FetchNotificationResources(
102 const blink::WebNotificationData& notification_data,
103 const NotificationResourcesFetchedCallback& callback,
104 const scoped_refptr<NotificationImageLoader>& image_loader) {
105 int notification_id = pending_notifications_.Add(
106 new PendingNotification(image_loader, callback));
108 main_thread_task_runner_->PostTask(
109 FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread,
110 image_loader, notification_id,
111 GURL(notification_data.icon.spec())));
113 return notification_id;
116 } // namespace content