OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 CONTENT_BROWSER_LOADER_ASYNC_REVALIDATION_DRIVER_H_ | |
6 #define CONTENT_BROWSER_LOADER_ASYNC_REVALIDATION_DRIVER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/timer/timer.h" | |
15 #include "content/common/content_export.h" | |
16 #include "content/public/browser/resource_controller.h" | |
17 #include "content/public/browser/resource_throttle.h" | |
18 #include "net/base/io_buffer.h" | |
19 #include "net/url_request/url_request.h" | |
20 | |
21 namespace net { | |
22 | |
23 class HttpCache; | |
24 | |
25 } // namespace net | |
davidben
2015/10/08 21:57:51
If there's just a few forward decls, I think we pr
Adam Rice
2015/10/13 22:53:16
Done.
| |
26 | |
27 namespace content { | |
28 | |
29 class ResourceContext; | |
30 | |
31 // The key used to lookup an AsyncRevalidationDriver in a map. This key has a | |
32 // distinct value for every in-progress async revalidation. | |
33 struct AsyncRevalidationKey { | |
34 AsyncRevalidationKey(const ResourceContext* resource_context, | |
35 const net::HttpCache* http_cache, | |
36 const GURL& url); | |
37 | |
38 // Create a prefix key that is used to match all of the | |
39 // AsyncRevalidationDrivers using |resource_context| in the map. | |
40 explicit AsyncRevalidationKey(const ResourceContext* resource_context); | |
41 | |
42 // The key for a map needs to be copyable. | |
43 AsyncRevalidationKey(const AsyncRevalidationKey& rhs); | |
44 ~AsyncRevalidationKey(); | |
45 | |
46 // No operator= is generated because the struct members are immutable. | |
47 | |
48 // |resource_context| and |http_cache| are never dereferenced; they are only | |
49 // compared to other values. We need to be able to find and delete all | |
50 // in-progress async revalidations using a particular ResourceContext, so that | |
51 // forms the first part of the key to make that operation efficient. | |
davidben
2015/10/08 21:57:51
It would be better to make this key into a per-URL
Adam Rice
2015/10/13 22:53:16
I don't understand this comment. Each request must
| |
52 const ResourceContext* const resource_context; | |
53 | |
54 // TODO(ricea): Check the assumption that different ResourceContext objects | |
55 // always have a distinct HttpCache. | |
davidben
2015/10/08 21:57:51
A ResourceContext doesn't have an HttpCache. There
Adam Rice
2015/10/13 22:53:16
This comment was confusing, sorry. I have verified
| |
56 const net::HttpCache* const http_cache; | |
57 | |
58 // Derived from the url via net::HttpUtil::SpecForRequest(). | |
59 const std::string url_key; | |
60 | |
61 // Overloading operator< is bad, according to | |
62 // http://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Operator_Ov erloading | |
63 // (accessed on 2014-07-17), so we have to overload operator() instead. | |
davidben
2015/10/08 21:57:51
This comment is unnecessary.
Adam Rice
2015/10/13 22:53:16
Removed.
| |
64 struct LessThan { | |
65 bool operator()(const AsyncRevalidationKey& lhs, | |
66 const AsyncRevalidationKey& rhs) const { | |
67 if (lhs.resource_context != rhs.resource_context) | |
68 return lhs.resource_context < rhs.resource_context; | |
69 | |
70 if (lhs.http_cache != rhs.http_cache) | |
71 return lhs.http_cache < rhs.http_cache; | |
72 | |
73 return lhs.url_key < rhs.url_key; | |
74 } | |
75 }; | |
76 }; | |
77 | |
78 // This class is responsible for driving the URLRequest for an async | |
79 // revalidation. It is passed an instance of ResourceThrottle created by | |
80 // content::ResourceScheduler to perform throttling on the request. | |
81 class CONTENT_EXPORT AsyncRevalidationDriver : public net::URLRequest::Delegate, | |
82 public ResourceController { | |
83 public: | |
84 // |completion_callback| is guaranteed to be called on completion, | |
85 // regardless of success or failure. | |
86 AsyncRevalidationDriver(scoped_ptr<net::URLRequest> request, | |
87 scoped_ptr<ResourceThrottle> throttle, | |
88 const base::Closure& completion_callback); | |
89 ~AsyncRevalidationDriver() override; | |
90 | |
91 void StartRequest(); | |
92 void CancelRequest(); | |
93 | |
94 // net::URLRequest* request() { return request_.get(); } | |
davidben
2015/10/08 21:57:51
?
Adam Rice
2015/10/13 22:53:16
Good question. Removed.
| |
95 | |
96 private: | |
97 enum DeferredStage { | |
98 DEFERRED_NONE, | |
99 DEFERRED_START, | |
100 DEFERRED_NETWORK_START, | |
101 DEFERRED_READ, | |
102 }; | |
103 | |
104 // net::URLRequest::Delegate implementation: | |
105 void OnReceivedRedirect(net::URLRequest* request, | |
106 const net::RedirectInfo& redirect_info, | |
107 bool* defer) override; | |
108 void OnAuthRequired(net::URLRequest* request, | |
109 net::AuthChallengeInfo* info) override; | |
110 void OnCertificateRequested(net::URLRequest* request, | |
111 net::SSLCertRequestInfo* info) override; | |
112 void OnSSLCertificateError(net::URLRequest* request, | |
113 const net::SSLInfo& info, | |
114 bool fatal) override; | |
115 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override; | |
116 void OnResponseStarted(net::URLRequest* request) override; | |
117 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | |
118 | |
119 // ResourceController implementation: | |
120 void Resume() override; | |
121 | |
122 // For simplicity, this class assumes that ResourceScheduler never cancels | |
123 // requests, and so these three methods are never called. | |
124 void Cancel() override; | |
125 void CancelAndIgnore() override; | |
126 void CancelWithError(int error_code) override; | |
127 | |
128 // Internal methods. | |
129 void StartRequestInternal(); | |
130 void CancelRequestInternal(int error); | |
131 void CompleteResponseStarted(); | |
132 void StartReading(bool is_continuation); | |
133 void ResumeReading(); | |
134 void ReadMore(int* bytes_read); | |
135 // Calls |completion_callback_|. | |
136 void ResponseCompleted(); | |
137 void OnReadTimeout(); | |
138 void RecordDefer(DeferredStage deferred_stage); | |
139 | |
140 bool is_deferred() const { return deferred_stage_ != DEFERRED_NONE; } | |
141 | |
142 DeferredStage deferred_stage_ = DEFERRED_NONE; | |
143 | |
144 // When this class cancels a redirect, URLRequest calls both the | |
145 // OnResponseStarted() and OnReadCompleted() callbacks. This class should not | |
146 // run |completion_callback_|. | |
147 // | |
148 // TODO(ricea): Work out why URLRequest calls both methods on cancellation and | |
149 // make it stop. | |
davidben
2015/10/08 21:57:51
OnResponseStarted should happen. That's to signal
| |
150 bool has_already_failed_ = false; | |
151 | |
152 scoped_refptr<net::IOBuffer> read_buffer_; | |
153 base::Timer read_timer_; | |
154 | |
155 const scoped_ptr<net::URLRequest> request_; | |
156 scoped_ptr<ResourceThrottle> throttle_; | |
157 | |
158 base::Closure completion_callback_; | |
159 | |
160 base::WeakPtrFactory<AsyncRevalidationDriver> weak_ptr_factory_; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationDriver); | |
163 }; | |
164 | |
165 } // namespace content | |
166 | |
167 #endif // CONTENT_BROWSER_LOADER_ASYNC_REVALIDATION_DRIVER_H_ | |
OLD | NEW |