Chromium Code Reviews| 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. This is a problem because now we have no way to | |
| 62 // get a ResourceContext and URLRequestContext to do the AsyncRevalidation | |
| 63 // with. | |
| 64 // TODO(ricea): Resolve these lifetime issues. crbug.com/561591 | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 int child_id = info->GetChildID(); | |
| 69 int route_id = info->GetRouteID(); | |
| 70 | |
| 71 // ResourceMessageFilter::GetContexts requires a ResourceHostMsg_Request | |
| 72 // object to be passed but only cares about the resource_type and origin_pid | |
| 73 // fields. | |
| 74 // TODO(ricea): Fix this after landing crrev.com/1481583002 | |
| 75 ResourceHostMsg_Request dummy_resource_host_msg_request; | |
| 76 dummy_resource_host_msg_request.resource_type = info->GetResourceType(); | |
| 77 dummy_resource_host_msg_request.origin_pid = info->GetOriginPID(); | |
| 78 ResourceContext* resource_context = nullptr; | |
| 79 net::URLRequestContext* request_context = nullptr; | |
| 80 | |
| 81 // The embedder of //content needs to ensure that the URLRequestContext object | |
| 82 // remains valid until after the ResourceContext object is destroyed. | |
| 83 info->filter()->GetContexts(dummy_resource_host_msg_request, | |
| 84 &resource_context, &request_context); | |
| 85 | |
| 86 AsyncRevalidationKey async_revalidation_key( | |
| 87 resource_context, request_context->http_transaction_factory()->GetCache(), | |
| 88 for_request->url()); | |
| 89 std::pair<AsyncRevalidationMap::iterator, bool> insert_result = | |
| 90 in_progress_.insert(AsyncRevalidationMap::value_type( | |
| 91 async_revalidation_key, scoped_ptr<AsyncRevalidationDriver>())); | |
| 92 if (!insert_result.second) { | |
| 93 // A matching async revalidation is already in progress for this cache; we | |
| 94 // don't need another one. | |
| 95 // | |
| 96 // Complication: If the server response included a Vary header | |
| 97 // which included request headers which differed between the requests, we | |
| 98 // could fail to send an async revalidation which strictly speaking we | |
| 99 // should have sent. However, since the cache only stores one response per | |
| 100 // URL it will flip-flop between the two variants, and performing more | |
| 101 // requests will only cause more useless flip-flops. | |
| 102 // | |
| 103 // This scenario requires a very odd server configuration in which different | |
| 104 // variants of the same resource are supplied to the same user, and the | |
| 105 // max-age is short enough that the requests collide during the | |
| 106 // stale-while-revalidate interval. | |
| 107 // | |
| 108 // TODO(ricea): It's probably not hard to include |vary_data| in the key to | |
| 109 // AsyncRevalidationMap, but we need to decide if there is any benefit in | |
| 110 // doing so. | |
|
kinuko
2015/11/27 08:50:01
nit: maybe record UMA to see how often this could
Adam Rice
2015/11/27 14:45:13
I just checked the code in net::HttpCache::Transac
| |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 net::HttpRequestHeaders headers; | |
| 115 headers.AddHeadersFromString(info->original_headers()); | |
| 116 | |
| 117 // Construct the request. | |
| 118 scoped_ptr<net::URLRequest> new_request = request_context->CreateRequest( | |
| 119 for_request->url(), net::MINIMUM_PRIORITY, nullptr); | |
| 120 | |
| 121 new_request->set_method(for_request->method()); | |
| 122 new_request->set_first_party_for_cookies( | |
| 123 for_request->first_party_for_cookies()); | |
| 124 new_request->set_first_party_url_policy( | |
| 125 for_request->first_party_url_policy()); | |
| 126 | |
| 127 new_request->SetReferrer(for_request->referrer()); | |
| 128 new_request->set_referrer_policy(for_request->referrer_policy()); | |
| 129 | |
| 130 new_request->SetExtraRequestHeaders(headers); | |
| 131 | |
| 132 // Remove LOAD_SUPPORT_ASYNC_REVALIDATION and LOAD_MAIN_FRAME flags. | |
| 133 // Also remove things which shouldn't have been there to begin with, | |
| 134 // and unrecognised flags. | |
| 135 int load_flags = | |
| 136 for_request->load_flags() & | |
| 137 (net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_BYPASS_PROXY | | |
| 138 net::LOAD_VERIFY_EV_CERT | net::LOAD_DO_NOT_SEND_COOKIES | | |
| 139 net::LOAD_DO_NOT_SEND_AUTH_DATA | net::LOAD_PREFETCH | | |
| 140 net::LOAD_MAYBE_USER_GESTURE | net::LOAD_DO_NOT_USE_EMBEDDED_IDENTITY); | |
| 141 new_request->SetLoadFlags(load_flags); | |
| 142 | |
| 143 scoped_ptr<ResourceThrottle> throttle = | |
| 144 scheduler->ScheduleRequest(child_id, route_id, false, new_request.get()); | |
| 145 | |
| 146 // AsyncRevalidationDriver does not outlive its entry in |in_progress_|, | |
| 147 // so the iterator and |this| may be passed to base::Bind directly. | |
| 148 insert_result.first->second.reset(new AsyncRevalidationDriver( | |
| 149 std::move(new_request), std::move(throttle), | |
| 150 base::Bind(&AsyncRevalidationManager::OnAsyncRevalidationComplete, | |
| 151 base::Unretained(this), insert_result.first))); | |
| 152 insert_result.first->second->StartRequest(); | |
| 153 } | |
| 154 | |
| 155 void AsyncRevalidationManager::CancelAsyncRevalidationsForResourceContext( | |
| 156 ResourceContext* resource_context) { | |
| 157 // For this algorithm to work, elements using |resource_context| must be | |
| 158 // contiguous in the map (ie. it must form the first part of the key). | |
|
kinuko
2015/11/27 08:50:01
nit: feels a bit verbose, would something like fol
Adam Rice
2015/11/27 14:45:13
That is much clearer, thanks!
| |
| 159 AsyncRevalidationKey partial_key(resource_context); | |
| 160 for (auto it = in_progress_.lower_bound(partial_key); | |
| 161 it != in_progress_.end() && | |
| 162 it->first.resource_context == resource_context;) { | |
| 163 it->second->CancelRequest(); | |
| 164 it = in_progress_.erase(it); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 bool AsyncRevalidationManager::QualifiesForAsyncRevalidation( | |
| 169 const ResourceHostMsg_Request& request) { | |
| 170 if (request.load_flags & | |
| 171 (net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | |
| 172 net::LOAD_VALIDATE_CACHE | net::LOAD_PREFERRING_CACHE | | |
| 173 net::LOAD_ONLY_FROM_CACHE | net::LOAD_IGNORE_LIMITS)) { | |
| 174 return false; | |
| 175 } | |
| 176 if (request.method != "GET") | |
| 177 return false; | |
| 178 // A GET request should not have a body. | |
| 179 if (request.request_body.get()) | |
| 180 return false; | |
| 181 if (!request.url.SchemeIsHTTPOrHTTPS()) | |
| 182 return false; | |
| 183 | |
| 184 return true; | |
| 185 } | |
| 186 | |
| 187 void AsyncRevalidationManager::OnAsyncRevalidationComplete( | |
| 188 AsyncRevalidationMap::iterator it) { | |
| 189 in_progress_.erase(it); | |
| 190 } | |
| 191 | |
| 192 } // namespace content | |
| OLD | NEW |