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 "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/WebNotificati
onData.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 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) {} |
| 25 |
| 26 scoped_refptr<NotificationImageLoader> image_loader; |
| 27 NotificationResourcesFetchedCallback callback; |
| 28 }; |
| 29 |
| 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) {} |
| 34 |
| 35 PendingNotificationsTracker::~PendingNotificationsTracker() {} |
| 36 |
| 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())); |
| 49 } |
| 50 |
| 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())); |
| 62 } |
| 63 |
| 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; |
| 69 |
| 70 pending_notifications_.Remove(iter->second); |
| 71 delegate_to_pending_id_map_.erase(iter); |
| 72 |
| 73 return true; |
| 74 } |
| 75 |
| 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); |
| 83 |
| 84 pending_notification->callback.Run(icon); |
| 85 |
| 86 delegate_to_pending_id_map_.erase(delegate); |
| 87 pending_notifications_.Remove(notification_id); |
| 88 } |
| 89 |
| 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); |
| 95 |
| 96 pending_notification->callback.Run(icon); |
| 97 |
| 98 pending_notifications_.Remove(notification_id); |
| 99 } |
| 100 |
| 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)); |
| 107 |
| 108 main_thread_task_runner_->PostTask( |
| 109 FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, |
| 110 image_loader, notification_id, |
| 111 GURL(notification_data.icon.spec()))); |
| 112 |
| 113 return notification_id; |
| 114 } |
| 115 |
| 116 } // namespace content |
OLD | NEW |