Chromium Code Reviews| 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/image_resource_fetcher.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/bind.h" |
|
csilv
2011/11/21 21:26:31
add #include "base/bind_helpers.h"
dcheng
2011/11/21 22:04:16
Done.
| |
| 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 9 #include "ui/gfx/size.h" | 9 #include "ui/gfx/size.h" |
| 10 #include "webkit/glue/image_decoder.h" | 10 #include "webkit/glue/image_decoder.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 | 12 |
| 13 using WebKit::WebFrame; | 13 using WebKit::WebFrame; |
| 14 using WebKit::WebURLRequest; | 14 using WebKit::WebURLRequest; |
| 15 using WebKit::WebURLResponse; | 15 using WebKit::WebURLResponse; |
| 16 | 16 |
| 17 namespace webkit_glue { | 17 namespace webkit_glue { |
| 18 | 18 |
| 19 ImageResourceFetcher::ImageResourceFetcher( | 19 ImageResourceFetcher::ImageResourceFetcher( |
| 20 const GURL& image_url, | 20 const GURL& image_url, |
| 21 WebFrame* frame, | 21 WebFrame* frame, |
| 22 int id, | 22 int id, |
| 23 int image_size, | 23 int image_size, |
| 24 WebURLRequest::TargetType target_type, | 24 WebURLRequest::TargetType target_type, |
| 25 Callback* callback) | 25 const Callback& callback) |
| 26 : callback_(callback), | 26 : callback_(callback), |
| 27 id_(id), | 27 id_(id), |
| 28 image_url_(image_url), | 28 image_url_(image_url), |
| 29 image_size_(image_size) { | 29 image_size_(image_size) { |
| 30 fetcher_.reset(new ResourceFetcher( | 30 fetcher_.reset(new ResourceFetcher( |
| 31 image_url, frame, target_type, | 31 image_url, frame, target_type, |
| 32 NewCallback(this, &ImageResourceFetcher::OnURLFetchComplete))); | 32 base::Bind(&ImageResourceFetcher::OnURLFetchComplete, |
| 33 base::Unretained(this)))); | |
| 33 } | 34 } |
| 34 | 35 |
| 35 ImageResourceFetcher::~ImageResourceFetcher() { | 36 ImageResourceFetcher::~ImageResourceFetcher() { |
| 36 if (!fetcher_->completed()) | 37 if (!fetcher_->completed()) |
| 37 fetcher_->Cancel(); | 38 fetcher_->Cancel(); |
| 38 } | 39 } |
| 39 | 40 |
| 40 void ImageResourceFetcher::OnURLFetchComplete( | 41 void ImageResourceFetcher::OnURLFetchComplete( |
| 41 const WebURLResponse& response, | 42 const WebURLResponse& response, |
| 42 const std::string& data) { | 43 const std::string& data) { |
| 43 SkBitmap bitmap; | 44 SkBitmap bitmap; |
| 44 if (!response.isNull() && response.httpStatusCode() == 200) { | 45 if (!response.isNull() && response.httpStatusCode() == 200) { |
| 45 // Request succeeded, try to convert it to an image. | 46 // Request succeeded, try to convert it to an image. |
| 46 ImageDecoder decoder(gfx::Size(image_size_, image_size_)); | 47 ImageDecoder decoder(gfx::Size(image_size_, image_size_)); |
| 47 bitmap = decoder.Decode( | 48 bitmap = decoder.Decode( |
| 48 reinterpret_cast<const unsigned char*>(data.data()), data.size()); | 49 reinterpret_cast<const unsigned char*>(data.data()), data.size()); |
| 49 } // else case: | 50 } // else case: |
| 50 // If we get here, it means no image from server or couldn't decode the | 51 // If we get here, it means no image from server or couldn't decode the |
| 51 // response as an image. The delegate will see a null image, indicating | 52 // response as an image. The delegate will see a null image, indicating |
| 52 // that an error occurred. | 53 // that an error occurred. |
| 53 | 54 |
| 54 // Take care to clear callback_ before running the callback as it may lead to | 55 // Take care to clear callback_ before running the callback as it may lead to |
| 55 // our destruction. | 56 // our destruction. |
| 56 scoped_ptr<Callback> callback; | 57 Callback callback = callback_; |
| 57 callback.swap(callback_); | 58 callback_.Reset(); |
|
awong
2011/11/21 21:46:56
This works, but semantically is slightly funny. I
dcheng
2011/11/21 22:04:16
Done.
| |
| 58 callback->Run(this, bitmap); | 59 callback.Run(this, bitmap); |
| 59 } | 60 } |
| 60 | 61 |
| 61 } // namespace webkit_glue | 62 } // namespace webkit_glue |
| OLD | NEW |