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 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) |
| 19 : main_task_runner_(main_task_runner), weak_factory_(this) {} |
| 20 |
| 21 PendingNotification::~PendingNotification() {} |
| 22 |
| 23 void PendingNotification::FetchResources( |
| 24 const blink::WebNotificationData& notification_data, |
| 25 const base::Closure& fetches_finished_callback) { |
| 26 // TODO(mvanouwerkerk): Add a timeout mechanism: crbug.com/579137 |
| 27 |
| 28 size_t num_actions = notification_data.actions.size(); |
| 29 action_icons_.resize(num_actions); |
| 30 |
| 31 size_t num_closures = 1 /* notification icon */ + num_actions; |
| 32 fetches_finished_barrier_closure_ = |
| 33 base::BarrierClosure(num_closures, fetches_finished_callback); |
| 34 |
| 35 FetchImageResource(notification_data.icon, |
| 36 base::Bind(&PendingNotification::DidFetchNotificationIcon, |
| 37 weak_factory_.GetWeakPtr())); |
| 38 for (size_t i = 0; i < num_actions; i++) { |
| 39 FetchImageResource(notification_data.actions[i].icon, |
| 40 base::Bind(&PendingNotification::DidFetchActionIcon, |
| 41 weak_factory_.GetWeakPtr(), i)); |
| 42 } |
| 43 } |
| 44 |
| 45 NotificationResources PendingNotification::GetResources() const { |
| 46 NotificationResources resources; |
| 47 resources.notification_icon = notification_icon_; |
| 48 resources.action_icons = action_icons_; |
| 49 return resources; |
| 50 } |
| 51 |
| 52 void PendingNotification::FetchImageResource( |
| 53 const blink::WebURL& image_web_url, |
| 54 const ImageLoadCompletedCallback& image_callback) { |
| 55 if (image_web_url.isEmpty()) { |
| 56 image_callback.Run(SkBitmap()); |
| 57 return; |
| 58 } |
| 59 |
| 60 // Explicitly convert the WebURL to a GURL before passing it to a different |
| 61 // thread. This is important because WebURLs must not be passed between |
| 62 // threads, and per base::Bind() semantics conversion would otherwise be done |
| 63 // on the receiving thread. |
| 64 GURL image_gurl(image_web_url); |
| 65 |
| 66 scoped_refptr<NotificationImageLoader> image_loader( |
| 67 new NotificationImageLoader(image_callback, |
| 68 base::ThreadTaskRunnerHandle::Get(), |
| 69 main_task_runner_)); |
| 70 image_loaders_.push_back(image_loader); |
| 71 main_task_runner_->PostTask( |
| 72 FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, |
| 73 image_loader, image_gurl)); |
| 74 } |
| 75 |
| 76 void PendingNotification::DidFetchNotificationIcon( |
| 77 const SkBitmap& notification_icon) { |
| 78 notification_icon_ = notification_icon; |
| 79 fetches_finished_barrier_closure_.Run(); |
| 80 } |
| 81 |
| 82 void PendingNotification::DidFetchActionIcon(size_t action_index, |
| 83 const SkBitmap& action_icon) { |
| 84 DCHECK_LT(action_index, action_icons_.size()); |
| 85 |
| 86 action_icons_[action_index] = action_icon; |
| 87 fetches_finished_barrier_closure_.Run(); |
| 88 } |
| 89 |
| 90 } // namespace content |
OLD | NEW |