Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: content/browser/loader/async_revalidation_manager.cc

Issue 1041993004: content::ResourceDispatcherHostImpl changes for stale-while-revalidate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s-w-r-yhirano-patch
Patch Set: AsyncRevalidationDriver test reorganisation. Comment nits. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "content/browser/loader/async_revalidation_manager.h"
6
7 #include <tuple>
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "content/browser/loader/async_revalidation_driver.h"
12 #include "content/browser/loader/resource_message_filter.h"
13 #include "content/browser/loader/resource_request_info_impl.h"
14 #include "content/browser/loader/resource_scheduler.h"
15 #include "content/common/resource_messages.h"
16 #include "content/public/browser/resource_throttle.h"
17 #include "net/base/load_flags.h"
18 #include "net/http/http_transaction_factory.h"
19 #include "net/http/http_util.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_context.h"
22 #include "url/gurl.h"
23
24 namespace content {
25
26 AsyncRevalidationManager::AsyncRevalidationKey::AsyncRevalidationKey(
27 const ResourceContext* resource_context,
28 const net::HttpCache* http_cache,
29 const GURL& url)
30 : resource_context(resource_context),
31 http_cache(http_cache),
32 url_key(net::HttpUtil::SpecForRequest(url)) {}
33
34 AsyncRevalidationManager::AsyncRevalidationKey::AsyncRevalidationKey(
35 const ResourceContext* resource_context)
36 : resource_context(resource_context), http_cache(nullptr), url_key() {}
37
38 AsyncRevalidationManager::AsyncRevalidationKey::~AsyncRevalidationKey() {}
39
40 bool AsyncRevalidationManager::AsyncRevalidationKey::LessThan::operator()(
41 const AsyncRevalidationKey& lhs,
42 const AsyncRevalidationKey& rhs) const {
43 return std::tie(lhs.resource_context, lhs.http_cache, lhs.url_key) <
44 std::tie(rhs.resource_context, rhs.http_cache, rhs.url_key);
45 }
46
47 AsyncRevalidationManager::AsyncRevalidationManager() {}
48
49 AsyncRevalidationManager::~AsyncRevalidationManager() {
50 DCHECK(in_progress_.empty());
51 }
52
53 void AsyncRevalidationManager::BeginAsyncRevalidation(
54 const net::URLRequest& for_request,
55 ResourceScheduler* scheduler) {
56 DCHECK_EQ(for_request.url_chain().size(), 1u);
57 const ResourceRequestInfoImpl* info =
58 ResourceRequestInfoImpl::ForRequest(&for_request);
59 DCHECK(info);
60 if (!info->filter()) {
61 // The child has gone away and we can no longer get ResourceContext and
62 // URLRequestContext to perform async revalidation.
63 // This can happen in the following cases, ordered from bad to not-so-bad
64 //
65 // 1. PlzNavigate (but enabling PlzNavigate automatically disables
66 // stale-while-revalidate; see crbug.com/561609)
67 // 2. <link rel=prefetch> may read a stale cache entry without a
68 // revalidation being performed if the original renderer goes away. This
69 // is a lost optimisation opportunity.
70 //
71 // Not an issue:
72 // 1. Implicit downloads. This method is called before
73 // MimeTypeResourceHandler calls set_is_download, so the renderer process
74 // must still exist for the request not to have been canceled.
75 // 2. Explicit downloads (ie. started via "Save As"). These never use
76 // stale-while-revalidate.
77 // 3. Non-PlzNavigate navigations between renderers. The old renderer
78 // still exists when this method is called.
79 // 4. <a ping>, navigation.sendBeacon() and Content-Security-Policy reports
80 // are POST requests, so they never use stale-while-revalidate.
81 //
82 // TODO(ricea): Resolve these lifetime issues. crbug.com/561591
83 return;
84 }
85
86 ResourceContext* resource_context = nullptr;
87 net::URLRequestContext* request_context = nullptr;
88
89 // The embedder of //content needs to ensure that the URLRequestContext object
90 // remains valid until after the ResourceContext object is destroyed.
91 info->filter()->GetContexts(info->GetResourceType(), info->GetOriginPID(),
92 &resource_context, &request_context);
93
94 AsyncRevalidationKey async_revalidation_key(
95 resource_context, request_context->http_transaction_factory()->GetCache(),
96 for_request.url());
97 std::pair<AsyncRevalidationMap::iterator, bool> insert_result =
98 in_progress_.insert(AsyncRevalidationMap::value_type(
99 async_revalidation_key, scoped_ptr<AsyncRevalidationDriver>()));
100 if (!insert_result.second) {
101 // A matching async revalidation is already in progress for this cache; we
102 // don't need another one. An async revalidation is never triggered when a
103 // Vary mismatch occurs. This means that the response will be usable even if
104 // the request headers differ.
davidben 2015/12/07 23:56:04 Huh? What does this comment even mean? I would, l
Adam Rice 2015/12/08 18:05:35 Removed in favour of the comment in the header.
105 return;
106 }
107
108 net::HttpRequestHeaders headers;
109 headers.AddHeadersFromString(info->original_headers());
110
111 // Construct the request.
112 scoped_ptr<net::URLRequest> new_request = request_context->CreateRequest(
113 for_request.url(), net::MINIMUM_PRIORITY, nullptr);
114
115 new_request->set_method(for_request.method());
116 new_request->set_first_party_for_cookies(
117 for_request.first_party_for_cookies());
118 new_request->set_first_party_url_policy(
119 for_request.first_party_url_policy());
120
121 new_request->SetReferrer(for_request.referrer());
122 new_request->set_referrer_policy(for_request.referrer_policy());
123
124 new_request->SetExtraRequestHeaders(headers);
125
126 // Remove LOAD_SUPPORT_ASYNC_REVALIDATION and LOAD_MAIN_FRAME flags.
127 // Also remove things which shouldn't have been there to begin with,
128 // and unrecognised flags.
129 int load_flags =
130 for_request.load_flags() &
131 (net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_BYPASS_PROXY |
132 net::LOAD_VERIFY_EV_CERT | net::LOAD_DO_NOT_SEND_COOKIES |
133 net::LOAD_DO_NOT_SEND_AUTH_DATA | net::LOAD_PREFETCH |
134 net::LOAD_MAYBE_USER_GESTURE | net::LOAD_DO_NOT_USE_EMBEDDED_IDENTITY);
davidben 2015/12/07 23:56:04 Carrying over LOAD_PREFETCH seems wrong. That turn
Adam Rice 2015/12/08 18:05:35 I added LOAD_PREFETCH to the prohibited flags in Q
135 new_request->SetLoadFlags(load_flags);
136
137 // These values would be -1 if the request was created by PlzNavigate. This
138 // would cause the async revalidation to start immediately.
139 // stale-while-revalidate is disabled when PlzNavigate is enabled
140 // to prevent this and other issues. See crbug.com/561610.
141 int child_id = info->GetChildID();
142 int route_id = info->GetRouteID();
143
144 scoped_ptr<ResourceThrottle> throttle =
145 scheduler->ScheduleRequest(child_id, route_id, false, new_request.get());
146
147 // AsyncRevalidationDriver does not outlive its entry in |in_progress_|,
148 // so the iterator and |this| may be passed to base::Bind directly.
149 insert_result.first->second.reset(new AsyncRevalidationDriver(
150 std::move(new_request), std::move(throttle),
151 base::Bind(&AsyncRevalidationManager::OnAsyncRevalidationComplete,
152 base::Unretained(this), insert_result.first)));
153 insert_result.first->second->StartRequest();
154 }
155
156 void AsyncRevalidationManager::CancelAsyncRevalidationsForResourceContext(
157 ResourceContext* resource_context) {
158 // |resource_context| is the first part of the key, so elements to be
159 // cancelled are contiguous in the map.
160 AsyncRevalidationKey partial_key(resource_context);
161 for (auto it = in_progress_.lower_bound(partial_key);
162 it != in_progress_.end() &&
163 it->first.resource_context == resource_context;) {
164 it->second->CancelRequest();
165 it = in_progress_.erase(it);
166 }
167 }
168
169 bool AsyncRevalidationManager::QualifiesForAsyncRevalidation(
170 const ResourceHostMsg_Request& request) {
171 if (request.load_flags &
172 (net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
173 net::LOAD_VALIDATE_CACHE | net::LOAD_PREFERRING_CACHE |
174 net::LOAD_ONLY_FROM_CACHE | net::LOAD_IGNORE_LIMITS)) {
175 return false;
176 }
177 if (request.method != "GET")
178 return false;
179 // A GET request should not have a body.
180 if (request.request_body.get())
181 return false;
182 if (!request.url.SchemeIsHTTPOrHTTPS())
183 return false;
184
185 return true;
186 }
187
188 void AsyncRevalidationManager::OnAsyncRevalidationComplete(
189 AsyncRevalidationMap::iterator it) {
190 in_progress_.erase(it);
191 }
192
193 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698