| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/tools/quic/quic_in_memory_cache.h" | 5 #include "net/tools/quic/quic_in_memory_cache.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 135 } |
| 136 | 136 |
| 137 // static | 137 // static |
| 138 QuicInMemoryCache* QuicInMemoryCache::GetInstance() { | 138 QuicInMemoryCache* QuicInMemoryCache::GetInstance() { |
| 139 return base::Singleton<QuicInMemoryCache>::get(); | 139 return base::Singleton<QuicInMemoryCache>::get(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( | 142 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( |
| 143 StringPiece host, | 143 StringPiece host, |
| 144 StringPiece path) const { | 144 StringPiece path) const { |
| 145 base::AutoLock lock(response_mutex_); |
| 146 |
| 145 ResponseMap::const_iterator it = responses_.find(GetKey(host, path)); | 147 ResponseMap::const_iterator it = responses_.find(GetKey(host, path)); |
| 146 if (it == responses_.end()) { | 148 if (it == responses_.end()) { |
| 147 DVLOG(1) << "Get response for resource failed: host " << host << " path " | 149 DVLOG(1) << "Get response for resource failed: host " << host << " path " |
| 148 << path; | 150 << path; |
| 149 if (default_response_.get()) { | 151 if (default_response_.get()) { |
| 150 return default_response_.get(); | 152 return default_response_.get(); |
| 151 } | 153 } |
| 152 return nullptr; | 154 return nullptr; |
| 153 } | 155 } |
| 154 return it->second; | 156 return it->second; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 171 StringPiece host, | 173 StringPiece host, |
| 172 StringPiece path, | 174 StringPiece path, |
| 173 int response_code, | 175 int response_code, |
| 174 StringPiece body, | 176 StringPiece body, |
| 175 list<ServerPushInfo> push_resources) { | 177 list<ServerPushInfo> push_resources) { |
| 176 AddSimpleResponse(host, path, response_code, body); | 178 AddSimpleResponse(host, path, response_code, body); |
| 177 MaybeAddServerPushResources(host, path, push_resources); | 179 MaybeAddServerPushResources(host, path, push_resources); |
| 178 } | 180 } |
| 179 | 181 |
| 180 void QuicInMemoryCache::AddDefaultResponse(Response* response) { | 182 void QuicInMemoryCache::AddDefaultResponse(Response* response) { |
| 183 base::AutoLock lock(response_mutex_); |
| 181 default_response_.reset(response); | 184 default_response_.reset(response); |
| 182 } | 185 } |
| 183 | 186 |
| 184 void QuicInMemoryCache::AddResponse(StringPiece host, | 187 void QuicInMemoryCache::AddResponse(StringPiece host, |
| 185 StringPiece path, | 188 StringPiece path, |
| 186 SpdyHeaderBlock response_headers, | 189 SpdyHeaderBlock response_headers, |
| 187 StringPiece response_body) { | 190 StringPiece response_body) { |
| 188 AddResponseImpl(host, path, REGULAR_RESPONSE, std::move(response_headers), | 191 AddResponseImpl(host, path, REGULAR_RESPONSE, std::move(response_headers), |
| 189 response_body, SpdyHeaderBlock()); | 192 response_body, SpdyHeaderBlock()); |
| 190 } | 193 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 201 void QuicInMemoryCache::AddSpecialResponse(StringPiece host, | 204 void QuicInMemoryCache::AddSpecialResponse(StringPiece host, |
| 202 StringPiece path, | 205 StringPiece path, |
| 203 SpecialResponseType response_type) { | 206 SpecialResponseType response_type) { |
| 204 AddResponseImpl(host, path, response_type, SpdyHeaderBlock(), "", | 207 AddResponseImpl(host, path, response_type, SpdyHeaderBlock(), "", |
| 205 SpdyHeaderBlock()); | 208 SpdyHeaderBlock()); |
| 206 } | 209 } |
| 207 | 210 |
| 208 QuicInMemoryCache::QuicInMemoryCache() {} | 211 QuicInMemoryCache::QuicInMemoryCache() {} |
| 209 | 212 |
| 210 void QuicInMemoryCache::ResetForTests() { | 213 void QuicInMemoryCache::ResetForTests() { |
| 214 base::AutoLock lock(response_mutex_); |
| 211 base::STLDeleteValues(&responses_); | 215 base::STLDeleteValues(&responses_); |
| 212 server_push_resources_.clear(); | 216 server_push_resources_.clear(); |
| 213 } | 217 } |
| 214 | 218 |
| 215 void QuicInMemoryCache::InitializeFromDirectory(const string& cache_directory) { | 219 void QuicInMemoryCache::InitializeFromDirectory(const string& cache_directory) { |
| 216 if (cache_directory.empty()) { | 220 if (cache_directory.empty()) { |
| 217 QUIC_BUG << "cache_directory must not be empty."; | 221 QUIC_BUG << "cache_directory must not be empty."; |
| 218 return; | 222 return; |
| 219 } | 223 } |
| 220 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: " | 224 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: " |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 net::kV3LowestPriority, | 265 net::kV3LowestPriority, |
| 262 response->body().as_string())); | 266 response->body().as_string())); |
| 263 } | 267 } |
| 264 MaybeAddServerPushResources(resource_file->host(), resource_file->path(), | 268 MaybeAddServerPushResources(resource_file->host(), resource_file->path(), |
| 265 push_resources); | 269 push_resources); |
| 266 } | 270 } |
| 267 } | 271 } |
| 268 | 272 |
| 269 list<ServerPushInfo> QuicInMemoryCache::GetServerPushResources( | 273 list<ServerPushInfo> QuicInMemoryCache::GetServerPushResources( |
| 270 string request_url) { | 274 string request_url) { |
| 275 base::AutoLock lock(response_mutex_); |
| 276 |
| 271 list<ServerPushInfo> resources; | 277 list<ServerPushInfo> resources; |
| 272 auto resource_range = server_push_resources_.equal_range(request_url); | 278 auto resource_range = server_push_resources_.equal_range(request_url); |
| 273 for (auto it = resource_range.first; it != resource_range.second; ++it) { | 279 for (auto it = resource_range.first; it != resource_range.second; ++it) { |
| 274 resources.push_back(it->second); | 280 resources.push_back(it->second); |
| 275 } | 281 } |
| 276 DVLOG(1) << "Found " << resources.size() << " push resources for " | 282 DVLOG(1) << "Found " << resources.size() << " push resources for " |
| 277 << request_url; | 283 << request_url; |
| 278 return resources; | 284 return resources; |
| 279 } | 285 } |
| 280 | 286 |
| 281 QuicInMemoryCache::~QuicInMemoryCache() { | 287 QuicInMemoryCache::~QuicInMemoryCache() { |
| 282 base::STLDeleteValues(&responses_); | 288 { |
| 289 base::AutoLock lock(response_mutex_); |
| 290 base::STLDeleteValues(&responses_); |
| 291 } |
| 283 } | 292 } |
| 284 | 293 |
| 285 void QuicInMemoryCache::AddResponseImpl(StringPiece host, | 294 void QuicInMemoryCache::AddResponseImpl(StringPiece host, |
| 286 StringPiece path, | 295 StringPiece path, |
| 287 SpecialResponseType response_type, | 296 SpecialResponseType response_type, |
| 288 SpdyHeaderBlock response_headers, | 297 SpdyHeaderBlock response_headers, |
| 289 StringPiece response_body, | 298 StringPiece response_body, |
| 290 SpdyHeaderBlock response_trailers) { | 299 SpdyHeaderBlock response_trailers) { |
| 300 base::AutoLock lock(response_mutex_); |
| 301 |
| 291 DCHECK(!host.empty()) << "Host must be populated, e.g. \"www.google.com\""; | 302 DCHECK(!host.empty()) << "Host must be populated, e.g. \"www.google.com\""; |
| 292 string key = GetKey(host, path); | 303 string key = GetKey(host, path); |
| 293 if (base::ContainsKey(responses_, key)) { | 304 if (base::ContainsKey(responses_, key)) { |
| 294 QUIC_BUG << "Response for '" << key << "' already exists!"; | 305 QUIC_BUG << "Response for '" << key << "' already exists!"; |
| 295 return; | 306 return; |
| 296 } | 307 } |
| 297 Response* new_response = new Response(); | 308 Response* new_response = new Response(); |
| 298 new_response->set_response_type(response_type); | 309 new_response->set_response_type(response_type); |
| 299 new_response->set_headers(std::move(response_headers)); | 310 new_response->set_headers(std::move(response_headers)); |
| 300 new_response->set_body(response_body); | 311 new_response->set_body(response_body); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 314 string request_url = GetKey(request_host, request_path); | 325 string request_url = GetKey(request_host, request_path); |
| 315 | 326 |
| 316 for (const auto& push_resource : push_resources) { | 327 for (const auto& push_resource : push_resources) { |
| 317 if (PushResourceExistsInCache(request_url, push_resource)) { | 328 if (PushResourceExistsInCache(request_url, push_resource)) { |
| 318 continue; | 329 continue; |
| 319 } | 330 } |
| 320 | 331 |
| 321 DVLOG(1) << "Add request-resource association: request url " << request_url | 332 DVLOG(1) << "Add request-resource association: request url " << request_url |
| 322 << " push url " << push_resource.request_url | 333 << " push url " << push_resource.request_url |
| 323 << " response headers " << push_resource.headers.DebugString(); | 334 << " response headers " << push_resource.headers.DebugString(); |
| 324 server_push_resources_.insert(std::make_pair(request_url, push_resource)); | 335 { |
| 336 base::AutoLock lock(response_mutex_); |
| 337 server_push_resources_.insert(std::make_pair(request_url, push_resource)); |
| 338 } |
| 325 string host = push_resource.request_url.host(); | 339 string host = push_resource.request_url.host(); |
| 326 if (host.empty()) { | 340 if (host.empty()) { |
| 327 host = request_host.as_string(); | 341 host = request_host.as_string(); |
| 328 } | 342 } |
| 329 string path = push_resource.request_url.path(); | 343 string path = push_resource.request_url.path(); |
| 330 if (responses_.find(GetKey(host, path)) == responses_.end()) { | 344 bool found_existing_response = false; |
| 345 { |
| 346 base::AutoLock lock(response_mutex_); |
| 347 found_existing_response = base::ContainsKey(responses_, GetKey(host, path)
); |
| 348 } |
| 349 if (!found_existing_response) { |
| 331 // Add a server push response to responses map, if it is not in the map. | 350 // Add a server push response to responses map, if it is not in the map. |
| 332 StringPiece body = push_resource.body; | 351 StringPiece body = push_resource.body; |
| 333 DVLOG(1) << "Add response for push resource: host " << host << " path " | 352 DVLOG(1) << "Add response for push resource: host " << host << " path " |
| 334 << path; | 353 << path; |
| 335 AddResponse(host, path, push_resource.headers.Clone(), body); | 354 AddResponse(host, path, push_resource.headers.Clone(), body); |
| 336 } | 355 } |
| 337 } | 356 } |
| 338 } | 357 } |
| 339 | 358 |
| 340 bool QuicInMemoryCache::PushResourceExistsInCache(string original_request_url, | 359 bool QuicInMemoryCache::PushResourceExistsInCache(string original_request_url, |
| 341 ServerPushInfo resource) { | 360 ServerPushInfo resource) { |
| 361 base::AutoLock lock(response_mutex_); |
| 342 auto resource_range = | 362 auto resource_range = |
| 343 server_push_resources_.equal_range(original_request_url); | 363 server_push_resources_.equal_range(original_request_url); |
| 344 for (auto it = resource_range.first; it != resource_range.second; ++it) { | 364 for (auto it = resource_range.first; it != resource_range.second; ++it) { |
| 345 ServerPushInfo push_resource = it->second; | 365 ServerPushInfo push_resource = it->second; |
| 346 if (push_resource.request_url.spec() == resource.request_url.spec()) { | 366 if (push_resource.request_url.spec() == resource.request_url.spec()) { |
| 347 return true; | 367 return true; |
| 348 } | 368 } |
| 349 } | 369 } |
| 350 return false; | 370 return false; |
| 351 } | 371 } |
| 352 | 372 |
| 353 } // namespace net | 373 } // namespace net |
| OLD | NEW |