Index: third_party/WebKit/Source/modules/notifications/NotificationResourcesLoader.cpp |
diff --git a/third_party/WebKit/Source/modules/notifications/NotificationResourcesLoader.cpp b/third_party/WebKit/Source/modules/notifications/NotificationResourcesLoader.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..617ce8fe093be2c6bf9b34f1c46d13fcce3a418a |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/notifications/NotificationResourcesLoader.cpp |
@@ -0,0 +1,158 @@ |
+// 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 "modules/notifications/NotificationResourcesLoader.h" |
+ |
+#include "core/dom/ExecutionContext.h" |
+#include "public/platform/modules/notifications/WebNotificationData.h" |
+#include "public/platform/modules/notifications/WebNotificationResources.h" |
+#include "skia/ext/image_operations.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+// TODO(mvanouwerkerk): Get icon dimensions from the embedder. |
+ |
+// The maximum reasonable notification icon size, scaled from dip units to |
+// pixels using the largest supported scaling factor. |
+static const int kMaxIconSizePx = 320; // 80 dip * 4 |
+ |
+// The maximum reasonable badge size, scaled from dip units to pixels using the |
+// largest supported scaling factor. |
+static const int kMaxBadgeSizePx = 96; // 24 dip * 4 |
+ |
+// The maximum reasonable action icon size, scaled from dip units to pixels |
+// using the largest supported scaling factor. |
+static const int kMaxActionIconSizePx = 128; // 32 dip * 4 |
+ |
+// Scales down |image| to fit within |maxSizePx| if its width or height is |
+// larger than |maxSizePx| and returns the result. Otherwise does nothing and |
+// returns |image| unchanged. |
+SkBitmap scaleDownIfNeeded(const SkBitmap& image, int maxSizePx) |
+{ |
+ if (image.width() > maxSizePx || image.height() > maxSizePx) { |
+ return skia::ImageOperations::Resize(image, |
+ skia::ImageOperations::RESIZE_BEST, |
+ std::min(image.width(), maxSizePx), |
+ std::min(image.height(), maxSizePx)); |
+ } |
+ return image; |
+} |
+ |
+} // namespace |
+ |
+NotificationResourcesLoader::NotificationResourcesLoader( |
+ ExecutionContext* executionContext, |
+ PassOwnPtr<CompletionCallback> completionCallback) |
+ : ContextLifecycleObserver(executionContext) |
+ , m_completionCallback(completionCallback) |
+ , m_pendingRequestCount(0) |
+{ |
+ ThreadState::current()->registerPreFinalizer(this); |
Peter Beverloo
2016/04/13 18:32:27
DCHECK(m_completionCallback)
Michael van Ouwerkerk
2016/04/14 13:42:11
Done.
|
+} |
+ |
+NotificationResourcesLoader::~NotificationResourcesLoader() |
+{ |
+} |
+ |
+void NotificationResourcesLoader::start( |
Peter Beverloo
2016/04/13 18:32:27
micro nit: nowrap
Michael van Ouwerkerk
2016/04/14 13:42:11
Done.
|
+ const WebNotificationData& notificationData) |
+{ |
+ size_t numActions = notificationData.actions.size(); |
+ m_pendingRequestCount = 2 /* icon and badge */ + numActions; |
+ |
+ loadImage(notificationData.icon, |
+ bind<const SkBitmap&>(&NotificationResourcesLoader::didLoadIcon, |
+ WeakPersistentThisPointer<NotificationResourcesLoader>(this))); |
+ loadImage(notificationData.badge, |
+ bind<const SkBitmap&>(&NotificationResourcesLoader::didLoadBadge, |
+ WeakPersistentThisPointer<NotificationResourcesLoader>(this))); |
+ |
+ m_actionIcons.resize(numActions); |
+ for (size_t i = 0; i < numActions; i++) { |
+ loadImage(notificationData.actions[i].icon, |
+ bind<const SkBitmap&>( |
+ &NotificationResourcesLoader::didLoadActionIcon, |
+ WeakPersistentThisPointer<NotificationResourcesLoader>(this), |
+ i)); |
+ } |
+} |
+ |
+std::unique_ptr<WebNotificationResources> |
+NotificationResourcesLoader::getResources() const |
+{ |
+ std::unique_ptr<WebNotificationResources> |
Peter Beverloo
2016/04/13 18:32:27
micro nit: nowrap
Michael van Ouwerkerk
2016/04/14 13:42:11
Done.
|
+ resources(new WebNotificationResources()); |
+ resources->icon = m_icon; |
+ resources->badge = m_badge; |
+ resources->actionIcons = m_actionIcons; |
+ return resources; |
+} |
+ |
+void NotificationResourcesLoader::contextDestroyed() |
+{ |
+ stop(); |
+} |
+ |
+void NotificationResourcesLoader::stop() |
+{ |
+ for (auto imageLoader : m_imageLoaders) |
+ imageLoader->stop(); |
+} |
+ |
+DEFINE_TRACE(NotificationResourcesLoader) |
+{ |
+ visitor->trace(m_imageLoaders); |
+ ContextLifecycleObserver::trace(visitor); |
+} |
+ |
+void NotificationResourcesLoader::loadImage( |
+ const KURL& url, |
+ PassOwnPtr<NotificationImageLoader::ImageCallback> imageCallback) |
+{ |
+ if (url.isNull() || url.isEmpty() || !url.isValid()) { |
+ didFinishRequest(); |
+ return; |
+ } |
+ |
+ NotificationImageLoader* imageLoader = new NotificationImageLoader(); |
+ m_imageLoaders.append(imageLoader); |
+ imageLoader->start(getExecutionContext(), url, imageCallback); |
+} |
+ |
+void NotificationResourcesLoader::didLoadIcon(const SkBitmap& image) |
+{ |
+ m_icon = scaleDownIfNeeded(image, kMaxIconSizePx); |
+ didFinishRequest(); |
+} |
+ |
+void NotificationResourcesLoader::didLoadBadge(const SkBitmap& image) |
+{ |
+ m_badge = scaleDownIfNeeded(image, kMaxBadgeSizePx); |
Peter Beverloo
2016/04/13 18:32:28
As a.. refactoring opportunity, have you considere
Michael van Ouwerkerk
2016/04/14 13:42:11
I've added a TODO at scaleDownIfNeeded. In the cur
Peter Beverloo
2016/04/15 11:07:29
I.e. unpredictable performance from the developer'
|
+ didFinishRequest(); |
+} |
+ |
+void NotificationResourcesLoader::didLoadActionIcon(size_t actionIndex, |
Peter Beverloo
2016/04/13 18:32:27
micro nit: nowrap
Michael van Ouwerkerk
2016/04/14 13:42:11
Done.
|
+ const SkBitmap& image) |
+{ |
+ DCHECK_LT(actionIndex, m_actionIcons.size()); |
+ |
+ m_actionIcons[actionIndex] = scaleDownIfNeeded(image, kMaxActionIconSizePx); |
+ didFinishRequest(); |
+} |
+ |
+void NotificationResourcesLoader::didFinishRequest() |
+{ |
+ DCHECK_GT(m_pendingRequestCount, 0); |
+ m_pendingRequestCount--; |
+ if (!m_pendingRequestCount) { |
+ m_imageLoaders.clear(); |
+ (*m_completionCallback)(this); |
+ // The |this| pointer may have been deleted now. |
+ } |
+} |
+ |
+} // namespace blink |