| 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> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 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 navigation based on the |
| 31 // input list of resources. | 31 // 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 // Denotes the prefetch request for a single subresource. | |
| 39 struct Request { | |
| 40 explicit Request(const GURL& i_resource_url); | |
| 41 Request(const Request& other); | |
| 42 | |
| 43 enum PrefetchStatus { | |
| 44 PREFETCH_STATUS_NOT_STARTED, | |
| 45 PREFETCH_STATUS_STARTED, | |
| 46 | |
| 47 // Cancellation reasons. | |
| 48 PREFETCH_STATUS_REDIRECTED, | |
| 49 PREFETCH_STATUS_AUTH_REQUIRED, | |
| 50 PREFETCH_STATUS_CERT_REQUIRED, | |
| 51 PREFETCH_STATUS_CERT_ERROR, | |
| 52 PREFETCH_STATUS_CANCELLED, | |
| 53 PREFETCH_STATUS_FAILED, | |
| 54 | |
| 55 // Successful prefetch states. | |
| 56 PREFETCH_STATUS_FROM_CACHE, | |
| 57 PREFETCH_STATUS_FROM_NETWORK | |
| 58 }; | |
| 59 | |
| 60 enum UsageStatus { | |
| 61 USAGE_STATUS_NOT_REQUESTED, | |
| 62 USAGE_STATUS_FROM_CACHE, | |
| 63 USAGE_STATUS_FROM_NETWORK, | |
| 64 USAGE_STATUS_NAVIGATION_ABANDONED | |
| 65 }; | |
| 66 | |
| 67 GURL resource_url; | |
| 68 PrefetchStatus prefetch_status; | |
| 69 UsageStatus usage_status; | |
| 70 }; | |
| 71 typedef ScopedVector<Request> RequestVector; | |
| 72 | |
| 73 // 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 |
| 74 // on the IO thread. | 39 // on the IO thread. |
| 75 class Delegate { | 40 class Delegate { |
| 76 public: | 41 public: |
| 77 virtual ~Delegate() { } | 42 virtual ~Delegate() { } |
| 78 | 43 |
| 79 // Called when the ResourcePrefetcher is finished, i.e. there is nothing | 44 // Called when the ResourcePrefetcher is finished, i.e. there is nothing |
| 80 // pending in flight. Should take ownership of |requests|. | 45 // pending in flight. |
| 81 virtual void ResourcePrefetcherFinished( | 46 virtual void ResourcePrefetcherFinished(ResourcePrefetcher* prefetcher) = 0; |
| 82 ResourcePrefetcher* prefetcher, | |
| 83 RequestVector* requests) = 0; | |
| 84 | 47 |
| 85 virtual net::URLRequestContext* GetURLRequestContext() = 0; | 48 virtual net::URLRequestContext* GetURLRequestContext() = 0; |
| 86 }; | 49 }; |
| 87 | 50 |
| 88 // |delegate| has to outlive the ResourcePrefetcher. The ResourcePrefetcher | 51 // |delegate| has to outlive the ResourcePrefetcher. |
| 89 // takes ownership of |requests|. | |
| 90 ResourcePrefetcher(Delegate* delegate, | 52 ResourcePrefetcher(Delegate* delegate, |
| 91 const ResourcePrefetchPredictorConfig& config, | 53 const ResourcePrefetchPredictorConfig& config, |
| 92 const NavigationID& navigation_id, | 54 const NavigationID& navigation_id, |
| 93 PrefetchKeyType key_type, | 55 PrefetchKeyType key_type, |
| 94 std::unique_ptr<RequestVector> requests); | 56 const std::vector<GURL>& urls); |
| 95 ~ResourcePrefetcher() override; | 57 ~ResourcePrefetcher() override; |
| 96 | 58 |
| 97 void Start(); // Kicks off the prefetching. Can only be called once. | 59 void Start(); // Kicks off the prefetching. Can only be called once. |
| 98 void Stop(); // No additional prefetches will be queued after this. | 60 void Stop(); // No additional prefetches will be queued after this. |
| 99 | 61 |
| 100 const NavigationID& navigation_id() const { return navigation_id_; } | 62 const NavigationID& navigation_id() const { return navigation_id_; } |
| 101 PrefetchKeyType key_type() const { return key_type_; } | 63 PrefetchKeyType key_type() const { return key_type_; } |
| 102 | 64 |
| 103 private: | 65 private: |
| 104 friend class ResourcePrefetcherTest; | 66 friend class ResourcePrefetcherTest; |
| 105 friend class TestResourcePrefetcher; | 67 friend class TestResourcePrefetcher; |
| 106 | 68 |
| 107 // Launches new prefetch requests if possible. | 69 // Launches new prefetch requests if possible. |
| 108 void TryToLaunchPrefetchRequests(); | 70 void TryToLaunchPrefetchRequests(); |
| 109 | 71 |
| 110 // Starts a net::URLRequest for the input |request|. | 72 // Starts a net::URLRequest for the input |url|. |
| 111 void SendRequest(Request* request); | 73 void SendRequest(const GURL& url); |
| 112 | 74 |
| 113 // Called by |SendRequest| to start the |request|. This is necessary to stub | 75 // Called by |SendRequest| to start the |request|. This is necessary to stub |
| 114 // out the Start() call to net::URLRequest for unittesting. | 76 // out the Start() call to net::URLRequest for unittesting. |
| 115 virtual void StartURLRequest(net::URLRequest* request); | 77 virtual void StartURLRequest(net::URLRequest* request); |
| 116 | 78 |
| 117 // Marks the request as finished, with the given status. | 79 // Marks the request as finished, with the given status. |
| 118 void FinishRequest(net::URLRequest* request, Request::PrefetchStatus status); | 80 void FinishRequest(net::URLRequest* request); |
| 119 | 81 |
| 120 // Reads the response data from the response - required for the resource to | 82 // Reads the response data from the response - required for the resource to |
| 121 // be cached correctly. Stubbed out during testing. | 83 // be cached correctly. Stubbed out during testing. |
| 122 virtual void ReadFullResponse(net::URLRequest* request); | 84 virtual void ReadFullResponse(net::URLRequest* request); |
| 123 | 85 |
| 124 // Returns true if the request has more data that needs to be read. If it | 86 // Returns true if the request has more data that needs to be read. If it |
| 125 // returns false, the request should not be referenced again. | 87 // returns false, the request should not be referenced again. |
| 126 bool ShouldContinueReadingRequest(net::URLRequest* request, int bytes_read); | 88 bool ShouldContinueReadingRequest(net::URLRequest* request, int bytes_read); |
| 127 | 89 |
| 128 // net::URLRequest::Delegate methods. | 90 // net::URLRequest::Delegate methods. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 146 STOPPED = 2, // Prefetching started, not allowed to add more requests. | 108 STOPPED = 2, // Prefetching started, not allowed to add more requests. |
| 147 FINISHED = 3 // No more inflight request, new requests not possible. | 109 FINISHED = 3 // No more inflight request, new requests not possible. |
| 148 }; | 110 }; |
| 149 | 111 |
| 150 base::ThreadChecker thread_checker_; | 112 base::ThreadChecker thread_checker_; |
| 151 PrefetcherState state_; | 113 PrefetcherState state_; |
| 152 Delegate* const delegate_; | 114 Delegate* const delegate_; |
| 153 ResourcePrefetchPredictorConfig const config_; | 115 ResourcePrefetchPredictorConfig const config_; |
| 154 NavigationID navigation_id_; | 116 NavigationID navigation_id_; |
| 155 PrefetchKeyType key_type_; | 117 PrefetchKeyType key_type_; |
| 156 std::unique_ptr<RequestVector> request_vector_; | 118 std::unique_ptr<GURL> urls_; |
| 157 | 119 |
| 158 std::map<net::URLRequest*, | 120 std::map<net::URLRequest*, std::unique_ptr<net::URLRequest>> |
| 159 std::pair<std::unique_ptr<net::URLRequest>, Request*>> | |
| 160 inflight_requests_; | 121 inflight_requests_; |
| 161 std::list<Request*> request_queue_; | 122 std::list<GURL> request_queue_; |
| 162 std::map<std::string, size_t> host_inflight_counts_; | 123 std::map<std::string, size_t> host_inflight_counts_; |
| 163 | 124 |
| 164 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); | 125 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); |
| 165 }; | 126 }; |
| 166 | 127 |
| 167 } // namespace predictors | 128 } // namespace predictors |
| 168 | 129 |
| 169 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | 130 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
| OLD | NEW |