• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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/multi_resolution_image_resource_fetcher.h"
6 
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "content/child/image_decoder.h"
10 #include "content/public/renderer/resource_fetcher.h"
11 #include "third_party/WebKit/public/platform/WebURLResponse.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/gfx/size.h"
15 
16 using blink::WebFrame;
17 using blink::WebURLRequest;
18 using blink::WebURLResponse;
19 
20 namespace content {
21 
MultiResolutionImageResourceFetcher(const GURL & image_url,WebFrame * frame,int id,WebURLRequest::RequestContext request_context,const Callback & callback)22 MultiResolutionImageResourceFetcher::MultiResolutionImageResourceFetcher(
23     const GURL& image_url,
24     WebFrame* frame,
25     int id,
26     WebURLRequest::RequestContext request_context,
27     const Callback& callback)
28     : callback_(callback),
29       id_(id),
30       http_status_code_(0),
31       image_url_(image_url) {
32   fetcher_.reset(ResourceFetcher::Create(image_url));
33   fetcher_->Start(
34       frame,
35       request_context,
36       WebURLRequest::FrameTypeNone,
37       ResourceFetcher::PLATFORM_LOADER,
38       base::Bind(&MultiResolutionImageResourceFetcher::OnURLFetchComplete,
39                  base::Unretained(this)));
40 }
41 
~MultiResolutionImageResourceFetcher()42 MultiResolutionImageResourceFetcher::~MultiResolutionImageResourceFetcher() {
43 }
44 
OnURLFetchComplete(const WebURLResponse & response,const std::string & data)45 void MultiResolutionImageResourceFetcher::OnURLFetchComplete(
46     const WebURLResponse& response,
47     const std::string& data) {
48   std::vector<SkBitmap> bitmaps;
49   if (!response.isNull()) {
50     http_status_code_ = response.httpStatusCode();
51     GURL url(response.url());
52     if (http_status_code_ == 200 || url.SchemeIsFile()) {
53       // Request succeeded, try to convert it to an image.
54       bitmaps = ImageDecoder::DecodeAll(
55           reinterpret_cast<const unsigned char*>(data.data()), data.size());
56     }
57   } // else case:
58     // If we get here, it means no image from server or couldn't decode the
59     // response as an image. The delegate will see an empty vector.
60 
61   // Take a reference to the callback as running the callback may lead to our
62   // destruction.
63   Callback callback = callback_;
64   callback.Run(this, bitmaps);
65 }
66 
67 }  // namespace content
68