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 #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 net::URLRequest* for_request, | |
55 ResourceScheduler* scheduler) { | |
56 DCHECK_EQ(for_request->url_chain().size(), 1u); | |
57 ResourceRequestInfoImpl* info = | |
58 ResourceRequestInfoImpl::ForRequest(for_request); | |
59 DCHECK(info); | |
60 if (!info->filter()) { | |
61 // The child has gone away. Since it never received the body of the stale | |
62 // resource, it is okay not to perform an async revalidation. | |
davidben
2015/11/23 23:40:42
This comment is inaccurate. If the request turned
Adam Rice
2015/11/25 19:39:39
I added a TODO with a bug.
| |
63 return; | |
64 } | |
65 | |
66 int child_id = info->GetChildID(); | |
67 int route_id = info->GetRouteID(); | |
davidben
2015/11/23 23:40:42
This also will not work with PlzNavigate.
Adam Rice
2015/11/25 19:39:39
I filed crbug.com/561610 to track the incompatibil
| |
68 | |
69 // ResourceMsgFilter::GetContexts requires a ResourceHostMsg_Request object be | |
70 // passed but only cares about the resource_type field. | |
davidben
2015/11/23 23:40:42
Change the type of the method to only take the res
Adam Rice
2015/11/25 19:39:40
Done in http://crrev.com/1481583002
| |
71 ResourceHostMsg_Request dummy_resource_host_msg_request; | |
72 dummy_resource_host_msg_request.resource_type = info->GetResourceType(); | |
73 ResourceContext* resource_context = nullptr; | |
74 net::URLRequestContext* request_context = nullptr; | |
75 | |
76 // This |request_context| needs to be valid until | |
77 // RemoveResourceContext(resource_context) is called. This is currently | |
78 // correct, see the comment about member variable ordering in | |
79 // //chrome/browser/profiles/profile_io_data.h. | |
davidben
2015/11/23 23:40:42
//content referencing //chrome is a clear layering
Adam Rice
2015/11/25 19:39:39
Comment changed. PTAL.
| |
80 info->filter()->GetContexts(dummy_resource_host_msg_request, | |
81 &resource_context, &request_context); | |
82 | |
83 AsyncRevalidationKey async_revalidation_key( | |
84 resource_context, request_context->http_transaction_factory()->GetCache(), | |
85 for_request->url()); | |
86 std::pair<AsyncRevalidationMap::iterator, bool> insert_result = | |
87 in_progress_.insert(AsyncRevalidationMap::value_type( | |
88 async_revalidation_key, scoped_ptr<AsyncRevalidationDriver>())); | |
89 if (!insert_result.second) { | |
90 // A matching async revalidation is already in progress for this cache; we | |
91 // don't need another one. | |
davidben
2015/11/23 23:40:42
This comment is inaccurate. If there is a Vary hea
Adam Rice
2015/11/25 19:39:40
I added an explanation of the Vary issue. PTAL.
| |
92 return; | |
93 } | |
94 | |
95 net::HttpRequestHeaders headers; | |
96 headers.AddHeadersFromString(info->original_headers()); | |
davidben
2015/11/23 23:40:42
What are the situations where this differs from UR
Adam Rice
2015/11/25 19:39:40
URLRequest-internal changes appear to only happen
| |
97 | |
98 // Construct the request. | |
99 scoped_ptr<net::URLRequest> new_request = request_context->CreateRequest( | |
100 for_request->url(), net::MINIMUM_PRIORITY, nullptr); | |
101 | |
102 new_request->set_method(for_request->method()); | |
103 new_request->set_first_party_for_cookies( | |
104 for_request->first_party_for_cookies()); | |
105 new_request->set_first_party_url_policy( | |
106 for_request->first_party_url_policy()); | |
107 | |
108 new_request->SetReferrer(for_request->referrer()); | |
109 new_request->set_referrer_policy(for_request->referrer_policy()); | |
110 | |
111 new_request->SetExtraRequestHeaders(headers); | |
112 | |
113 int load_flags = for_request->load_flags() & | |
114 ~net::LOAD_SUPPORT_ASYNC_REVALIDATION & | |
115 ~net::LOAD_MAIN_FRAME; | |
116 DCHECK_EQ(load_flags & net::LOAD_IGNORE_LIMITS, 0); | |
davidben
2015/11/23 23:40:42
Don't blacklist load flags. This will easily break
Adam Rice
2015/11/25 19:39:40
Done.
| |
117 new_request->SetLoadFlags(load_flags); | |
118 | |
119 scoped_ptr<ResourceThrottle> throttle = | |
120 scheduler->ScheduleRequest(child_id, route_id, false, new_request.get()); | |
121 | |
122 auto& async_revalidation = insert_result.first->second; | |
davidben
2015/11/23 23:40:42
No non-const references. Also please spell out the
Adam Rice
2015/11/25 19:39:40
Removed.
| |
123 DCHECK(!async_revalidation); | |
124 // This use of base::Unretained() is safe because the AsyncRevalidatonDriver | |
125 // object will be destroyed before this object is. The use of the raw iterator | |
126 // is safe because objects are only erased from the map when they are deleted, | |
127 // and a deleted AsyncRevalidationDriver will not call the completion | |
128 // callback. | |
davidben
2015/11/23 23:40:42
This is pretty verbose. How about:
// AsyncRevali
Adam Rice
2015/11/25 19:39:39
Done.
| |
129 async_revalidation.reset(new AsyncRevalidationDriver( | |
130 std::move(new_request), std::move(throttle), | |
131 base::Bind(&AsyncRevalidationManager::OnAsyncRevalidationComplete, | |
132 base::Unretained(this), insert_result.first))); | |
133 async_revalidation->StartRequest(); | |
134 } | |
135 | |
136 void AsyncRevalidationManager::CancelAsyncRevalidationsForResourceContext( | |
137 ResourceContext* resource_context) { | |
138 // For this algorithm to work, elements using |resource_context| must be | |
139 // contiguous in the map (ie. it must form the first part of the key). | |
140 AsyncRevalidationKey partial_key(resource_context); | |
141 for (auto it = in_progress_.lower_bound(partial_key); | |
142 it != in_progress_.end() && | |
143 it->first.resource_context == resource_context;) { | |
144 // It is necessary to delete behind the iterator because an iterator can't | |
145 // be incremented once it has been removed from the map. | |
146 auto prev_it = it; | |
147 ++it; | |
148 prev_it->second->CancelRequest(); | |
149 in_progress_.erase(prev_it); | |
davidben
2015/11/23 23:40:42
All this can be replaced with:
it->second->Cancel
Adam Rice
2015/11/25 19:39:40
That's much better, thank you.
| |
150 } | |
151 } | |
152 | |
153 bool AsyncRevalidationManager::QualifiesForAsyncRevalidation( | |
154 const ResourceHostMsg_Request& request) { | |
155 if (request.load_flags & | |
156 (net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | |
157 net::LOAD_VALIDATE_CACHE | net::LOAD_PREFERRING_CACHE | | |
158 net::LOAD_ONLY_FROM_CACHE | net::LOAD_IGNORE_LIMITS)) { | |
159 return false; | |
160 } | |
161 if (request.method != "GET") | |
162 return false; | |
163 // A GET request should not have a body, but don't leave it to chance. | |
davidben
2015/11/23 23:40:42
"but don't leave it to chance" is weird to put in
Adam Rice
2015/11/25 19:39:39
Done.
| |
164 if (request.request_body.get()) | |
165 return false; | |
166 if (!request.url.SchemeIsHTTPOrHTTPS()) | |
167 return false; | |
168 | |
169 return true; | |
170 } | |
171 | |
172 void AsyncRevalidationManager::OnAsyncRevalidationComplete( | |
173 AsyncRevalidationMap::iterator it) { | |
174 in_progress_.erase(it); | |
175 } | |
176 | |
177 } // namespace content | |
OLD | NEW |