blob: e562cf97f6e82a28020fcdb27d2162aff21cd426 [file] [log] [blame]
[email protected]b1c1e092012-08-06 08:52:001// 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.
4
5#include "webkit/glue/multi_resolution_image_resource_fetcher.h"
6
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"
13
14using WebKit::WebFrame;
15using WebKit::WebURLRequest;
16using WebKit::WebURLResponse;
17
18namespace webkit_glue {
19
20MultiResolutionImageResourceFetcher::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))));
33}
34
35MultiResolutionImageResourceFetcher::~MultiResolutionImageResourceFetcher() {
36 if (!fetcher_->completed())
37 fetcher_->Cancel();
38}
39
40void 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.
51
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);
56}
57
58} // namespace webkit_glue