Chromium Code Reviews

Side by Side Diff: chrome/browser/predictors/resource_prefetcher.h

Issue 462423004: Revert CL 117933003. Re-add resource speculative prefetching code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014 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 <list>
9 #include <map>
10 #include <vector>
11
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "chrome/browser/predictors/resource_prefetch_common.h"
17 #include "net/url_request/redirect_info.h"
18 #include "net/url_request/url_request.h"
19 #include "url/gurl.h"
20
21 namespace net {
22 class URLRequestContext;
23 }
24
25 namespace predictors {
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,
Lei Zhang 2014/09/09 03:32:20 nit: We generally prefer using ThreadChecker over
Zhen Wang 2014/09/13 00:36:32 Done.
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 // Used to communicate when the prefetching is done. All methods are invoked
72 // on the IO thread.
73 class Delegate {
74 public:
75 virtual ~Delegate() { }
76
77 // Called when the ResourcePrefetcher is finished, i.e. there is nothing
78 // pending in flight. Should take ownership of |requests|.
79 virtual void ResourcePrefetcherFinished(
80 ResourcePrefetcher* prefetcher,
81 RequestVector* requests) = 0;
82
83 virtual net::URLRequestContext* GetURLRequestContext() = 0;
84 };
85
86 // |delegate| has to outlive the ResourcePrefetcher. The ResourcePrefetcher
87 // takes ownership of |requests|.
88 ResourcePrefetcher(Delegate* delegate,
89 const ResourcePrefetchPredictorConfig& config,
90 const NavigationID& navigation_id,
91 PrefetchKeyType key_type,
92 scoped_ptr<RequestVector> requests);
93 virtual ~ResourcePrefetcher();
94
95 void Start(); // Kicks off the prefetching. Can only be called once.
96 void Stop(); // No additional prefetches will be queued after this.
97
98 const NavigationID& navigation_id() const { return navigation_id_; }
99 PrefetchKeyType key_type() const { return key_type_; }
100
101 private:
102 friend class ResourcePrefetcherTest;
103 friend class TestResourcePrefetcher;
104
105 // Launches new prefetch requests if possible.
106 void TryToLaunchPrefetchRequests();
107
108 // Starts a net::URLRequest for the input |request|.
109 void SendRequest(Request* request);
110
111 // Called by |SendRequest| to start the |request|. This is necessary to stub
112 // out the Start() call to net::URLRequest for unittesting.
113 virtual void StartURLRequest(net::URLRequest* request);
114
115 // Marks the request as finished, with the given status.
116 void FinishRequest(net::URLRequest* request, Request::PrefetchStatus status);
117
118 // Reads the response data from the response - required for the resource to
119 // be cached correctly. Stubbed out during testing.
120 virtual void ReadFullResponse(net::URLRequest* request);
121
122 // Returns true if the request has more data that needs to be read. If it
123 // returns false, the request should not be referenced again.
124 bool ShouldContinueReadingRequest(net::URLRequest* request, int bytes_read);
125
126 // net::URLRequest::Delegate methods.
127 virtual void OnReceivedRedirect(net::URLRequest* request,
128 const net::RedirectInfo& redirect_info,
129 bool* defer_redirect) OVERRIDE;
130 virtual void OnAuthRequired(net::URLRequest* request,
131 net::AuthChallengeInfo* auth_info) OVERRIDE;
132 virtual void OnCertificateRequested(
133 net::URLRequest* request,
134 net::SSLCertRequestInfo* cert_request_info) OVERRIDE;
135 virtual void OnSSLCertificateError(net::URLRequest* request,
136 const net::SSLInfo& ssl_info,
137 bool fatal) OVERRIDE;
138 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
139 virtual void OnReadCompleted(net::URLRequest* request,
140 int bytes_read) OVERRIDE;
141
142 enum PrefetcherState {
143 INITIALIZED = 0, // Prefetching hasn't started.
144 RUNNING = 1, // Prefetching started, allowed to add more requests.
145 STOPPED = 2, // Prefetching started, not allowed to add more requests.
146 FINISHED = 3 // No more inflight request, new requests not possible.
147 };
148
149 PrefetcherState state_;
150 Delegate* const delegate_;
151 ResourcePrefetchPredictorConfig const config_;
152 NavigationID navigation_id_;
153 PrefetchKeyType key_type_;
154 scoped_ptr<RequestVector> request_vector_;
155
156 std::map<net::URLRequest*, Request*> inflight_requests_;
157 std::list<Request*> request_queue_;
158 std::map<std::string, int> host_inflight_counts_;
159
160 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher);
161 };
162
163 } // namespace predictors
164
165 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_
OLDNEW

Powered by Google App Engine