OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ | 5 #ifndef COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ |
6 #define COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ | 6 #define COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "net/url_request/url_fetcher_delegate.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
| 19 class URLFetcher; |
18 class URLRequestContextGetter; | 20 class URLRequestContextGetter; |
19 } // namespace net | 21 } // namespace net |
20 | 22 |
21 namespace image_fetcher { | 23 namespace image_fetcher { |
22 | 24 |
23 class ImageDataFetcher { | 25 class ImageDataFetcher : public net::URLFetcherDelegate { |
24 public: | 26 public: |
25 using ImageDataFetcherCallback = | 27 using ImageDataFetcherCallback = |
26 base::Callback<void(const std::string& image_data)>; | 28 base::Callback<void(const std::string& image_data)>; |
27 | 29 |
28 explicit ImageDataFetcher( | 30 explicit ImageDataFetcher( |
29 net::URLRequestContextGetter* url_request_context_getter); | 31 net::URLRequestContextGetter* url_request_context_getter); |
30 ~ImageDataFetcher(); | 32 ~ImageDataFetcher() override; |
31 | 33 |
32 // Fetches the raw image bytes from the given |image_url| and calls the given | 34 // Fetches the raw image bytes from the given |image_url| and calls the given |
33 // |callback|. The callback is run even if fetching the URL fails. In case | 35 // |callback|. The callback is run even if fetching the URL fails. In case |
34 // of an error an empty string is passed to the callback. | 36 // of an error an empty string is passed to the callback. |
35 void FetchImageData(const GURL& image_url, | 37 void FetchImageData(const GURL& image_url, |
36 const ImageDataFetcherCallback& callback); | 38 const ImageDataFetcherCallback& callback); |
37 | 39 |
38 private: | 40 private: |
39 class ImageDataFetcherRequest; | 41 struct ImageDataFetcherRequest; |
40 | 42 |
41 // Removes the ImageDataFetcherRequest for the given |image_url| from the | 43 // Method inherited from URLFetcherDelegate |
42 // internal request queue. | 44 void OnURLFetchComplete(const net::URLFetcher* source) override; |
43 void RemoveImageDataFetcherRequest(const GURL& image_url); | |
44 | 45 |
45 // All active image url requests. | 46 // All active image url requests. |
46 std::map<const GURL, std::unique_ptr<ImageDataFetcherRequest>> | 47 std::map<const net::URLFetcher*, std::unique_ptr<ImageDataFetcherRequest>> |
47 pending_requests_; | 48 pending_requests_; |
48 | 49 |
49 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 50 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
50 | 51 |
51 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcher); | 52 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcher); |
52 }; | 53 }; |
53 | 54 |
54 } // namespace image_fetcher | 55 } // namespace image_fetcher |
55 | 56 |
56 #endif // COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ | 57 #endif // COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ |
OLD | NEW |