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.
5 #include "content/renderer/fetchers/multi_resolution_image_resource_fetcher.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/WebKit/public/web/WebURLLoaderOptions.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/geometry/size.h"
17 using blink::WebFrame
;
18 using blink::WebURLLoaderOptions
;
19 using blink::WebURLRequest
;
20 using blink::WebURLResponse
;
24 MultiResolutionImageResourceFetcher::MultiResolutionImageResourceFetcher(
25 const GURL
& image_url
,
28 WebURLRequest::RequestContext request_context
,
29 blink::WebURLRequest::CachePolicy cache_policy
,
30 const Callback
& callback
)
31 : callback_(callback
),
34 image_url_(image_url
) {
35 fetcher_
.reset(ResourceFetcher::Create(image_url
));
37 WebURLLoaderOptions options
;
38 options
.allowCredentials
= true;
39 options
.crossOriginRequestPolicy
=
40 WebURLLoaderOptions::CrossOriginRequestPolicyAllow
;
41 fetcher_
->SetLoaderOptions(options
);
43 // To prevent cache tainting, the favicon requests have to by-pass the service
44 // workers. This should ideally not happen or at least not all the time.
45 // See https://crbug.com/448427
46 if (request_context
== WebURLRequest::RequestContextFavicon
)
47 fetcher_
->SetSkipServiceWorker(true);
49 fetcher_
->SetCachePolicy(cache_policy
);
54 WebURLRequest::FrameTypeNone
,
55 ResourceFetcher::FRAME_ASSOCIATED_LOADER
,
56 base::Bind(&MultiResolutionImageResourceFetcher::OnURLFetchComplete
,
57 base::Unretained(this)));
60 MultiResolutionImageResourceFetcher::~MultiResolutionImageResourceFetcher() {
63 void MultiResolutionImageResourceFetcher::OnURLFetchComplete(
64 const WebURLResponse
& response
,
65 const std::string
& data
) {
66 std::vector
<SkBitmap
> bitmaps
;
67 if (!response
.isNull()) {
68 http_status_code_
= response
.httpStatusCode();
69 GURL
url(response
.url());
70 if (http_status_code_
== 200 || url
.SchemeIsFile()) {
71 // Request succeeded, try to convert it to an image.
72 bitmaps
= ImageDecoder::DecodeAll(
73 reinterpret_cast<const unsigned char*>(data
.data()), data
.size());
76 // If we get here, it means no image from server or couldn't decode the
77 // response as an image. The delegate will see an empty vector.
79 // Take a reference to the callback as running the callback may lead to our
81 Callback callback
= callback_
;
82 callback
.Run(this, bitmaps
);
85 } // namespace content