| OLD | NEW |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef IOS_WEB_PUBLIC_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ | 5 #ifndef IOS_WEB_PUBLIC_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ |
| 6 #define IOS_WEB_PUBLIC_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ | 6 #define IOS_WEB_PUBLIC_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/mac/scoped_block.h" | 10 #include "base/mac/scoped_block.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 namespace web { | 27 namespace web { |
| 28 | 28 |
| 29 // Callback that informs of the download of an image encoded in |data|, | 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 | 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. | 31 // url is a data URL, |http_response_code| is always 200. |
| 32 using ImageFetchedCallback = void (^)(const GURL& url, | 32 using ImageFetchedCallback = void (^)(const GURL& url, |
| 33 int http_response_code, | 33 int http_response_code, |
| 34 NSData* data); | 34 NSData* data); |
| 35 | 35 |
| 36 // Callback that informs of the download of an image encoded in |data|, |
| 37 // downloaded from |url|, and with the http status |http_response_code|. If the |
| 38 // url is a data URL, |http_response_code| is always 200. |
| 39 using ImageFetchedCallbackWithMime = void (^)(const GURL& url, |
| 40 int http_response_code, |
| 41 const std::string& mime_type, |
| 42 NSData* data); |
| 43 |
| 36 // Utility class that will retrieve an image from an URL. The image is returned | 44 // 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 | 45 // as NSData which can be used with +[UIImage imageWithData:]. This class |
| 38 // usually returns the raw bytes retrieved from the network without any | 46 // usually returns the raw bytes retrieved from the network without any |
| 39 // processing, with the exception of WebP encoded images. Those are decoded and | 47 // processing, with the exception of WebP encoded images. Those are decoded and |
| 40 // then reencoded in a format suitable for UIImage. | 48 // then reencoded in a format suitable for UIImage. |
| 41 // An instance of this class can download a number of images at the same time. | 49 // An instance of this class can download a number of images at the same time. |
| 42 class ImageDataFetcher : public net::URLFetcherDelegate { | 50 class ImageDataFetcher : public net::URLFetcherDelegate { |
| 43 public: | 51 public: |
| 44 // The TaskRunner is used to eventually decode the image. | 52 // The TaskRunner is used to eventually decode the image. |
| 45 explicit ImageDataFetcher(const scoped_refptr<base::TaskRunner>& task_runner); | 53 explicit ImageDataFetcher(const scoped_refptr<base::TaskRunner>& task_runner); |
| 46 ~ImageDataFetcher() override; | 54 ~ImageDataFetcher() override; |
| 47 | 55 |
| 48 // Start downloading the image at the given |url|. The |callback| will be | 56 // 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 | 57 // called with the downloaded image, or nil if any error happened. The |
| 50 // |referrer| and |referrer_policy| will be passed on to the underlying | 58 // |referrer| and |referrer_policy| will be passed on to the underlying |
| 51 // URLFetcher. | 59 // URLFetcher. |
| 52 // This method assumes the request context getter has been set. | 60 // This method assumes the request context getter has been set. |
| 53 // (virtual for testing) | 61 // (virtual for testing) |
| 62 virtual void StartDownloadWithMime( |
| 63 const GURL& url, |
| 64 ImageFetchedCallbackWithMime callback, |
| 65 const std::string& referrer, |
| 66 net::URLRequest::ReferrerPolicy referrer_policy); |
| 67 |
| 68 // Start downloading the image at the given |url|. The |callback| will be |
| 69 // called with the downloaded image, or nil if any error happened. The |
| 70 // |referrer| and |referrer_policy| will be passed on to the underlying |
| 71 // URLFetcher. |
| 72 // This method assumes the request context getter has been set. |
| 73 // (virtual for testing) |
| 54 virtual void StartDownload(const GURL& url, | 74 virtual void StartDownload(const GURL& url, |
| 55 ImageFetchedCallback callback, | 75 ImageFetchedCallback callback, |
| 56 const std::string& referrer, | 76 const std::string& referrer, |
| 57 net::URLRequest::ReferrerPolicy referrer_policy); | 77 net::URLRequest::ReferrerPolicy referrer_policy); |
| 58 | 78 |
| 59 // Helper method to call StartDownload without a referrer. | 79 // Helper method to call StartDownload without a referrer. |
| 60 // (virtual for testing) | 80 // (virtual for testing) |
| 61 virtual void StartDownload(const GURL& url, ImageFetchedCallback callback); | 81 virtual void StartDownload(const GURL& url, ImageFetchedCallback callback); |
| 62 | 82 |
| 63 // A valid request context getter is required before starting the download. | 83 // A valid request context getter is required before starting the download. |
| 64 // (virtual for testing) | 84 // (virtual for testing) |
| 65 virtual void SetRequestContextGetter( | 85 virtual void SetRequestContextGetter( |
| 66 const scoped_refptr<net::URLRequestContextGetter>& | 86 const scoped_refptr<net::URLRequestContextGetter>& |
| 67 request_context_getter); | 87 request_context_getter); |
| 68 | 88 |
| 69 // net::URLFetcherDelegate: | 89 // net::URLFetcherDelegate: |
| 70 void OnURLFetchComplete(const net::URLFetcher* source) override; | 90 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 71 | 91 |
| 72 private: | 92 private: |
| 73 // Runs the callback with the given arguments. | 93 // Runs the callback with the given arguments. |
| 74 void RunCallback(const base::mac::ScopedBlock<ImageFetchedCallback>& callback, | 94 void RunCallback( |
| 75 const GURL& url, | 95 const base::mac::ScopedBlock<ImageFetchedCallbackWithMime>& callback, |
| 76 const int http_response_code, | 96 const GURL& url, |
| 77 NSData* data); | 97 const int http_response_code, |
| 98 const std::string& mime_type, |
| 99 NSData* data); |
| 78 | 100 |
| 79 // Tracks open download requests. The key is the URLFetcher object doing the | 101 // 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 | 102 // fetch; the value is the callback to use when the download request |
| 81 // completes. When a download request completes, the URLFetcher must be | 103 // completes. When a download request completes, the URLFetcher must be |
| 82 // deleted and the callback called and released. | 104 // deleted and the callback called and released. |
| 83 std::map<const net::URLFetcher*, ImageFetchedCallback> downloads_in_progress_; | 105 std::map<const net::URLFetcher*, ImageFetchedCallbackWithMime> |
| 106 downloads_in_progress_; |
| 84 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 107 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 85 | 108 |
| 86 // The task runner used to decode images if necessary. | 109 // The task runner used to decode images if necessary. |
| 87 const scoped_refptr<base::TaskRunner> task_runner_; | 110 const scoped_refptr<base::TaskRunner> task_runner_; |
| 88 | 111 |
| 89 // The WeakPtrFactory is used to cancel callbacks if ImageDataFetcher is | 112 // The WeakPtrFactory is used to cancel callbacks if ImageDataFetcher is |
| 90 // destroyed during WebP decoding. | 113 // destroyed during WebP decoding. |
| 91 base::WeakPtrFactory<ImageDataFetcher> weak_factory_; | 114 base::WeakPtrFactory<ImageDataFetcher> weak_factory_; |
| 92 }; | 115 }; |
| 93 | 116 |
| 94 #endif // IOS_WEB_PUBLIC_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ | 117 #endif // IOS_WEB_PUBLIC_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ |
| 95 | 118 |
| 96 } // namespace web | 119 } // namespace web |
| OLD | NEW |