| 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 "modules/notifications/NotificationResourcesLoader.h" | 5 #include "modules/notifications/NotificationResourcesLoader.h" |
| 6 | 6 |
| 7 #include "platform/bitmap_type_converters.h" |
| 7 #include "platform/weborigin/KURL.h" | 8 #include "platform/weborigin/KURL.h" |
| 8 #include "public/platform/modules/notifications/WebNotificationConstants.h" | 9 #include "public/platform/modules/notifications/WebNotificationConstants.h" |
| 9 #include "public/platform/modules/notifications/WebNotificationData.h" | |
| 10 #include "public/platform/modules/notifications/WebNotificationResources.h" | |
| 11 #include "skia/ext/image_operations.h" | 10 #include "skia/ext/image_operations.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 | 12 |
| 14 namespace blink { | 13 namespace blink { |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 // Scales down |image| to fit within |maxSizePx| if its width or height is | 17 // Scales down |image| to fit within |maxSizePx| if its width or height is |
| 19 // larger than |maxSizePx| and returns the result. Otherwise does nothing and | 18 // larger than |maxSizePx| and returns the result. Otherwise does nothing and |
| 20 // returns |image| unchanged. | 19 // returns |image| unchanged. |
| 21 // TODO(mvanouwerkerk): Explore doing the scaling on a background thread. | 20 // TODO(mvanouwerkerk): Explore doing the scaling on a background thread. |
| 22 SkBitmap scaleDownIfNeeded(const SkBitmap& image, int maxSizePx) | 21 SkBitmap scaleDownIfNeeded(const SkBitmap& image, int maxSizePx) |
| 23 { | 22 { |
| 24 if (image.width() > maxSizePx || image.height() > maxSizePx) | 23 if (image.width() > maxSizePx || image.height() > maxSizePx) |
| 25 return skia::ImageOperations::Resize(image, skia::ImageOperations::RESIZ
E_BEST, std::min(image.width(), maxSizePx), std::min(image.height(), maxSizePx))
; | 24 return skia::ImageOperations::Resize(image, skia::ImageOperations::RESIZ
E_BEST, std::min(image.width(), maxSizePx), std::min(image.height(), maxSizePx))
; |
| 26 return image; | 25 return image; |
| 27 } | 26 } |
| 28 | 27 |
| 29 } // namespace | 28 } // namespace |
| 30 | 29 |
| 31 NotificationResourcesLoader::NotificationResourcesLoader(PassOwnPtr<CompletionCa
llback> completionCallback) | 30 NotificationResourcesLoader::NotificationResourcesLoader(PassOwnPtr<CompletionCa
llback> completionCallback) |
| 32 : m_started(false), m_completionCallback(std::move(completionCallback)), m_p
endingRequestCount(0) | 31 : m_completionCallback(std::move(completionCallback)), m_pendingRequestCount
(0) |
| 33 { | 32 { |
| 34 ThreadState::current()->registerPreFinalizer(this); | 33 ThreadState::current()->registerPreFinalizer(this); |
| 35 DCHECK(m_completionCallback); | 34 DCHECK(m_completionCallback); |
| 36 } | 35 } |
| 37 | 36 |
| 38 NotificationResourcesLoader::~NotificationResourcesLoader() | 37 NotificationResourcesLoader::~NotificationResourcesLoader() |
| 39 { | 38 { |
| 40 } | 39 } |
| 41 | 40 |
| 42 void NotificationResourcesLoader::start(ExecutionContext* executionContext, cons
t WebNotificationData& notificationData) | 41 void NotificationResourcesLoader::start(ExecutionContext* executionContext, mojo
m::blink::NotificationPtr notification) |
| 43 { | 42 { |
| 44 DCHECK(!m_started); | 43 DCHECK(m_notification.is_null()); |
| 45 m_started = true; | 44 DCHECK(!notification.is_null()); |
| 46 | 45 |
| 47 size_t numActions = notificationData.actions.size(); | 46 m_notification = std::move(notification); |
| 47 |
| 48 size_t numActions = m_notification->actions.size(); |
| 48 m_pendingRequestCount = 2 /* icon and badge */ + numActions; | 49 m_pendingRequestCount = 2 /* icon and badge */ + numActions; |
| 49 | 50 |
| 50 loadImage(executionContext, notificationData.icon, bind<const SkBitmap&>(&No
tificationResourcesLoader::didLoadIcon, WeakPersistentThisPointer<NotificationRe
sourcesLoader>(this))); | 51 loadImage(executionContext, KURL(ParsedURLString, m_notification->icon), bin
d<const SkBitmap&>(&NotificationResourcesLoader::didLoadIcon, WeakPersistentThis
Pointer<NotificationResourcesLoader>(this))); |
| 51 loadImage(executionContext, notificationData.badge, bind<const SkBitmap&>(&N
otificationResourcesLoader::didLoadBadge, WeakPersistentThisPointer<Notification
ResourcesLoader>(this))); | 52 loadImage(executionContext, KURL(ParsedURLString, m_notification->badge), bi
nd<const SkBitmap&>(&NotificationResourcesLoader::didLoadBadge, WeakPersistentTh
isPointer<NotificationResourcesLoader>(this))); |
| 52 | 53 |
| 53 m_actionIcons.resize(numActions); | 54 m_actionIcons.resize(numActions); |
| 54 for (size_t i = 0; i < numActions; i++) | 55 for (size_t i = 0; i < numActions; i++) |
| 55 loadImage(executionContext, notificationData.actions[i].icon, bind<const
SkBitmap&>(&NotificationResourcesLoader::didLoadActionIcon, WeakPersistentThisP
ointer<NotificationResourcesLoader>(this), i)); | 56 loadImage(executionContext, KURL(ParsedURLString, m_notification->action
s[i]->icon), bind<const SkBitmap&>(&NotificationResourcesLoader::didLoadActionIc
on, WeakPersistentThisPointer<NotificationResourcesLoader>(this), i)); |
| 56 } | 57 } |
| 57 | 58 |
| 58 std::unique_ptr<WebNotificationResources> NotificationResourcesLoader::getResour
ces() const | 59 mojom::blink::NotificationResourcesPtr NotificationResourcesLoader::getResources
() const |
| 59 { | 60 { |
| 60 std::unique_ptr<WebNotificationResources> resources(new WebNotificationResou
rces()); | 61 mojom::blink::NotificationResourcesPtr resources = mojom::blink::Notificatio
nResources::New(); |
| 61 resources->icon = m_icon; | 62 resources->icon = mojom::blink::Bitmap::From(m_icon); |
| 62 resources->badge = m_badge; | 63 resources->badge = mojom::blink::Bitmap::From(m_badge); |
| 63 resources->actionIcons = m_actionIcons; | 64 |
| 65 resources->action_icons.resize(m_actionIcons.size()); |
| 66 for (size_t i = 0; i < m_actionIcons.size(); i++) |
| 67 resources->action_icons[i] = mojom::blink::Bitmap::From(m_actionIcons[i]
); |
| 68 |
| 64 return resources; | 69 return resources; |
| 65 } | 70 } |
| 66 | 71 |
| 67 void NotificationResourcesLoader::stop() | 72 void NotificationResourcesLoader::stop() |
| 68 { | 73 { |
| 69 for (auto imageLoader : m_imageLoaders) | 74 for (auto imageLoader : m_imageLoaders) |
| 70 imageLoader->stop(); | 75 imageLoader->stop(); |
| 71 } | 76 } |
| 72 | 77 |
| 73 DEFINE_TRACE(NotificationResourcesLoader) | 78 DEFINE_TRACE(NotificationResourcesLoader) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 m_actionIcons[actionIndex] = scaleDownIfNeeded(image, kWebNotificationMaxAct
ionIconSizePx); | 111 m_actionIcons[actionIndex] = scaleDownIfNeeded(image, kWebNotificationMaxAct
ionIconSizePx); |
| 107 didFinishRequest(); | 112 didFinishRequest(); |
| 108 } | 113 } |
| 109 | 114 |
| 110 void NotificationResourcesLoader::didFinishRequest() | 115 void NotificationResourcesLoader::didFinishRequest() |
| 111 { | 116 { |
| 112 DCHECK_GT(m_pendingRequestCount, 0); | 117 DCHECK_GT(m_pendingRequestCount, 0); |
| 113 m_pendingRequestCount--; | 118 m_pendingRequestCount--; |
| 114 if (!m_pendingRequestCount) { | 119 if (!m_pendingRequestCount) { |
| 115 stop(); | 120 stop(); |
| 116 (*m_completionCallback)(this); | 121 |
| 122 (*m_completionCallback)(this, std::move(m_notification)); |
| 117 // The |this| pointer may have been deleted now. | 123 // The |this| pointer may have been deleted now. |
| 118 } | 124 } |
| 119 } | 125 } |
| 120 | 126 |
| 121 } // namespace blink | 127 } // namespace blink |
| OLD | NEW |