| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_notification.h" | 5 #include "content/child/notifications/pending_notification.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 PendingNotification::~PendingNotification() {} | 44 PendingNotification::~PendingNotification() {} |
| 45 | 45 |
| 46 void PendingNotification::FetchResources( | 46 void PendingNotification::FetchResources( |
| 47 const blink::WebNotificationData& notification_data, | 47 const blink::WebNotificationData& notification_data, |
| 48 const base::Closure& fetches_finished_callback) { | 48 const base::Closure& fetches_finished_callback) { |
| 49 // TODO(mvanouwerkerk): Add a timeout mechanism: crbug.com/579137 | 49 // TODO(mvanouwerkerk): Add a timeout mechanism: crbug.com/579137 |
| 50 | 50 |
| 51 size_t num_actions = notification_data.actions.size(); | 51 size_t num_actions = notification_data.actions.size(); |
| 52 action_icons_.resize(num_actions); | 52 action_icons_.resize(num_actions); |
| 53 | 53 |
| 54 size_t num_closures = 1 /* notification icon */ + num_actions; | 54 size_t num_closures = 2 /* notification icon and badge */ + num_actions; |
| 55 fetches_finished_barrier_closure_ = | 55 fetches_finished_barrier_closure_ = |
| 56 base::BarrierClosure(num_closures, fetches_finished_callback); | 56 base::BarrierClosure(num_closures, fetches_finished_callback); |
| 57 | 57 |
| 58 FetchImageResource(notification_data.icon, | 58 FetchImageResource(notification_data.icon, |
| 59 base::Bind(&PendingNotification::DidFetchNotificationIcon, | 59 base::Bind(&PendingNotification::DidFetchNotificationIcon, |
| 60 weak_factory_.GetWeakPtr())); | 60 weak_factory_.GetWeakPtr())); |
| 61 FetchImageResource(notification_data.badge, |
| 62 base::Bind(&PendingNotification::DidFetchBadge, |
| 63 weak_factory_.GetWeakPtr())); |
| 61 for (size_t i = 0; i < num_actions; i++) { | 64 for (size_t i = 0; i < num_actions; i++) { |
| 62 FetchImageResource(notification_data.actions[i].icon, | 65 FetchImageResource(notification_data.actions[i].icon, |
| 63 base::Bind(&PendingNotification::DidFetchActionIcon, | 66 base::Bind(&PendingNotification::DidFetchActionIcon, |
| 64 weak_factory_.GetWeakPtr(), i)); | 67 weak_factory_.GetWeakPtr(), i)); |
| 65 } | 68 } |
| 66 } | 69 } |
| 67 | 70 |
| 68 NotificationResources PendingNotification::GetResources() const { | 71 NotificationResources PendingNotification::GetResources() const { |
| 69 NotificationResources resources; | 72 NotificationResources resources; |
| 70 resources.notification_icon = notification_icon_; | 73 resources.notification_icon = notification_icon_; |
| 74 resources.badge = badge_; |
| 71 resources.action_icons = action_icons_; | 75 resources.action_icons = action_icons_; |
| 72 return resources; | 76 return resources; |
| 73 } | 77 } |
| 74 | 78 |
| 75 void PendingNotification::FetchImageResource( | 79 void PendingNotification::FetchImageResource( |
| 76 const blink::WebURL& image_web_url, | 80 const blink::WebURL& image_web_url, |
| 77 const ImageLoadCompletedCallback& image_callback) { | 81 const ImageLoadCompletedCallback& image_callback) { |
| 78 if (image_web_url.isEmpty()) { | 82 if (image_web_url.isEmpty()) { |
| 79 image_callback.Run(SkBitmap()); | 83 image_callback.Run(SkBitmap()); |
| 80 return; | 84 return; |
| 81 } | 85 } |
| 82 | 86 |
| 83 // Explicitly convert the WebURL to a GURL before passing it to a different | 87 // Explicitly convert the WebURL to a GURL before passing it to a different |
| 84 // thread. This is important because WebURLs must not be passed between | 88 // thread. This is important because WebURLs must not be passed between |
| 85 // threads, and per base::Bind() semantics conversion would otherwise be done | 89 // threads, and per base::Bind() semantics conversion would otherwise be done |
| 86 // on the receiving thread. | 90 // on the receiving thread. |
| 87 GURL image_gurl(image_web_url); | 91 GURL image_gurl(image_web_url); |
| 88 | 92 |
| 89 scoped_refptr<NotificationImageLoader> image_loader( | 93 scoped_refptr<NotificationImageLoader> image_loader( |
| 90 new NotificationImageLoader(image_callback, | 94 new NotificationImageLoader(image_callback, |
| 91 base::ThreadTaskRunnerHandle::Get(), | 95 base::ThreadTaskRunnerHandle::Get(), |
| 92 main_task_runner_)); | 96 main_task_runner_)); |
| 93 image_loaders_.push_back(image_loader); | 97 image_loaders_.push_back(image_loader); |
| 94 main_task_runner_->PostTask( | 98 main_task_runner_->PostTask( |
| 95 FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, | 99 FROM_HERE, base::Bind(&NotificationImageLoader::StartOnMainThread, |
| 96 image_loader, image_gurl)); | 100 image_loader, image_gurl)); |
| 97 } | 101 } |
| 98 | 102 |
| 99 void PendingNotification::DidFetchNotificationIcon(const SkBitmap& icon) { | 103 void PendingNotification::DidFetchNotificationIcon(const SkBitmap& bitmap) { |
| 100 notification_icon_ = | 104 notification_icon_ = |
| 101 ScaleDownIfNeeded(icon, kPlatformNotificationMaxIconSizePx); | 105 ScaleDownIfNeeded(bitmap, kPlatformNotificationMaxIconSizePx); |
| 106 fetches_finished_barrier_closure_.Run(); |
| 107 } |
| 108 |
| 109 void PendingNotification::DidFetchBadge(const SkBitmap& bitmap) { |
| 110 badge_ = ScaleDownIfNeeded(bitmap, kPlatformNotificationMaxBadgeSizePx); |
| 102 fetches_finished_barrier_closure_.Run(); | 111 fetches_finished_barrier_closure_.Run(); |
| 103 } | 112 } |
| 104 | 113 |
| 105 void PendingNotification::DidFetchActionIcon(size_t action_index, | 114 void PendingNotification::DidFetchActionIcon(size_t action_index, |
| 106 const SkBitmap& icon) { | 115 const SkBitmap& bitmap) { |
| 107 DCHECK_LT(action_index, action_icons_.size()); | 116 DCHECK_LT(action_index, action_icons_.size()); |
| 108 | 117 |
| 109 action_icons_[action_index] = | 118 action_icons_[action_index] = |
| 110 ScaleDownIfNeeded(icon, kPlatformNotificationMaxActionIconSizePx); | 119 ScaleDownIfNeeded(bitmap, kPlatformNotificationMaxActionIconSizePx); |
| 111 fetches_finished_barrier_closure_.Run(); | 120 fetches_finished_barrier_closure_.Run(); |
| 112 } | 121 } |
| 113 | 122 |
| 114 } // namespace content | 123 } // namespace content |
| OLD | NEW |