OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
31 #include "net/http/http_response_info.h" | 31 #include "net/http/http_response_info.h" |
32 #include "net/http/http_util.h" | 32 #include "net/http/http_util.h" |
33 #include "net/spdy/spdy_session_pool.h" | 33 #include "net/spdy/spdy_session_pool.h" |
34 | 34 |
35 namespace net { | 35 namespace net { |
36 | 36 |
37 int HttpCache::DefaultBackend::CreateBackend(disk_cache::Backend** backend, | 37 int HttpCache::DefaultBackend::CreateBackend(disk_cache::Backend** backend, |
38 CompletionCallback* callback) { | 38 CompletionCallback* callback) { |
39 DCHECK_GE(max_bytes_, 0); | 39 DCHECK_GE(max_bytes_, 0); |
40 if (callback) | 40 return disk_cache::CreateCacheBackend(type_, path_, max_bytes_, true, |
41 return disk_cache::CreateCacheBackend(type_, path_, max_bytes_, true, | 41 thread_, backend, callback); |
42 thread_, backend, callback); | |
43 | |
44 // This is just old code needed to support synchronous cache creation. | |
45 // TODO(rvargas): Remove this once all callers provide a callback. | |
46 if (type_ == MEMORY_CACHE) { | |
47 *backend = disk_cache::CreateInMemoryCacheBackend(max_bytes_); | |
48 } else { | |
49 *backend = disk_cache::CreateCacheBackend(path_, true, max_bytes_, type_); | |
50 } | |
51 return (*backend ? OK : ERR_FAILED); | |
52 } | 42 } |
53 | 43 |
54 //----------------------------------------------------------------------------- | 44 //----------------------------------------------------------------------------- |
55 | 45 |
56 HttpCache::ActiveEntry::ActiveEntry(disk_cache::Entry* e) | 46 HttpCache::ActiveEntry::ActiveEntry(disk_cache::Entry* e) |
57 : disk_entry(e), | 47 : disk_entry(e), |
58 writer(NULL), | 48 writer(NULL), |
59 will_process_pending_queue(false), | 49 will_process_pending_queue(false), |
60 doomed(false) { | 50 doomed(false) { |
61 } | 51 } |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 // though they are waiting for a callback that will never fire. | 284 // though they are waiting for a callback that will never fire. |
295 PendingOp* pending_op = pending_it->second; | 285 PendingOp* pending_op = pending_it->second; |
296 delete pending_op->writer; | 286 delete pending_op->writer; |
297 delete pending_op->callback; | 287 delete pending_op->callback; |
298 | 288 |
299 STLDeleteElements(&pending_op->pending_queue); | 289 STLDeleteElements(&pending_op->pending_queue); |
300 delete pending_op; | 290 delete pending_op; |
301 } | 291 } |
302 } | 292 } |
303 | 293 |
304 disk_cache::Backend* HttpCache::GetBackend() { | |
305 if (disk_cache_.get()) | |
306 return disk_cache_.get(); | |
307 | |
308 if (backend_factory_.get()) { | |
309 disk_cache::Backend* backend; | |
310 if (OK == backend_factory_->CreateBackend(&backend, NULL)) | |
311 disk_cache_.reset(backend); | |
312 backend_factory_.reset(); // Reclaim memory. | |
313 } | |
314 return disk_cache_.get(); | |
315 } | |
316 | |
317 int HttpCache::GetBackend(disk_cache::Backend** backend, | 294 int HttpCache::GetBackend(disk_cache::Backend** backend, |
318 CompletionCallback* callback) { | 295 CompletionCallback* callback) { |
319 DCHECK(callback != NULL); | 296 DCHECK(callback != NULL); |
320 | 297 |
321 if (disk_cache_.get()) { | 298 if (disk_cache_.get()) { |
322 *backend = disk_cache_.get(); | 299 *backend = disk_cache_.get(); |
323 return OK; | 300 return OK; |
324 } | 301 } |
325 | 302 |
326 return CreateBackend(backend, callback); | 303 return CreateBackend(backend, callback); |
(...skipping 20 matching lines...) Expand all Loading... |
347 net::HttpNetworkLayer* network = | 324 net::HttpNetworkLayer* network = |
348 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); | 325 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); |
349 return network->GetSession(); | 326 return network->GetSession(); |
350 } | 327 } |
351 | 328 |
352 void HttpCache::Suspend(bool suspend) { | 329 void HttpCache::Suspend(bool suspend) { |
353 network_layer_->Suspend(suspend); | 330 network_layer_->Suspend(suspend); |
354 } | 331 } |
355 | 332 |
356 // static | 333 // static |
357 bool HttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry, | |
358 HttpResponseInfo* response_info, | |
359 bool* response_truncated) { | |
360 int size = disk_entry->GetDataSize(kResponseInfoIndex); | |
361 | |
362 scoped_refptr<IOBuffer> buffer = new IOBuffer(size); | |
363 int rv = disk_entry->ReadData(kResponseInfoIndex, 0, buffer, size, NULL); | |
364 if (rv != size) { | |
365 DLOG(ERROR) << "ReadData failed: " << rv; | |
366 return false; | |
367 } | |
368 | |
369 return ParseResponseInfo(buffer->data(), size, response_info, | |
370 response_truncated); | |
371 } | |
372 | |
373 // static | |
374 bool HttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry, | |
375 const HttpResponseInfo* response_info, | |
376 bool skip_transient_headers, | |
377 bool response_truncated) { | |
378 Pickle pickle; | |
379 response_info->Persist( | |
380 &pickle, skip_transient_headers, response_truncated); | |
381 | |
382 scoped_refptr<WrappedIOBuffer> data = new WrappedIOBuffer( | |
383 reinterpret_cast<const char*>(pickle.data())); | |
384 int len = static_cast<int>(pickle.size()); | |
385 | |
386 return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL, | |
387 true) == len; | |
388 } | |
389 | |
390 // static | |
391 bool HttpCache::ParseResponseInfo(const char* data, int len, | 334 bool HttpCache::ParseResponseInfo(const char* data, int len, |
392 HttpResponseInfo* response_info, | 335 HttpResponseInfo* response_info, |
393 bool* response_truncated) { | 336 bool* response_truncated) { |
394 Pickle pickle(data, len); | 337 Pickle pickle(data, len); |
395 return response_info->InitFromPickle(pickle, response_truncated); | 338 return response_info->InitFromPickle(pickle, response_truncated); |
396 } | 339 } |
397 | 340 |
398 void HttpCache::WriteMetadata(const GURL& url, | 341 void HttpCache::WriteMetadata(const GURL& url, |
399 base::Time expected_response_time, IOBuffer* buf, | 342 base::Time expected_response_time, IOBuffer* buf, |
400 int buf_len) { | 343 int buf_len) { |
401 if (!buf_len) | 344 if (!buf_len) |
402 return; | 345 return; |
403 | 346 |
404 GetBackend(); | 347 // Do lazy initialization of disk cache if needed. |
| 348 if (!disk_cache_.get()) |
| 349 CreateBackend(NULL, NULL); // We don't care about the result. |
| 350 |
405 HttpCache::Transaction* trans = | 351 HttpCache::Transaction* trans = |
406 new HttpCache::Transaction(this, enable_range_support_); | 352 new HttpCache::Transaction(this, enable_range_support_); |
407 MetadataWriter* writer = new MetadataWriter(trans); | 353 MetadataWriter* writer = new MetadataWriter(trans); |
408 | 354 |
409 // The writer will self destruct when done. | 355 // The writer will self destruct when done. |
410 writer->Write(url, expected_response_time, buf, buf_len); | 356 writer->Write(url, expected_response_time, buf, buf_len); |
411 } | 357 } |
412 | 358 |
413 void HttpCache::CloseCurrentConnections() { | 359 void HttpCache::CloseCurrentConnections() { |
414 net::HttpNetworkLayer* network = | 360 net::HttpNetworkLayer* network = |
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 // This could be an external caller or a transaction waiting on Start(). | 988 // This could be an external caller or a transaction waiting on Start(). |
1043 pending_item->DoCallback(result, temp_backend_); | 989 pending_item->DoCallback(result, temp_backend_); |
1044 pending_item->NotifyTransaction(result, NULL); | 990 pending_item->NotifyTransaction(result, NULL); |
1045 } | 991 } |
1046 | 992 |
1047 DeletePendingOp(pending_op); | 993 DeletePendingOp(pending_op); |
1048 building_backend_ = false; | 994 building_backend_ = false; |
1049 } | 995 } |
1050 | 996 |
1051 } // namespace net | 997 } // namespace net |
OLD | NEW |