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 PrecacheUnfinishedWork; | |
| 32 | 38 |
| 33 // Visible for testing. | 39 // Visible for testing. |
| 34 extern const int kNoTracking; | 40 extern const int kNoTracking; |
| 35 | 41 |
| 36 // Public interface to code that fetches resources that the user is likely to | 42 // 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. | 43 // 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 | 44 // 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. | 45 // hours ahead of the time when the resources are actually needed. |
| 40 // | 46 // |
| 41 // This class takes as input a prioritized list of URL domains that the user | 47 // 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 | 58 // manifests are fetched from. These can be set by using command line switches |
| 53 // or by providing default values. | 59 // or by providing default values. |
| 54 // | 60 // |
| 55 // Sample interaction: | 61 // Sample interaction: |
| 56 // | 62 // |
| 57 // class MyPrecacheFetcherDelegate : public PrecacheFetcher::PrecacheDelegate { | 63 // class MyPrecacheFetcherDelegate : public PrecacheFetcher::PrecacheDelegate { |
| 58 // public: | 64 // public: |
| 59 // void PrecacheResourcesForTopURLs( | 65 // void PrecacheResourcesForTopURLs( |
| 60 // net::URLRequestContextGetter* request_context, | 66 // net::URLRequestContextGetter* request_context, |
| 61 // const std::list<GURL>& top_urls) { | 67 // const std::list<GURL>& top_urls) { |
| 62 // fetcher_.reset(new PrecacheFetcher(request_context, top_urls, this)); | 68 // fetcher_.reset(new PrecacheFetcher(...)); |
| 63 // fetcher_->Start(); | 69 // fetcher_->Start(); |
| 64 // } | 70 // } |
| 65 // | 71 // |
| 72 // void Cancel() { | |
| 73 // std::unique_ptr<PrecacheUnfinishedWork> unfinished_work = | |
| 74 // fetcher_->GetUnfinishedWork(); | |
| 75 // fetcher_.reset(); | |
| 76 // } | |
| 77 // | |
| 66 // virtual void OnDone() { | 78 // virtual void OnDone() { |
| 67 // // Do something when precaching is done. | 79 // // Do something when precaching is done. |
| 68 // } | 80 // } |
| 69 // | 81 // |
| 70 // private: | 82 // private: |
| 71 // std::unique_ptr<PrecacheFetcher> fetcher_; | 83 // std::unique_ptr<PrecacheFetcher> fetcher_; |
| 72 // }; | 84 // }; |
| 73 class PrecacheFetcher { | 85 class PrecacheFetcher : public base::SupportsWeakPtr<PrecacheFetcher> { |
| 74 public: | 86 public: |
| 75 class PrecacheDelegate { | 87 class PrecacheDelegate { |
| 76 public: | 88 public: |
| 77 // Called when the fetching of resources has finished, whether the resources | 89 // Called when the fetching of resources has finished, whether the resources |
| 78 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is | 90 // 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. | 91 // called, then precaching will be canceled and OnDone will not be called. |
| 80 virtual void OnDone() = 0; | 92 virtual void OnDone() = 0; |
| 93 | |
| 81 }; | 94 }; |
| 82 | 95 |
| 83 // Visible for testing. | 96 // Visible for testing. |
| 84 class Fetcher; | 97 class Fetcher; |
| 85 | 98 |
| 86 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a | 99 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a |
| 87 // prioritized list of hosts that the user commonly visits. These hosts are | 100 // 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 | 101 // used by a server side component to construct a list of resource URLs that |
| 89 // the user is likely to fetch. | 102 // the user is likely to fetch. |
| 90 PrecacheFetcher(const std::vector<std::string>& starting_hosts, | 103 PrecacheFetcher( |
|
sclittle
2016/05/19 01:59:46
Could this be separated into two constructors, wit
bengr
2016/05/19 23:19:31
Done.
| |
| 91 net::URLRequestContextGetter* request_context, | 104 const std::vector<std::string>& starting_hosts, |
| 92 const GURL& config_url, | 105 net::URLRequestContextGetter* request_context, |
| 93 const std::string& manifest_url_prefix, | 106 const GURL& config_url, |
| 94 PrecacheDelegate* precache_delegate); | 107 const std::string& manifest_url_prefix, |
| 108 const PrecacheUnfinishedWork* unfinished_work, | |
|
sclittle
2016/05/19 01:59:45
Could you just pass in an unfinished_work with the
bengr
2016/05/19 23:19:31
Yes.
| |
| 109 PrecacheDelegate* precache_delegate); | |
| 95 | 110 |
| 96 virtual ~PrecacheFetcher(); | 111 virtual ~PrecacheFetcher(); |
| 97 | 112 |
| 98 // Starts fetching resources to precache. URLs are fetched sequentially. Can | 113 // Starts fetching resources to precache. URLs are fetched sequentially. Can |
| 99 // be called from any thread. Start should only be called once on a | 114 // be called from any thread. Start should only be called once on a |
| 100 // PrecacheFetcher instance. | 115 // PrecacheFetcher instance. |
| 101 void Start(); | 116 void Start(); |
| 102 | 117 |
| 118 std::unique_ptr<PrecacheUnfinishedWork> GetUnfinishedWork(); | |
| 119 | |
| 120 // Used for testing to put the fetcher into a state where it has work to do. | |
| 121 void AssignWorkForTest(const std::list<GURL>& manifests, | |
| 122 const std::list<GURL>& resources, | |
| 123 size_t total_response_bytes, | |
| 124 size_t network_response_bytes, | |
| 125 size_t num_manifest_urls_to_fetch, | |
| 126 const base::Time& start_time); | |
| 127 | |
| 103 private: | 128 private: |
| 104 // Fetches the next resource or manifest URL, if any remain. Fetching is done | 129 // 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 | 130 // 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 | 131 // 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. | 132 // the |resource_urls_to_fetch_| list, reducing the memory usage. |
| 108 void StartNextFetch(); | 133 void StartNextFetch(); |
| 109 | 134 |
| 110 void StartNextManifestFetch(); | 135 void StartNextManifestFetch(); |
| 111 void StartNextResourceFetch(); | 136 void StartNextResourceFetch(); |
| 112 | 137 |
| 113 // Called when the precache configuration settings have been fetched. | 138 // Called when the precache configuration settings have been fetched. |
| 114 // Determines the list of manifest URLs to fetch according to the list of | 139 // Determines the list of manifest URLs to fetch according to the list of |
| 115 // |starting_hosts_| and information from the precache configuration settings. | 140 // |starting_hosts_| and information from the precache configuration settings. |
| 116 // If the fetch of the configuration settings fails, then precaching ends. | 141 // If the fetch of the configuration settings fails, then precaching ends. |
| 117 void OnConfigFetchComplete(const Fetcher& source); | 142 void OnConfigFetchComplete(const Fetcher& source); |
| 118 | 143 |
| 144 // Constructs manifest URLs using a manifest URL prefix, and lists of hosts. | |
| 145 void DetermineManifests(); | |
| 146 | |
| 119 // Called when a precache manifest has been fetched. Builds the list of | 147 // Called when a precache manifest has been fetched. Builds the list of |
| 120 // resource URLs to fetch according to the URLs in the manifest. If the fetch | 148 // resource URLs to fetch according to the URLs in the manifest. If the fetch |
| 121 // of a manifest fails, then it skips to the next manifest. | 149 // of a manifest fails, then it skips to the next manifest. |
| 122 void OnManifestFetchComplete(const Fetcher& source); | 150 void OnManifestFetchComplete(const Fetcher& source); |
| 123 | 151 |
| 124 // Called when a resource has been fetched. | 152 // Called when a resource has been fetched. |
| 125 void OnResourceFetchComplete(const Fetcher& source); | 153 void OnResourceFetchComplete(const Fetcher& source); |
| 126 | 154 |
| 127 // Adds up the response sizes. | 155 // Adds up the response sizes. |
| 128 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); | 156 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); |
| 129 | 157 |
| 130 // The prioritized list of starting hosts that the server will pick resource | 158 // The prioritized list of starting hosts that the server will pick resource |
| 131 // URLs to be precached for. | 159 // URLs to be precached for. |
| 132 const std::vector<std::string> starting_hosts_; | 160 const std::vector<std::string> starting_hosts_; |
| 133 | 161 |
| 134 // The request context used when fetching URLs. | 162 // The request context used when fetching URLs. |
| 135 const scoped_refptr<net::URLRequestContextGetter> request_context_; | 163 const scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 136 | 164 |
| 165 // True if configuration settings have been retrieved, either from the network | |
| 166 // or from unfinished work. | |
| 167 bool has_config_; | |
|
sclittle
2016/05/19 01:59:45
Could you just check if |config_| is non-null?
bengr
2016/05/19 23:19:31
Done.
| |
| 168 | |
| 137 // The custom URL to use when fetching the config. If not provided, the | 169 // The custom URL to use when fetching the config. If not provided, the |
| 138 // default flag-specified URL will be used. | 170 // default flag-specified URL will be used. |
| 139 const GURL config_url_; | 171 const GURL config_url_; |
| 140 | 172 |
| 141 // The custom URL prefix to use when fetching manifests. If not provided, the | 173 // The custom URL prefix to use when fetching manifests. If not provided, the |
| 142 // default flag-specified prefix will be used. | 174 // default flag-specified prefix will be used. |
| 143 const std::string manifest_url_prefix_; | 175 const std::string manifest_url_prefix_; |
| 144 | 176 |
| 145 // Non-owning pointer. Should not be NULL. | 177 // Non-owning pointer. Should not be NULL. |
| 146 PrecacheDelegate* precache_delegate_; | 178 PrecacheDelegate* precache_delegate_; |
| 147 | 179 |
| 148 std::unique_ptr<PrecacheConfigurationSettings> config_; | 180 std::unique_ptr<PrecacheConfigurationSettings> config_; |
| 149 | 181 |
| 150 // Tally of the total number of bytes contained in URL fetches, including | 182 // 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 | 183 // config, manifests, and resources. This the number of bytes as they would be |
| 152 // compressed over the network. | 184 // compressed over the network. |
| 153 size_t total_response_bytes_; | 185 size_t total_response_bytes_; |
| 154 | 186 |
| 155 // Tally of the total number of bytes received over the network from URL | 187 // 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 | 188 // fetches (the same ones as in total_response_bytes_). This includes response |
| 157 // headers and intermediate responses such as 30xs. | 189 // headers and intermediate responses such as 30xs. |
| 158 size_t network_response_bytes_; | 190 size_t network_response_bytes_; |
| 159 | 191 |
| 160 // Time when the prefetch was started. | 192 // Time when the prefetch was started. |
| 161 base::TimeTicks start_time_; | 193 base::Time start_time_; |
| 162 | 194 |
| 163 int num_manifest_urls_to_fetch_; | 195 size_t num_manifest_urls_to_fetch_; |
| 164 std::list<GURL> manifest_urls_to_fetch_; | 196 std::list<GURL> manifest_urls_to_fetch_; |
| 165 std::list<GURL> resource_urls_to_fetch_; | 197 std::list<GURL> resource_urls_to_fetch_; |
| 166 | 198 |
| 167 FetcherPool<Fetcher> pool_; | 199 FetcherPool<Fetcher> pool_; |
| 168 | 200 |
| 169 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); | 201 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); |
| 170 }; | 202 }; |
| 171 | 203 |
| 172 // Class that fetches a URL, and runs the specified callback when the fetch is | 204 // 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 | 205 // complete. This class exists so that a different method can be run in |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 std::unique_ptr<net::URLFetcher> network_url_fetcher_; | 259 std::unique_ptr<net::URLFetcher> network_url_fetcher_; |
| 228 int64_t response_bytes_; | 260 int64_t response_bytes_; |
| 229 int64_t network_response_bytes_; | 261 int64_t network_response_bytes_; |
| 230 | 262 |
| 231 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 263 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
| 232 }; | 264 }; |
| 233 | 265 |
| 234 } // namespace precache | 266 } // namespace precache |
| 235 | 267 |
| 236 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 268 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
| OLD | NEW |