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

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

Issue 2540763002: Notifications: Split up image loading histograms by image type (Closed)
Patch Set: Created 4 years 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
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/NotificationImageLoader.h" 5 #include "modules/notifications/NotificationImageLoader.h"
6 6
7 #include "core/dom/ExecutionContext.h" 7 #include "core/dom/ExecutionContext.h"
8 #include "core/fetch/ResourceLoaderOptions.h" 8 #include "core/fetch/ResourceLoaderOptions.h"
9 #include "platform/Histogram.h" 9 #include "platform/Histogram.h"
10 #include "platform/image-decoders/ImageDecoder.h" 10 #include "platform/image-decoders/ImageDecoder.h"
11 #include "platform/image-decoders/ImageFrame.h" 11 #include "platform/image-decoders/ImageFrame.h"
12 #include "platform/network/ResourceError.h" 12 #include "platform/network/ResourceError.h"
13 #include "platform/network/ResourceLoadPriority.h" 13 #include "platform/network/ResourceLoadPriority.h"
14 #include "platform/network/ResourceRequest.h" 14 #include "platform/network/ResourceRequest.h"
15 #include "platform/weborigin/KURL.h" 15 #include "platform/weborigin/KURL.h"
16 #include "public/platform/WebURLRequest.h" 16 #include "public/platform/WebURLRequest.h"
17 #include "third_party/skia/include/core/SkBitmap.h" 17 #include "public/platform/modules/notifications/WebNotificationConstants.h"
18 #include "skia/ext/image_operations.h"
18 #include "wtf/CurrentTime.h" 19 #include "wtf/CurrentTime.h"
19 #include "wtf/Threading.h" 20 #include "wtf/Threading.h"
20 #include <memory> 21 #include <memory>
21 22
23 #define DEFINE_SINGLE_TYPE_COUNT_HISTOGRAM(metric, type_name, value, max) \
24 case NotificationImageLoader::Type::type_name: { \
25 DEFINE_THREAD_SAFE_STATIC_LOCAL( \
26 CustomCountHistogram, metric##type_name##Histogram, \
27 new CustomCountHistogram( \
28 "Notifications." #metric "." #type_name, \
29 1 /* min */, max, 50 /* buckets */)); \
Peter Beverloo 2016/11/29 19:01:40 nit: fix alignment
johnme 2016/11/29 20:04:29 Done.
30 metric##type_name##Histogram.count(value); \
31 break; \
32 }
33
34 #define DEFINE_COUNT_HISTOGRAM(metric, type, value, max) \
35 switch (type) { \
36 DEFINE_SINGLE_TYPE_COUNT_HISTOGRAM(metric, Image, value, max); \
37 DEFINE_SINGLE_TYPE_COUNT_HISTOGRAM(metric, Icon, value, max); \
38 DEFINE_SINGLE_TYPE_COUNT_HISTOGRAM(metric, Badge, value, max); \
39 DEFINE_SINGLE_TYPE_COUNT_HISTOGRAM(metric, ActionIcon, value, max); \
Peter Beverloo 2016/11/29 19:01:40 No semi-colons after these lines. (You wouldn't ad
johnme 2016/11/29 20:04:28 Done.
40 }
41
22 namespace { 42 namespace {
23 43
24 // 99.9% of all images were fetched successfully in 90 seconds. 44 // 99.9% of all images were fetched successfully in 90 seconds.
25 const unsigned long kImageFetchTimeoutInMs = 90000; 45 const unsigned long kImageFetchTimeoutInMs = 90000;
26 46
27 } // namespace 47 } // namespace
28 48
29 namespace blink { 49 namespace blink {
30 50
31 NotificationImageLoader::NotificationImageLoader() 51 NotificationImageLoader::NotificationImageLoader(Type type)
32 : m_stopped(false), m_startTime(0.0) {} 52 : m_type(type), m_stopped(false), m_startTime(0.0) {}
33 53
34 NotificationImageLoader::~NotificationImageLoader() {} 54 NotificationImageLoader::~NotificationImageLoader() {}
35 55
56 // static
57 SkBitmap NotificationImageLoader::scaleDownIfNeeded(const SkBitmap& image,
58 Type type) {
59 int maxWidthPx = -1, maxHeightPx = -1;
60 switch (type) {
61 case Type::Image:
62 maxWidthPx = kWebNotificationMaxImageWidthPx;
63 maxHeightPx = kWebNotificationMaxImageHeightPx;
Peter Beverloo 2016/11/29 19:01:40 You're going to want some |break| statements in th
johnme 2016/11/29 20:04:28 Done :)
64 case Type::Icon:
65 maxWidthPx = kWebNotificationMaxIconSizePx;
66 maxHeightPx = kWebNotificationMaxIconSizePx;
67 case Type::Badge:
68 maxWidthPx = kWebNotificationMaxBadgeSizePx;
69 maxHeightPx = kWebNotificationMaxBadgeSizePx;
70 case Type::ActionIcon:
71 maxWidthPx = kWebNotificationMaxActionIconSizePx;
72 maxHeightPx = kWebNotificationMaxActionIconSizePx;
73 }
Peter Beverloo 2016/11/29 19:01:40 DCHECK_NE on maxWidthPx/maxHeightPx not being -1
johnme 2016/11/29 20:04:28 Done (DCHECK_GT 0).
74 // TODO(mvanouwerkerk): Explore doing the scaling on a background thread.
Peter Beverloo 2016/11/29 19:01:40 s/mvanouwerkerk/another name/
johnme 2016/11/29 20:04:28 Done.
75 if (image.width() > maxWidthPx || image.height() > maxHeightPx) {
76 double scale = std::min(static_cast<double>(maxWidthPx) / image.width(),
77 static_cast<double>(maxHeightPx) / image.height());
78 double startTime = monotonicallyIncreasingTimeMS();
79 // TODO(peter): Try using RESIZE_BETTER for large images.
80 SkBitmap scaledImage =
81 skia::ImageOperations::Resize(image, skia::ImageOperations::RESIZE_BEST,
82 std::lround(scale * image.width()),
83 std::lround(scale * image.height()));
84 DEFINE_COUNT_HISTOGRAM(LoadScaleDownTime, type,
85 monotonicallyIncreasingTimeMS() - startTime,
86 1000 * 10 /* 10 seconds max */);
87 return scaledImage;
Peter Beverloo 2016/11/29 19:01:40 nit: std::move(scaledImage) to prevent silly compi
johnme 2016/11/29 20:04:29 error: moving a local object in a return statement
88 }
89 return image;
90 }
91
36 void NotificationImageLoader::start( 92 void NotificationImageLoader::start(
37 ExecutionContext* executionContext, 93 ExecutionContext* executionContext,
38 const KURL& url, 94 const KURL& url,
39 std::unique_ptr<ImageCallback> imageCallback) { 95 std::unique_ptr<ImageCallback> imageCallback) {
40 DCHECK(!m_stopped); 96 DCHECK(!m_stopped);
41 97
42 m_startTime = monotonicallyIncreasingTimeMS(); 98 m_startTime = monotonicallyIncreasingTimeMS();
43 m_imageCallback = std::move(imageCallback); 99 m_imageCallback = std::move(imageCallback);
44 100
45 ThreadableLoaderOptions threadableLoaderOptions; 101 ThreadableLoaderOptions threadableLoaderOptions;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 m_data->append(data, length); 138 m_data->append(data, length);
83 } 139 }
84 140
85 void NotificationImageLoader::didFinishLoading(unsigned long resourceIdentifier, 141 void NotificationImageLoader::didFinishLoading(unsigned long resourceIdentifier,
86 double finishTime) { 142 double finishTime) {
87 // If this has been stopped it is not desirable to trigger further work, 143 // If this has been stopped it is not desirable to trigger further work,
88 // there is a shutdown of some sort in progress. 144 // there is a shutdown of some sort in progress.
89 if (m_stopped) 145 if (m_stopped)
90 return; 146 return;
91 147
92 DEFINE_THREAD_SAFE_STATIC_LOCAL( 148 DEFINE_COUNT_HISTOGRAM(LoadFinishTime, m_type,
93 CustomCountHistogram, finishedTimeHistogram, 149 monotonicallyIncreasingTimeMS() - m_startTime,
94 new CustomCountHistogram("Notifications.Icon.LoadFinishTime", 1, 150 1000 * 60 * 60 /* 1 hour max */);
Peter Beverloo 2016/11/29 19:01:40 Please find a better name for DEFINE_COUNT_HISTOGR
johnme 2016/11/29 20:04:28 Done (NOTIFICATION_HISTOGRAM_COUNTS, by analogy to
95 1000 * 60 * 60 /* 1 hour max */,
96 50 /* buckets */));
97 finishedTimeHistogram.count(monotonicallyIncreasingTimeMS() - m_startTime);
98 151
99 if (m_data) { 152 if (m_data) {
100 DEFINE_THREAD_SAFE_STATIC_LOCAL( 153 DEFINE_COUNT_HISTOGRAM(LoadFileSize, m_type,
101 CustomCountHistogram, fileSizeHistogram, 154 m_data->size(), 10000000 /* ~10mb max */);
102 new CustomCountHistogram("Notifications.Icon.FileSize", 1,
103 10000000 /* ~10mb max */, 50 /* buckets */));
104 fileSizeHistogram.count(m_data->size());
105 155
106 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::create( 156 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::create(
107 m_data, true /* dataComplete */, ImageDecoder::AlphaPremultiplied, 157 m_data, true /* dataComplete */, ImageDecoder::AlphaPremultiplied,
108 ImageDecoder::ColorSpaceApplied); 158 ImageDecoder::ColorSpaceApplied);
109 if (decoder) { 159 if (decoder) {
110 // The |ImageFrame*| is owned by the decoder. 160 // The |ImageFrame*| is owned by the decoder.
111 ImageFrame* imageFrame = decoder->frameBufferAtIndex(0); 161 ImageFrame* imageFrame = decoder->frameBufferAtIndex(0);
112 if (imageFrame) { 162 if (imageFrame) {
113 (*m_imageCallback)(imageFrame->bitmap()); 163 (*m_imageCallback)(imageFrame->bitmap());
114 return; 164 return;
115 } 165 }
116 } 166 }
117 } 167 }
118 runCallbackWithEmptyBitmap(); 168 runCallbackWithEmptyBitmap();
119 } 169 }
120 170
121 void NotificationImageLoader::didFail(const ResourceError& error) { 171 void NotificationImageLoader::didFail(const ResourceError& error) {
122 DEFINE_THREAD_SAFE_STATIC_LOCAL( 172 DEFINE_COUNT_HISTOGRAM(LoadFailTime, m_type,
123 CustomCountHistogram, failedTimeHistogram, 173 monotonicallyIncreasingTimeMS() - m_startTime,
124 new CustomCountHistogram("Notifications.Icon.LoadFailTime", 1, 174 1000 * 60 * 60 /* 1 hour max */);
125 1000 * 60 * 60 /* 1 hour max */,
126 50 /* buckets */));
127 failedTimeHistogram.count(monotonicallyIncreasingTimeMS() - m_startTime);
128 175
129 runCallbackWithEmptyBitmap(); 176 runCallbackWithEmptyBitmap();
130 } 177 }
131 178
132 void NotificationImageLoader::didFailRedirectCheck() { 179 void NotificationImageLoader::didFailRedirectCheck() {
133 runCallbackWithEmptyBitmap(); 180 runCallbackWithEmptyBitmap();
134 } 181 }
135 182
136 void NotificationImageLoader::runCallbackWithEmptyBitmap() { 183 void NotificationImageLoader::runCallbackWithEmptyBitmap() {
137 // If this has been stopped it is not desirable to trigger further work, 184 // If this has been stopped it is not desirable to trigger further work,
138 // there is a shutdown of some sort in progress. 185 // there is a shutdown of some sort in progress.
139 if (m_stopped) 186 if (m_stopped)
140 return; 187 return;
141 188
142 (*m_imageCallback)(SkBitmap()); 189 (*m_imageCallback)(SkBitmap());
143 } 190 }
144 191
145 } // namespace blink 192 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698