| 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/disk_cache/blockfile/backend_worker_v3.h" | 5 #include "net/disk_cache/blockfile/backend_worker_v3.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 // Sets group for the current experiment. Returns false if the files should be | 66 // Sets group for the current experiment. Returns false if the files should be |
| 67 // discarded. | 67 // discarded. |
| 68 bool InitExperiment(disk_cache::IndexHeader* header, bool cache_created) { | 68 bool InitExperiment(disk_cache::IndexHeader* header, bool cache_created) { |
| 69 if (header->experiment == disk_cache::EXPERIMENT_OLD_FILE1 || | 69 if (header->experiment == disk_cache::EXPERIMENT_OLD_FILE1 || |
| 70 header->experiment == disk_cache::EXPERIMENT_OLD_FILE2) { | 70 header->experiment == disk_cache::EXPERIMENT_OLD_FILE2) { |
| 71 // Discard current cache. | 71 // Discard current cache. |
| 72 return false; | 72 return false; |
| 73 } | 73 } |
| 74 | 74 |
| 75 if (base::FieldTrialList::FindFullName("SimpleCacheTrial") == | 75 if (base::FieldTrialList::FindFullName("SimpleCacheTrial") == |
| 76 "ExperimentControl") { | 76 "ExperimentControl") { |
| 77 if (cache_created) { | 77 if (cache_created) { |
| 78 header->experiment = disk_cache::EXPERIMENT_SIMPLE_CONTROL; | 78 header->experiment = disk_cache::EXPERIMENT_SIMPLE_CONTROL; |
| 79 return true; | 79 return true; |
| 80 } else if (header->experiment != disk_cache::EXPERIMENT_SIMPLE_CONTROL) { | 80 } else if (header->experiment != disk_cache::EXPERIMENT_SIMPLE_CONTROL) { |
| 81 return false; | 81 return false; |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 header->experiment = disk_cache::NO_EXPERIMENT; | 85 header->experiment = disk_cache::NO_EXPERIMENT; |
| 86 return true; | 86 return true; |
| 87 } | 87 } |
| 88 #endif // defined(V3_NOT_JUST_YET_READY). | 88 #endif // defined(V3_NOT_JUST_YET_READY). |
| 89 | 89 |
| 90 } // namespace | 90 } // namespace |
| 91 | 91 |
| 92 // ------------------------------------------------------------------------ | 92 // ------------------------------------------------------------------------ |
| 93 | 93 |
| 94 namespace disk_cache { | 94 namespace disk_cache { |
| 95 | 95 |
| 96 BackendImplV3::Worker::Worker(const base::FilePath& path, | 96 BackendImplV3::Worker::Worker(const base::FilePath& path, |
| 97 base::MessageLoopProxy* main_thread) | 97 base::MessageLoopProxy* main_thread) |
| 98 : path_(path), | 98 : path_(path), block_files_(path), init_(false) { |
| 99 block_files_(path), | |
| 100 init_(false) { | |
| 101 } | 99 } |
| 102 | 100 |
| 103 #if defined(V3_NOT_JUST_YET_READY) | 101 #if defined(V3_NOT_JUST_YET_READY) |
| 104 | 102 |
| 105 int BackendImpl::SyncInit() { | 103 int BackendImpl::SyncInit() { |
| 106 #if defined(NET_BUILD_STRESS_CACHE) | 104 #if defined(NET_BUILD_STRESS_CACHE) |
| 107 // Start evictions right away. | 105 // Start evictions right away. |
| 108 up_ticks_ = kTrimDelay * 2; | 106 up_ticks_ = kTrimDelay * 2; |
| 109 #endif | 107 #endif |
| 110 DCHECK(!init_); | 108 DCHECK(!init_); |
| 111 if (init_) | 109 if (init_) |
| 112 return net::ERR_FAILED; | 110 return net::ERR_FAILED; |
| 113 | 111 |
| 114 bool create_files = false; | 112 bool create_files = false; |
| 115 if (!InitBackingStore(&create_files)) { | 113 if (!InitBackingStore(&create_files)) { |
| 116 ReportError(ERR_STORAGE_ERROR); | 114 ReportError(ERR_STORAGE_ERROR); |
| 117 return net::ERR_FAILED; | 115 return net::ERR_FAILED; |
| 118 } | 116 } |
| 119 | 117 |
| 120 num_refs_ = num_pending_io_ = max_refs_ = 0; | 118 num_refs_ = num_pending_io_ = max_refs_ = 0; |
| 121 entry_count_ = byte_count_ = 0; | 119 entry_count_ = byte_count_ = 0; |
| 122 | 120 |
| 123 if (!restarted_) { | 121 if (!restarted_) { |
| 124 buffer_bytes_ = 0; | 122 buffer_bytes_ = 0; |
| 125 trace_object_ = TraceObject::GetTraceObject(); | 123 trace_object_ = TraceObject::GetTraceObject(); |
| 126 // Create a recurrent timer of 30 secs. | 124 // Create a recurrent timer of 30 secs. |
| 127 int timer_delay = unit_test_ ? 1000 : 30000; | 125 int timer_delay = unit_test_ ? 1000 : 30000; |
| 128 timer_.reset(new base::RepeatingTimer<BackendImpl>()); | 126 timer_.reset(new base::RepeatingTimer<BackendImpl>()); |
| 129 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(timer_delay), this, | 127 timer_->Start(FROM_HERE, |
| 128 TimeDelta::FromMilliseconds(timer_delay), |
| 129 this, |
| 130 &BackendImpl::OnStatsTimer); | 130 &BackendImpl::OnStatsTimer); |
| 131 } | 131 } |
| 132 | 132 |
| 133 init_ = true; | 133 init_ = true; |
| 134 Trace("Init"); | 134 Trace("Init"); |
| 135 | 135 |
| 136 if (data_->header.experiment != NO_EXPERIMENT && | 136 if (data_->header.experiment != NO_EXPERIMENT && |
| 137 cache_type_ != net::DISK_CACHE) { | 137 cache_type_ != net::DISK_CACHE) { |
| 138 // No experiment for other caches. | 138 // No experiment for other caches. |
| 139 return net::ERR_FAILED; | 139 return net::ERR_FAILED; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 | 303 |
| 304 return file->SetLength(GetIndexSize(header.table_len)); | 304 return file->SetLength(GetIndexSize(header.table_len)); |
| 305 } | 305 } |
| 306 | 306 |
| 307 bool BackendImpl::InitBackingStore(bool* file_created) { | 307 bool BackendImpl::InitBackingStore(bool* file_created) { |
| 308 if (!base::CreateDirectory(path_)) | 308 if (!base::CreateDirectory(path_)) |
| 309 return false; | 309 return false; |
| 310 | 310 |
| 311 base::FilePath index_name = path_.AppendASCII(kIndexName); | 311 base::FilePath index_name = path_.AppendASCII(kIndexName); |
| 312 | 312 |
| 313 int flags = base::PLATFORM_FILE_READ | | 313 int flags = base::PLATFORM_FILE_READ | base::PLATFORM_FILE_WRITE | |
| 314 base::PLATFORM_FILE_WRITE | | |
| 315 base::PLATFORM_FILE_OPEN_ALWAYS | | 314 base::PLATFORM_FILE_OPEN_ALWAYS | |
| 316 base::PLATFORM_FILE_EXCLUSIVE_WRITE; | 315 base::PLATFORM_FILE_EXCLUSIVE_WRITE; |
| 317 scoped_refptr<disk_cache::File> file(new disk_cache::File( | 316 scoped_refptr<disk_cache::File> file(new disk_cache::File( |
| 318 base::CreatePlatformFile(index_name, flags, file_created, NULL))); | 317 base::CreatePlatformFile(index_name, flags, file_created, NULL))); |
| 319 | 318 |
| 320 if (!file->IsValid()) | 319 if (!file->IsValid()) |
| 321 return false; | 320 return false; |
| 322 | 321 |
| 323 bool ret = true; | 322 bool ret = true; |
| 324 if (*file_created) | 323 if (*file_created) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 347 | 346 |
| 348 void BackendImpl::ReportError(int error) { | 347 void BackendImpl::ReportError(int error) { |
| 349 STRESS_DCHECK(!error || error == ERR_PREVIOUS_CRASH || | 348 STRESS_DCHECK(!error || error == ERR_PREVIOUS_CRASH || |
| 350 error == ERR_CACHE_CREATED); | 349 error == ERR_CACHE_CREATED); |
| 351 | 350 |
| 352 // We transmit positive numbers, instead of direct error codes. | 351 // We transmit positive numbers, instead of direct error codes. |
| 353 DCHECK_LE(error, 0); | 352 DCHECK_LE(error, 0); |
| 354 CACHE_UMA(CACHE_ERROR, "Error", 0, error * -1); | 353 CACHE_UMA(CACHE_ERROR, "Error", 0, error * -1); |
| 355 } | 354 } |
| 356 | 355 |
| 357 | |
| 358 bool BackendImpl::CheckIndex() { | 356 bool BackendImpl::CheckIndex() { |
| 359 DCHECK(data_); | 357 DCHECK(data_); |
| 360 | 358 |
| 361 size_t current_size = index_->GetLength(); | 359 size_t current_size = index_->GetLength(); |
| 362 if (current_size < sizeof(Index)) { | 360 if (current_size < sizeof(Index)) { |
| 363 LOG(ERROR) << "Corrupt Index file"; | 361 LOG(ERROR) << "Corrupt Index file"; |
| 364 return false; | 362 return false; |
| 365 } | 363 } |
| 366 | 364 |
| 367 if (new_eviction_) { | 365 if (new_eviction_) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 return false; | 435 return false; |
| 438 } | 436 } |
| 439 | 437 |
| 440 // Load the required data. | 438 // Load the required data. |
| 441 size = address.num_blocks() * address.BlockSize(); | 439 size = address.num_blocks() * address.BlockSize(); |
| 442 MappedFile* file = File(address); | 440 MappedFile* file = File(address); |
| 443 if (!file) | 441 if (!file) |
| 444 return false; | 442 return false; |
| 445 | 443 |
| 446 scoped_ptr<char[]> data(new char[size]); | 444 scoped_ptr<char[]> data(new char[size]); |
| 447 size_t offset = address.start_block() * address.BlockSize() + | 445 size_t offset = |
| 448 kBlockHeaderSize; | 446 address.start_block() * address.BlockSize() + kBlockHeaderSize; |
| 449 if (!file->Read(data.get(), size, offset)) | 447 if (!file->Read(data.get(), size, offset)) |
| 450 return false; | 448 return false; |
| 451 | 449 |
| 452 if (!stats_.Init(data.get(), size, address)) | 450 if (!stats_.Init(data.get(), size, address)) |
| 453 return false; | 451 return false; |
| 454 if (cache_type_ == net::DISK_CACHE && ShouldReportAgain()) | 452 if (cache_type_ == net::DISK_CACHE && ShouldReportAgain()) |
| 455 stats_.InitSizeHistogram(); | 453 stats_.InitSizeHistogram(); |
| 456 return true; | 454 return true; |
| 457 } | 455 } |
| 458 | 456 |
| 459 #endif // defined(V3_NOT_JUST_YET_READY). | 457 #endif // defined(V3_NOT_JUST_YET_READY). |
| 460 | 458 |
| 461 int BackendImplV3::Worker::Init(const CompletionCallback& callback) { | 459 int BackendImplV3::Worker::Init(const CompletionCallback& callback) { |
| 462 return net::ERR_FAILED; | 460 return net::ERR_FAILED; |
| 463 } | 461 } |
| 464 | 462 |
| 465 BackendImplV3::Worker::~Worker() { | 463 BackendImplV3::Worker::~Worker() { |
| 466 } | 464 } |
| 467 | 465 |
| 468 } // namespace disk_cache | 466 } // namespace disk_cache |
| OLD | NEW |