Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / glue / multi_resolution_image_resource_fetcher.cc
blobe562cf97f6e82a28020fcdb27d2162aff21cd426
1 // Copyright (c) 2012 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 "webkit/glue/multi_resolution_image_resource_fetcher.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
10 #include "ui/gfx/size.h"
11 #include "webkit/glue/image_decoder.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
14 using WebKit::WebFrame;
15 using WebKit::WebURLRequest;
16 using WebKit::WebURLResponse;
18 namespace webkit_glue {
20 MultiResolutionImageResourceFetcher::MultiResolutionImageResourceFetcher(
21 const GURL& image_url,
22 WebFrame* frame,
23 int id,
24 WebURLRequest::TargetType target_type,
25 const Callback& callback)
26 : callback_(callback),
27 id_(id),
28 image_url_(image_url) {
29 fetcher_.reset(new ResourceFetcher(
30 image_url, frame, target_type,
31 base::Bind(&MultiResolutionImageResourceFetcher::OnURLFetchComplete,
32 base::Unretained(this))));
35 MultiResolutionImageResourceFetcher::~MultiResolutionImageResourceFetcher() {
36 if (!fetcher_->completed())
37 fetcher_->Cancel();
40 void MultiResolutionImageResourceFetcher::OnURLFetchComplete(
41 const WebURLResponse& response,
42 const std::string& data) {
43 std::vector<SkBitmap> bitmaps;
44 if (!response.isNull() && response.httpStatusCode() == 200) {
45 // Request succeeded, try to convert it to an image.
46 bitmaps = ImageDecoder::DecodeAll(
47 reinterpret_cast<const unsigned char*>(data.data()), data.size());
48 } // else case:
49 // If we get here, it means no image from server or couldn't decode the
50 // response as an image. The delegate will see an empty vector.
52 // Take a reference to the callback as running the callback may lead to our
53 // destruction.
54 Callback callback = callback_;
55 callback.Run(this, bitmaps);
58 } // namespace webkit_glue