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 3 // found in the LICENSE file. 4 5 #include "content/renderer/fetchers/image_resource_fetcher.h" 6 7 #include "base/bind.h" 8 #include "base/bind_helpers.h" 9 #include "base/debug/crash_logging.h" 10 #include "content/child/image_decoder.h" 11 #include "third_party/WebKit/public/web/WebFrame.h" 12 #include "third_party/skia/include/core/SkBitmap.h" 13 #include "ui/gfx/size.h" 14 15 using WebKit::WebFrame; 16 using WebKit::WebURLRequest; 17 using WebKit::WebURLResponse; 18 19 namespace content { 20 21 ImageResourceFetcher::ImageResourceFetcher( 22 const GURL& image_url, 23 WebFrame* frame, 24 int id, 25 int image_size, 26 WebURLRequest::TargetType target_type, 27 const Callback& callback) 28 : callback_(callback), 29 id_(id), 30 image_url_(image_url), 31 image_size_(image_size) { 32 fetcher_.reset(new ResourceFetcher( 33 image_url, frame, target_type, 34 base::Bind(&ImageResourceFetcher::OnURLFetchComplete, 35 base::Unretained(this)))); 36 37 // Set subresource URL for crash reporting. 38 base::debug::SetCrashKeyValue("subresource_url", image_url.spec()); 39 } 40 41 ImageResourceFetcher::~ImageResourceFetcher() { 42 if (!fetcher_->completed()) 43 fetcher_->Cancel(); 44 } 45 46 void ImageResourceFetcher::OnURLFetchComplete( 47 const WebURLResponse& response, 48 const std::string& data) { 49 SkBitmap bitmap; 50 if (!response.isNull() && response.httpStatusCode() == 200) { 51 // Request succeeded, try to convert it to an image. 52 ImageDecoder decoder(gfx::Size(image_size_, image_size_)); 53 bitmap = decoder.Decode( 54 reinterpret_cast<const unsigned char*>(data.data()), data.size()); 55 } // else case: 56 // If we get here, it means no image from server or couldn't decode the 57 // response as an image. The delegate will see a null image, indicating 58 // that an error occurred. 59 60 // Take a reference to the callback as running the callback may lead to our 61 // destruction. 62 Callback callback = callback_; 63 callback.Run(this, bitmap); 64 } 65 66 } // namespace content 67