license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 4 | |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 5 | #include "webkit/glue/image_resource_fetcher.h" |
| 6 | |
| 7 | #include "base/gfx/size.h" |
[email protected] | dd7daa8 | 2009-08-10 05:46:45 | [diff] [blame^] | 8 | #include "webkit/api/public/WebFrame.h" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 9 | #include "webkit/glue/image_decoder.h" |
[email protected] | d5282e7 | 2009-05-13 13:16:52 | [diff] [blame] | 10 | #include "third_party/skia/include/core/SkBitmap.h" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 11 | |
[email protected] | dd7daa8 | 2009-08-10 05:46:45 | [diff] [blame^] | 12 | using WebKit::WebFrame; |
| 13 | |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 14 | namespace webkit_glue { |
| 15 | |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 16 | ImageResourceFetcher::ImageResourceFetcher( |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 17 | const GURL& image_url, |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 18 | WebFrame* frame, |
| 19 | int id, |
| 20 | int image_size, |
| 21 | Callback* callback) |
| 22 | : callback_(callback), |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 23 | id_(id), |
| 24 | image_url_(image_url), |
| 25 | image_size_(image_size) { |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 26 | fetcher_.reset(new ResourceFetcher( |
| 27 | image_url, frame, |
| 28 | NewCallback(this, &ImageResourceFetcher::OnURLFetchComplete))); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | ImageResourceFetcher::~ImageResourceFetcher() { |
| 32 | if (!fetcher_->completed()) |
| 33 | fetcher_->Cancel(); |
| 34 | } |
| 35 | |
| 36 | void ImageResourceFetcher::OnURLFetchComplete( |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 37 | const WebKit::WebURLResponse& response, |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 38 | const std::string& data) { |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 39 | SkBitmap bitmap; |
| 40 | if (!response.isNull() && response.httpStatusCode() == 200) { |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 41 | // Request succeeded, try to convert it to an image. |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 42 | ImageDecoder decoder(gfx::Size(image_size_, image_size_)); |
| 43 | bitmap = decoder.Decode( |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 44 | 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] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 47 | // response as an image. The delegate will see a null image, indicating |
| 48 | // that an error occurred. |
| 49 | callback_->Run(this, bitmap); |
[email protected] | cb1da540 | 2009-07-07 23:53:29 | [diff] [blame] | 50 | callback_.reset(); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 51 | } |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 52 | |
| 53 | } // namespace webkit_glue |