blob: 0a30f53697f00f9df456ff7516c9de31f389d60d [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commitf5b16fe2008-07-27 00:20:514
initial.commitf5b16fe2008-07-27 00:20:515#include "webkit/glue/image_resource_fetcher.h"
6
7#include "base/gfx/size.h"
[email protected]dd7daa82009-08-10 05:46:458#include "webkit/api/public/WebFrame.h"
initial.commitf5b16fe2008-07-27 00:20:519#include "webkit/glue/image_decoder.h"
[email protected]d5282e72009-05-13 13:16:5210#include "third_party/skia/include/core/SkBitmap.h"
initial.commitf5b16fe2008-07-27 00:20:5111
[email protected]dd7daa82009-08-10 05:46:4512using WebKit::WebFrame;
13
[email protected]f6134ff2009-07-07 16:44:3914namespace webkit_glue {
15
initial.commitf5b16fe2008-07-27 00:20:5116ImageResourceFetcher::ImageResourceFetcher(
initial.commitf5b16fe2008-07-27 00:20:5117 const GURL& image_url,
[email protected]f6134ff2009-07-07 16:44:3918 WebFrame* frame,
19 int id,
20 int image_size,
21 Callback* callback)
22 : callback_(callback),
initial.commitf5b16fe2008-07-27 00:20:5123 id_(id),
24 image_url_(image_url),
25 image_size_(image_size) {
[email protected]f6134ff2009-07-07 16:44:3926 fetcher_.reset(new ResourceFetcher(
27 image_url, frame,
28 NewCallback(this, &ImageResourceFetcher::OnURLFetchComplete)));
initial.commitf5b16fe2008-07-27 00:20:5129}
30
31ImageResourceFetcher::~ImageResourceFetcher() {
32 if (!fetcher_->completed())
33 fetcher_->Cancel();
34}
35
36void ImageResourceFetcher::OnURLFetchComplete(
[email protected]f6134ff2009-07-07 16:44:3937 const WebKit::WebURLResponse& response,
initial.commitf5b16fe2008-07-27 00:20:5138 const std::string& data) {
[email protected]f6134ff2009-07-07 16:44:3939 SkBitmap bitmap;
40 if (!response.isNull() && response.httpStatusCode() == 200) {
initial.commitf5b16fe2008-07-27 00:20:5141 // Request succeeded, try to convert it to an image.
[email protected]f6134ff2009-07-07 16:44:3942 ImageDecoder decoder(gfx::Size(image_size_, image_size_));
43 bitmap = decoder.Decode(
initial.commitf5b16fe2008-07-27 00:20:5144 reinterpret_cast<const unsigned char*>(data.data()), data.size());
45 } // else case:
46 // If we get here, it means no image from server or couldn't decode the
[email protected]f6134ff2009-07-07 16:44:3947 // response as an image. The delegate will see a null image, indicating
48 // that an error occurred.
49 callback_->Run(this, bitmap);
[email protected]cb1da5402009-07-07 23:53:2950 callback_.reset();
initial.commitf5b16fe2008-07-27 00:20:5151}
[email protected]f6134ff2009-07-07 16:44:3952
53} // namespace webkit_glue