OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/glue/image_resource_fetcher.h" | 5 #include "webkit/glue/multi_resolution_image_resource_fetcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
10 #include "ui/gfx/size.h" | 10 #include "ui/gfx/size.h" |
11 #include "webkit/glue/image_decoder.h" | 11 #include "webkit/glue/image_decoder.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
13 | 13 |
14 using WebKit::WebFrame; | 14 using WebKit::WebFrame; |
15 using WebKit::WebURLRequest; | 15 using WebKit::WebURLRequest; |
16 using WebKit::WebURLResponse; | 16 using WebKit::WebURLResponse; |
17 | 17 |
18 namespace webkit_glue { | 18 namespace webkit_glue { |
19 | 19 |
20 ImageResourceFetcher::ImageResourceFetcher( | 20 MultiResolutionImageResourceFetcher::MultiResolutionImageResourceFetcher( |
21 const GURL& image_url, | 21 const GURL& image_url, |
22 WebFrame* frame, | 22 WebFrame* frame, |
23 int id, | 23 int id, |
24 int image_size, | |
25 WebURLRequest::TargetType target_type, | 24 WebURLRequest::TargetType target_type, |
26 const Callback& callback) | 25 const Callback& callback) |
27 : callback_(callback), | 26 : callback_(callback), |
28 id_(id), | 27 id_(id), |
29 image_url_(image_url), | 28 image_url_(image_url) { |
30 image_size_(image_size) { | |
31 fetcher_.reset(new ResourceFetcher( | 29 fetcher_.reset(new ResourceFetcher( |
32 image_url, frame, target_type, | 30 image_url, frame, target_type, |
33 base::Bind(&ImageResourceFetcher::OnURLFetchComplete, | 31 base::Bind(&MultiResolutionImageResourceFetcher::OnURLFetchComplete, |
34 base::Unretained(this)))); | 32 base::Unretained(this)))); |
35 } | 33 } |
36 | 34 |
37 ImageResourceFetcher::~ImageResourceFetcher() { | 35 MultiResolutionImageResourceFetcher::~MultiResolutionImageResourceFetcher() { |
38 if (!fetcher_->completed()) | 36 if (!fetcher_->completed()) |
39 fetcher_->Cancel(); | 37 fetcher_->Cancel(); |
40 } | 38 } |
41 | 39 |
42 void ImageResourceFetcher::OnURLFetchComplete( | 40 void MultiResolutionImageResourceFetcher::OnURLFetchComplete( |
43 const WebURLResponse& response, | 41 const WebURLResponse& response, |
44 const std::string& data) { | 42 const std::string& data) { |
45 SkBitmap bitmap; | 43 std::vector<SkBitmap> bitmaps; |
46 if (!response.isNull() && response.httpStatusCode() == 200) { | 44 if (!response.isNull() && response.httpStatusCode() == 200) { |
47 // Request succeeded, try to convert it to an image. | 45 // Request succeeded, try to convert it to an image. |
48 ImageDecoder decoder(gfx::Size(image_size_, image_size_)); | 46 bitmaps = ImageDecoder::DecodeAll( |
49 bitmap = decoder.Decode( | |
50 reinterpret_cast<const unsigned char*>(data.data()), data.size()); | 47 reinterpret_cast<const unsigned char*>(data.data()), data.size()); |
51 } // else case: | 48 } // else case: |
52 // If we get here, it means no image from server or couldn't decode the | 49 // If we get here, it means no image from server or couldn't decode the |
53 // response as an image. The delegate will see a null image, indicating | 50 // response as an image. The delegate will see an empty vector. |
54 // that an error occurred. | |
55 | 51 |
56 // Take a reference to the callback as running the callback may lead to our | 52 // Take a reference to the callback as running the callback may lead to our |
57 // destruction. | 53 // destruction. |
58 Callback callback = callback_; | 54 Callback callback = callback_; |
59 callback.Run(this, bitmap); | 55 callback.Run(this, bitmaps); |
60 } | 56 } |
61 | 57 |
62 } // namespace webkit_glue | 58 } // namespace webkit_glue |
OLD | NEW |