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