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
3 // found in the LICENSE file.
5 #include "content/renderer/notification_icon_loader.h"
7 #include "base/logging.h"
8 #include "content/child/image_decoder.h"
9 #include "third_party/WebKit/public/platform/Platform.h"
10 #include "third_party/WebKit/public/platform/WebURL.h"
11 #include "third_party/WebKit/public/platform/WebURLLoader.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
15 using blink::WebURLError
;
16 using blink::WebURLLoader
;
17 using blink::WebURLRequest
;
21 NotificationIconLoader::NotificationIconLoader(
23 const DownloadCompletedCallback
& callback
)
24 : notification_id_(notification_id
),
28 NotificationIconLoader::~NotificationIconLoader() {}
30 void NotificationIconLoader::Start(const WebURL
& icon_url
) {
33 WebURLRequest
request(icon_url
);
34 request
.setRequestContext(WebURLRequest::RequestContextImage
);
36 loader_
.reset(blink::Platform::current()->createURLLoader());
37 loader_
->loadAsynchronously(request
, this);
40 void NotificationIconLoader::Cancel() {
47 void NotificationIconLoader::didReceiveData(
51 int encoded_data_length
) {
53 DCHECK_GT(data_length
, 0);
55 buffer_
.insert(buffer_
.end(), data
, data
+ data_length
);
58 void NotificationIconLoader::didFinishLoading(
61 int64_t total_encoded_data_length
) {
65 if (!buffer_
.empty()) {
67 icon
= decoder
.Decode(&buffer_
[0], buffer_
.size());
71 callback_
.Run(notification_id_
, icon
);
74 void NotificationIconLoader::didFail(
75 WebURLLoader
* loader
, const WebURLError
& error
) {
80 callback_
.Run(notification_id_
, SkBitmap());
83 } // namespace content