| 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 "components/data_use_measurement/core/data_use_user_data.h" | 15 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
| 17 #include "net/url_request/url_request.h" |
| 17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 class URLFetcher; | 21 class URLFetcher; |
| 21 class URLRequestContextGetter; | 22 class URLRequestContextGetter; |
| 22 } // namespace net | 23 } // namespace net |
| 23 | 24 |
| 24 namespace image_fetcher { | 25 namespace image_fetcher { |
| 25 | 26 |
| 26 class ImageDataFetcher : public net::URLFetcherDelegate { | 27 class ImageDataFetcher : public net::URLFetcherDelegate { |
| 27 public: | 28 public: |
| 28 using ImageDataFetcherCallback = | 29 using ImageDataFetcherCallback = |
| 29 base::Callback<void(const std::string& image_data)>; | 30 base::Callback<void(const std::string& image_data)>; |
| 30 | 31 |
| 31 using DataUseServiceName = data_use_measurement::DataUseUserData::ServiceName; | 32 using DataUseServiceName = data_use_measurement::DataUseUserData::ServiceName; |
| 32 | 33 |
| 33 explicit ImageDataFetcher( | 34 explicit ImageDataFetcher( |
| 34 net::URLRequestContextGetter* url_request_context_getter); | 35 net::URLRequestContextGetter* url_request_context_getter); |
| 35 ~ImageDataFetcher() override; | 36 ~ImageDataFetcher() override; |
| 36 | 37 |
| 37 // Sets a service name against which to track data usage. | 38 // Sets a service name against which to track data usage. |
| 38 void SetDataUseServiceName(DataUseServiceName data_use_service_name); | 39 void SetDataUseServiceName(DataUseServiceName data_use_service_name); |
| 39 | 40 |
| 40 // Fetches the raw image bytes from the given |image_url| and calls the given | 41 // Fetches the raw image bytes from the given |image_url| and calls the given |
| 41 // |callback|. The callback is run even if fetching the URL fails. In case | 42 // |callback|. The callback is run even if fetching the URL fails. In case |
| 42 // of an error an empty string is passed to the callback. | 43 // of an error an empty string is passed to the callback. |
| 43 void FetchImageData(const GURL& image_url, | 44 void FetchImageData(const GURL& image_url, |
| 44 const ImageDataFetcherCallback& callback); | 45 const ImageDataFetcherCallback& callback); |
| 45 | 46 |
| 47 // Like above, but lets the caller set a referrer. |
| 48 void FetchImageData(const GURL& image_url, |
| 49 const ImageDataFetcherCallback& callback, |
| 50 const std::string& referrer, |
| 51 net::URLRequest::ReferrerPolicy referrer_policy); |
| 52 |
| 46 private: | 53 private: |
| 47 struct ImageDataFetcherRequest; | 54 struct ImageDataFetcherRequest; |
| 48 | 55 |
| 49 // Method inherited from URLFetcherDelegate | 56 // Method inherited from URLFetcherDelegate |
| 50 void OnURLFetchComplete(const net::URLFetcher* source) override; | 57 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 51 | 58 |
| 52 // All active image url requests. | 59 // All active image url requests. |
| 53 std::map<const net::URLFetcher*, std::unique_ptr<ImageDataFetcherRequest>> | 60 std::map<const net::URLFetcher*, std::unique_ptr<ImageDataFetcherRequest>> |
| 54 pending_requests_; | 61 pending_requests_; |
| 55 | 62 |
| 56 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 63 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 57 | 64 |
| 58 DataUseServiceName data_use_service_name_; | 65 DataUseServiceName data_use_service_name_; |
| 59 | 66 |
| 60 // The next ID to use for a newly created URLFetcher. Each URLFetcher gets an | 67 // The next ID to use for a newly created URLFetcher. Each URLFetcher gets an |
| 61 // id when it is created. The |url_fetcher_id_| is incremented by one for each | 68 // id when it is created. The |url_fetcher_id_| is incremented by one for each |
| 62 // newly created URLFetcher. The URLFetcher ID can be used during testing to | 69 // newly created URLFetcher. The URLFetcher ID can be used during testing to |
| 63 // get individual URLFetchers and modify their state. Outside of tests this ID | 70 // get individual URLFetchers and modify their state. Outside of tests this ID |
| 64 // is not used. | 71 // is not used. |
| 65 int next_url_fetcher_id_; | 72 int next_url_fetcher_id_; |
| 66 | 73 |
| 67 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcher); | 74 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcher); |
| 68 }; | 75 }; |
| 69 | 76 |
| 70 } // namespace image_fetcher | 77 } // namespace image_fetcher |
| 71 | 78 |
| 72 #endif // COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ | 79 #endif // COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ |
| OLD | NEW |