| 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 <list> |
| 11 #include <memory> | 12 #include <memory> |
| 12 #include <string> | 13 #include <string> |
| 13 #include <vector> | 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/callback.h" | 16 #include "base/callback.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 19 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 33 } | 34 } |
| 34 | 35 |
| 35 namespace precache { | 36 namespace precache { |
| 36 | 37 |
| 37 class PrecacheConfigurationSettings; | 38 class PrecacheConfigurationSettings; |
| 38 class PrecacheDatabase; | 39 class PrecacheDatabase; |
| 39 class PrecacheUnfinishedWork; | 40 class PrecacheUnfinishedWork; |
| 40 | 41 |
| 41 // Visible for testing. | 42 // Visible for testing. |
| 42 extern const int kNoTracking; | 43 extern const int kNoTracking; |
| 44 extern const int kMaxParallelFetches; |
| 43 | 45 |
| 44 // Contains the information about manifest for a host. | 46 // Information about the manifest for a host. |
| 45 struct ManifestHostInfo { | 47 struct ManifestHostInfo { |
| 46 ManifestHostInfo(int64_t manifest_id, | 48 ManifestHostInfo(int64_t manifest_id, |
| 47 const std::string& hostname, | 49 const std::string& hostname, |
| 50 int64_t visits, |
| 48 const std::string& used_url_hash, | 51 const std::string& used_url_hash, |
| 49 const std::string& unused_url_hash); | 52 const std::string& unused_url_hash); |
| 53 ~ManifestHostInfo(); |
| 50 ManifestHostInfo(ManifestHostInfo&&); | 54 ManifestHostInfo(ManifestHostInfo&&); |
| 51 ManifestHostInfo& operator=(ManifestHostInfo&&); | 55 ManifestHostInfo& operator=(ManifestHostInfo&&); |
| 52 | 56 // Copy constructor and assignment operator are implicitly deleted. |
| 53 ~ManifestHostInfo(); | |
| 54 | 57 |
| 55 int64_t manifest_id; | 58 int64_t manifest_id; |
| 56 std::string hostname; | 59 std::string hostname; |
| 57 GURL manifest_url; | 60 GURL manifest_url; |
| 61 int64_t visits; |
| 58 std::string used_url_hash; | 62 std::string used_url_hash; |
| 59 std::string unused_url_hash; | 63 std::string unused_url_hash; |
| 60 }; | 64 }; |
| 61 | 65 |
| 66 // Information about a resource to be downloaded. |
| 67 struct ResourceInfo { |
| 68 ResourceInfo(const GURL& url, const std::string& referrer, double weight); |
| 69 ~ResourceInfo(); |
| 70 ResourceInfo(ResourceInfo&&); |
| 71 ResourceInfo& operator=(ResourceInfo&&); |
| 72 // Copy constructor and assignment operator are implicitly deleted. |
| 73 |
| 74 GURL url; // The resource being requested. |
| 75 std::string referrer; // The host of the manifest requesting this resource. |
| 76 double weight; // Estimate of the expected utility of this resource. |
| 77 }; |
| 78 |
| 62 // Public interface to code that fetches resources that the user is likely to | 79 // Public interface to code that fetches resources that the user is likely to |
| 63 // want to fetch in the future, putting them in the network stack disk cache. | 80 // want to fetch in the future, putting them in the network stack disk cache. |
| 64 // Precaching is intended to be done when Chrome is not actively in use, likely | 81 // Precaching is intended to be done when Chrome is not actively in use, likely |
| 65 // hours ahead of the time when the resources are actually needed. | 82 // hours ahead of the time when the resources are actually needed. |
| 66 // | 83 // |
| 67 // This class takes as input a prioritized list of URL domains that the user | 84 // This class takes as input a prioritized list of URL domains that the user |
| 68 // commonly visits, referred to as starting hosts. This class interacts with a | 85 // commonly visits, referred to as starting hosts. This class interacts with a |
| 69 // server, sending it the list of starting hosts sequentially. For each starting | 86 // server, sending it the list of starting hosts sequentially. For each starting |
| 70 // host, the server returns a manifest of resource URLs that are good candidates | 87 // host, the server returns a manifest of resource URLs that are good candidates |
| 71 // for precaching. Every resource returned is fetched, and responses are cached | 88 // for precaching. Every resource returned is fetched, and responses are cached |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // std::unique_ptr<PrecacheFetcher> fetcher_; | 120 // std::unique_ptr<PrecacheFetcher> fetcher_; |
| 104 // }; | 121 // }; |
| 105 class PrecacheFetcher : public base::SupportsWeakPtr<PrecacheFetcher> { | 122 class PrecacheFetcher : public base::SupportsWeakPtr<PrecacheFetcher> { |
| 106 public: | 123 public: |
| 107 class PrecacheDelegate { | 124 class PrecacheDelegate { |
| 108 public: | 125 public: |
| 109 // Called when the fetching of resources has finished, whether the resources | 126 // Called when the fetching of resources has finished, whether the resources |
| 110 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is | 127 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is |
| 111 // called, then precaching will be canceled and OnDone will not be called. | 128 // called, then precaching will be canceled and OnDone will not be called. |
| 112 virtual void OnDone() = 0; | 129 virtual void OnDone() = 0; |
| 113 | |
| 114 }; | 130 }; |
| 115 | 131 |
| 116 // Visible for testing. | 132 // Visible for testing. |
| 117 class Fetcher; | 133 class Fetcher; |
| 118 | 134 |
| 119 static void RecordCompletionStatistics( | 135 static void RecordCompletionStatistics( |
| 120 const PrecacheUnfinishedWork& unfinished_work, | 136 const PrecacheUnfinishedWork& unfinished_work, |
| 121 size_t remaining_manifest_urls_to_fetch, | 137 size_t remaining_manifest_urls_to_fetch, |
| 122 size_t remaining_resource_urls_to_fetch); | 138 size_t remaining_resource_urls_to_fetch); |
| 123 | 139 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 145 // be called from any thread. Start should only be called once on a | 161 // be called from any thread. Start should only be called once on a |
| 146 // PrecacheFetcher instance. | 162 // PrecacheFetcher instance. |
| 147 void Start(); | 163 void Start(); |
| 148 | 164 |
| 149 // Stops all precaching work. The PreacheFetcher should not be used after | 165 // Stops all precaching work. The PreacheFetcher should not be used after |
| 150 // calling this method. | 166 // calling this method. |
| 151 std::unique_ptr<PrecacheUnfinishedWork> CancelPrecaching(); | 167 std::unique_ptr<PrecacheUnfinishedWork> CancelPrecaching(); |
| 152 | 168 |
| 153 private: | 169 private: |
| 154 friend class PrecacheFetcherTest; | 170 friend class PrecacheFetcherTest; |
| 171 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, |
| 172 GloballyRankResourcesAfterPauseResume); |
| 155 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, FetcherPoolMaxLimitReached); | 173 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, FetcherPoolMaxLimitReached); |
| 156 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, | 174 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, |
| 157 CancelPrecachingAfterAllManifestFetch); | 175 CancelPrecachingAfterAllManifestFetch); |
| 158 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, DailyQuota); | 176 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, DailyQuota); |
| 159 | 177 |
| 160 // Notifies the precache delete that precaching is done, and report | 178 // Notifies the precache delete that precaching is done, and report |
| 161 // completion statistics. | 179 // completion statistics. |
| 162 void NotifyDone(size_t remaining_manifest_urls_to_fetch, | 180 void NotifyDone(size_t remaining_manifest_urls_to_fetch, |
| 163 size_t remaining_resource_urls_to_fetch); | 181 size_t remaining_resource_urls_to_fetch); |
| 164 | 182 |
| 165 // Fetches the next resource or manifest URL, if any remain. Fetching is done | 183 // Fetches the next resource or manifest URL, if any remain. Fetching is done |
| 166 // sequentially and depth-first: all resources are fetched for a manifest | 184 // sequentially and depth-first: all resources are fetched for a manifest |
| 167 // before the next manifest is fetched. This is done to limit the length of | 185 // before the next manifest is fetched. This is done to limit the length of |
| 168 // the |resource_urls_to_fetch_| list, reducing the memory usage. | 186 // the |resource_urls_to_fetch_| list, reducing the memory usage. |
| 169 void StartNextFetch(); | 187 void StartNextFetch(); |
| 170 | 188 |
| 171 void StartNextManifestFetch(); | 189 void StartNextManifestFetches(); |
| 172 void StartNextResourceFetch(); | 190 void StartNextResourceFetch(); |
| 173 | 191 |
| 174 // Called when the precache configuration settings have been fetched. | 192 // Called when the precache configuration settings have been fetched. |
| 175 // Determines the list of manifest URLs to fetch according to the list of | 193 // Determines the list of manifest URLs to fetch according to the list of |
| 176 // |starting_hosts_| and information from the precache configuration settings. | 194 // |starting_hosts_| and information from the precache configuration settings. |
| 177 // If the fetch of the configuration settings fails, then precaching ends. | 195 // If the fetch of the configuration settings fails, then precaching ends. |
| 178 void OnConfigFetchComplete(const Fetcher& source); | 196 void OnConfigFetchComplete(const Fetcher& source); |
| 179 | 197 |
| 180 // Constructs manifest URLs using a manifest URL prefix, and lists of hosts. | 198 // Constructs manifest URLs using a manifest URL prefix, and lists of hosts. |
| 181 void DetermineManifests(); | 199 void DetermineManifests(); |
| 182 | 200 |
| 183 // Called when a precache manifest has been fetched. Builds the list of | 201 // Called when a precache manifest has been fetched. Builds the list of |
| 184 // resource URLs to fetch according to the URLs in the manifest. If the fetch | 202 // resource URLs to fetch according to the URLs in the manifest. If the fetch |
| 185 // of a manifest fails, then it skips to the next manifest. | 203 // of a manifest fails, then it skips to the next manifest. |
| 186 void OnManifestFetchComplete(const Fetcher& source); | 204 void OnManifestFetchComplete(int64_t host_visits, const Fetcher& source); |
| 205 |
| 206 // Moves the pending resource URLs into the to-be-fetched queue, and sorts and |
| 207 // truncates if specified by the PrecacheConfigurationSettings. Called by |
| 208 // OnManifestFetchComplete after the last manifest is fetched, so that |
| 209 // StartNextFetch will begin fetching resource URLs. |
| 210 void QueueResourcesForFetch(); |
| 187 | 211 |
| 188 // Called when a resource has been fetched. | 212 // Called when a resource has been fetched. |
| 189 void OnResourceFetchComplete(const Fetcher& source); | 213 void OnResourceFetchComplete(const Fetcher& source); |
| 190 | 214 |
| 191 // Adds up the response sizes. | 215 // Adds up the response sizes. |
| 192 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); | 216 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); |
| 193 | 217 |
| 194 // Callback invoked when the manifest info for all the top hosts is retrieved. | 218 // Callback invoked when the manifest info for all the top hosts is retrieved. |
| 195 void OnManifestInfoRetrieved(std::deque<ManifestHostInfo> manifests_info); | 219 void OnManifestInfoRetrieved(std::deque<ManifestHostInfo> manifests_info); |
| 196 | 220 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 208 // default flag-specified prefix will be used. | 232 // default flag-specified prefix will be used. |
| 209 const std::string manifest_url_prefix_; | 233 const std::string manifest_url_prefix_; |
| 210 | 234 |
| 211 // PrecacheDatabase should be accessed on the DB thread. | 235 // PrecacheDatabase should be accessed on the DB thread. |
| 212 base::WeakPtr<PrecacheDatabase> precache_database_; | 236 base::WeakPtr<PrecacheDatabase> precache_database_; |
| 213 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_; | 237 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_; |
| 214 | 238 |
| 215 // Non-owning pointer. Should not be NULL. | 239 // Non-owning pointer. Should not be NULL. |
| 216 PrecacheDelegate* precache_delegate_; | 240 PrecacheDelegate* precache_delegate_; |
| 217 | 241 |
| 242 // Top hosts for which manifests still need to be fetched (i.e. no Fetcher has |
| 243 // been created yet). |
| 218 std::deque<ManifestHostInfo> top_hosts_to_fetch_; | 244 std::deque<ManifestHostInfo> top_hosts_to_fetch_; |
| 219 std::deque<std::pair<GURL, std::string>> resources_to_fetch_; | 245 |
| 246 // Top hosts for which manifests are currently being fetched. |
| 247 std::list<ManifestHostInfo> top_hosts_fetching_; |
| 248 |
| 249 // Resources to be fetched, in desired fetch order. Populated only after |
| 250 // manifest fetching is complete. |
| 251 std::deque<ResourceInfo> resources_to_fetch_; |
| 252 |
| 253 // Resources currently being fetched, in the order requested. |
| 254 std::list<ResourceInfo> resources_fetching_; |
| 255 |
| 256 // Resources to be fetched, not yet ranked. Valid until manifest fetching is |
| 257 // done, after which resources are sorted and places in resources_to_fetch_. |
| 258 std::deque<ResourceInfo> resources_to_rank_; |
| 220 | 259 |
| 221 FetcherPool<Fetcher> pool_; | 260 FetcherPool<Fetcher> pool_; |
| 222 | 261 |
| 223 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; | 262 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; |
| 224 | 263 |
| 225 // Daily quota. | 264 // Daily quota. |
| 226 PrecacheQuota quota_; | 265 PrecacheQuota quota_; |
| 227 | 266 |
| 228 // The fieldtrial experiment ID. | 267 // The fieldtrial experiment ID. |
| 229 uint32_t experiment_id_; | 268 uint32_t experiment_id_; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 int64_t response_bytes_; | 336 int64_t response_bytes_; |
| 298 int64_t network_response_bytes_; | 337 int64_t network_response_bytes_; |
| 299 bool was_cached_; | 338 bool was_cached_; |
| 300 | 339 |
| 301 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 340 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
| 302 }; | 341 }; |
| 303 | 342 |
| 304 } // namespace precache | 343 } // namespace precache |
| 305 | 344 |
| 306 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 345 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
| OLD | NEW |