| 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/location.h" | 
| 9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" | 
| 10 #include "content/child/notifications/notification_image_loader.h" | 10 #include "content/child/notifications/notification_image_loader.h" | 
| 11 #include "content/child/notifications/notification_manager.h" | 11 #include "content/child/notifications/notification_manager.h" | 
|  | 12 #include "content/child/notifications/notification_sound_loader.h" | 
| 12 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" | 13 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" | 
| 13 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
     onData.h" | 14 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
     onData.h" | 
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" | 
| 15 | 16 | 
| 16 namespace content { | 17 namespace content { | 
| 17 | 18 | 
| 18 // Stores the information associated with a pending notification. | 19 // Stores the information associated with a pending notification. | 
| 19 struct PendingNotificationsTracker::PendingNotification { | 20 struct PendingNotificationsTracker::PendingNotification { | 
| 20   PendingNotification( | 21   PendingNotification( | 
|  | 22       const blink::WebNotificationData& notification, | 
| 21       const scoped_refptr<NotificationImageLoader>& image_loader, | 23       const scoped_refptr<NotificationImageLoader>& image_loader, | 
|  | 24       const scoped_refptr<NotificationSoundLoader>& sound_loader, | 
| 22       const NotificationResourcesFetchedCallback& callback) | 25       const NotificationResourcesFetchedCallback& callback) | 
| 23       : image_loader(image_loader), | 26       : notification(notification), | 
|  | 27         image_loader(image_loader), | 
|  | 28         sound_loader(sound_loader), | 
| 24         callback(callback) {} | 29         callback(callback) {} | 
| 25 | 30 | 
|  | 31   blink::WebNotificationData notification; | 
| 26   scoped_refptr<NotificationImageLoader> image_loader; | 32   scoped_refptr<NotificationImageLoader> image_loader; | 
|  | 33   scoped_refptr<NotificationSoundLoader> sound_loader; | 
| 27   NotificationResourcesFetchedCallback callback; | 34   NotificationResourcesFetchedCallback callback; | 
| 28 }; | 35 }; | 
| 29 | 36 | 
| 30 PendingNotificationsTracker::PendingNotificationsTracker( | 37 PendingNotificationsTracker::PendingNotificationsTracker( | 
| 31     scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 38     scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 
| 32     : main_thread_task_runner_(main_thread_task_runner), | 39     : main_thread_task_runner_(main_thread_task_runner), | 
| 33       weak_factory_(this) {} | 40       weak_factory_(this) {} | 
| 34 | 41 | 
| 35 PendingNotificationsTracker::~PendingNotificationsTracker() {} | 42 PendingNotificationsTracker::~PendingNotificationsTracker() {} | 
| 36 | 43 | 
| 37 void PendingNotificationsTracker::FetchPageNotificationResources( | 44 void PendingNotificationsTracker::FetchPageNotificationResources( | 
| 38     const blink::WebNotificationData& notification_data, | 45     const blink::WebNotificationData& notification_data, | 
| 39     blink::WebNotificationDelegate* delegate, | 46     blink::WebNotificationDelegate* delegate, | 
| 40     const NotificationResourcesFetchedCallback& callback) { | 47     const NotificationResourcesFetchedCallback& callback) { | 
| 41   delegate_to_pending_id_map_[delegate] = FetchNotificationResources( | 48 | 
| 42       notification_data, | 49   if (!notification_data.icon.isEmpty()) { | 
| 43       callback, | 50     delegate_to_pending_id_map_[delegate] = FetchNotificationIcon( | 
| 44       new NotificationImageLoader( | 51     notification_data, | 
| 45           base::Bind( | 52     callback, | 
| 46               &PendingNotificationsTracker::DidFetchPageNotification, | 53     new NotificationImageLoader( | 
| 47               weak_factory_.GetWeakPtr(), delegate), | 54         base::Bind( | 
| 48           base::ThreadTaskRunnerHandle::Get())); | 55             &PendingNotificationsTracker::DidFetchPageNotificationIcon, | 
|  | 56             weak_factory_.GetWeakPtr(), delegate), | 
|  | 57         base::ThreadTaskRunnerHandle::Get())); | 
|  | 58   } | 
|  | 59   else if (!notification_data.sound.isEmpty()) { | 
|  | 60     delegate_to_pending_id_map_[delegate] = FetchNotificationSound( | 
|  | 61     notification_data, | 
|  | 62     callback, | 
|  | 63     new NotificationSoundLoader( | 
|  | 64         base::Bind( | 
|  | 65             &PendingNotificationsTracker::DidFetchPageNotificationSound, | 
|  | 66             weak_factory_.GetWeakPtr(), delegate), | 
|  | 67         base::ThreadTaskRunnerHandle::Get())); | 
|  | 68   } | 
| 49 } | 69 } | 
| 50 | 70 | 
| 51 void PendingNotificationsTracker::FetchPersistentNotificationResources( | 71 void PendingNotificationsTracker::FetchPersistentNotificationResources( | 
| 52     const blink::WebNotificationData& notification_data, | 72     const blink::WebNotificationData& notification_data, | 
| 53     const NotificationResourcesFetchedCallback& callback) { | 73     const NotificationResourcesFetchedCallback& callback) { | 
| 54   FetchNotificationResources( | 74   if (!notification_data.icon.isEmpty()) { | 
| 55       notification_data, | 75     FetchNotificationIcon( | 
| 56       callback, | 76     notification_data, | 
| 57       new NotificationImageLoader( | 77     callback, | 
| 58           base::Bind( | 78     new NotificationImageLoader( | 
| 59               &PendingNotificationsTracker::DidFetchPersistentNotification, | 79         base::Bind( | 
| 60               weak_factory_.GetWeakPtr()), | 80             &PendingNotificationsTracker::DidFetchPersistentNotificationIcon, | 
| 61           base::ThreadTaskRunnerHandle::Get())); | 81             weak_factory_.GetWeakPtr()), | 
|  | 82         base::ThreadTaskRunnerHandle::Get())); | 
|  | 83   } | 
|  | 84   else if (!notification_data.sound.isEmpty()) { | 
|  | 85     FetchNotificationSound( | 
|  | 86     notification_data, | 
|  | 87     callback, | 
|  | 88     new NotificationSoundLoader( | 
|  | 89         base::Bind( | 
|  | 90             &PendingNotificationsTracker::DidFetchPersistentNotificationSound, | 
|  | 91             weak_factory_.GetWeakPtr()), | 
|  | 92         base::ThreadTaskRunnerHandle::Get())); | 
|  | 93   } | 
| 62 } | 94 } | 
| 63 | 95 | 
| 64 bool PendingNotificationsTracker::CancelPageNotificationFetches( | 96 bool PendingNotificationsTracker::CancelPageNotificationFetches( | 
| 65     blink::WebNotificationDelegate* delegate) { | 97     blink::WebNotificationDelegate* delegate) { | 
| 66   auto iter = delegate_to_pending_id_map_.find(delegate); | 98   auto iter = delegate_to_pending_id_map_.find(delegate); | 
| 67   if (iter == delegate_to_pending_id_map_.end()) | 99   if (iter == delegate_to_pending_id_map_.end()) | 
| 68     return false; | 100     return false; | 
| 69 | 101 | 
| 70   pending_notifications_.Remove(iter->second); | 102   pending_notifications_.Remove(iter->second); | 
| 71   delegate_to_pending_id_map_.erase(iter); | 103   delegate_to_pending_id_map_.erase(iter); | 
| 72 | 104 | 
| 73   return true; | 105   return true; | 
| 74 } | 106 } | 
| 75 | 107 | 
| 76 void PendingNotificationsTracker::DidFetchPageNotification( | 108 void PendingNotificationsTracker::DidFetchPageNotificationIcon( | 
| 77     blink::WebNotificationDelegate* delegate, | 109     blink::WebNotificationDelegate* delegate, | 
| 78     int notification_id, | 110     int notification_id, | 
| 79     const SkBitmap& icon) { | 111     const SkBitmap& icon) { | 
|  | 112 | 
| 80   PendingNotification* pending_notification = | 113   PendingNotification* pending_notification = | 
| 81       pending_notifications_.Lookup(notification_id); | 114       pending_notifications_.Lookup(notification_id); | 
| 82   DCHECK(pending_notification); | 115   DCHECK(pending_notification); | 
| 83 | 116 | 
| 84   pending_notification->callback.Run(icon); | 117   if (!pending_notification->notification.sound.isEmpty()) { | 
|  | 118 // Need to check | 
|  | 119 #if 0 | 
|  | 120     pending_notification->sound_loader = new NotificationSoundLoader( | 
|  | 121         base::Bind( | 
|  | 122             &PendingNotificationsTracker::DidFetchPageNotificationSound, | 
|  | 123             weak_factory_.GetWeakPtr(), delegate), | 
|  | 124         base::ThreadTaskRunnerHandle::Get()); | 
|  | 125 | 
|  | 126     main_thread_task_runner_->PostTask( | 
|  | 127         FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, | 
|  | 128               pending_notification->sound_loader, notification_id, | 
|  | 129               GURL(pending_notification->notification.sound.spec()))); | 
|  | 130 #endif | 
|  | 131   } | 
|  | 132   else { | 
|  | 133     pending_notification->callback.Run(icon); | 
|  | 134 | 
|  | 135     delegate_to_pending_id_map_.erase(delegate); | 
|  | 136     pending_notifications_.Remove(notification_id); | 
|  | 137   } | 
|  | 138 } | 
|  | 139 | 
|  | 140 void PendingNotificationsTracker::DidFetchPageNotificationSound( | 
|  | 141     blink::WebNotificationDelegate* delegate, | 
|  | 142     int notification_id, | 
|  | 143     const SkBitmap& sound) { | 
|  | 144   PendingNotification* pending_notification = | 
|  | 145       pending_notifications_.Lookup(notification_id); | 
|  | 146   DCHECK(pending_notification); | 
|  | 147 | 
|  | 148   pending_notification->callback.Run(sound); | 
| 85 | 149 | 
| 86   delegate_to_pending_id_map_.erase(delegate); | 150   delegate_to_pending_id_map_.erase(delegate); | 
| 87   pending_notifications_.Remove(notification_id); | 151   pending_notifications_.Remove(notification_id); | 
| 88 } | 152 } | 
| 89 | 153 | 
| 90 void PendingNotificationsTracker::DidFetchPersistentNotification( | 154 void PendingNotificationsTracker::DidFetchPersistentNotificationIcon( | 
| 91     int notification_id, const SkBitmap& icon) { | 155     int notification_id, const SkBitmap& icon) { | 
| 92   PendingNotification* pending_notification = | 156   PendingNotification* pending_notification = | 
| 93       pending_notifications_.Lookup(notification_id); | 157       pending_notifications_.Lookup(notification_id); | 
| 94   DCHECK(pending_notification); | 158   DCHECK(pending_notification); | 
| 95 | 159 | 
| 96   pending_notification->callback.Run(icon); | 160   if (!pending_notification->notification.sound.isEmpty()) { | 
|  | 161 // Need to Check | 
|  | 162 #if 0 | 
|  | 163     pending_notification->sound_loader(new NotificationSoundLoader( | 
|  | 164         base::Bind( | 
|  | 165             &PendingNotificationsTracker::DidFetchPersistentNotificationSound, | 
|  | 166             weak_factory_.GetWeakPtr()), | 
|  | 167         base::ThreadTaskRunnerHandle::Get())); | 
| 97 | 168 | 
|  | 169     main_thread_task_runner_->PostTask( | 
|  | 170         FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, | 
|  | 171               pending_notification->sound_loader, notification_id, | 
|  | 172               GURL(pending_notification->notification.sound.spec()))); | 
|  | 173 #endif | 
|  | 174   } | 
|  | 175   else { | 
|  | 176     pending_notification->callback.Run(icon); | 
|  | 177     pending_notifications_.Remove(notification_id); | 
|  | 178   } | 
|  | 179 } | 
|  | 180 | 
|  | 181 void PendingNotificationsTracker::DidFetchPersistentNotificationSound( | 
|  | 182     int notification_id, const SkBitmap& sound) { | 
|  | 183   PendingNotification* pending_notification = | 
|  | 184       pending_notifications_.Lookup(notification_id); | 
|  | 185   DCHECK(pending_notification); | 
|  | 186 | 
|  | 187   pending_notification->callback.Run(sound); | 
| 98   pending_notifications_.Remove(notification_id); | 188   pending_notifications_.Remove(notification_id); | 
| 99 } | 189 } | 
| 100 | 190 | 
| 101 int PendingNotificationsTracker::FetchNotificationResources( | 191 int PendingNotificationsTracker::FetchNotificationIcon( | 
| 102     const blink::WebNotificationData& notification_data, | 192     const blink::WebNotificationData& notification_data, | 
| 103     const NotificationResourcesFetchedCallback& callback, | 193     const NotificationResourcesFetchedCallback& callback, | 
| 104     const scoped_refptr<NotificationImageLoader>& image_loader) { | 194     const scoped_refptr<NotificationImageLoader>& image_loader) { | 
| 105   int notification_id = pending_notifications_.Add( | 195   int notification_id = pending_notifications_.Add( | 
| 106       new PendingNotification(image_loader, callback)); | 196       new PendingNotification( | 
|  | 197           notification_data, image_loader, nullptr, callback)); | 
| 107 | 198 | 
| 108   main_thread_task_runner_->PostTask( | 199   main_thread_task_runner_->PostTask( | 
| 109       FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, | 200       FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, | 
| 110                             image_loader, notification_id, | 201                             image_loader, notification_id, | 
| 111                             GURL(notification_data.icon.spec()))); | 202                             GURL(notification_data.icon.spec()))); | 
| 112 | 203 | 
| 113   return notification_id; | 204   return notification_id; | 
| 114 } | 205 } | 
| 115 | 206 | 
|  | 207 int PendingNotificationsTracker::FetchNotificationSound( | 
|  | 208     const blink::WebNotificationData& notification_data, | 
|  | 209     const NotificationResourcesFetchedCallback& callback, | 
|  | 210     const scoped_refptr<NotificationSoundLoader>& sound_loader) { | 
|  | 211   int notification_id = pending_notifications_.Add( | 
|  | 212       new PendingNotification( | 
|  | 213           notification_data, nullptr, sound_loader, callback)); | 
|  | 214 | 
|  | 215   main_thread_task_runner_->PostTask( | 
|  | 216       FROM_HERE, base::Bind(&NotificationSoundLoader::StartOnMainThread, | 
|  | 217                             sound_loader, notification_id, | 
|  | 218                             GURL(notification_data.sound.spec()))); | 
|  | 219 | 
|  | 220   return notification_id; | 
|  | 221 } | 
| 116 }  // namespace content | 222 }  // namespace content | 
| OLD | NEW | 
|---|