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