Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: components/image_fetcher/core/image_data_fetcher.h

Issue 2781473003: Add |SetImageDownloadLimit| to ImageFetcher to limit downloaded bytes (Closed)
Patch Set: Add |SetImageDownloadLimit| to ImageDataFetcher Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_CORE_IMAGE_DATA_FETCHER_H_ 5 #ifndef COMPONENTS_IMAGE_FETCHER_CORE_IMAGE_DATA_FETCHER_H_
6 #define COMPONENTS_IMAGE_FETCHER_CORE_IMAGE_DATA_FETCHER_H_ 6 #define COMPONENTS_IMAGE_FETCHER_CORE_IMAGE_DATA_FETCHER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
(...skipping 24 matching lines...) Expand all
35 35
36 using DataUseServiceName = data_use_measurement::DataUseUserData::ServiceName; 36 using DataUseServiceName = data_use_measurement::DataUseUserData::ServiceName;
37 37
38 explicit ImageDataFetcher( 38 explicit ImageDataFetcher(
39 net::URLRequestContextGetter* url_request_context_getter); 39 net::URLRequestContextGetter* url_request_context_getter);
40 ~ImageDataFetcher() override; 40 ~ImageDataFetcher() override;
41 41
42 // Sets a service name against which to track data usage. 42 // Sets a service name against which to track data usage.
43 void SetDataUseServiceName(DataUseServiceName data_use_service_name); 43 void SetDataUseServiceName(DataUseServiceName data_use_service_name);
44 44
45 // Sets an upper limit for image downloads that is by default disabled.
Marc Treib 2017/03/27 13:16:42 nit: Remove "that is by default disabled", IMO tha
fhorschig 2017/03/27 14:33:43 Done.
46 // Setting |max_download_bytes| to a negative value will disable the limit.
Marc Treib 2017/03/27 13:16:42 Hm. Make it a base::Optional<int64_t> instead? IMO
fhorschig 2017/03/27 14:33:43 Done.
47 // Already running downloads are immediately affected.
48 void SetImageDownloadLimit(int64_t max_download_bytes) {
49 max_download_bytes_ = max_download_bytes < 0 ? 0 : max_download_bytes;
Marc Treib 2017/03/27 13:16:42 Move implementation to .cc file?
fhorschig 2017/03/27 14:33:43 Done.
50 }
51
45 // Fetches the raw image bytes from the given |image_url| and calls the given 52 // Fetches the raw image bytes from the given |image_url| and calls the given
46 // |callback|. The callback is run even if fetching the URL fails. In case 53 // |callback|. The callback is run even if fetching the URL fails. In case
47 // of an error an empty string is passed to the callback. 54 // of an error an empty string is passed to the callback.
48 void FetchImageData(const GURL& image_url, 55 void FetchImageData(const GURL& image_url,
49 const ImageDataFetcherCallback& callback); 56 const ImageDataFetcherCallback& callback);
50 57
51 // Like above, but lets the caller set a referrer. 58 // Like above, but lets the caller set a referrer.
52 void FetchImageData(const GURL& image_url, 59 void FetchImageData(const GURL& image_url,
53 const ImageDataFetcherCallback& callback, 60 const ImageDataFetcherCallback& callback,
54 const std::string& referrer, 61 const std::string& referrer,
55 net::URLRequest::ReferrerPolicy referrer_policy); 62 net::URLRequest::ReferrerPolicy referrer_policy);
56 63
57 private: 64 private:
58 struct ImageDataFetcherRequest; 65 struct ImageDataFetcherRequest;
59 66
60 // Method inherited from URLFetcherDelegate 67 // Methods inherited from URLFetcherDelegate
61 void OnURLFetchComplete(const net::URLFetcher* source) override; 68 void OnURLFetchComplete(const net::URLFetcher* source) override;
69 void OnURLFetchDownloadProgress(const net::URLFetcher* source,
70 int64_t current,
71 int64_t total,
72 int64_t current_network_bytes) override;
62 73
63 // All active image url requests. 74 // All active image url requests.
64 std::map<const net::URLFetcher*, std::unique_ptr<ImageDataFetcherRequest>> 75 std::map<const net::URLFetcher*, std::unique_ptr<ImageDataFetcherRequest>>
65 pending_requests_; 76 pending_requests_;
66 77
67 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 78 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
68 79
69 DataUseServiceName data_use_service_name_; 80 DataUseServiceName data_use_service_name_;
70 81
71 // The next ID to use for a newly created URLFetcher. Each URLFetcher gets an 82 // The next ID to use for a newly created URLFetcher. Each URLFetcher gets an
72 // id when it is created. The |url_fetcher_id_| is incremented by one for each 83 // id when it is created. The |url_fetcher_id_| is incremented by one for each
73 // newly created URLFetcher. The URLFetcher ID can be used during testing to 84 // newly created URLFetcher. The URLFetcher ID can be used during testing to
74 // get individual URLFetchers and modify their state. Outside of tests this ID 85 // get individual URLFetchers and modify their state. Outside of tests this ID
75 // is not used. 86 // is not used.
76 int next_url_fetcher_id_; 87 int next_url_fetcher_id_;
77 88
89 // Running requests exceeding this limit will be canceled immediately. By
Marc Treib 2017/03/27 13:16:42 Upper limit for the number of bytes to download pe
fhorschig 2017/03/27 14:33:43 Done.
90 // default negative and therefore disabled.
91 int64_t max_download_bytes_;
92
78 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcher); 93 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcher);
79 }; 94 };
80 95
81 } // namespace image_fetcher 96 } // namespace image_fetcher
82 97
83 #endif // COMPONENTS_IMAGE_FETCHER_CORE_IMAGE_DATA_FETCHER_H_ 98 #endif // COMPONENTS_IMAGE_FETCHER_CORE_IMAGE_DATA_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698