OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_notification.h" |
| 6 |
| 7 #include "base/barrier_closure.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/location.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "content/public/common/notification_resources.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 PendingNotification::PendingNotification( |
| 18 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
| 19 const blink::WebNotificationData& notification_data, |
| 20 const base::Closure& fetches_finished_callback) |
| 21 : main_thread_task_runner_(main_thread_task_runner), |
| 22 notification_data_(notification_data), |
| 23 action_icons_(std::vector<SkBitmap>(notification_data_.actions.size())), |
| 24 fetches_finished_barrier_closure_(BarrierClosure( |
| 25 1 /* notification icon */ + notification_data_.actions.size(), |
| 26 fetches_finished_callback)), |
| 27 weak_factory_(this) {} |
| 28 |
| 29 void PendingNotification::FetchResources() { |
| 30 // TODO(mvanouwerkerk): Add a timeout mechanism: crbug.com/579137 |
| 31 FetchImageResource(notification_data_.icon, |
| 32 base::Bind(&PendingNotification::DidFetchNotificationIcon, |
| 33 weak_factory_.GetWeakPtr())); |
| 34 for (size_t i = 0; i < notification_data_.actions.size(); i++) { |
| 35 FetchImageResource(notification_data_.actions[i].icon, |
| 36 base::Bind(&PendingNotification::DidFetchActionIcon, |
| 37 weak_factory_.GetWeakPtr(), i)); |
| 38 } |
| 39 } |
| 40 |
| 41 NotificationResources PendingNotification::GetResources() { |
| 42 NotificationResources resources; |
| 43 resources.notification_icon = notification_icon_; |
| 44 resources.action_icons = action_icons_; |
| 45 return resources; |
| 46 } |
| 47 |
| 48 void PendingNotification::FetchImageResource( |
| 49 const blink::WebURL& image_web_url, |
| 50 const ImageLoadCompletedCallback& image_callback) { |
| 51 if (image_web_url.isEmpty()) { |
| 52 image_callback.Run(SkBitmap()); |
| 53 return; |
| 54 } |
| 55 |
| 56 // Convert to GURL before sending to the main thread (crbug.com/458640). |
| 57 GURL image_gurl(image_web_url); |
| 58 |
| 59 scoped_refptr<NotificationImageLoader> image_loader( |
| 60 new NotificationImageLoader(image_callback, |
| 61 base::ThreadTaskRunnerHandle::Get())); |
| 62 image_loaders_.insert(image_loader); |
| 63 main_thread_task_runner_->PostTask( |
| 64 FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, |
| 65 image_loader, image_web_url)); |
| 66 } |
| 67 |
| 68 void PendingNotification::DidFetchNotificationIcon( |
| 69 const SkBitmap& notification_icon) { |
| 70 notification_icon_ = notification_icon; |
| 71 fetches_finished_barrier_closure_.Run(); |
| 72 } |
| 73 |
| 74 void PendingNotification::DidFetchActionIcon(int action_index, |
| 75 const SkBitmap& action_icon) { |
| 76 action_icons_[action_index] = action_icon; |
| 77 fetches_finished_barrier_closure_.Run(); |
| 78 } |
| 79 |
| 80 } // namespace content |
OLD | NEW |