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