Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: ios/chrome/browser/net/image_fetcher.h

Issue 805713004: Cleanup image_fetcher::ImageFetcher (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ios/chrome/browser/net/image_fetcher.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 12 #include "base/memory/weak_ptr.h"
14 #include "net/url_request/url_fetcher_delegate.h" 13 #include "net/url_request/url_fetcher_delegate.h"
15 #include "net/url_request/url_request.h" 14 #include "net/url_request/url_request.h"
16 #include "net/url_request/url_request_context_getter.h"
justincohen 2014/12/17 13:52:39 I see lots of these with this removal: In file in
17 #include "url/gurl.h"
18 15
16 class GURL;
19 @class NSData; 17 @class NSData;
20 18
19 namespace base {
20 class SequencedWorkerPool;
21 }
22
23 namespace net {
24 class URLRequestContextGetter;
25 }
26
21 namespace image_fetcher { 27 namespace image_fetcher {
22 28
23 // 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|,
24 // 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
25 // url is a data URL, |http_response_code| is always 200. 31 // url is a data URL, |http_response_code| is always 200.
26 typedef void (^Callback)(const GURL& url, int http_response_code, NSData* data); 32 using ImageFetchedCallback =
33 void (^)(const GURL& url, int http_response_code, NSData* data);
27 34
28 // Utility class that will retrieve an image from an URL. The image is returned 35 // 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 36 // as NSData which can be used with +[UIImage imageWithData:]. This class
30 // usually returns the raw bytes retrieved from the network without any 37 // usually returns the raw bytes retrieved from the network without any
31 // processing with the exception of WebP encoded images. Those are decoded and 38 // processing, with the exception of WebP encoded images. Those are decoded and
32 // then reencoded in a format suitable for UIImage. 39 // then reencoded in a format suitable for UIImage.
33 // An instance of this class can download a number of images at the same time. 40 // An instance of this class can download a number of images at the same time.
34 class ImageFetcher : public net::URLFetcherDelegate { 41 class ImageFetcher : public net::URLFetcherDelegate {
35 public: 42 public:
36 // The WorkerPool is used to eventually decode the image. 43 // The WorkerPool is used to eventually decode the image.
37 explicit ImageFetcher( 44 explicit ImageFetcher(
38 const scoped_refptr<base::SequencedWorkerPool> decoding_pool); 45 const scoped_refptr<base::SequencedWorkerPool>& decoding_pool);
39 ~ImageFetcher() override; 46 ~ImageFetcher() override;
40 47
41 // Start downloading the image at the given |url|. The |callback| will be 48 // 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 49 // called with the downloaded image, or nil if any error happened. The
43 // |referrer| and |referrer_policy| will be passed on to the underlying 50 // |referrer| and |referrer_policy| will be passed on to the underlying
44 // URLFetcher. 51 // URLFetcher.
45 // This method assumes the request context getter has been set. 52 // This method assumes the request context getter has been set.
46 // (virtual for testing) 53 // (virtual for testing)
47 virtual void StartDownload(const GURL& url, 54 virtual void StartDownload(const GURL& url,
48 Callback callback, 55 ImageFetchedCallback callback,
49 const std::string& referrer, 56 const std::string& referrer,
50 net::URLRequest::ReferrerPolicy referrer_policy); 57 net::URLRequest::ReferrerPolicy referrer_policy);
51 58
52 // Helper method to call StartDownload without a referrer. 59 // Helper method to call StartDownload without a referrer.
53 // (virtual for testing) 60 // (virtual for testing)
54 virtual void StartDownload(const GURL& url, Callback callback); 61 virtual void StartDownload(const GURL& url, ImageFetchedCallback callback);
55 62
56 // A valid request context getter is required before starting the download. 63 // A valid request context getter is required before starting the download.
57 // (virtual for testing) 64 // (virtual for testing)
58 virtual void SetRequestContextGetter( 65 virtual void SetRequestContextGetter(const scoped_refptr<
59 net::URLRequestContextGetter* request_context_getter); 66 net::URLRequestContextGetter>& request_context_getter);
60 67
61 // content::URLFetcherDelegate methods. 68 // net::URLFetcherDelegate:
62 void OnURLFetchComplete(const net::URLFetcher* source) override; 69 void OnURLFetchComplete(const net::URLFetcher* source) override;
63 70
64 private: 71 private:
65 // Runs the callback with the given arguments. 72 // Runs the callback with the given arguments.
66 void RunCallback(const base::mac::ScopedBlock<Callback>& callback, 73 void RunCallback(const base::mac::ScopedBlock<ImageFetchedCallback>& callback,
67 const GURL& url, 74 const GURL& url,
68 const int http_response_code, 75 const int http_response_code,
69 NSData* data); 76 NSData* data);
70 77
71 // Tracks open download requests. The key is the URLFetcher object doing the 78 // 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 79 // fetch; the value is the callback to use when the download request
73 // completes. When a download request completes, the URLFetcher must be 80 // completes. When a download request completes, the URLFetcher must be
74 // deleted and the callback called and released. 81 // deleted and the callback called and released.
75 std::map<const net::URLFetcher*, Callback> downloads_in_progress_; 82 std::map<const net::URLFetcher*, ImageFetchedCallback> downloads_in_progress_;
76 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 83 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
77 84
78 // The WeakPtrFactory is used to cancel callbacks if ImageFetcher is destroyed 85 // The WeakPtrFactory is used to cancel callbacks if ImageFetcher is destroyed
79 // during WebP decoding. 86 // during WebP decoding.
80 base::WeakPtrFactory<ImageFetcher> weak_factory_; 87 base::WeakPtrFactory<ImageFetcher> weak_factory_;
81 88
82 // The pool used to decode images if necessary. 89 // The pool used to decode images if necessary.
83 const scoped_refptr<base::SequencedWorkerPool> decoding_pool_; 90 const scoped_refptr<base::SequencedWorkerPool> decoding_pool_;
84 }; 91 };
85 92
86 } // namespace image_fetcher 93 } // namespace image_fetcher
87 94
88 #endif // IOS_CHROME_BROWSER_NET_IMAGE_FETCHER_H_ 95 #endif // IOS_CHROME_BROWSER_NET_IMAGE_FETCHER_H_
OLDNEW
« no previous file with comments | « no previous file | ios/chrome/browser/net/image_fetcher.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698