Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: third_party/WebKit/Source/modules/notifications/NotificationResourcesLoader.cpp

Issue 1847863002: Move notification resource loading from content/child to blink (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address peter's comments. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)
45 : m_started(false), m_completionCallback(completionCallback), m_pendingReque stCount(0)
46 {
47 ThreadState::current()->registerPreFinalizer(this);
48 DCHECK(m_completionCallback);
49 }
50
51 NotificationResourcesLoader::~NotificationResourcesLoader()
52 {
53 }
54
55 void NotificationResourcesLoader::start(ExecutionContext* executionContext, cons t WebNotificationData& notificationData)
56 {
57 DCHECK(!m_started);
58 m_started = true;
59
60 size_t numActions = notificationData.actions.size();
61 m_pendingRequestCount = 2 /* icon and badge */ + numActions;
62
63 loadImage(executionContext, notificationData.icon, bind<const SkBitmap&>(&No tificationResourcesLoader::didLoadIcon, WeakPersistentThisPointer<NotificationRe sourcesLoader>(this)));
64 loadImage(executionContext, notificationData.badge, bind<const SkBitmap&>(&N otificationResourcesLoader::didLoadBadge, WeakPersistentThisPointer<Notification ResourcesLoader>(this)));
65
66 m_actionIcons.resize(numActions);
67 for (size_t i = 0; i < numActions; i++)
68 loadImage(executionContext, notificationData.actions[i].icon, bind<const SkBitmap&>(&NotificationResourcesLoader::didLoadActionIcon, WeakPersistentThisP ointer<NotificationResourcesLoader>(this), i));
69 }
70
71 std::unique_ptr<WebNotificationResources> NotificationResourcesLoader::getResour ces() const
72 {
73 std::unique_ptr<WebNotificationResources> resources(new WebNotificationResou rces());
74 resources->icon = m_icon;
75 resources->badge = m_badge;
76 resources->actionIcons = m_actionIcons;
77 return resources;
78 }
79
80 void NotificationResourcesLoader::stop()
81 {
82 for (auto imageLoader : m_imageLoaders)
83 imageLoader->stop();
haraken 2016/04/19 01:12:52 Would it be better to clear m_imageLoaders to avoi
Michael van Ouwerkerk 2016/04/19 09:47:31 Not sure, as there is already a guard in that stop
84 }
85
86 DEFINE_TRACE(NotificationResourcesLoader)
87 {
88 visitor->trace(m_imageLoaders);
89 }
90
91 void NotificationResourcesLoader::loadImage(ExecutionContext* executionContext, const KURL& url, PassOwnPtr<NotificationImageLoader::ImageCallback> imageCallbac k)
92 {
93 if (url.isNull() || url.isEmpty() || !url.isValid()) {
94 didFinishRequest();
95 return;
96 }
97
98 NotificationImageLoader* imageLoader = new NotificationImageLoader();
99 m_imageLoaders.append(imageLoader);
100 imageLoader->start(executionContext, url, imageCallback);
101 }
102
103 void NotificationResourcesLoader::didLoadIcon(const SkBitmap& image)
104 {
105 m_icon = scaleDownIfNeeded(image, kMaxIconSizePx);
106 didFinishRequest();
107 }
108
109 void NotificationResourcesLoader::didLoadBadge(const SkBitmap& image)
110 {
111 m_badge = scaleDownIfNeeded(image, kMaxBadgeSizePx);
112 didFinishRequest();
113 }
114
115 void NotificationResourcesLoader::didLoadActionIcon(size_t actionIndex, const Sk Bitmap& image)
116 {
117 DCHECK_LT(actionIndex, m_actionIcons.size());
118
119 m_actionIcons[actionIndex] = scaleDownIfNeeded(image, kMaxActionIconSizePx);
120 didFinishRequest();
121 }
122
123 void NotificationResourcesLoader::didFinishRequest()
124 {
125 DCHECK_GT(m_pendingRequestCount, 0);
126 m_pendingRequestCount--;
127 if (!m_pendingRequestCount) {
128 stop();
129 (*m_completionCallback)(this);
130 // The |this| pointer may have been deleted now.
131 }
132 }
133
134 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698