| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 5 #ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
| 6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "components/precache/core/fetcher_pool.h" | 21 #include "components/precache/core/fetcher_pool.h" |
| 22 #include "components/precache/core/proto/quota.pb.h" |
| 22 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
| 23 #include "net/url_request/url_fetcher_delegate.h" | 24 #include "net/url_request/url_fetcher_delegate.h" |
| 24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 25 | 26 |
| 26 namespace base { | 27 namespace base { |
| 27 class SingleThreadTaskRunner; | 28 class SingleThreadTaskRunner; |
| 28 } | 29 } |
| 29 | 30 |
| 30 namespace net { | 31 namespace net { |
| 31 class URLRequestContextGetter; | 32 class URLRequestContextGetter; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 148 |
| 148 // Stops all precaching work. The PreacheFetcher should not be used after | 149 // Stops all precaching work. The PreacheFetcher should not be used after |
| 149 // calling this method. | 150 // calling this method. |
| 150 std::unique_ptr<PrecacheUnfinishedWork> CancelPrecaching(); | 151 std::unique_ptr<PrecacheUnfinishedWork> CancelPrecaching(); |
| 151 | 152 |
| 152 private: | 153 private: |
| 153 friend class PrecacheFetcherTest; | 154 friend class PrecacheFetcherTest; |
| 154 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, FetcherPoolMaxLimitReached); | 155 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, FetcherPoolMaxLimitReached); |
| 155 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, | 156 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, |
| 156 CancelPrecachingAfterAllManifestFetch); | 157 CancelPrecachingAfterAllManifestFetch); |
| 158 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, DailyQuota); |
| 157 | 159 |
| 158 // Notifies the precache delete that precaching is done, and report | 160 // Notifies the precache delete that precaching is done, and report |
| 159 // completion statistics. | 161 // completion statistics. |
| 160 void NotifyDone(size_t remaining_manifest_urls_to_fetch, | 162 void NotifyDone(size_t remaining_manifest_urls_to_fetch, |
| 161 size_t remaining_resource_urls_to_fetch); | 163 size_t remaining_resource_urls_to_fetch); |
| 162 | 164 |
| 163 // Fetches the next resource or manifest URL, if any remain. Fetching is done | 165 // Fetches the next resource or manifest URL, if any remain. Fetching is done |
| 164 // sequentially and depth-first: all resources are fetched for a manifest | 166 // sequentially and depth-first: all resources are fetched for a manifest |
| 165 // before the next manifest is fetched. This is done to limit the length of | 167 // before the next manifest is fetched. This is done to limit the length of |
| 166 // the |resource_urls_to_fetch_| list, reducing the memory usage. | 168 // the |resource_urls_to_fetch_| list, reducing the memory usage. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 185 | 187 |
| 186 // Called when a resource has been fetched. | 188 // Called when a resource has been fetched. |
| 187 void OnResourceFetchComplete(const Fetcher& source); | 189 void OnResourceFetchComplete(const Fetcher& source); |
| 188 | 190 |
| 189 // Adds up the response sizes. | 191 // Adds up the response sizes. |
| 190 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); | 192 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); |
| 191 | 193 |
| 192 // Callback invoked when the manifest info for all the top hosts is retrieved. | 194 // Callback invoked when the manifest info for all the top hosts is retrieved. |
| 193 void OnManifestInfoRetrieved(std::deque<ManifestHostInfo> manifests_info); | 195 void OnManifestInfoRetrieved(std::deque<ManifestHostInfo> manifests_info); |
| 194 | 196 |
| 197 // Callback invoked when the quota is retrieved. |
| 198 void OnQuotaInfoRetrieved(const PrecacheQuota& quota); |
| 199 |
| 195 // The request context used when fetching URLs. | 200 // The request context used when fetching URLs. |
| 196 const scoped_refptr<net::URLRequestContextGetter> request_context_; | 201 const scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 197 | 202 |
| 198 // The custom URL to use when fetching the config. If not provided, the | 203 // The custom URL to use when fetching the config. If not provided, the |
| 199 // default flag-specified URL will be used. | 204 // default flag-specified URL will be used. |
| 200 const GURL config_url_; | 205 const GURL config_url_; |
| 201 | 206 |
| 202 // The custom URL prefix to use when fetching manifests. If not provided, the | 207 // The custom URL prefix to use when fetching manifests. If not provided, the |
| 203 // default flag-specified prefix will be used. | 208 // default flag-specified prefix will be used. |
| 204 const std::string manifest_url_prefix_; | 209 const std::string manifest_url_prefix_; |
| 205 | 210 |
| 206 // PrecacheDatabase should be accessed on the DB thread. | 211 // PrecacheDatabase should be accessed on the DB thread. |
| 207 base::WeakPtr<PrecacheDatabase> precache_database_; | 212 base::WeakPtr<PrecacheDatabase> precache_database_; |
| 208 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_; | 213 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_; |
| 209 | 214 |
| 210 // Non-owning pointer. Should not be NULL. | 215 // Non-owning pointer. Should not be NULL. |
| 211 PrecacheDelegate* precache_delegate_; | 216 PrecacheDelegate* precache_delegate_; |
| 212 | 217 |
| 213 std::deque<ManifestHostInfo> top_hosts_to_fetch_; | 218 std::deque<ManifestHostInfo> top_hosts_to_fetch_; |
| 214 std::deque<std::pair<GURL, std::string>> resources_to_fetch_; | 219 std::deque<std::pair<GURL, std::string>> resources_to_fetch_; |
| 215 | 220 |
| 216 FetcherPool<Fetcher> pool_; | 221 FetcherPool<Fetcher> pool_; |
| 217 | 222 |
| 218 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; | 223 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; |
| 219 | 224 |
| 225 // Daily quota. |
| 226 PrecacheQuota quota_; |
| 227 |
| 220 // The fieldtrial experiment ID. | 228 // The fieldtrial experiment ID. |
| 221 uint32_t experiment_id_; | 229 uint32_t experiment_id_; |
| 222 | 230 |
| 223 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); | 231 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); |
| 224 }; | 232 }; |
| 225 | 233 |
| 226 // Class that fetches a URL, and runs the specified callback when the fetch is | 234 // Class that fetches a URL, and runs the specified callback when the fetch is |
| 227 // complete. This class exists so that a different method can be run in | 235 // complete. This class exists so that a different method can be run in |
| 228 // response to different kinds of fetches, e.g. OnConfigFetchComplete when | 236 // response to different kinds of fetches, e.g. OnConfigFetchComplete when |
| 229 // configuration settings are fetched, OnManifestFetchComplete when a manifest | 237 // configuration settings are fetched, OnManifestFetchComplete when a manifest |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 int64_t response_bytes_; | 296 int64_t response_bytes_; |
| 289 int64_t network_response_bytes_; | 297 int64_t network_response_bytes_; |
| 290 bool was_cached_; | 298 bool was_cached_; |
| 291 | 299 |
| 292 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 300 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
| 293 }; | 301 }; |
| 294 | 302 |
| 295 } // namespace precache | 303 } // namespace precache |
| 296 | 304 |
| 297 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 305 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
| OLD | NEW |