OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/renderer/notification_icon_loader.h" | |
6 | |
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" | |
13 | |
14 using blink::WebURL; | |
15 using blink::WebURLError; | |
16 using blink::WebURLLoader; | |
17 using blink::WebURLRequest; | |
18 | |
19 namespace content { | |
20 | |
21 NotificationIconLoader::NotificationIconLoader( | |
22 int notification_id, | |
23 const DownloadCompletedCallback& callback) | |
24 : notification_id_(notification_id), | |
25 callback_(callback), | |
26 completed_(false) {} | |
27 | |
28 NotificationIconLoader::~NotificationIconLoader() {} | |
29 | |
30 void NotificationIconLoader::Start(const WebURL& icon_url) { | |
31 DCHECK(!loader_); | |
32 | |
33 WebURLRequest request(icon_url); | |
34 request.setRequestContext(WebURLRequest::RequestContextImage); | |
35 | |
36 loader_.reset(blink::Platform::current()->createURLLoader()); | |
37 loader_->loadAsynchronously(request, this); | |
38 } | |
39 | |
40 void NotificationIconLoader::Cancel() { | |
41 DCHECK(loader_); | |
42 | |
43 completed_ = true; | |
44 loader_->cancel(); | |
45 } | |
46 | |
47 void NotificationIconLoader::didReceiveData( | |
48 WebURLLoader* loader, | |
49 const char* data, | |
50 int data_length, | |
51 int encoded_data_length) { | |
52 DCHECK(!completed_); | |
53 DCHECK_GT(data_length, 0); | |
54 | |
55 buffer_.insert(buffer_.end(), data, data + data_length); | |
56 } | |
57 | |
58 void NotificationIconLoader::didFinishLoading( | |
59 WebURLLoader* loader, | |
60 double finish_time, | |
61 int64_t total_encoded_data_length) { | |
62 DCHECK(!completed_); | |
63 | |
64 SkBitmap icon; | |
65 if (!buffer_.empty()) { | |
66 ImageDecoder decoder; | |
67 icon = decoder.Decode(&buffer_[0], buffer_.size()); | |
68 } | |
69 | |
70 completed_ = true; | |
71 callback_.Run(notification_id_, icon); | |
72 } | |
73 | |
74 void NotificationIconLoader::didFail( | |
75 WebURLLoader* loader, const WebURLError& error) { | |
76 if (completed_) | |
77 return; | |
78 | |
79 completed_ = true; | |
80 callback_.Run(notification_id_, SkBitmap()); | |
81 } | |
82 | |
83 } // namespace content | |
OLD | NEW |