Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | |
| 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <list> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "base/threading/non_thread_safe.h" | |
| 15 #include "chrome/browser/predictors/resource_prefetch_common.h" | |
| 16 #include "googleurl/src/gurl.h" | |
| 17 #include "net/url_request/url_request.h" | |
| 18 | |
| 19 namespace net { | |
| 20 class URLRequestContext; | |
| 21 } | |
| 22 | |
| 23 namespace predictors { | |
| 24 | |
| 25 class ResourcePrefetcherManager; | |
| 26 | |
| 27 // Responsible for prefetching resources for a single navigation based on the | |
| 28 // input list of resources. | |
| 29 // - Limits the max number of resources in flight for any host and also across | |
| 30 // hosts. | |
| 31 // - When stopped, will wait for the pending requests to finish. | |
| 32 // - Lives entirely on the IO thread. | |
| 33 class ResourcePrefetcher : public base::NonThreadSafe, | |
| 34 public net::URLRequest::Delegate { | |
| 35 public: | |
| 36 // Denotes the prefetch request for a single subresource. | |
| 37 struct Request { | |
| 38 explicit Request(const GURL& i_resource_url); | |
| 39 Request(const Request& other); | |
| 40 | |
| 41 enum PrefetchStatus { | |
| 42 PREFETCH_STATUS_NOT_STARTED, | |
| 43 PREFETCH_STATUS_STARTED, | |
| 44 | |
| 45 // Cancellation reasons. | |
| 46 PREFETCH_STATUS_REDIRECTED, | |
| 47 PREFETCH_STATUS_AUTH_REQUIRED, | |
| 48 PREFETCH_STATUS_CERT_REQUIRED, | |
| 49 PREFETCH_STATUS_CERT_ERROR, | |
| 50 PREFETCH_STATUS_CANCELLED, | |
| 51 PREFETCH_STATUS_FAILED, | |
| 52 | |
| 53 // Successful prefetch states. | |
| 54 PREFETCH_STATUS_FROM_CACHE, | |
| 55 PREFETCH_STATUS_FROM_NETWORK | |
| 56 }; | |
| 57 | |
| 58 enum UsageStatus { | |
| 59 USAGE_STATUS_NOT_REQUESTED, | |
| 60 USAGE_STATUS_FROM_CACHE, | |
| 61 USAGE_STATUS_FROM_NETWORK, | |
| 62 USAGE_STATUS_NAVIGATION_ABANDONED | |
| 63 }; | |
| 64 | |
| 65 GURL resource_url; | |
| 66 PrefetchStatus prefetch_status; | |
| 67 UsageStatus usage_status; | |
| 68 }; | |
| 69 typedef ScopedVector<Request> RequestVector; | |
| 70 | |
| 71 // 'manager' has to outlive the ResourcePrefetcher. The ResourcePrefetcher | |
|
dominich
2012/08/02 15:00:50
nit: chromium style uses || around parameters refe
Shishir
2012/08/02 22:06:54
Done.
| |
| 72 // takes ownership of 'requests'. | |
| 73 ResourcePrefetcher(ResourcePrefetcherManager* manager, | |
| 74 const ResourcePrefetchPredictorConfig& config, | |
| 75 const NavigationID& navigation_id, | |
| 76 scoped_ptr<RequestVector> requests); | |
| 77 virtual ~ResourcePrefetcher(); | |
| 78 | |
| 79 void Start(); // Kicks off the prefetching. Can only be called once. | |
| 80 void Stop(); // No additional prefetches will be queued after this. | |
| 81 | |
| 82 const NavigationID& navigation_id() const { | |
| 83 return navigation_id_; | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 // Launches new prefetch requests if possible. | |
| 88 void TryToLaunchPrefetchRequests(); | |
| 89 | |
| 90 // Starts a net::URLRequest for the input 'request'. | |
| 91 void SendRequest(Request* request); | |
| 92 | |
| 93 // Marks the request as finished, with the given status. | |
| 94 void FinishRequest(net::URLRequest* request, Request::PrefetchStatus status); | |
| 95 | |
| 96 // Reads the response data from the response - required for the resource to | |
| 97 // be cached correctly. | |
| 98 void ReadFullResponse(net::URLRequest* request); | |
| 99 | |
| 100 // net::URLRequest::Delegate methods. | |
| 101 virtual void OnReceivedRedirect(net::URLRequest* request, | |
| 102 const GURL& new_url, | |
| 103 bool* defer_redirect) OVERRIDE; | |
| 104 virtual void OnAuthRequired(net::URLRequest* request, | |
| 105 net::AuthChallengeInfo* auth_info) OVERRIDE; | |
| 106 virtual void OnCertificateRequested( | |
| 107 net::URLRequest* request, | |
| 108 net::SSLCertRequestInfo* cert_request_info) OVERRIDE; | |
| 109 virtual void OnSSLCertificateError(net::URLRequest* request, | |
| 110 const net::SSLInfo& ssl_info, | |
| 111 bool fatal) OVERRIDE; | |
| 112 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | |
| 113 virtual void OnReadCompleted(net::URLRequest* request, | |
| 114 int bytes_read) OVERRIDE; | |
| 115 | |
| 116 enum PrefetcherState { | |
| 117 INITIALIZED = 0, // Prefetching hasn't started. | |
| 118 RUNNING = 1, // Prefetching started, allowed to add more requests. | |
| 119 STOPPED = 2, // Prefetching started, not allowed to add more requests. | |
| 120 FINISHED = 3 // No more inflight request, new requests not possible. | |
| 121 }; | |
| 122 | |
| 123 PrefetcherState state_; | |
| 124 ResourcePrefetcherManager* const manager_; | |
| 125 ResourcePrefetchPredictorConfig const config_; | |
| 126 NavigationID navigation_id_; | |
| 127 scoped_ptr<RequestVector> request_vector_; | |
| 128 | |
| 129 std::map<net::URLRequest*, Request*> inflight_requests_; | |
| 130 std::list<Request*> request_queue_; | |
| 131 std::map<std::string, int> host_inflight_counts_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); | |
| 134 }; | |
| 135 | |
| 136 } // namespace predictors | |
| 137 | |
| 138 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | |
| OLD | NEW |