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 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 7 | #include "base/callback.h" |
[email protected] | c1d9cdc | 2011-01-17 06:50:01 | [diff] [blame] | 8 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 9 | #include "ui/gfx/size.h" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 10 | #include "webkit/glue/image_decoder.h" |
[email protected] | d5282e7 | 2009-05-13 13:16:52 | [diff] [blame] | 11 | #include "third_party/skia/include/core/SkBitmap.h" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 12 | |
[email protected] | dd7daa8 | 2009-08-10 05:46:45 | [diff] [blame] | 13 | using WebKit::WebFrame; |
| 14 | |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 15 | namespace webkit_glue { |
| 16 | |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 17 | ImageResourceFetcher::ImageResourceFetcher( |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 18 | const GURL& image_url, |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 19 | WebFrame* frame, |
| 20 | int id, |
| 21 | int image_size, |
| 22 | Callback* callback) |
| 23 | : callback_(callback), |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 24 | id_(id), |
| 25 | image_url_(image_url), |
| 26 | image_size_(image_size) { |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 27 | fetcher_.reset(new ResourceFetcher( |
| 28 | image_url, frame, |
| 29 | NewCallback(this, &ImageResourceFetcher::OnURLFetchComplete))); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | ImageResourceFetcher::~ImageResourceFetcher() { |
| 33 | if (!fetcher_->completed()) |
| 34 | fetcher_->Cancel(); |
| 35 | } |
| 36 | |
| 37 | void ImageResourceFetcher::OnURLFetchComplete( |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 38 | const WebKit::WebURLResponse& response, |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 39 | const std::string& data) { |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 40 | SkBitmap bitmap; |
| 41 | if (!response.isNull() && response.httpStatusCode() == 200) { |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 42 | // Request succeeded, try to convert it to an image. |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 43 | ImageDecoder decoder(gfx::Size(image_size_, image_size_)); |
| 44 | bitmap = decoder.Decode( |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 45 | reinterpret_cast<const unsigned char*>(data.data()), data.size()); |
| 46 | } // else case: |
| 47 | // 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] | 48 | // response as an image. The delegate will see a null image, indicating |
| 49 | // that an error occurred. |
[email protected] | 0fd62c4 | 2011-01-20 20:35:56 | [diff] [blame] | 50 | |
| 51 | // Take care to clear callback_ before running the callback as it may lead to |
| 52 | // our destruction. |
| 53 | scoped_ptr<Callback> callback; |
| 54 | callback.swap(callback_); |
| 55 | callback->Run(this, bitmap); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 56 | } |
[email protected] | f6134ff | 2009-07-07 16:44:39 | [diff] [blame] | 57 | |
| 58 | } // namespace webkit_glue |