Chromium Code Reviews| 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 "modules/notifications/NotificationResourcesLoader.h" | |
| 6 | |
| 7 #include "platform/weborigin/KURL.h" | |
| 8 #include "public/platform/modules/notifications/WebNotificationData.h" | |
| 9 #include "public/platform/modules/notifications/WebNotificationResources.h" | |
| 10 #include "skia/ext/image_operations.h" | |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | |
| 12 | |
| 13 namespace blink { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // TODO(mvanouwerkerk): Get icon dimensions from the embedder. | |
| 18 | |
| 19 // The maximum reasonable notification icon size, scaled from dip units to | |
| 20 // pixels using the largest supported scaling factor. | |
| 21 static const int kMaxIconSizePx = 320; // 80 dip * 4 | |
| 22 | |
| 23 // The maximum reasonable badge size, scaled from dip units to pixels using the | |
| 24 // largest supported scaling factor. | |
| 25 static const int kMaxBadgeSizePx = 96; // 24 dip * 4 | |
| 26 | |
| 27 // The maximum reasonable action icon size, scaled from dip units to pixels | |
| 28 // using the largest supported scaling factor. | |
| 29 static const int kMaxActionIconSizePx = 128; // 32 dip * 4 | |
| 30 | |
| 31 // Scales down |image| to fit within |maxSizePx| if its width or height is | |
| 32 // larger than |maxSizePx| and returns the result. Otherwise does nothing and | |
| 33 // returns |image| unchanged. | |
| 34 // TODO(mvanouwerkerk): Explore doing the scaling on a background thread. | |
| 35 SkBitmap scaleDownIfNeeded(const SkBitmap& image, int maxSizePx) | |
| 36 { | |
| 37 if (image.width() > maxSizePx || image.height() > maxSizePx) | |
| 38 return skia::ImageOperations::Resize(image, skia::ImageOperations::RESIZ E_BEST, std::min(image.width(), maxSizePx), std::min(image.height(), maxSizePx)) ; | |
| 39 return image; | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 NotificationResourcesLoader::NotificationResourcesLoader(PassOwnPtr<CompletionCa llback> completionCallback) : m_started(false), m_completionCallback(completionC allback), m_pendingRequestCount(0) | |
|
Peter Beverloo
2016/04/18 15:22:37
nit: initialization list on its own lines, elsewhe
Michael van Ouwerkerk
2016/04/18 17:03:15
Done.
| |
| 45 { | |
| 46 ThreadState::current()->registerPreFinalizer(this); | |
| 47 DCHECK(m_completionCallback); | |
| 48 } | |
| 49 | |
| 50 NotificationResourcesLoader::~NotificationResourcesLoader() | |
| 51 { | |
| 52 } | |
| 53 | |
| 54 void NotificationResourcesLoader::start(ExecutionContext* executionContext, cons t WebNotificationData& notificationData) | |
| 55 { | |
| 56 DCHECK(!m_started); | |
| 57 m_started = true; | |
| 58 | |
| 59 size_t numActions = notificationData.actions.size(); | |
| 60 m_pendingRequestCount = 2 /* icon and badge */ + numActions; | |
| 61 | |
| 62 loadImage(executionContext, notificationData.icon, bind<const SkBitmap&>(&No tificationResourcesLoader::didLoadIcon, WeakPersistentThisPointer<NotificationRe sourcesLoader>(this))); | |
| 63 loadImage(executionContext, notificationData.badge, bind<const SkBitmap&>(&N otificationResourcesLoader::didLoadBadge, WeakPersistentThisPointer<Notification ResourcesLoader>(this))); | |
| 64 | |
| 65 m_actionIcons.resize(numActions); | |
| 66 for (size_t i = 0; i < numActions; i++) | |
| 67 loadImage(executionContext, notificationData.actions[i].icon, bind<const SkBitmap&>(&NotificationResourcesLoader::didLoadActionIcon, WeakPersistentThisP ointer<NotificationResourcesLoader>(this), i)); | |
| 68 } | |
| 69 | |
| 70 std::unique_ptr<WebNotificationResources> NotificationResourcesLoader::getResour ces() const | |
| 71 { | |
| 72 std::unique_ptr<WebNotificationResources> resources(new WebNotificationResou rces()); | |
| 73 resources->icon = m_icon; | |
| 74 resources->badge = m_badge; | |
| 75 resources->actionIcons = m_actionIcons; | |
| 76 return resources; | |
| 77 } | |
| 78 | |
| 79 void NotificationResourcesLoader::stop() | |
| 80 { | |
| 81 for (auto imageLoader : m_imageLoaders) | |
| 82 imageLoader->stop(); | |
| 83 } | |
| 84 | |
| 85 DEFINE_TRACE(NotificationResourcesLoader) | |
| 86 { | |
| 87 visitor->trace(m_imageLoaders); | |
| 88 } | |
| 89 | |
| 90 void NotificationResourcesLoader::loadImage(ExecutionContext* executionContext, const KURL& url, PassOwnPtr<NotificationImageLoader::ImageCallback> imageCallbac k) | |
| 91 { | |
| 92 if (url.isNull() || url.isEmpty() || !url.isValid()) { | |
| 93 didFinishRequest(); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 NotificationImageLoader* imageLoader = new NotificationImageLoader(); | |
| 98 m_imageLoaders.append(imageLoader); | |
| 99 imageLoader->start(executionContext, url, imageCallback); | |
| 100 } | |
| 101 | |
| 102 void NotificationResourcesLoader::didLoadIcon(const SkBitmap& image) | |
| 103 { | |
| 104 m_icon = scaleDownIfNeeded(image, kMaxIconSizePx); | |
| 105 didFinishRequest(); | |
| 106 } | |
| 107 | |
| 108 void NotificationResourcesLoader::didLoadBadge(const SkBitmap& image) | |
| 109 { | |
| 110 m_badge = scaleDownIfNeeded(image, kMaxBadgeSizePx); | |
| 111 didFinishRequest(); | |
| 112 } | |
| 113 | |
| 114 void NotificationResourcesLoader::didLoadActionIcon(size_t actionIndex, const Sk Bitmap& image) | |
| 115 { | |
| 116 DCHECK_LT(actionIndex, m_actionIcons.size()); | |
| 117 | |
| 118 m_actionIcons[actionIndex] = scaleDownIfNeeded(image, kMaxActionIconSizePx); | |
| 119 didFinishRequest(); | |
| 120 } | |
| 121 | |
| 122 void NotificationResourcesLoader::didFinishRequest() | |
| 123 { | |
| 124 DCHECK_GT(m_pendingRequestCount, 0); | |
| 125 m_pendingRequestCount--; | |
| 126 if (!m_pendingRequestCount) { | |
| 127 stop(); | |
| 128 (*m_completionCallback)(this); | |
| 129 // The |this| pointer may have been deleted now. | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 } // namespace blink | |
| OLD | NEW |