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