Index: content/child/notifications/pending_notification_tracker.cc |
diff --git a/content/child/notifications/pending_notification_tracker.cc b/content/child/notifications/pending_notification_tracker.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..898d908837a894d55763c0c95b88c322f95422bd |
--- /dev/null |
+++ b/content/child/notifications/pending_notification_tracker.cc |
@@ -0,0 +1,157 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/child/notifications/pending_notification_tracker.h" |
+ |
+#include "base/bind.h" |
+#include "base/location.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "content/child/notifications/notification_image_loader.h" |
+#include "content/child/notifications/notification_manager.h" |
+#include "third_party/WebKit/public/platform/WebSerializedOrigin.h" |
+#include "third_party/WebKit/public/platform/modules/notifications/WebNotificationData.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+ |
+namespace content { |
+ |
+// Stores the information associated with a pending page notification. |
+struct PendingNotificationTracker::PendingPageNotification { |
+ PendingPageNotification( |
+ const scoped_refptr<NotificationImageLoader> image_loader, |
+ const blink::WebSerializedOrigin& origin, |
+ const blink::WebNotificationData& notification_data, |
+ blink::WebNotificationDelegate* delegate) |
+ : image_loader(image_loader), |
+ origin(origin), |
+ notification_data(notification_data), |
+ delegate(delegate) {} |
+ |
+ scoped_refptr<NotificationImageLoader> image_loader; |
+ blink::WebSerializedOrigin origin; |
+ blink::WebNotificationData notification_data; |
+ blink::WebNotificationDelegate* delegate; |
+}; |
+ |
+// Stores the information associated with a pending persistent notification. |
+struct PendingNotificationTracker::PendingPersistentNotification { |
+ PendingPersistentNotification( |
+ const scoped_refptr<NotificationImageLoader> image_loader, |
+ const blink::WebSerializedOrigin& origin, |
+ const blink::WebNotificationData& notification_data, |
+ int64 service_worker_registration_id, |
+ scoped_ptr<blink::WebNotificationShowCallbacks> callbacks) |
+ : image_loader(image_loader), |
+ origin(origin), |
+ notification_data(notification_data), |
+ service_worker_registration_id(service_worker_registration_id), |
+ callbacks(callbacks.Pass()) {} |
+ |
+ scoped_refptr<NotificationImageLoader> image_loader; |
+ blink::WebSerializedOrigin origin; |
+ blink::WebNotificationData notification_data; |
+ int64 service_worker_registration_id; |
+ scoped_ptr<blink::WebNotificationShowCallbacks> callbacks; |
+}; |
+ |
+PendingNotificationTracker::PendingNotificationTracker( |
+ NotificationManager* manager, |
+ scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) |
+ : manager_(manager), |
+ main_thread_task_runner_(main_thread_task_runner), |
+ weak_factory_(this) {} |
+ |
+PendingNotificationTracker::~PendingNotificationTracker() {} |
+ |
+void PendingNotificationTracker::FetchPageNotification( |
+ const blink::WebSerializedOrigin& origin, |
+ const blink::WebNotificationData& notification_data, |
+ blink::WebNotificationDelegate* delegate) { |
+ scoped_refptr<NotificationImageLoader> image_loader( |
+ new NotificationImageLoader( |
+ base::Bind(&PendingNotificationTracker::DidFetchPageNotification, |
+ weak_factory_.GetWeakPtr()), |
+ base::ThreadTaskRunnerHandle::Get())); |
+ |
+ int notification_id = pending_page_notifications_.Add( |
+ new PendingPageNotification(image_loader, |
+ origin, |
+ notification_data, |
+ delegate)); |
+ |
+ delegate_to_pending_id_map_[delegate] = notification_id; |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, |
+ image_loader, notification_id, |
+ notification_data.icon)); |
+} |
+ |
+void PendingNotificationTracker::FetchPersistentNotification( |
+ const blink::WebSerializedOrigin& origin, |
+ const blink::WebNotificationData& notification_data, |
+ int64 service_worker_registration_id, |
+ scoped_ptr<blink::WebNotificationShowCallbacks> callbacks) { |
+ scoped_refptr<NotificationImageLoader> image_loader( |
+ new NotificationImageLoader( |
+ base::Bind( |
+ &PendingNotificationTracker::DidFetchPersistentNotification, |
+ weak_factory_.GetWeakPtr()), |
+ base::ThreadTaskRunnerHandle::Get())); |
+ |
+ int notification_id = pending_persistent_notifications_.Add( |
+ new PendingPersistentNotification(image_loader, |
+ origin, |
+ notification_data, |
+ service_worker_registration_id, |
+ callbacks.Pass())); |
+ |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, |
+ image_loader, notification_id, |
+ notification_data.icon)); |
+} |
+ |
+bool PendingNotificationTracker::CancelPageNotificationFetches( |
+ blink::WebNotificationDelegate* delegate) { |
+ auto iter = delegate_to_pending_id_map_.find(delegate); |
+ if (iter == delegate_to_pending_id_map_.end()) |
+ return false; |
+ |
+ pending_page_notifications_.Remove(iter->second); |
+ delegate_to_pending_id_map_.erase(iter); |
+ |
+ return true; |
+} |
+ |
+void PendingNotificationTracker::DidFetchPageNotification( |
+ int notification_id, const SkBitmap& icon) { |
+ PendingPageNotification* notification = |
+ pending_page_notifications_.Lookup(notification_id); |
+ DCHECK(notification); |
mlamouri (slow - plz ping)
2015/02/18 14:53:58
Could you add a test for that to try the situation
Peter Beverloo
2015/02/18 15:11:50
I've included serviceworkerregistration-service-wo
|
+ |
+ manager_->DisplayPageNotification( |
+ notification->origin, |
+ notification->notification_data, |
+ notification->delegate, |
+ icon); |
+ |
+ pending_page_notifications_.Remove(notification_id); |
+} |
+ |
+void PendingNotificationTracker::DidFetchPersistentNotification( |
+ int notification_id, const SkBitmap& icon) { |
+ PendingPersistentNotification* notification = |
+ pending_persistent_notifications_.Lookup(notification_id); |
+ DCHECK(notification); |
+ |
+ manager_->DisplayPersistentNotification( |
+ notification->origin, |
+ notification->notification_data, |
+ notification->service_worker_registration_id, |
+ notification->callbacks.Pass(), |
+ icon); |
+ |
+ pending_persistent_notifications_.Remove(notification_id); |
+} |
+ |
+} // namespace content |