Chromium Code Reviews| 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 |
| 41 // Helper to fetches images data without a referrer. | |
|
Marc Treib
2017/02/02 15:19:13
s/fetches images/fetch image/
Though I might do t
gambard
2017/02/03 12:11:34
Done.
| |
| 42 void FetchImageData(const GURL& image_url, | |
| 43 const ImageDataFetcherCallback& callback); | |
| 44 | |
| 40 // Fetches the raw image bytes from the given |image_url| and calls the given | 45 // 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 | 46 // |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. | 47 // of an error an empty string is passed to the callback. |
| 48 // The |referrer| and |referrer_policy| will be passed on to the underlying | |
| 49 // URLFetcher. | |
| 43 void FetchImageData(const GURL& image_url, | 50 void FetchImageData(const GURL& image_url, |
| 44 const ImageDataFetcherCallback& callback); | 51 const ImageDataFetcherCallback& callback, |
| 52 const std::string& referrer, | |
| 53 net::URLRequest::ReferrerPolicy referrer_policy); | |
| 45 | 54 |
| 46 private: | 55 private: |
| 47 struct ImageDataFetcherRequest; | 56 struct ImageDataFetcherRequest; |
| 48 | 57 |
| 49 // Method inherited from URLFetcherDelegate | 58 // Method inherited from URLFetcherDelegate |
| 50 void OnURLFetchComplete(const net::URLFetcher* source) override; | 59 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 51 | 60 |
| 52 // All active image url requests. | 61 // All active image url requests. |
| 53 std::map<const net::URLFetcher*, std::unique_ptr<ImageDataFetcherRequest>> | 62 std::map<const net::URLFetcher*, std::unique_ptr<ImageDataFetcherRequest>> |
| 54 pending_requests_; | 63 pending_requests_; |
| 55 | 64 |
| 56 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 65 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 57 | 66 |
| 58 DataUseServiceName data_use_service_name_; | 67 DataUseServiceName data_use_service_name_; |
| 59 | 68 |
| 60 // The next ID to use for a newly created URLFetcher. Each URLFetcher gets an | 69 // 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 | 70 // 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 | 71 // 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 | 72 // get individual URLFetchers and modify their state. Outside of tests this ID |
| 64 // is not used. | 73 // is not used. |
| 65 int next_url_fetcher_id_; | 74 int next_url_fetcher_id_; |
| 66 | 75 |
| 67 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcher); | 76 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcher); |
| 68 }; | 77 }; |
| 69 | 78 |
| 70 } // namespace image_fetcher | 79 } // namespace image_fetcher |
| 71 | 80 |
| 72 #endif // COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ | 81 #endif // COMPONENTS_IMAGE_FETCHER_IMAGE_DATA_FETCHER_H_ |
| OLD | NEW |