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 <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "content/browser/loader/async_revalidation_driver.h" | |
11 #include "content/browser/loader/resource_message_filter.h" | |
12 #include "content/browser/loader/resource_request_info_impl.h" | |
13 #include "content/browser/loader/resource_scheduler.h" | |
14 #include "content/common/resource_messages.h" | |
15 #include "content/public/browser/resource_context.h" | |
Bence
2015/11/17 13:12:22
You only have pointers to ResourceContext, I don't
Adam Rice
2015/11/17 17:45:52
Good point. Removed.
| |
16 #include "content/public/browser/resource_throttle.h" | |
Bence
2015/11/17 13:12:22
I believe a forward declaration would be enough fo
Adam Rice
2015/11/17 17:45:52
A scoped_ptr<ResourceThrottle> object is instantia
Bence
2015/11/17 21:34:28
Of course, what was I thinking? Thanks for clarif
| |
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 if (lhs.resource_context != rhs.resource_context) | |
44 return lhs.resource_context < rhs.resource_context; | |
45 | |
46 if (lhs.http_cache != rhs.http_cache) | |
47 return lhs.http_cache < rhs.http_cache; | |
48 | |
49 return lhs.url_key < rhs.url_key; | |
50 } | |
51 | |
52 AsyncRevalidationManager::AsyncRevalidationManager() {} | |
53 | |
54 AsyncRevalidationManager::~AsyncRevalidationManager() { | |
55 DCHECK(in_progress_.empty()); | |
56 } | |
57 | |
58 void AsyncRevalidationManager::BeginAsyncRevalidation( | |
59 net::URLRequest* for_request, | |
60 ResourceScheduler* scheduler) { | |
61 DCHECK_EQ(for_request->url_chain().size(), 1u); | |
62 ResourceRequestInfoImpl* info = | |
63 ResourceRequestInfoImpl::ForRequest(for_request); | |
64 DCHECK(info); | |
65 if (!info->filter()) { | |
66 // The child has gone away. Since it never received the body of the stale | |
67 // resource, it is okay not to perform an async revalidation. | |
68 return; | |
69 } | |
70 | |
71 int child_id = info->GetChildID(); | |
72 int route_id = info->GetRouteID(); | |
73 | |
74 // ResourceMsgFilter::GetContexts requires a ResourceHostMsg_Request object be | |
75 // passed but only cares about the resource_type field. | |
76 ResourceHostMsg_Request dummy_resource_host_msg_request; | |
77 dummy_resource_host_msg_request.resource_type = info->GetResourceType(); | |
78 ResourceContext* resource_context = NULL; | |
Bence
2015/11/17 13:12:23
Please use nullptr in this line and the one below.
Adam Rice
2015/11/17 17:45:52
Done.
| |
79 net::URLRequestContext* request_context = NULL; | |
80 // This |request_context| needs to be valid until | |
Bence
2015/11/17 13:12:23
Optional: add newline above this comment for bette
Adam Rice
2015/11/17 17:45:52
Done.
| |
81 // RemoveResourceContext(resource_context) is called. This is currently | |
82 // correct, see | |
83 // https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/p rofiles/profile_io_data.h&sq=package:chromium&l=481 | |
Bence
2015/11/17 13:12:22
Please don't use such URL, because (1) the awkward
Adam Rice
2015/11/17 17:45:52
Okay, replaced with a simple reference to the file
Bence
2015/11/17 21:34:28
Neat.
| |
84 info->filter()->GetContexts(dummy_resource_host_msg_request, | |
85 &resource_context, &request_context); | |
86 | |
87 AsyncRevalidationKey async_revalidation_key( | |
88 resource_context, request_context->http_transaction_factory()->GetCache(), | |
89 for_request->url()); | |
90 std::pair<AsyncRevalidationMap::iterator, bool> insert_result = | |
91 in_progress_.insert( | |
92 AsyncRevalidationMap::value_type(async_revalidation_key, nullptr)); | |
93 if (!insert_result.second) { | |
94 // A matching async revalidation is already in progress for this cache; we | |
95 // don't need another one. | |
96 return; | |
97 } | |
98 auto& async_revalidation = insert_result.first->second; | |
99 DCHECK(async_revalidation == nullptr); | |
100 | |
101 net::HttpRequestHeaders headers; | |
102 headers.AddHeadersFromString(info->original_headers()); | |
103 | |
104 // Construct the request. | |
105 scoped_ptr<net::URLRequest> new_request = request_context->CreateRequest( | |
106 for_request->url(), net::MINIMUM_PRIORITY, NULL); | |
Bence
2015/11/17 13:12:22
Please use nullptr instead of NULL.
Adam Rice
2015/11/17 17:45:52
Done.
| |
107 | |
108 new_request->set_method(for_request->method()); | |
109 new_request->set_first_party_for_cookies( | |
110 for_request->first_party_for_cookies()); | |
111 new_request->set_first_party_url_policy( | |
112 for_request->first_party_url_policy()); | |
113 | |
114 new_request->SetReferrer(for_request->referrer()); | |
115 new_request->set_referrer_policy(for_request->referrer_policy()); | |
116 | |
117 new_request->SetExtraRequestHeaders(headers); | |
118 | |
119 int load_flags = for_request->load_flags() & | |
120 ~net::LOAD_SUPPORT_ASYNC_REVALIDATION & | |
121 ~net::LOAD_MAIN_FRAME; | |
122 DCHECK_EQ(load_flags & net::LOAD_IGNORE_LIMITS, 0); | |
123 new_request->SetLoadFlags(load_flags); | |
124 | |
125 scoped_ptr<ResourceThrottle> throttle = | |
126 scheduler->ScheduleRequest(child_id, route_id, false, new_request.get()); | |
127 // This use of base::Unretained() is safe because the AsyncRevalidatonDriver | |
128 // object will be destroyed before this object is. The use of the raw iterator | |
129 // is safe because objects are only erased from the map after being deleted, | |
130 // and a deleted AsyncRevalidationDriver will not call the completion | |
131 // callback. | |
132 async_revalidation = new AsyncRevalidationDriver( | |
133 new_request.Pass(), throttle.Pass(), | |
134 base::Bind(&AsyncRevalidationManager::OnAsyncRevalidationComplete, | |
135 base::Unretained(this), insert_result.first)); | |
136 async_revalidation->StartRequest(); | |
137 } | |
138 | |
139 void AsyncRevalidationManager::CancelAsyncRevalidationsForResourceContext( | |
140 ResourceContext* resource_context) { | |
141 // For this algorithm to work, elements using |resource_context| must be | |
142 // contiguous in the map (ie. it must form the first part of the key). | |
143 AsyncRevalidationKey partial_key(resource_context); | |
144 auto next_it = in_progress_.lower_bound(partial_key); | |
Bence
2015/11/17 13:12:22
Optional: use |it| instead of |next_it|, |prev_it|
Adam Rice
2015/11/17 17:45:52
Done.
| |
145 while (next_it != in_progress_.end() && | |
Bence
2015/11/17 13:12:22
Optional: use for loop instead, make |next_it| loc
Adam Rice
2015/11/17 17:45:52
Done.
| |
146 next_it->first.resource_context == resource_context) { | |
147 // Erasing the element invalidates the iterator. Increment it first. | |
148 auto current_it = next_it++; | |
Bence
2015/11/17 13:12:22
Please don't do assignment and increment in the sa
Adam Rice
2015/11/17 17:45:52
Done.
| |
149 current_it->second->CancelRequest(); | |
150 delete current_it->second; | |
151 in_progress_.erase(current_it); | |
152 } | |
153 } | |
154 | |
155 bool AsyncRevalidationManager::QualifiesForAsyncRevalidation( | |
156 const ResourceHostMsg_Request& request) { | |
157 if (request.load_flags & | |
158 (net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | |
159 net::LOAD_VALIDATE_CACHE | net::LOAD_PREFERRING_CACHE | | |
160 net::LOAD_ONLY_FROM_CACHE | net::LOAD_IGNORE_LIMITS)) { | |
161 return false; | |
162 } | |
163 if (request.method != "GET") | |
164 return false; | |
165 // A GET request should not have a body, but don't leave it to chance. | |
166 if (request.request_body.get()) | |
167 return false; | |
168 if (!request.url.SchemeIsHTTPOrHTTPS()) | |
169 return false; | |
170 return true; | |
Bence
2015/11/17 13:12:23
Please add an empty line above this final return.
Adam Rice
2015/11/17 17:45:52
Done.
| |
171 } | |
172 | |
173 void AsyncRevalidationManager::OnAsyncRevalidationComplete( | |
174 AsyncRevalidationMap::iterator it) { | |
175 delete it->second; | |
176 in_progress_.erase(it); | |
177 } | |
178 | |
179 } // namespace content | |
OLD | NEW |