| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
| 18 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 19 #include "chrome/browser/predictors/resource_prefetch_common.h" | 19 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 20 #include "net/url_request/redirect_info.h" | 20 #include "net/url_request/redirect_info.h" |
| 21 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 class URLRequestContext; | 25 class URLRequestContext; |
| 26 } | 26 } |
| 27 | 27 |
| 28 namespace predictors { | 28 namespace predictors { |
| 29 | 29 |
| 30 // Responsible for prefetching resources for a single navigation based on the | 30 // Responsible for prefetching resources for a single main frame URL based on |
| 31 // input list of resources. | 31 // the input list of resources. |
| 32 // - Limits the max number of resources in flight for any host and also across | 32 // - Limits the max number of resources in flight for any host and also across |
| 33 // hosts. | 33 // hosts. |
| 34 // - When stopped, will wait for the pending requests to finish. | 34 // - When stopped, will wait for the pending requests to finish. |
| 35 // - Lives entirely on the IO thread. | 35 // - Lives entirely on the IO thread. |
| 36 class ResourcePrefetcher : public net::URLRequest::Delegate { | 36 class ResourcePrefetcher : public net::URLRequest::Delegate { |
| 37 public: | 37 public: |
| 38 // Used to communicate when the prefetching is done. All methods are invoked | 38 // Used to communicate when the prefetching is done. All methods are invoked |
| 39 // on the IO thread. | 39 // on the IO thread. |
| 40 class Delegate { | 40 class Delegate { |
| 41 public: | 41 public: |
| 42 virtual ~Delegate() { } | 42 virtual ~Delegate() { } |
| 43 | 43 |
| 44 // Called when the ResourcePrefetcher is finished, i.e. there is nothing | 44 // Called when the ResourcePrefetcher is finished, i.e. there is nothing |
| 45 // pending in flight. | 45 // pending in flight. |
| 46 virtual void ResourcePrefetcherFinished(ResourcePrefetcher* prefetcher) = 0; | 46 virtual void ResourcePrefetcherFinished(ResourcePrefetcher* prefetcher) = 0; |
| 47 | 47 |
| 48 virtual net::URLRequestContext* GetURLRequestContext() = 0; | 48 virtual net::URLRequestContext* GetURLRequestContext() = 0; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 // |delegate| has to outlive the ResourcePrefetcher. | 51 // |delegate| has to outlive the ResourcePrefetcher. |
| 52 ResourcePrefetcher(Delegate* delegate, | 52 ResourcePrefetcher(Delegate* delegate, |
| 53 const ResourcePrefetchPredictorConfig& config, | 53 const ResourcePrefetchPredictorConfig& config, |
| 54 const NavigationID& navigation_id, | 54 const GURL& main_frame_url, |
| 55 PrefetchKeyType key_type, | |
| 56 const std::vector<GURL>& urls); | 55 const std::vector<GURL>& urls); |
| 57 ~ResourcePrefetcher() override; | 56 ~ResourcePrefetcher() override; |
| 58 | 57 |
| 59 void Start(); // Kicks off the prefetching. Can only be called once. | 58 void Start(); // Kicks off the prefetching. Can only be called once. |
| 60 void Stop(); // No additional prefetches will be queued after this. | 59 void Stop(); // No additional prefetches will be queued after this. |
| 61 | 60 |
| 62 const NavigationID& navigation_id() const { return navigation_id_; } | 61 const GURL& main_frame_url() const { return main_frame_url_; } |
| 63 PrefetchKeyType key_type() const { return key_type_; } | |
| 64 | 62 |
| 65 private: | 63 private: |
| 66 friend class ResourcePrefetcherTest; | 64 friend class ResourcePrefetcherTest; |
| 67 friend class TestResourcePrefetcher; | 65 friend class TestResourcePrefetcher; |
| 68 | 66 |
| 69 // Launches new prefetch requests if possible. | 67 // Launches new prefetch requests if possible. |
| 70 void TryToLaunchPrefetchRequests(); | 68 void TryToLaunchPrefetchRequests(); |
| 71 | 69 |
| 72 // Starts a net::URLRequest for the input |url|. | 70 // Starts a net::URLRequest for the input |url|. |
| 73 void SendRequest(const GURL& url); | 71 void SendRequest(const GURL& url); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 INITIALIZED = 0, // Prefetching hasn't started. | 104 INITIALIZED = 0, // Prefetching hasn't started. |
| 107 RUNNING = 1, // Prefetching started, allowed to add more requests. | 105 RUNNING = 1, // Prefetching started, allowed to add more requests. |
| 108 STOPPED = 2, // Prefetching started, not allowed to add more requests. | 106 STOPPED = 2, // Prefetching started, not allowed to add more requests. |
| 109 FINISHED = 3 // No more inflight request, new requests not possible. | 107 FINISHED = 3 // No more inflight request, new requests not possible. |
| 110 }; | 108 }; |
| 111 | 109 |
| 112 base::ThreadChecker thread_checker_; | 110 base::ThreadChecker thread_checker_; |
| 113 PrefetcherState state_; | 111 PrefetcherState state_; |
| 114 Delegate* const delegate_; | 112 Delegate* const delegate_; |
| 115 ResourcePrefetchPredictorConfig const config_; | 113 ResourcePrefetchPredictorConfig const config_; |
| 116 NavigationID navigation_id_; | 114 GURL main_frame_url_; |
| 117 PrefetchKeyType key_type_; | |
| 118 std::unique_ptr<GURL> urls_; | 115 std::unique_ptr<GURL> urls_; |
| 119 | 116 |
| 120 std::map<net::URLRequest*, std::unique_ptr<net::URLRequest>> | 117 std::map<net::URLRequest*, std::unique_ptr<net::URLRequest>> |
| 121 inflight_requests_; | 118 inflight_requests_; |
| 122 std::list<GURL> request_queue_; | 119 std::list<GURL> request_queue_; |
| 123 std::map<std::string, size_t> host_inflight_counts_; | 120 std::map<std::string, size_t> host_inflight_counts_; |
| 124 | 121 |
| 125 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); | 122 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); |
| 126 }; | 123 }; |
| 127 | 124 |
| 128 } // namespace predictors | 125 } // namespace predictors |
| 129 | 126 |
| 130 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | 127 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
| OLD | NEW |