OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/child/notifications/notification_image_loader.h" | 5 #include "content/child/notifications/notification_image_loader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram_macros.h" |
10 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
11 #include "content/child/image_decoder.h" | 12 #include "content/child/image_decoder.h" |
12 #include "third_party/WebKit/public/platform/Platform.h" | 13 #include "third_party/WebKit/public/platform/Platform.h" |
13 #include "third_party/WebKit/public/platform/WebURL.h" | 14 #include "third_party/WebKit/public/platform/WebURL.h" |
14 #include "third_party/WebKit/public/platform/WebURLLoader.h" | 15 #include "third_party/WebKit/public/platform/WebURLLoader.h" |
15 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 16 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
16 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
17 | 18 |
18 using blink::WebURL; | 19 using blink::WebURL; |
19 using blink::WebURLError; | 20 using blink::WebURLError; |
(...skipping 12 matching lines...) Expand all Loading... |
32 completed_(false) {} | 33 completed_(false) {} |
33 | 34 |
34 NotificationImageLoader::~NotificationImageLoader() { | 35 NotificationImageLoader::~NotificationImageLoader() { |
35 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 36 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
36 } | 37 } |
37 | 38 |
38 void NotificationImageLoader::StartOnMainThread(const GURL& image_url) { | 39 void NotificationImageLoader::StartOnMainThread(const GURL& image_url) { |
39 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 40 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
40 DCHECK(!url_loader_); | 41 DCHECK(!url_loader_); |
41 | 42 |
| 43 start_time_ = base::TimeTicks::Now(); |
| 44 |
42 WebURL image_web_url(image_url); | 45 WebURL image_web_url(image_url); |
43 WebURLRequest request(image_web_url); | 46 WebURLRequest request(image_web_url); |
44 request.setRequestContext(WebURLRequest::RequestContextImage); | 47 request.setRequestContext(WebURLRequest::RequestContextImage); |
45 | 48 |
46 url_loader_.reset(blink::Platform::current()->createURLLoader()); | 49 url_loader_.reset(blink::Platform::current()->createURLLoader()); |
47 url_loader_->loadAsynchronously(request, this); | 50 url_loader_->loadAsynchronously(request, this); |
48 } | 51 } |
49 | 52 |
50 void NotificationImageLoader::didReceiveData(WebURLLoader* loader, | 53 void NotificationImageLoader::didReceiveData(WebURLLoader* loader, |
51 const char* data, | 54 const char* data, |
52 int data_length, | 55 int data_length, |
53 int encoded_data_length) { | 56 int encoded_data_length) { |
54 DCHECK(!completed_); | 57 DCHECK(!completed_); |
55 DCHECK_GT(data_length, 0); | 58 DCHECK_GT(data_length, 0); |
56 | 59 |
57 buffer_.insert(buffer_.end(), data, data + data_length); | 60 buffer_.insert(buffer_.end(), data, data + data_length); |
58 } | 61 } |
59 | 62 |
60 void NotificationImageLoader::didFinishLoading( | 63 void NotificationImageLoader::didFinishLoading( |
61 WebURLLoader* loader, | 64 WebURLLoader* loader, |
62 double finish_time, | 65 double finish_time, |
63 int64_t total_encoded_data_length) { | 66 int64_t total_encoded_data_length) { |
64 DCHECK(!completed_); | 67 DCHECK(!completed_); |
65 | 68 |
| 69 UMA_HISTOGRAM_LONG_TIMES("Notifications.Icon.LoadFinishTime", |
| 70 base::TimeTicks::Now() - start_time_); |
| 71 |
66 RunCallbackOnWorkerThread(); | 72 RunCallbackOnWorkerThread(); |
67 } | 73 } |
68 | 74 |
69 void NotificationImageLoader::didFail(WebURLLoader* loader, | 75 void NotificationImageLoader::didFail(WebURLLoader* loader, |
70 const WebURLError& error) { | 76 const WebURLError& error) { |
71 if (completed_) | 77 if (completed_) |
72 return; | 78 return; |
73 | 79 |
| 80 UMA_HISTOGRAM_LONG_TIMES("Notifications.Icon.LoadFailTime", |
| 81 base::TimeTicks::Now() - start_time_); |
| 82 |
74 RunCallbackOnWorkerThread(); | 83 RunCallbackOnWorkerThread(); |
75 } | 84 } |
76 | 85 |
77 void NotificationImageLoader::RunCallbackOnWorkerThread() { | 86 void NotificationImageLoader::RunCallbackOnWorkerThread() { |
78 url_loader_.reset(); | 87 url_loader_.reset(); |
79 | 88 |
80 completed_ = true; | 89 completed_ = true; |
81 SkBitmap icon = GetDecodedImage(); | 90 SkBitmap icon = GetDecodedImage(); |
82 | 91 |
83 if (worker_task_runner_->BelongsToCurrentThread()) { | 92 if (worker_task_runner_->BelongsToCurrentThread()) { |
84 callback_.Run(icon); | 93 callback_.Run(icon); |
85 } else { | 94 } else { |
86 worker_task_runner_->PostTask(FROM_HERE, base::Bind(callback_, icon)); | 95 worker_task_runner_->PostTask(FROM_HERE, base::Bind(callback_, icon)); |
87 } | 96 } |
88 } | 97 } |
89 | 98 |
90 SkBitmap NotificationImageLoader::GetDecodedImage() const { | 99 SkBitmap NotificationImageLoader::GetDecodedImage() const { |
91 DCHECK(completed_); | 100 DCHECK(completed_); |
| 101 |
| 102 UMA_HISTOGRAM_CUSTOM_COUNTS("Notifications.Icon.FileSize", buffer_.size(), 1, |
| 103 10000000 /* ~10mb */, 50); |
| 104 |
92 if (buffer_.empty()) | 105 if (buffer_.empty()) |
93 return SkBitmap(); | 106 return SkBitmap(); |
94 | 107 |
95 ImageDecoder decoder; | 108 ImageDecoder decoder; |
96 return decoder.Decode(&buffer_[0], buffer_.size()); | 109 return decoder.Decode(&buffer_[0], buffer_.size()); |
97 } | 110 } |
98 | 111 |
99 void NotificationImageLoader::DeleteOnCorrectThread() const { | 112 void NotificationImageLoader::DeleteOnCorrectThread() const { |
100 if (!main_task_runner_->BelongsToCurrentThread()) { | 113 if (!main_task_runner_->BelongsToCurrentThread()) { |
101 main_task_runner_->DeleteSoon(FROM_HERE, this); | 114 main_task_runner_->DeleteSoon(FROM_HERE, this); |
102 return; | 115 return; |
103 } | 116 } |
104 | 117 |
105 delete this; | 118 delete this; |
106 } | 119 } |
107 | 120 |
108 } // namespace content | 121 } // namespace content |
OLD | NEW |