OLD | NEW |
(Empty) | |
| 1 // Copyright 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 #ifndef COMPONENTS_IMAGE_FETCHER_IOS_RAW_IMAGE_FETCHER_H_ |
| 6 #define COMPONENTS_IMAGE_FETCHER_IOS_RAW_IMAGE_FETCHER_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/mac/scoped_block.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "net/url_request/url_fetcher_delegate.h" |
| 14 #include "net/url_request/url_request.h" |
| 15 |
| 16 class GURL; |
| 17 @class NSData; |
| 18 |
| 19 namespace base { |
| 20 class TaskRunner; |
| 21 } |
| 22 |
| 23 namespace net { |
| 24 class URLRequestContextGetter; |
| 25 } |
| 26 |
| 27 namespace image_fetcher { |
| 28 |
| 29 // Callback that informs of the download of an image encoded in |data|, |
| 30 // downloaded from |url|, and with the http status |http_response_code|. If the |
| 31 // url is a data URL, |http_response_code| is always 200. |
| 32 using ImageFetchedCallback = void (^)(const GURL& url, |
| 33 int http_response_code, |
| 34 NSData* data); |
| 35 |
| 36 // Utility class that will retrieve an image from an URL. The image is returned |
| 37 // as NSData which can be used with +[UIImage imageWithData:]. This class |
| 38 // usually returns the raw bytes retrieved from the network without any |
| 39 // processing, with the exception of WebP encoded images. Those are decoded and |
| 40 // then reencoded in a format suitable for UIImage. |
| 41 // An instance of this class can download a number of images at the same time. |
| 42 class RawImageFetcher : public net::URLFetcherDelegate { |
| 43 public: |
| 44 // The TaskRunner is used to eventually decode the image. |
| 45 explicit RawImageFetcher(const scoped_refptr<base::TaskRunner>& task_runner); |
| 46 ~RawImageFetcher() override; |
| 47 |
| 48 // Start downloading the image at the given |url|. The |callback| will be |
| 49 // called with the downloaded image, or nil if any error happened. The |
| 50 // |referrer| and |referrer_policy| will be passed on to the underlying |
| 51 // URLFetcher. |
| 52 // This method assumes the request context getter has been set. |
| 53 // (virtual for testing) |
| 54 virtual void StartDownload(const GURL& url, |
| 55 ImageFetchedCallback callback, |
| 56 const std::string& referrer, |
| 57 net::URLRequest::ReferrerPolicy referrer_policy); |
| 58 |
| 59 // Helper method to call StartDownload without a referrer. |
| 60 // (virtual for testing) |
| 61 virtual void StartDownload(const GURL& url, ImageFetchedCallback callback); |
| 62 |
| 63 // A valid request context getter is required before starting the download. |
| 64 // (virtual for testing) |
| 65 virtual void SetRequestContextGetter( |
| 66 const scoped_refptr<net::URLRequestContextGetter>& |
| 67 request_context_getter); |
| 68 |
| 69 // net::URLFetcherDelegate: |
| 70 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 71 |
| 72 private: |
| 73 // Runs the callback with the given arguments. |
| 74 void RunCallback(const base::mac::ScopedBlock<ImageFetchedCallback>& callback, |
| 75 const GURL& url, |
| 76 const int http_response_code, |
| 77 NSData* data); |
| 78 |
| 79 // Tracks open download requests. The key is the URLFetcher object doing the |
| 80 // fetch; the value is the callback to use when the download request |
| 81 // completes. When a download request completes, the URLFetcher must be |
| 82 // deleted and the callback called and released. |
| 83 std::map<const net::URLFetcher*, ImageFetchedCallback> downloads_in_progress_; |
| 84 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 85 |
| 86 // The task runner used to decode images if necessary. |
| 87 const scoped_refptr<base::TaskRunner> task_runner_; |
| 88 |
| 89 // The WeakPtrFactory is used to cancel callbacks if RawImageFetcher is |
| 90 // destroyed during WebP decoding. |
| 91 base::WeakPtrFactory<RawImageFetcher> weak_factory_; |
| 92 }; |
| 93 |
| 94 } // namespace image_fetcher |
| 95 |
| 96 #endif // COMPONENTS_IMAGE_FETCHER_IOS_RAW_IMAGE_FETCHER_H_ |
OLD | NEW |