e7d7c8d76a1d355abd321688ead56684fa487241
[platform/framework/web/crosswalk.git] / src / content / renderer / fetchers / image_resource_fetcher.cc
1 // Copyright (c) 2011 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/fetchers/image_resource_fetcher.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/debug/crash_logging.h"
10 #include "content/child/image_decoder.h"
11 #include "content/public/renderer/resource_fetcher.h"
12 #include "third_party/WebKit/public/platform/WebURLResponse.h"
13 #include "third_party/WebKit/public/web/WebFrame.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/size.h"
16
17 using blink::WebFrame;
18 using blink::WebURLRequest;
19 using blink::WebURLResponse;
20
21 namespace content {
22
23 ImageResourceFetcher::ImageResourceFetcher(
24     const GURL& image_url,
25     WebFrame* frame,
26     int id,
27     int image_size,
28     WebURLRequest::RequestContext request_context,
29     const Callback& callback)
30     : callback_(callback),
31       id_(id),
32       image_url_(image_url),
33       image_size_(image_size) {
34   fetcher_.reset(ResourceFetcher::Create(image_url));
35   fetcher_->Start(frame,
36                   request_context,
37                   WebURLRequest::FrameTypeNone,
38                   base::Bind(&ImageResourceFetcher::OnURLFetchComplete,
39                              base::Unretained(this)));
40
41   // Set subresource URL for crash reporting.
42   base::debug::SetCrashKeyValue("subresource_url", image_url.spec());
43 }
44
45 ImageResourceFetcher::~ImageResourceFetcher() {
46 }
47
48 void ImageResourceFetcher::OnURLFetchComplete(
49     const WebURLResponse& response,
50     const std::string& data) {
51   SkBitmap bitmap;
52   if (!response.isNull() && response.httpStatusCode() == 200) {
53     // Request succeeded, try to convert it to an image.
54     ImageDecoder decoder(gfx::Size(image_size_, image_size_));
55     bitmap = decoder.Decode(
56         reinterpret_cast<const unsigned char*>(data.data()), data.size());
57   } // else case:
58     // If we get here, it means no image from server or couldn't decode the
59     // response as an image. The delegate will see a null image, indicating
60     // that an error occurred.
61
62   // Take a reference to the callback as running the callback may lead to our
63   // destruction.
64   Callback callback = callback_;
65   callback.Run(this, bitmap);
66 }
67
68 }  // namespace content