Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / ios / chrome / browser / net / image_fetcher.h
blobe579a362cb831980c6102c7e80a1c87553bede47
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.
5 #ifndef IOS_CHROME_BROWSER_NET_IMAGE_FETCHER_H_
6 #define IOS_CHROME_BROWSER_NET_IMAGE_FETCHER_H_
8 #include <map>
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"
16 class GURL;
17 @class NSData;
19 namespace base {
20 class TaskRunner;
23 namespace net {
24 class URLRequestContextGetter;
27 namespace image_fetcher {
29 // Callback that informs of the download of an image encoded in |data|,
30 // downloaded from |url|, and with the http status |http_response_code|. If the
31 // url is a data URL, |http_response_code| is always 200.
32 using ImageFetchedCallback =
33 void (^)(const GURL& url, int http_response_code, NSData* data);
35 // Utility class that will retrieve an image from an URL. The image is returned
36 // as NSData which can be used with +[UIImage imageWithData:]. This class
37 // usually returns the raw bytes retrieved from the network without any
38 // processing, with the exception of WebP encoded images. Those are decoded and
39 // then reencoded in a format suitable for UIImage.
40 // An instance of this class can download a number of images at the same time.
41 class ImageFetcher : public net::URLFetcherDelegate {
42 public:
43 // The TaskRunner is used to eventually decode the image.
44 explicit ImageFetcher(const scoped_refptr<base::TaskRunner>& task_runner);
45 ~ImageFetcher() override;
47 // Start downloading the image at the given |url|. The |callback| will be
48 // called with the downloaded image, or nil if any error happened. The
49 // |referrer| and |referrer_policy| will be passed on to the underlying
50 // URLFetcher.
51 // This method assumes the request context getter has been set.
52 // (virtual for testing)
53 virtual void StartDownload(const GURL& url,
54 ImageFetchedCallback callback,
55 const std::string& referrer,
56 net::URLRequest::ReferrerPolicy referrer_policy);
58 // Helper method to call StartDownload without a referrer.
59 // (virtual for testing)
60 virtual void StartDownload(const GURL& url, ImageFetchedCallback callback);
62 // A valid request context getter is required before starting the download.
63 // (virtual for testing)
64 virtual void SetRequestContextGetter(const scoped_refptr<
65 net::URLRequestContextGetter>& request_context_getter);
67 // net::URLFetcherDelegate:
68 void OnURLFetchComplete(const net::URLFetcher* source) override;
70 private:
71 // Runs the callback with the given arguments.
72 void RunCallback(const base::mac::ScopedBlock<ImageFetchedCallback>& callback,
73 const GURL& url,
74 const int http_response_code,
75 NSData* data);
77 // Tracks open download requests. The key is the URLFetcher object doing the
78 // fetch; the value is the callback to use when the download request
79 // completes. When a download request completes, the URLFetcher must be
80 // deleted and the callback called and released.
81 std::map<const net::URLFetcher*, ImageFetchedCallback> downloads_in_progress_;
82 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
84 // The task runner used to decode images if necessary.
85 const scoped_refptr<base::TaskRunner> task_runner_;
87 // The WeakPtrFactory is used to cancel callbacks if ImageFetcher is destroyed
88 // during WebP decoding.
89 base::WeakPtrFactory<ImageFetcher> weak_factory_;
92 } // namespace image_fetcher
94 #endif // IOS_CHROME_BROWSER_NET_IMAGE_FETCHER_H_