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> |
| 11 #include <list> | 11 #include <list> |
| 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/single_thread_task_runner.h" | |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 20 #include "components/precache/core/fetcher_pool.h" | 21 #include "components/precache/core/fetcher_pool.h" |
| 21 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
| 23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 24 | 25 |
| 26 namespace base { | |
| 27 class SingleThreadTaskRunner; | |
| 28 } | |
| 29 | |
| 25 namespace net { | 30 namespace net { |
| 26 class URLRequestContextGetter; | 31 class URLRequestContextGetter; |
| 27 } | 32 } |
| 28 | 33 |
| 29 namespace precache { | 34 namespace precache { |
| 30 | 35 |
| 31 class PrecacheConfigurationSettings; | 36 class PrecacheConfigurationSettings; |
| 37 class PrecacheDatabase; | |
| 38 class UnfinishedWork; | |
| 32 | 39 |
| 33 // Visible for testing. | 40 // Visible for testing. |
| 34 extern const int kNoTracking; | 41 extern const int kNoTracking; |
| 35 | 42 |
| 36 // Public interface to code that fetches resources that the user is likely to | 43 // Public interface to code that fetches resources that the user is likely to |
| 37 // want to fetch in the future, putting them in the network stack disk cache. | 44 // want to fetch in the future, putting them in the network stack disk cache. |
| 38 // Precaching is intended to be done when Chrome is not actively in use, likely | 45 // Precaching is intended to be done when Chrome is not actively in use, likely |
| 39 // hours ahead of the time when the resources are actually needed. | 46 // hours ahead of the time when the resources are actually needed. |
| 40 // | 47 // |
| 41 // This class takes as input a prioritized list of URL domains that the user | 48 // This class takes as input a prioritized list of URL domains that the user |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 52 // manifests are fetched from. These can be set by using command line switches | 59 // manifests are fetched from. These can be set by using command line switches |
| 53 // or by providing default values. | 60 // or by providing default values. |
| 54 // | 61 // |
| 55 // Sample interaction: | 62 // Sample interaction: |
| 56 // | 63 // |
| 57 // class MyPrecacheFetcherDelegate : public PrecacheFetcher::PrecacheDelegate { | 64 // class MyPrecacheFetcherDelegate : public PrecacheFetcher::PrecacheDelegate { |
| 58 // public: | 65 // public: |
| 59 // void PrecacheResourcesForTopURLs( | 66 // void PrecacheResourcesForTopURLs( |
| 60 // net::URLRequestContextGetter* request_context, | 67 // net::URLRequestContextGetter* request_context, |
| 61 // const std::list<GURL>& top_urls) { | 68 // const std::list<GURL>& top_urls) { |
| 62 // fetcher_.reset(new PrecacheFetcher(request_context, top_urls, this)); | 69 // fetcher_.reset(new PrecacheFetcher(...)); |
| 70 // fetcher_->Init(); | |
| 71 // } | |
| 72 // | |
| 73 // virtual void OnInitDone() { | |
| 63 // fetcher_->Start(); | 74 // fetcher_->Start(); |
| 64 // } | 75 // } |
| 65 // | 76 // |
| 66 // virtual void OnDone() { | 77 // virtual void OnDone() { |
| 67 // // Do something when precaching is done. | 78 // // Do something when precaching is done, e.g.: |
| 79 // fetcher_->Shutdown(); | |
| 80 // } | |
| 81 // | |
| 82 // virtual void OnShutdownDone() { | |
| 83 // fetcher_.reset(); | |
| 68 // } | 84 // } |
| 69 // | 85 // |
| 70 // private: | 86 // private: |
| 71 // std::unique_ptr<PrecacheFetcher> fetcher_; | 87 // std::unique_ptr<PrecacheFetcher> fetcher_; |
| 72 // }; | 88 // }; |
| 73 class PrecacheFetcher { | 89 class PrecacheFetcher : public base::SupportsWeakPtr<PrecacheFetcher> { |
| 74 public: | 90 public: |
| 75 class PrecacheDelegate { | 91 class PrecacheDelegate { |
| 76 public: | 92 public: |
| 77 // Called when the fetching of resources has finished, whether the resources | 93 // Called when the fetching of resources has finished, whether the resources |
| 78 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is | 94 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is |
| 79 // called, then precaching will be canceled and OnDone will not be called. | 95 // called, then precaching will be canceled and OnDone will not be called. |
| 80 virtual void OnDone() = 0; | 96 virtual void OnDone() = 0; |
| 97 | |
| 98 // Called when |PrecacheFetcher| has finished initializing and can be | |
| 99 // started. | |
| 100 virtual void OnInitDone() = 0; | |
| 101 | |
| 102 // Called after |PrecacheFetcher| has shut down and can be destroyed. | |
| 103 virtual void OnShutdownDone() = 0; | |
| 81 }; | 104 }; |
| 82 | 105 |
| 83 // Visible for testing. | 106 // Visible for testing. |
| 84 class Fetcher; | 107 class Fetcher; |
| 85 | 108 |
| 86 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a | 109 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a |
| 87 // prioritized list of hosts that the user commonly visits. These hosts are | 110 // prioritized list of hosts that the user commonly visits. These hosts are |
| 88 // used by a server side component to construct a list of resource URLs that | 111 // used by a server side component to construct a list of resource URLs that |
| 89 // the user is likely to fetch. | 112 // the user is likely to fetch. |
| 90 PrecacheFetcher(const std::vector<std::string>& starting_hosts, | 113 PrecacheFetcher( |
| 91 net::URLRequestContextGetter* request_context, | 114 const std::vector<std::string>& starting_hosts, |
| 92 const GURL& config_url, | 115 net::URLRequestContextGetter* request_context, |
| 93 const std::string& manifest_url_prefix, | 116 const GURL& config_url, |
| 94 PrecacheDelegate* precache_delegate); | 117 const std::string& manifest_url_prefix, |
| 118 PrecacheDelegate* precache_delegate, | |
| 119 base::WeakPtr<PrecacheDatabase> precache_database, | |
|
sclittle
2016/05/10 00:01:27
It seems odd for the PrecacheFetcher to have knowl
bengr
2016/05/19 01:25:44
Done.
| |
| 120 scoped_refptr<base::SingleThreadTaskRunner> database_task_runner); | |
| 95 | 121 |
| 96 virtual ~PrecacheFetcher(); | 122 virtual ~PrecacheFetcher(); |
| 97 | 123 |
| 124 void Init(const base::TimeDelta& max_age); | |
| 125 | |
| 98 // Starts fetching resources to precache. URLs are fetched sequentially. Can | 126 // Starts fetching resources to precache. URLs are fetched sequentially. Can |
| 99 // be called from any thread. Start should only be called once on a | 127 // be called from any thread. Start should only be called once on a |
| 100 // PrecacheFetcher instance. | 128 // PrecacheFetcher instance. |
| 101 void Start(); | 129 void Start(); |
| 102 | 130 |
| 131 void Shutdown(); | |
| 132 | |
| 133 // Used for testing to put the fetcher into a state where it has work to do. | |
| 134 void AssignWorkForTest(const std::list<GURL>& manifests, | |
| 135 const std::list<GURL>& resources, | |
| 136 size_t total_response_bytes, | |
| 137 size_t network_response_bytes, | |
| 138 int num_manifest_urls_to_fetch, | |
|
sclittle
2016/05/10 00:01:26
nit: Why an int? Why not a size_t?
bengr
2016/05/19 01:25:44
Done.
| |
| 139 const base::TimeTicks& start_time); | |
| 140 | |
| 103 private: | 141 private: |
| 142 void OnInitDone(scoped_refptr<UnfinishedWork> unfinished_work); | |
| 143 void OnShutdownDone(); | |
| 144 | |
| 104 // Fetches the next resource or manifest URL, if any remain. Fetching is done | 145 // Fetches the next resource or manifest URL, if any remain. Fetching is done |
| 105 // sequentially and depth-first: all resources are fetched for a manifest | 146 // sequentially and depth-first: all resources are fetched for a manifest |
| 106 // before the next manifest is fetched. This is done to limit the length of | 147 // before the next manifest is fetched. This is done to limit the length of |
| 107 // the |resource_urls_to_fetch_| list, reducing the memory usage. | 148 // the |resource_urls_to_fetch_| list, reducing the memory usage. |
| 108 void StartNextFetch(); | 149 void StartNextFetch(); |
| 109 | 150 |
| 110 void StartNextManifestFetch(); | 151 void StartNextManifestFetch(); |
| 111 void StartNextResourceFetch(); | 152 void StartNextResourceFetch(); |
| 112 | 153 |
| 113 // Called when the precache configuration settings have been fetched. | 154 // Called when the precache configuration settings have been fetched. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 140 | 181 |
| 141 // The custom URL prefix to use when fetching manifests. If not provided, the | 182 // The custom URL prefix to use when fetching manifests. If not provided, the |
| 142 // default flag-specified prefix will be used. | 183 // default flag-specified prefix will be used. |
| 143 const std::string manifest_url_prefix_; | 184 const std::string manifest_url_prefix_; |
| 144 | 185 |
| 145 // Non-owning pointer. Should not be NULL. | 186 // Non-owning pointer. Should not be NULL. |
| 146 PrecacheDelegate* precache_delegate_; | 187 PrecacheDelegate* precache_delegate_; |
| 147 | 188 |
| 148 std::unique_ptr<PrecacheConfigurationSettings> config_; | 189 std::unique_ptr<PrecacheConfigurationSettings> config_; |
| 149 | 190 |
| 191 // The database for storing unfinished work. Should only be used on | |
| 192 // the DB thread. | |
| 193 base::WeakPtr<PrecacheDatabase> precache_database_; | |
| 194 | |
| 195 const scoped_refptr<base::SingleThreadTaskRunner> database_task_runner_; | |
| 196 | |
| 150 // Tally of the total number of bytes contained in URL fetches, including | 197 // Tally of the total number of bytes contained in URL fetches, including |
| 151 // config, manifests, and resources. This the number of bytes as they would be | 198 // config, manifests, and resources. This the number of bytes as they would be |
| 152 // compressed over the network. | 199 // compressed over the network. |
| 153 size_t total_response_bytes_; | 200 size_t total_response_bytes_; |
|
sclittle
2016/05/10 00:01:26
nit, maybe for a followup CL: This should probably
bengr
2016/05/19 01:25:44
Done.
| |
| 154 | 201 |
| 155 // Tally of the total number of bytes received over the network from URL | 202 // Tally of the total number of bytes received over the network from URL |
| 156 // fetches (the same ones as in total_response_bytes_). This includes response | 203 // fetches (the same ones as in total_response_bytes_). This includes response |
| 157 // headers and intermediate responses such as 30xs. | 204 // headers and intermediate responses such as 30xs. |
| 158 size_t network_response_bytes_; | 205 size_t network_response_bytes_; |
| 159 | 206 |
| 160 // Time when the prefetch was started. | 207 // Time when the prefetch was started. |
| 161 base::TimeTicks start_time_; | 208 base::TimeTicks start_time_; |
| 162 | 209 |
| 163 int num_manifest_urls_to_fetch_; | 210 int num_manifest_urls_to_fetch_; |
| 164 std::list<GURL> manifest_urls_to_fetch_; | 211 std::list<GURL> manifest_urls_to_fetch_; |
|
sclittle
2016/05/10 00:01:26
nit, maybe for a followup CL: This was originally
bengr
2016/05/19 01:25:44
Acknowledged.
| |
| 165 std::list<GURL> resource_urls_to_fetch_; | 212 std::list<GURL> resource_urls_to_fetch_; |
| 166 | 213 |
| 167 FetcherPool<Fetcher> pool_; | 214 FetcherPool<Fetcher> pool_; |
| 168 | 215 |
| 169 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); | 216 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); |
| 170 }; | 217 }; |
| 171 | 218 |
| 172 // Class that fetches a URL, and runs the specified callback when the fetch is | 219 // Class that fetches a URL, and runs the specified callback when the fetch is |
| 173 // complete. This class exists so that a different method can be run in | 220 // complete. This class exists so that a different method can be run in |
| 174 // response to different kinds of fetches, e.g. OnConfigFetchComplete when | 221 // response to different kinds of fetches, e.g. OnConfigFetchComplete when |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 std::unique_ptr<net::URLFetcher> network_url_fetcher_; | 274 std::unique_ptr<net::URLFetcher> network_url_fetcher_; |
| 228 int64_t response_bytes_; | 275 int64_t response_bytes_; |
| 229 int64_t network_response_bytes_; | 276 int64_t network_response_bytes_; |
| 230 | 277 |
| 231 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 278 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
| 232 }; | 279 }; |
| 233 | 280 |
| 234 } // namespace precache | 281 } // namespace precache |
| 235 | 282 |
| 236 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 283 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
| OLD | NEW |