Chromium Code Reviews| 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 <algorithm> | 10 #include <algorithm> |
|
sclittle
2016/08/11 22:52:36
Remove this include from the header file and put i
Raj
2016/08/12 19:04:21
Done.
| |
| 11 #include <list> | 11 #include <deque> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.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" |
| 21 #include "components/precache/core/fetcher_pool.h" | 22 #include "components/precache/core/fetcher_pool.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; |
| 32 } | 33 } |
| 33 | 34 |
| 34 namespace precache { | 35 namespace precache { |
| 35 | 36 |
| 36 class PrecacheConfigurationSettings; | 37 class PrecacheConfigurationSettings; |
| 38 class PrecacheDatabase; | |
| 37 class PrecacheUnfinishedWork; | 39 class PrecacheUnfinishedWork; |
| 38 | 40 |
| 39 // Visible for testing. | 41 // Visible for testing. |
| 40 extern const int kNoTracking; | 42 extern const int kNoTracking; |
| 41 | 43 |
| 44 // Contains the information about manifest for a host. | |
| 45 struct ManifestHostInfo { | |
| 46 ManifestHostInfo(int64_t manifest_id, | |
| 47 const std::string& hostname, | |
| 48 const std::string& used_url_hash, | |
| 49 const std::string& unused_url_hash); | |
| 50 ManifestHostInfo(const ManifestHostInfo& other); | |
|
sclittle
2016/08/11 22:52:35
Is it possible to make this struct movable as well
Raj
2016/08/12 19:04:21
Nice. Done.
| |
| 51 | |
| 52 ~ManifestHostInfo(); | |
| 53 | |
| 54 const int64_t manifest_id; | |
|
sclittle
2016/08/11 22:52:36
Can you change these to not be const? You can just
Raj
2016/08/12 19:04:21
Done.
| |
| 55 const std::string hostname; | |
| 56 GURL manifest_url; | |
| 57 const std::string used_url_hash; | |
| 58 const std::string unused_url_hash; | |
| 59 }; | |
| 60 | |
| 42 // Public interface to code that fetches resources that the user is likely to | 61 // Public interface to code that fetches resources that the user is likely to |
| 43 // want to fetch in the future, putting them in the network stack disk cache. | 62 // want to fetch in the future, putting them in the network stack disk cache. |
| 44 // Precaching is intended to be done when Chrome is not actively in use, likely | 63 // Precaching is intended to be done when Chrome is not actively in use, likely |
| 45 // hours ahead of the time when the resources are actually needed. | 64 // hours ahead of the time when the resources are actually needed. |
| 46 // | 65 // |
| 47 // This class takes as input a prioritized list of URL domains that the user | 66 // This class takes as input a prioritized list of URL domains that the user |
| 48 // commonly visits, referred to as starting hosts. This class interacts with a | 67 // commonly visits, referred to as starting hosts. This class interacts with a |
| 49 // server, sending it the list of starting hosts sequentially. For each starting | 68 // server, sending it the list of starting hosts sequentially. For each starting |
| 50 // host, the server returns a manifest of resource URLs that are good candidates | 69 // host, the server returns a manifest of resource URLs that are good candidates |
| 51 // for precaching. Every resource returned is fetched, and responses are cached | 70 // for precaching. Every resource returned is fetched, and responses are cached |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 }; | 113 }; |
| 95 | 114 |
| 96 // Visible for testing. | 115 // Visible for testing. |
| 97 class Fetcher; | 116 class Fetcher; |
| 98 | 117 |
| 99 static void RecordCompletionStatistics( | 118 static void RecordCompletionStatistics( |
| 100 const PrecacheUnfinishedWork& unfinished_work, | 119 const PrecacheUnfinishedWork& unfinished_work, |
| 101 size_t remaining_manifest_urls_to_fetch, | 120 size_t remaining_manifest_urls_to_fetch, |
| 102 size_t remaining_resource_urls_to_fetch); | 121 size_t remaining_resource_urls_to_fetch); |
| 103 | 122 |
| 104 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a | 123 // Constructs a new PrecacheFetcher. The |unfinished_work| contains the |
| 105 // prioritized list of hosts that the user commonly visits. These hosts are | 124 // prioritized list of hosts that the user commonly visits. These hosts are |
| 106 // used by a server side component to construct a list of resource URLs that | 125 // used by a server side component to construct a list of resource URLs that |
| 107 // the user is likely to fetch. Takes ownership of |unfinished_work|. | 126 // the user is likely to fetch. Takes ownership of |unfinished_work|. |
| 108 PrecacheFetcher(net::URLRequestContextGetter* request_context, | 127 // |precache_database| should be accessed on;y in |db_task_runner|. |
| 109 const GURL& config_url, | 128 PrecacheFetcher( |
| 110 const std::string& manifest_url_prefix, | 129 net::URLRequestContextGetter* request_context, |
| 111 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, | 130 const GURL& config_url, |
| 112 uint32_t experiment_id, | 131 const std::string& manifest_url_prefix, |
| 113 PrecacheDelegate* precache_delegate); | 132 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, |
| 133 uint32_t experiment_id, | |
| 134 base::WeakPtr<PrecacheDatabase> precache_database, | |
| 135 const scoped_refptr<base::SingleThreadTaskRunner>& db_task_runner, | |
| 136 PrecacheDelegate* precache_delegate); | |
| 114 | 137 |
| 115 virtual ~PrecacheFetcher(); | 138 virtual ~PrecacheFetcher(); |
| 116 | 139 |
| 117 // Starts fetching resources to precache. URLs are fetched sequentially. Can | 140 // Starts fetching resources to precache. URLs are fetched sequentially. Can |
| 118 // be called from any thread. Start should only be called once on a | 141 // be called from any thread. Start should only be called once on a |
| 119 // PrecacheFetcher instance. | 142 // PrecacheFetcher instance. |
| 120 void Start(); | 143 void Start(); |
| 121 | 144 |
| 122 // Stops all precaching work. The PreacheFetcher should not be used after | 145 // Stops all precaching work. The PreacheFetcher should not be used after |
| 123 // calling this method. | 146 // calling this method. |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 154 // resource URLs to fetch according to the URLs in the manifest. If the fetch | 177 // resource URLs to fetch according to the URLs in the manifest. If the fetch |
| 155 // of a manifest fails, then it skips to the next manifest. | 178 // of a manifest fails, then it skips to the next manifest. |
| 156 void OnManifestFetchComplete(const Fetcher& source); | 179 void OnManifestFetchComplete(const Fetcher& source); |
| 157 | 180 |
| 158 // Called when a resource has been fetched. | 181 // Called when a resource has been fetched. |
| 159 void OnResourceFetchComplete(const Fetcher& source); | 182 void OnResourceFetchComplete(const Fetcher& source); |
| 160 | 183 |
| 161 // Adds up the response sizes. | 184 // Adds up the response sizes. |
| 162 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); | 185 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); |
| 163 | 186 |
| 187 // Retrieves the manifest info in DB thread. Manifest info for each of the | |
|
bengr
2016/08/11 18:49:15
in -> on the
Raj
2016/08/12 19:04:21
Done.
| |
| 188 // hosts in |hosts_to_fetch|, is added to |hosts_info|. | |
| 189 void RetrieveManifestInfo( | |
| 190 std::unique_ptr<std::deque<std::string>> hosts_to_fetch, | |
| 191 std::deque<ManifestHostInfo>* hosts_info); | |
| 192 | |
| 193 // Callback invoked when manifest info for all the top hosts is retrieved. | |
|
bengr
2016/08/11 18:49:14
when -> when the
Raj
2016/08/12 19:04:21
Done.
| |
| 194 void OnManifestInfoRetrieved( | |
| 195 std::unique_ptr<std::deque<ManifestHostInfo>> manifests_info); | |
| 196 | |
| 164 // The request context used when fetching URLs. | 197 // The request context used when fetching URLs. |
| 165 const scoped_refptr<net::URLRequestContextGetter> request_context_; | 198 const scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 166 | 199 |
| 167 // The custom URL to use when fetching the config. If not provided, the | 200 // The custom URL to use when fetching the config. If not provided, the |
| 168 // default flag-specified URL will be used. | 201 // default flag-specified URL will be used. |
| 169 const GURL config_url_; | 202 const GURL config_url_; |
| 170 | 203 |
| 171 // The custom URL prefix to use when fetching manifests. If not provided, the | 204 // The custom URL prefix to use when fetching manifests. If not provided, the |
| 172 // default flag-specified prefix will be used. | 205 // default flag-specified prefix will be used. |
| 173 const std::string manifest_url_prefix_; | 206 const std::string manifest_url_prefix_; |
| 174 | 207 |
| 208 // PrecacheDatabase should be accessed in the DB thread. | |
|
bengr
2016/08/11 18:49:14
in -> on
Raj
2016/08/12 19:04:21
Done.
| |
| 209 base::WeakPtr<PrecacheDatabase> precache_database_; | |
| 210 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_; | |
| 211 | |
| 175 // Non-owning pointer. Should not be NULL. | 212 // Non-owning pointer. Should not be NULL. |
| 176 PrecacheDelegate* precache_delegate_; | 213 PrecacheDelegate* precache_delegate_; |
| 177 | 214 |
| 178 std::list<GURL> manifest_urls_to_fetch_; | 215 std::unique_ptr<std::deque<ManifestHostInfo>> top_hosts_to_fetch_; |
| 179 std::list<GURL> resource_urls_to_fetch_; | 216 std::list<std::pair<GURL, std::string>> resources_to_fetch_; |
|
sclittle
2016/08/11 22:52:36
If you do chose to keep this member variable inste
Raj
2016/08/12 19:04:21
Done.
| |
| 180 | 217 |
| 181 FetcherPool<Fetcher> pool_; | 218 FetcherPool<Fetcher> pool_; |
| 182 | 219 |
| 183 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; | 220 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; |
| 184 | 221 |
| 185 // The fieldtrial experiment ID. | 222 // The fieldtrial experiment ID. |
| 186 uint32_t experiment_id_; | 223 uint32_t experiment_id_; |
| 187 | 224 |
| 188 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); | 225 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); |
| 189 }; | 226 }; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 207 // | 244 // |
| 208 // On completion it calls the given callback. This class cancels requests whose | 245 // On completion it calls the given callback. This class cancels requests whose |
| 209 // responses are or will be larger than max_bytes. In such cases, | 246 // responses are or will be larger than max_bytes. In such cases, |
| 210 // network_url_fetcher() will return nullptr. | 247 // network_url_fetcher() will return nullptr. |
| 211 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { | 248 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { |
| 212 public: | 249 public: |
| 213 // Construct a new Fetcher. This will create and start a new URLFetcher for | 250 // Construct a new Fetcher. This will create and start a new URLFetcher for |
| 214 // the specified URL using the specified request context. | 251 // the specified URL using the specified request context. |
| 215 Fetcher(net::URLRequestContextGetter* request_context, | 252 Fetcher(net::URLRequestContextGetter* request_context, |
| 216 const GURL& url, | 253 const GURL& url, |
| 254 const std::string& referrer, | |
| 217 const base::Callback<void(const Fetcher&)>& callback, | 255 const base::Callback<void(const Fetcher&)>& callback, |
| 218 bool is_resource_request, | 256 bool is_resource_request, |
| 219 size_t max_bytes); | 257 size_t max_bytes); |
| 220 ~Fetcher() override; | 258 ~Fetcher() override; |
| 221 void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 259 void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| 222 int64_t current, | 260 int64_t current, |
| 223 int64_t total) override; | 261 int64_t total) override; |
| 224 void OnURLFetchComplete(const net::URLFetcher* source) override; | 262 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 225 int64_t response_bytes() const { return response_bytes_; } | 263 int64_t response_bytes() const { return response_bytes_; } |
| 226 int64_t network_response_bytes() const { return network_response_bytes_; } | 264 int64_t network_response_bytes() const { return network_response_bytes_; } |
| 227 const net::URLFetcher* network_url_fetcher() const { | 265 const net::URLFetcher* network_url_fetcher() const { |
| 228 return network_url_fetcher_.get(); | 266 return network_url_fetcher_.get(); |
| 229 } | 267 } |
| 230 const GURL& url() const { return url_; } | 268 const GURL& url() const { return url_; } |
| 269 const std::string& referrer() const { return referrer_; } | |
| 231 bool is_resource_request() const { return is_resource_request_; } | 270 bool is_resource_request() const { return is_resource_request_; } |
| 271 bool was_cached() const { return was_cached_; } | |
| 232 | 272 |
| 233 private: | 273 private: |
| 234 enum class FetchStage { CACHE, NETWORK }; | 274 enum class FetchStage { CACHE, NETWORK }; |
| 235 | 275 |
| 236 void LoadFromCache(); | 276 void LoadFromCache(); |
| 237 void LoadFromNetwork(); | 277 void LoadFromNetwork(); |
| 238 | 278 |
| 239 net::URLRequestContextGetter* const request_context_; | 279 net::URLRequestContextGetter* const request_context_; |
| 240 const GURL url_; | 280 const GURL url_; |
| 281 const std::string referrer_; | |
| 241 const base::Callback<void(const Fetcher&)> callback_; | 282 const base::Callback<void(const Fetcher&)> callback_; |
| 242 const bool is_resource_request_; | 283 const bool is_resource_request_; |
| 243 const size_t max_bytes_; | 284 const size_t max_bytes_; |
| 244 | 285 |
| 245 FetchStage fetch_stage_; | 286 FetchStage fetch_stage_; |
| 246 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. | 287 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. |
| 247 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; | 288 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; |
| 248 std::unique_ptr<net::URLFetcher> network_url_fetcher_; | 289 std::unique_ptr<net::URLFetcher> network_url_fetcher_; |
| 249 int64_t response_bytes_; | 290 int64_t response_bytes_; |
| 250 int64_t network_response_bytes_; | 291 int64_t network_response_bytes_; |
| 292 bool was_cached_; | |
| 251 | 293 |
| 252 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 294 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
| 253 }; | 295 }; |
| 254 | 296 |
| 255 } // namespace precache | 297 } // namespace precache |
| 256 | 298 |
| 257 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 299 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
| OLD | NEW |