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/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 // 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 | |
29 // url is a data URL, |http_response_code| is always 200. | |
30 using ImageFetchedCallback = | |
31 void (^)(const GURL& url, int http_response_code, NSData* data); | |
32 | |
33 // 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 // 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 // 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 class ImageFetcher : public net::URLFetcherDelegate { | |
40 public: | |
41 // The TaskRunner is used to eventually decode the image. | |
42 explicit ImageFetcher(const scoped_refptr<base::TaskRunner>& task_runner); | |
43 ~ImageFetcher() override; | |
44 | |
45 // Start downloading the image at the given |url|. The |callback| will be | |
46 // called with the downloaded image, or nil if any error happened. The | |
47 // |referrer| and |referrer_policy| will be passed on to the underlying | |
48 // URLFetcher. | |
49 // This method assumes the request context getter has been set. | |
50 // (virtual for testing) | |
51 virtual void StartDownload(const GURL& url, | |
52 ImageFetchedCallback callback, | |
53 const std::string& referrer, | |
54 net::URLRequest::ReferrerPolicy referrer_policy); | |
55 | |
56 // Helper method to call StartDownload without a referrer. | |
57 // (virtual for testing) | |
58 virtual void StartDownload(const GURL& url, ImageFetchedCallback callback); | |
59 | |
60 // A valid request context getter is required before starting the download. | |
61 // (virtual for testing) | |
62 virtual void SetRequestContextGetter(const scoped_refptr< | |
63 net::URLRequestContextGetter>& request_context_getter); | |
64 | |
65 // net::URLFetcherDelegate: | |
66 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
67 | |
68 private: | |
69 // Runs the callback with the given arguments. | |
70 void RunCallback(const base::mac::ScopedBlock<ImageFetchedCallback>& callback, | |
71 const GURL& url, | |
72 const int http_response_code, | |
73 NSData* data); | |
74 | |
75 // 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 | |
77 // completes. When a download request completes, the URLFetcher must be | |
78 // deleted and the callback called and released. | |
79 std::map<const net::URLFetcher*, ImageFetchedCallback> downloads_in_progress_; | |
80 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
81 | |
82 // The task runner used to decode images if necessary. | |
83 const scoped_refptr<base::TaskRunner> task_runner_; | |
84 | |
85 // The WeakPtrFactory is used to cancel callbacks if ImageFetcher is destroyed | |
86 // during WebP decoding. | |
87 base::WeakPtrFactory<ImageFetcher> weak_factory_; | |
88 }; | |
89 | |
90 #endif // IOS_CHROME_BROWSER_NET_IMAGE_FETCHER_H_ | |
OLD | NEW |