Index: content/child/notifications/pending_notification.cc |
diff --git a/content/child/notifications/pending_notification.cc b/content/child/notifications/pending_notification.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..851f282bd41ceea5a9682620855af4938cb2cfd0 |
--- /dev/null |
+++ b/content/child/notifications/pending_notification.cc |
@@ -0,0 +1,80 @@ |
+// Copyright 2016 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.h" |
+ |
+#include "base/barrier_closure.h" |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/location.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "content/public/common/notification_resources.h" |
+#include "url/gurl.h" |
+ |
+namespace content { |
+ |
+PendingNotification::PendingNotification( |
+ scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
+ const blink::WebNotificationData& notification_data, |
+ const base::Closure& fetches_finished_callback) |
+ : main_thread_task_runner_(main_thread_task_runner), |
+ notification_data_(notification_data), |
+ action_icons_(std::vector<SkBitmap>(notification_data_.actions.size())), |
+ fetches_finished_barrier_closure_(BarrierClosure( |
+ 1 /* notification icon */ + notification_data_.actions.size(), |
+ fetches_finished_callback)), |
+ weak_factory_(this) {} |
+ |
+void PendingNotification::FetchResources() { |
+ // TODO(mvanouwerkerk): Add a timeout mechanism: crbug.com/579137 |
+ FetchImageResource(notification_data_.icon, |
+ base::Bind(&PendingNotification::DidFetchNotificationIcon, |
+ weak_factory_.GetWeakPtr())); |
+ for (size_t i = 0; i < notification_data_.actions.size(); i++) { |
+ FetchImageResource(notification_data_.actions[i].icon, |
+ base::Bind(&PendingNotification::DidFetchActionIcon, |
+ weak_factory_.GetWeakPtr(), i)); |
+ } |
+} |
+ |
+NotificationResources PendingNotification::GetResources() { |
+ NotificationResources resources; |
+ resources.notification_icon = notification_icon_; |
+ resources.action_icons = action_icons_; |
+ return resources; |
+} |
+ |
+void PendingNotification::FetchImageResource( |
+ const blink::WebURL& image_web_url, |
+ const ImageLoadCompletedCallback& image_callback) { |
+ if (image_web_url.isEmpty()) { |
+ image_callback.Run(SkBitmap()); |
+ return; |
+ } |
+ |
+ // Convert to GURL before sending to the main thread (crbug.com/458640). |
+ GURL image_gurl(image_web_url); |
+ |
+ scoped_refptr<NotificationImageLoader> image_loader( |
+ new NotificationImageLoader(image_callback, |
+ base::ThreadTaskRunnerHandle::Get())); |
+ image_loaders_.insert(image_loader); |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, |
+ image_loader, image_web_url)); |
+} |
+ |
+void PendingNotification::DidFetchNotificationIcon( |
+ const SkBitmap& notification_icon) { |
+ notification_icon_ = notification_icon; |
+ fetches_finished_barrier_closure_.Run(); |
+} |
+ |
+void PendingNotification::DidFetchActionIcon(int action_index, |
+ const SkBitmap& action_icon) { |
+ action_icons_[action_index] = action_icon; |
+ fetches_finished_barrier_closure_.Run(); |
+} |
+ |
+} // namespace content |