Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / net / image_fetcher.mm
blobedf205cdc46046e12b8888eeeaf1bf60004f5c29
1 // Copyright 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 #import "ios/chrome/browser/net/image_fetcher.h"
7 #import <Foundation/Foundation.h>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/task_runner.h"
13 #include "components/webp_transcode/webp_decoder.h"
14 #include "ios/web/public/web_thread.h"
15 #include "net/base/load_flags.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_request_context_getter.h"
20 namespace {
22 class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate {
23  public:
24   NSData* data() const { return decoded_image_; }
26   // WebpDecoder::Delegate methods
27   void OnFinishedDecoding(bool success) override {
28     if (!success)
29       decoded_image_.reset();
30   }
31   void SetImageFeatures(
32       size_t total_size,
33       webp_transcode::WebpDecoder::DecodedImageFormat format) override {
34     decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]);
35   }
36   void OnDataDecoded(NSData* data) override {
37     DCHECK(decoded_image_);
38     [decoded_image_ appendData:data];
39   }
40  private:
41   ~WebpDecoderDelegate() override {}
42   base::scoped_nsobject<NSMutableData> decoded_image_;
45 // Content-type header for WebP images.
46 static const char kWEBPMimeType[] = "image/webp";
48 // Returns a NSData object containing the decoded image.
49 // Returns nil in case of failure.
50 base::scoped_nsobject<NSData> DecodeWebpImage(
51     const base::scoped_nsobject<NSData>& webp_image) {
52   scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate);
53   scoped_refptr<webp_transcode::WebpDecoder> decoder(
54       new webp_transcode::WebpDecoder(delegate.get()));
55   decoder->OnDataReceived(webp_image);
56   DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed.";
57   return base::scoped_nsobject<NSData>([delegate->data() retain]);
60 }  // namespace
62 namespace image_fetcher {
64 ImageFetcher::ImageFetcher(const scoped_refptr<base::TaskRunner>& task_runner)
65     : request_context_getter_(nullptr),
66       task_runner_(task_runner),
67       weak_factory_(this) {
68   DCHECK(task_runner_.get());
71 ImageFetcher::~ImageFetcher() {
72   // Delete all the entries in the |downloads_in_progress_| map.  This will in
73   // turn cancel all of the requests.
74   for (const auto& pair : downloads_in_progress_) {
75     [pair.second release];
76     delete pair.first;
77   }
80 void ImageFetcher::StartDownload(
81     const GURL& url,
82     ImageFetchedCallback callback,
83     const std::string& referrer,
84     net::URLRequest::ReferrerPolicy referrer_policy) {
85   DCHECK(request_context_getter_.get());
86   net::URLFetcher* fetcher =
87       net::URLFetcher::Create(url, net::URLFetcher::GET, this).release();
88   downloads_in_progress_[fetcher] = [callback copy];
89   fetcher->SetLoadFlags(
90       net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES |
91       net::LOAD_DO_NOT_SEND_AUTH_DATA);
92   fetcher->SetRequestContext(request_context_getter_.get());
93   fetcher->SetReferrer(referrer);
94   fetcher->SetReferrerPolicy(referrer_policy);
95   fetcher->Start();
98 void ImageFetcher::StartDownload(
99     const GURL& url, ImageFetchedCallback callback) {
100   ImageFetcher::StartDownload(
101       url, callback, std::string(), net::URLRequest::NEVER_CLEAR_REFERRER);
104 // Delegate callback that is called when URLFetcher completes.  If the image
105 // was fetched successfully, creates a new NSData and returns it to the
106 // callback, otherwise returns nil to the callback.
107 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) {
108   if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) {
109     LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher;
110     return;
111   }
113   // Ensures that |fetcher| will be deleted in the event of early return.
114   scoped_ptr<const net::URLFetcher> fetcher_deleter(fetcher);
116   // Retrieves the callback and ensures that it will be deleted in the event
117   // of early return.
118   base::mac::ScopedBlock<ImageFetchedCallback> callback(
119       downloads_in_progress_[fetcher]);
121   // Remove |fetcher| from the map.
122   downloads_in_progress_.erase(fetcher);
124   // Make sure the request was successful. For "data" requests, the response
125   // code has no meaning, because there is no actual server (data is encoded
126   // directly in the URL). In that case, set the response code to 200 (OK).
127   const GURL& original_url = fetcher->GetOriginalURL();
128   const int http_response_code = original_url.SchemeIs("data") ?
129       200 : fetcher->GetResponseCode();
130   if (http_response_code != 200) {
131     (callback.get())(original_url, http_response_code, nil);
132     return;
133   }
135   std::string response;
136   if (!fetcher->GetResponseAsString(&response)) {
137     (callback.get())(original_url, http_response_code, nil);
138     return;
139   }
141   // Create a NSData from the returned data and notify the callback.
142   base::scoped_nsobject<NSData> data([[NSData alloc]
143       initWithBytes:reinterpret_cast<const unsigned char*>(response.data())
144              length:response.size()]);
146   if (fetcher->GetResponseHeaders()) {
147     std::string mime_type;
148     fetcher->GetResponseHeaders()->GetMimeType(&mime_type);
149     if (mime_type == kWEBPMimeType) {
150       base::PostTaskAndReplyWithResult(task_runner_.get(),
151                                        FROM_HERE,
152                                        base::Bind(&DecodeWebpImage, data),
153                                        base::Bind(&ImageFetcher::RunCallback,
154                                                   weak_factory_.GetWeakPtr(),
155                                                   callback,
156                                                   original_url,
157                                                   http_response_code));
158       return;
159     }
160   }
161   (callback.get())(original_url, http_response_code, data);
164 void ImageFetcher::RunCallback(
165     const base::mac::ScopedBlock<ImageFetchedCallback>& callback,
166     const GURL& url,
167     int http_response_code,
168     NSData* data) {
169   (callback.get())(url, http_response_code, data);
172 void ImageFetcher::SetRequestContextGetter(
173     const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) {
174   request_context_getter_ = request_context_getter;
177 }  // namespace image_fetcher