| 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 "chrome/browser/chromeos/gdata/gdata_cache_metadata.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_cache_metadata.h" |
| 6 | 6 |
| 7 #include <leveldb/db.h> | 7 #include <leveldb/db.h> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 return true; | 280 return true; |
| 281 } else if (md5 == cache_entry.md5()) { | 281 } else if (md5 == cache_entry.md5()) { |
| 282 // Otherwise, compare the MD5. | 282 // Otherwise, compare the MD5. |
| 283 return true; | 283 return true; |
| 284 } | 284 } |
| 285 return false; | 285 return false; |
| 286 } | 286 } |
| 287 | 287 |
| 288 //////////////////////////////////////////////////////////////////////////////// | 288 //////////////////////////////////////////////////////////////////////////////// |
| 289 // GDataCacheMetadata implementation with std::map. | 289 // GDataCacheMetadata implementation with std::map. |
| 290 // Used for testing. |
| 290 | 291 |
| 291 class GDataCacheMetadataMap : public GDataCacheMetadata { | 292 class FakeGDataCacheMetadata : public GDataCacheMetadata { |
| 292 public: | 293 public: |
| 293 explicit GDataCacheMetadataMap( | 294 explicit FakeGDataCacheMetadata( |
| 294 base::SequencedTaskRunner* blocking_task_runner); | 295 base::SequencedTaskRunner* blocking_task_runner); |
| 295 | 296 |
| 296 private: | 297 private: |
| 297 virtual ~GDataCacheMetadataMap(); | 298 virtual ~FakeGDataCacheMetadata(); |
| 298 | 299 |
| 299 // GDataCacheMetadata overrides: | 300 // GDataCacheMetadata overrides: |
| 300 virtual void Initialize(const std::vector<FilePath>& cache_paths) OVERRIDE; | 301 virtual void Initialize(const std::vector<FilePath>& cache_paths) OVERRIDE; |
| 301 virtual void AddOrUpdateCacheEntry( | 302 virtual void AddOrUpdateCacheEntry( |
| 302 const std::string& resource_id, | 303 const std::string& resource_id, |
| 303 const GDataCacheEntry& cache_entry) OVERRIDE; | 304 const GDataCacheEntry& cache_entry) OVERRIDE; |
| 304 virtual void RemoveCacheEntry(const std::string& resource_id) OVERRIDE; | 305 virtual void RemoveCacheEntry(const std::string& resource_id) OVERRIDE; |
| 305 virtual bool GetCacheEntry(const std::string& resource_id, | 306 virtual bool GetCacheEntry(const std::string& resource_id, |
| 306 const std::string& md5, | 307 const std::string& md5, |
| 307 GDataCacheEntry* cache_entry) OVERRIDE; | 308 GDataCacheEntry* cache_entry) OVERRIDE; |
| 308 virtual void RemoveTemporaryFiles() OVERRIDE; | 309 virtual void RemoveTemporaryFiles() OVERRIDE; |
| 309 virtual void Iterate(const IterateCallback& callback) OVERRIDE; | 310 virtual void Iterate(const IterateCallback& callback) OVERRIDE; |
| 310 virtual void ForceRescanForTesting( | 311 virtual void ForceRescanForTesting( |
| 311 const std::vector<FilePath>& cache_paths) OVERRIDE; | 312 const std::vector<FilePath>& cache_paths) OVERRIDE; |
| 312 | 313 |
| 313 CacheMap cache_map_; | 314 CacheMap cache_map_; |
| 314 | 315 |
| 315 DISALLOW_COPY_AND_ASSIGN(GDataCacheMetadataMap); | 316 DISALLOW_COPY_AND_ASSIGN(FakeGDataCacheMetadata); |
| 316 }; | 317 }; |
| 317 | 318 |
| 318 GDataCacheMetadataMap::GDataCacheMetadataMap( | 319 FakeGDataCacheMetadata::FakeGDataCacheMetadata( |
| 319 base::SequencedTaskRunner* blocking_task_runner) | 320 base::SequencedTaskRunner* blocking_task_runner) |
| 320 : GDataCacheMetadata(blocking_task_runner) { | 321 : GDataCacheMetadata(blocking_task_runner) { |
| 321 AssertOnSequencedWorkerPool(); | 322 AssertOnSequencedWorkerPool(); |
| 322 } | 323 } |
| 323 | 324 |
| 324 GDataCacheMetadataMap::~GDataCacheMetadataMap() { | 325 FakeGDataCacheMetadata::~FakeGDataCacheMetadata() { |
| 325 AssertOnSequencedWorkerPool(); | 326 AssertOnSequencedWorkerPool(); |
| 326 } | 327 } |
| 327 | 328 |
| 328 void GDataCacheMetadataMap::Initialize( | 329 void FakeGDataCacheMetadata::Initialize( |
| 329 const std::vector<FilePath>& cache_paths) { | 330 const std::vector<FilePath>& cache_paths) { |
| 330 AssertOnSequencedWorkerPool(); | 331 AssertOnSequencedWorkerPool(); |
| 331 | 332 |
| 332 ScanCachePaths(cache_paths, &cache_map_); | 333 ScanCachePaths(cache_paths, &cache_map_); |
| 333 } | 334 } |
| 334 | 335 |
| 335 void GDataCacheMetadataMap::AddOrUpdateCacheEntry( | 336 void FakeGDataCacheMetadata::AddOrUpdateCacheEntry( |
| 336 const std::string& resource_id, | 337 const std::string& resource_id, |
| 337 const GDataCacheEntry& cache_entry) { | 338 const GDataCacheEntry& cache_entry) { |
| 338 AssertOnSequencedWorkerPool(); | 339 AssertOnSequencedWorkerPool(); |
| 339 | 340 |
| 340 CacheMap::iterator iter = cache_map_.find(resource_id); | 341 CacheMap::iterator iter = cache_map_.find(resource_id); |
| 341 if (iter == cache_map_.end()) { // New resource, create new entry. | 342 if (iter == cache_map_.end()) { // New resource, create new entry. |
| 342 cache_map_.insert(std::make_pair(resource_id, cache_entry)); | 343 cache_map_.insert(std::make_pair(resource_id, cache_entry)); |
| 343 } else { // Resource exists. | 344 } else { // Resource exists. |
| 344 cache_map_[resource_id] = cache_entry; | 345 cache_map_[resource_id] = cache_entry; |
| 345 } | 346 } |
| 346 } | 347 } |
| 347 | 348 |
| 348 void GDataCacheMetadataMap::RemoveCacheEntry(const std::string& resource_id) { | 349 void FakeGDataCacheMetadata::RemoveCacheEntry(const std::string& resource_id) { |
| 349 AssertOnSequencedWorkerPool(); | 350 AssertOnSequencedWorkerPool(); |
| 350 | 351 |
| 351 CacheMap::iterator iter = cache_map_.find(resource_id); | 352 CacheMap::iterator iter = cache_map_.find(resource_id); |
| 352 if (iter != cache_map_.end()) { | 353 if (iter != cache_map_.end()) { |
| 353 // Delete the CacheEntry and remove it from the map. | 354 // Delete the CacheEntry and remove it from the map. |
| 354 cache_map_.erase(iter); | 355 cache_map_.erase(iter); |
| 355 } | 356 } |
| 356 } | 357 } |
| 357 | 358 |
| 358 bool GDataCacheMetadataMap::GetCacheEntry(const std::string& resource_id, | 359 bool FakeGDataCacheMetadata::GetCacheEntry(const std::string& resource_id, |
| 359 const std::string& md5, | 360 const std::string& md5, |
| 360 GDataCacheEntry* entry) { | 361 GDataCacheEntry* entry) { |
| 361 DCHECK(entry); | 362 DCHECK(entry); |
| 362 AssertOnSequencedWorkerPool(); | 363 AssertOnSequencedWorkerPool(); |
| 363 | 364 |
| 364 CacheMap::iterator iter = cache_map_.find(resource_id); | 365 CacheMap::iterator iter = cache_map_.find(resource_id); |
| 365 if (iter == cache_map_.end()) { | 366 if (iter == cache_map_.end()) { |
| 366 DVLOG(1) << "Can't find " << resource_id << " in cache map"; | 367 DVLOG(1) << "Can't find " << resource_id << " in cache map"; |
| 367 return false; | 368 return false; |
| 368 } | 369 } |
| 369 | 370 |
| 370 const GDataCacheEntry& cache_entry = iter->second; | 371 const GDataCacheEntry& cache_entry = iter->second; |
| 371 | 372 |
| 372 if (!CheckIfMd5Matches(md5, cache_entry)) { | 373 if (!CheckIfMd5Matches(md5, cache_entry)) { |
| 373 return false; | 374 return false; |
| 374 } | 375 } |
| 375 | 376 |
| 376 *entry = cache_entry; | 377 *entry = cache_entry; |
| 377 return true; | 378 return true; |
| 378 } | 379 } |
| 379 | 380 |
| 380 void GDataCacheMetadataMap::RemoveTemporaryFiles() { | 381 void FakeGDataCacheMetadata::RemoveTemporaryFiles() { |
| 381 AssertOnSequencedWorkerPool(); | 382 AssertOnSequencedWorkerPool(); |
| 382 | 383 |
| 383 CacheMap::iterator iter = cache_map_.begin(); | 384 CacheMap::iterator iter = cache_map_.begin(); |
| 384 while (iter != cache_map_.end()) { | 385 while (iter != cache_map_.end()) { |
| 385 if (!iter->second.is_persistent()) { | 386 if (!iter->second.is_persistent()) { |
| 386 // Post-increment the iterator to avoid iterator invalidation. | 387 // Post-increment the iterator to avoid iterator invalidation. |
| 387 cache_map_.erase(iter++); | 388 cache_map_.erase(iter++); |
| 388 } else { | 389 } else { |
| 389 ++iter; | 390 ++iter; |
| 390 } | 391 } |
| 391 } | 392 } |
| 392 } | 393 } |
| 393 | 394 |
| 394 void GDataCacheMetadataMap::Iterate(const IterateCallback& callback) { | 395 void FakeGDataCacheMetadata::Iterate(const IterateCallback& callback) { |
| 395 AssertOnSequencedWorkerPool(); | 396 AssertOnSequencedWorkerPool(); |
| 396 | 397 |
| 397 for (CacheMap::const_iterator iter = cache_map_.begin(); | 398 for (CacheMap::const_iterator iter = cache_map_.begin(); |
| 398 iter != cache_map_.end(); ++iter) { | 399 iter != cache_map_.end(); ++iter) { |
| 399 callback.Run(iter->first, iter->second); | 400 callback.Run(iter->first, iter->second); |
| 400 } | 401 } |
| 401 } | 402 } |
| 402 | 403 |
| 403 void GDataCacheMetadataMap::ForceRescanForTesting( | 404 void FakeGDataCacheMetadata::ForceRescanForTesting( |
| 404 const std::vector<FilePath>& cache_paths) { | 405 const std::vector<FilePath>& cache_paths) { |
| 405 AssertOnSequencedWorkerPool(); | 406 AssertOnSequencedWorkerPool(); |
| 406 | 407 |
| 407 ScanCachePaths(cache_paths, &cache_map_); | 408 ScanCachePaths(cache_paths, &cache_map_); |
| 408 } | 409 } |
| 409 | 410 |
| 410 //////////////////////////////////////////////////////////////////////////////// | 411 //////////////////////////////////////////////////////////////////////////////// |
| 411 // GDataCacheMetadata implementation with level::db. | 412 // GDataCacheMetadata implementation with level::db. |
| 412 | 413 |
| 413 class GDataCacheMetadataDB : public GDataCacheMetadata { | 414 class GDataCacheMetadataDB : public GDataCacheMetadata { |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 AssertOnSequencedWorkerPool(); | 590 AssertOnSequencedWorkerPool(); |
| 590 } | 591 } |
| 591 | 592 |
| 592 // static | 593 // static |
| 593 scoped_ptr<GDataCacheMetadata> GDataCacheMetadata::CreateGDataCacheMetadata( | 594 scoped_ptr<GDataCacheMetadata> GDataCacheMetadata::CreateGDataCacheMetadata( |
| 594 base::SequencedTaskRunner* blocking_task_runner) { | 595 base::SequencedTaskRunner* blocking_task_runner) { |
| 595 return scoped_ptr<GDataCacheMetadata>( | 596 return scoped_ptr<GDataCacheMetadata>( |
| 596 new GDataCacheMetadataDB(blocking_task_runner)); | 597 new GDataCacheMetadataDB(blocking_task_runner)); |
| 597 } | 598 } |
| 598 | 599 |
| 600 // static |
| 601 scoped_ptr<GDataCacheMetadata> |
| 602 GDataCacheMetadata::CreateGDataCacheMetadataForTesting( |
| 603 base::SequencedTaskRunner* blocking_task_runner) { |
| 604 return scoped_ptr<GDataCacheMetadata>( |
| 605 new FakeGDataCacheMetadata(blocking_task_runner)); |
| 606 } |
| 607 |
| 599 void GDataCacheMetadata::AssertOnSequencedWorkerPool() { | 608 void GDataCacheMetadata::AssertOnSequencedWorkerPool() { |
| 600 DCHECK(!blocking_task_runner_ || | 609 DCHECK(!blocking_task_runner_ || |
| 601 blocking_task_runner_->RunsTasksOnCurrentThread()); | 610 blocking_task_runner_->RunsTasksOnCurrentThread()); |
| 602 } | 611 } |
| 603 | 612 |
| 604 } // namespace gdata | 613 } // namespace gdata |
| OLD | NEW |