Chromecast: extracts Linux window creation code to a common place.
[chromium-blink-merge.git] / content / renderer / notification_icon_loader.cc
blobd535837dbf92642f2946207fe5cd04e6e507c6a8
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"
14 using blink::WebURL;
15 using blink::WebURLError;
16 using blink::WebURLLoader;
17 using blink::WebURLRequest;
19 namespace content {
21 NotificationIconLoader::NotificationIconLoader(
22 int notification_id,
23 const DownloadCompletedCallback& callback)
24 : notification_id_(notification_id),
25 callback_(callback),
26 completed_(false) {}
28 NotificationIconLoader::~NotificationIconLoader() {}
30 void NotificationIconLoader::Start(const WebURL& icon_url) {
31 DCHECK(!loader_);
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() {
41 DCHECK(loader_);
43 completed_ = true;
44 loader_->cancel();
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);
55 buffer_.insert(buffer_.end(), data, data + data_length);
58 void NotificationIconLoader::didFinishLoading(
59 WebURLLoader* loader,
60 double finish_time,
61 int64_t total_encoded_data_length) {
62 DCHECK(!completed_);
64 SkBitmap icon;
65 if (!buffer_.empty()) {
66 ImageDecoder decoder;
67 icon = decoder.Decode(&buffer_[0], buffer_.size());
70 completed_ = true;
71 callback_.Run(notification_id_, icon);
74 void NotificationIconLoader::didFail(
75 WebURLLoader* loader, const WebURLError& error) {
76 if (completed_)
77 return;
79 completed_ = true;
80 callback_.Run(notification_id_, SkBitmap());
83 } // namespace content