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/linked_ptr.h" | |
13 #include "base/threading/non_thread_safe.h" | |
14 #include "chrome/browser/predictors/resource_prefetch_common.h" | |
15 #include "googleurl/src/gurl.h" | |
16 #include "net/url_request/url_request.h" | |
17 | |
18 namespace net { | |
19 class URLRequestContext; | |
20 } | |
21 | |
22 namespace predictors { | |
23 | |
24 // Responsible for prefetching resources for a single navigation based on the | |
25 // input list of resources. | |
26 // - Limits the max number of resources in flight for any host and also across | |
27 // hosts. | |
28 // - When stopped, will wait for the pending requests to finish. | |
29 // - Lives entirely on the IO thread. | |
30 class ResourcePrefetcher : public base::NonThreadSafe, | |
31 public net::URLRequest::Delegate { | |
32 public: | |
33 // Denotes the prefetch request for a single subresource. | |
34 struct Request { | |
35 explicit Request(const GURL& i_resource_url); | |
36 Request(const Request& other); | |
37 | |
38 enum PrefetchStatus { | |
39 PREFETCH_STATUS_NOT_STARTED, | |
40 PREFETCH_STATUS_STARTED, | |
41 PREFETCH_STATUS_CANCELLED, | |
42 PREFETCH_STATUS_FAILED, | |
43 PREFETCH_STATUS_FROM_CACHE, | |
44 PREFETCH_STATUS_FROM_NETWORK | |
45 }; | |
46 | |
47 enum UsageStatus { | |
48 USAGE_STATUS_NOT_REQUESTED, | |
49 USAGE_STATUS_FROM_CACHE, | |
50 USAGE_STATUS_FROM_NETWORK, | |
51 USAGE_STATUS_NAVIGATION_ABANDONED | |
52 }; | |
53 | |
54 GURL resource_url; | |
55 PrefetchStatus prefetch_status; | |
56 UsageStatus usage_status; | |
57 }; | |
58 typedef std::vector<linked_ptr<Request> > RequestVector; | |
dominich
2012/07/23 16:04:41
This being public makes it more difficult, but I t
Shishir
2012/08/01 22:35:24
Changed to ScopedVector.
| |
59 | |
60 // Used to communicate when the prefetching is done. All methods are invoked | |
61 // on the IO thread. | |
62 class Delegate { | |
63 public: | |
64 virtual ~Delegate() { } | |
65 | |
66 // Called when the ResourcePrefetcher is finished, i.e. there is nothing | |
67 // pending in flight. | |
68 virtual void ResourcePrefetcherFinished( | |
69 ResourcePrefetcher* prefetcher, | |
70 scoped_ptr<RequestVector> requests) = 0; | |
71 | |
72 virtual net::URLRequestContext* GetURLRequestContext() = 0; | |
73 }; | |
74 | |
75 // 'delegate' has to outlive the ResourcePrefetcher. | |
76 ResourcePrefetcher(Delegate* delegate, | |
dominich
2012/07/23 16:04:41
I think this should be an explicit dependency rath
dominich
2012/07/23 16:04:41
comment that this takes ownership of requests.
Shishir
2012/08/01 22:35:24
Done.
Shishir
2012/08/01 22:35:24
Done.
| |
77 const ResourcePrefetchPredictorConfig& config, | |
78 const NavigationID& navigation_id, | |
79 scoped_ptr<RequestVector> requests); | |
80 virtual ~ResourcePrefetcher(); | |
81 | |
82 void Start(); // Kicks off the prefetching. Can only be called once. | |
83 void Stop(); // No additional prefetches will be queued after this. | |
84 | |
85 const NavigationID& navigation_id() const { | |
86 return navigation_id_; | |
87 } | |
88 | |
89 private: | |
90 // Launches new prefetch requests if possible. | |
91 void TryToLaunchPrefetchRequests(); | |
92 | |
93 // Starts a net::URLRequest for the input 'request'. | |
94 void SendRequest(Request* request); | |
95 | |
96 // Marks the request as finished, with the given status. | |
97 void FinishRequest(net::URLRequest* request, Request::PrefetchStatus status); | |
98 | |
99 // Reads the response data from the response - required for the resource to | |
100 // be cached correctly. | |
101 void ReadFullResponse(net::URLRequest* request); | |
102 | |
103 // net::URLRequest::Delegate methods. | |
104 virtual void OnReceivedRedirect(net::URLRequest* request, | |
105 const GURL& new_url, | |
106 bool* defer_redirect) OVERRIDE; | |
107 virtual void OnAuthRequired(net::URLRequest* request, | |
108 net::AuthChallengeInfo* auth_info) OVERRIDE; | |
109 virtual void OnCertificateRequested( | |
110 net::URLRequest* request, | |
111 net::SSLCertRequestInfo* cert_request_info) OVERRIDE; | |
112 virtual void OnSSLCertificateError(net::URLRequest* request, | |
113 const net::SSLInfo& ssl_info, | |
114 bool fatal) OVERRIDE; | |
115 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | |
116 virtual void OnReadCompleted(net::URLRequest* request, | |
117 int bytes_read) OVERRIDE; | |
118 | |
119 enum PrefetcherState { | |
120 INITIALIZED = 0, // Prefetching hasn't started. | |
121 RUNNING = 1, // Prefetching started, allowed to add more requests. | |
122 STOPPED = 2, // Prefetching started, not allowed to add more requests. | |
123 FINISHED = 3 // No more inflight request, new requests not possible. | |
124 }; | |
125 | |
126 PrefetcherState state_; | |
127 Delegate* const delegate_; | |
128 ResourcePrefetchPredictorConfig config_; | |
129 NavigationID navigation_id_; | |
130 scoped_ptr<RequestVector> request_vector_; | |
131 | |
132 std::map<net::URLRequest*, Request*> inflight_requests_; | |
133 std::list<Request*> request_queue_; | |
134 std::map<std::string, int> host_inflight_counts_; | |
135 | |
136 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); | |
137 }; | |
138 | |
139 } // namespace predictors | |
140 | |
141 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | |
OLD | NEW |