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 <errno.h> | 5 #include <errno.h> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 FilePath GetCacheFilePath(const std::string& resource_id, | 415 FilePath GetCacheFilePath(const std::string& resource_id, |
416 const std::string& md5, | 416 const std::string& md5, |
417 GDataCache::CacheSubDirectoryType sub_dir_type, | 417 GDataCache::CacheSubDirectoryType sub_dir_type, |
418 GDataCache::CachedFileOrigin file_origin) { | 418 GDataCache::CachedFileOrigin file_origin) { |
419 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, | 419 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, |
420 file_origin); | 420 file_origin); |
421 } | 421 } |
422 | 422 |
423 // Helper function to call GetCacheEntry from origin thread. | 423 // Helper function to call GetCacheEntry from origin thread. |
424 // Note: This method calls RunAllPendingForIO. | 424 // Note: This method calls RunAllPendingForIO. |
425 scoped_ptr<GDataCache::CacheEntry> GetCacheEntryFromOriginThread( | 425 bool GetCacheEntryFromOriginThread(const std::string& resource_id, |
426 const std::string& resource_id, | 426 const std::string& md5, |
427 const std::string& md5) { | 427 GDataCache::CacheEntry* cache_entry) { |
428 scoped_ptr<GDataCache::CacheEntry> cache_entry; | 428 bool success = false; |
429 content::BrowserThread::GetBlockingPool() | 429 content::BrowserThread::GetBlockingPool() |
430 ->GetSequencedTaskRunner(sequence_token_)->PostTask( | 430 ->GetSequencedTaskRunner(sequence_token_)->PostTask( |
431 FROM_HERE, | 431 FROM_HERE, |
432 base::Bind( | 432 base::Bind( |
433 &GDataFileSystemTest::GetCacheEntryFromOriginThreadInternal, | 433 &GDataFileSystemTest::GetCacheEntryFromOriginThreadInternal, |
434 base::Unretained(this), | 434 base::Unretained(this), |
435 resource_id, | 435 resource_id, |
436 md5, | 436 md5, |
437 &cache_entry)); | 437 cache_entry, |
| 438 &success)); |
438 RunAllPendingForIO(); | 439 RunAllPendingForIO(); |
439 return cache_entry.Pass(); | 440 return success; |
440 } | 441 } |
441 | 442 |
442 // Used to implement GetCacheEntry. | 443 // Used to implement GetCacheEntry. |
443 void GetCacheEntryFromOriginThreadInternal( | 444 void GetCacheEntryFromOriginThreadInternal( |
444 const std::string& resource_id, | 445 const std::string& resource_id, |
445 const std::string& md5, | 446 const std::string& md5, |
446 scoped_ptr<GDataCache::CacheEntry>* cache_entry) { | 447 GDataCache::CacheEntry* cache_entry, |
447 cache_entry->reset(cache_->GetCacheEntry(resource_id, md5).release()); | 448 bool* success) { |
| 449 *success = cache_->GetCacheEntry(resource_id, md5, cache_entry); |
448 } | 450 } |
449 | 451 |
450 // Returns true if the cache entry exists for the given resource ID and MD5. | 452 // Returns true if the cache entry exists for the given resource ID and MD5. |
451 bool CacheEntryExists(const std::string& resource_id, | 453 bool CacheEntryExists(const std::string& resource_id, |
452 const std::string& md5) { | 454 const std::string& md5) { |
453 return GetCacheEntryFromOriginThread(resource_id, md5).get(); | 455 GDataCache::CacheEntry cache_entry; |
| 456 return GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
454 } | 457 } |
455 | 458 |
456 // Returns true if the cache file exists for the given resource ID and MD5. | 459 // Returns true if the cache file exists for the given resource ID and MD5. |
457 bool CacheFileExists(const std::string& resource_id, | 460 bool CacheFileExists(const std::string& resource_id, |
458 const std::string& md5) { | 461 const std::string& md5) { |
459 const FilePath file_path = cache_->GetCacheFilePath( | 462 const FilePath file_path = cache_->GetCacheFilePath( |
460 resource_id, | 463 resource_id, |
461 md5, | 464 md5, |
462 GDataCache::CACHE_TYPE_TMP, | 465 GDataCache::CACHE_TYPE_TMP, |
463 GDataCache::CACHED_FILE_FROM_SERVER); | 466 GDataCache::CACHED_FILE_FROM_SERVER); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 } | 562 } |
560 | 563 |
561 void VerifyRemoveFromCache(base::PlatformFileError error, | 564 void VerifyRemoveFromCache(base::PlatformFileError error, |
562 const std::string& resource_id, | 565 const std::string& resource_id, |
563 const std::string& md5) { | 566 const std::string& md5) { |
564 ++num_callback_invocations_; | 567 ++num_callback_invocations_; |
565 | 568 |
566 EXPECT_EQ(expected_error_, error); | 569 EXPECT_EQ(expected_error_, error); |
567 | 570 |
568 // Verify cache map. | 571 // Verify cache map. |
569 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 572 GDataCache::CacheEntry cache_entry; |
570 GetCacheEntryFromOriginThread(resource_id, md5); | 573 const bool cache_entry_found = |
571 if (cache_entry.get()) | 574 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
572 EXPECT_TRUE(cache_entry->IsDirty()); | 575 if (cache_entry_found) |
| 576 EXPECT_TRUE(cache_entry.IsDirty()); |
573 | 577 |
574 // If entry doesn't exist, verify that: | 578 // If entry doesn't exist, verify that: |
575 // - no files with "<resource_id>.* exists in persistent and tmp dirs | 579 // - no files with "<resource_id>.* exists in persistent and tmp dirs |
576 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. | 580 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. |
577 std::vector<PathToVerify> paths_to_verify; | 581 std::vector<PathToVerify> paths_to_verify; |
578 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. | 582 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. |
579 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", | 583 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", |
580 GDataCache::CACHE_TYPE_TMP, | 584 GDataCache::CACHE_TYPE_TMP, |
581 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); | 585 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); |
582 paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT. | 586 paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT. |
583 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", | 587 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", |
584 GDataCache::CACHE_TYPE_PERSISTENT, | 588 GDataCache::CACHE_TYPE_PERSISTENT, |
585 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); | 589 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); |
586 paths_to_verify.push_back( // Index 2: CACHE_TYPE_PINNED. | 590 paths_to_verify.push_back( // Index 2: CACHE_TYPE_PINNED. |
587 PathToVerify(cache_->GetCacheFilePath(resource_id, "", | 591 PathToVerify(cache_->GetCacheFilePath(resource_id, "", |
588 GDataCache::CACHE_TYPE_PINNED, | 592 GDataCache::CACHE_TYPE_PINNED, |
589 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); | 593 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); |
590 paths_to_verify.push_back( // Index 3: CACHE_TYPE_OUTGOING. | 594 paths_to_verify.push_back( // Index 3: CACHE_TYPE_OUTGOING. |
591 PathToVerify(cache_->GetCacheFilePath(resource_id, "", | 595 PathToVerify(cache_->GetCacheFilePath(resource_id, "", |
592 GDataCache::CACHE_TYPE_OUTGOING, | 596 GDataCache::CACHE_TYPE_OUTGOING, |
593 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); | 597 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); |
594 if (!cache_entry.get()) { | 598 if (!cache_entry_found) { |
595 for (size_t i = 0; i < paths_to_verify.size(); ++i) { | 599 for (size_t i = 0; i < paths_to_verify.size(); ++i) { |
596 file_util::FileEnumerator enumerator( | 600 file_util::FileEnumerator enumerator( |
597 paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/, | 601 paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/, |
598 static_cast<file_util::FileEnumerator::FileType>( | 602 static_cast<file_util::FileEnumerator::FileType>( |
599 file_util::FileEnumerator::FILES | | 603 file_util::FileEnumerator::FILES | |
600 file_util::FileEnumerator::SHOW_SYM_LINKS), | 604 file_util::FileEnumerator::SHOW_SYM_LINKS), |
601 paths_to_verify[i].path_to_scan.BaseName().value()); | 605 paths_to_verify[i].path_to_scan.BaseName().value()); |
602 EXPECT_TRUE(enumerator.Next().empty()); | 606 EXPECT_TRUE(enumerator.Next().empty()); |
603 } | 607 } |
604 } else { | 608 } else { |
(...skipping 10 matching lines...) Expand all Loading... |
615 GDataCache::CACHE_TYPE_PERSISTENT, | 619 GDataCache::CACHE_TYPE_PERSISTENT, |
616 GDataCache::CACHED_FILE_LOCALLY_MODIFIED); | 620 GDataCache::CACHED_FILE_LOCALLY_MODIFIED); |
617 | 621 |
618 // Change expected_existing_path of CACHE_TYPE_OUTGOING (index 3). | 622 // Change expected_existing_path of CACHE_TYPE_OUTGOING (index 3). |
619 paths_to_verify[3].expected_existing_path = | 623 paths_to_verify[3].expected_existing_path = |
620 GetCacheFilePath(resource_id, | 624 GetCacheFilePath(resource_id, |
621 std::string(), | 625 std::string(), |
622 GDataCache::CACHE_TYPE_OUTGOING, | 626 GDataCache::CACHE_TYPE_OUTGOING, |
623 GDataCache::CACHED_FILE_FROM_SERVER); | 627 GDataCache::CACHED_FILE_FROM_SERVER); |
624 | 628 |
625 if (cache_entry->IsPinned()) { | 629 if (cache_entry.IsPinned()) { |
626 // Change expected_existing_path of CACHE_TYPE_PINNED (index 2). | 630 // Change expected_existing_path of CACHE_TYPE_PINNED (index 2). |
627 paths_to_verify[2].expected_existing_path = | 631 paths_to_verify[2].expected_existing_path = |
628 GetCacheFilePath(resource_id, | 632 GetCacheFilePath(resource_id, |
629 std::string(), | 633 std::string(), |
630 GDataCache::CACHE_TYPE_PINNED, | 634 GDataCache::CACHE_TYPE_PINNED, |
631 GDataCache::CACHED_FILE_FROM_SERVER); | 635 GDataCache::CACHED_FILE_FROM_SERVER); |
632 } | 636 } |
633 | 637 |
634 for (size_t i = 0; i < paths_to_verify.size(); ++i) { | 638 for (size_t i = 0; i < paths_to_verify.size(); ++i) { |
635 const struct PathToVerify& verify = paths_to_verify[i]; | 639 const struct PathToVerify& verify = paths_to_verify[i]; |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
935 GDataCache::IsCachePresent(resource.cache_state) ? | 939 GDataCache::IsCachePresent(resource.cache_state) ? |
936 base::PLATFORM_FILE_OK : | 940 base::PLATFORM_FILE_OK : |
937 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 941 base::PLATFORM_FILE_ERROR_NOT_FOUND, |
938 resource.expected_file_extension); | 942 resource.expected_file_extension); |
939 EXPECT_EQ(1, num_callback_invocations_); | 943 EXPECT_EQ(1, num_callback_invocations_); |
940 | 944 |
941 // Verify cache state. | 945 // Verify cache state. |
942 std::string md5; | 946 std::string md5; |
943 if (GDataCache::IsCachePresent(resource.cache_state)) | 947 if (GDataCache::IsCachePresent(resource.cache_state)) |
944 md5 = resource.md5; | 948 md5 = resource.md5; |
945 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 949 GDataCache::CacheEntry cache_entry; |
946 GetCacheEntryFromOriginThread(resource.resource_id, md5); | 950 ASSERT_TRUE(GetCacheEntryFromOriginThread( |
947 ASSERT_TRUE(cache_entry.get()); | 951 resource.resource_id, md5, &cache_entry)); |
948 EXPECT_EQ(resource.cache_state, cache_entry->cache_state); | 952 EXPECT_EQ(resource.cache_state, cache_entry.cache_state); |
949 EXPECT_EQ(resource.expected_sub_dir_type, cache_entry->sub_dir_type); | 953 EXPECT_EQ(resource.expected_sub_dir_type, cache_entry.sub_dir_type); |
950 } | 954 } |
951 } | 955 } |
952 | 956 |
953 void VerifyCacheFileState(base::PlatformFileError error, | 957 void VerifyCacheFileState(base::PlatformFileError error, |
954 const std::string& resource_id, | 958 const std::string& resource_id, |
955 const std::string& md5) { | 959 const std::string& md5) { |
956 ++num_callback_invocations_; | 960 ++num_callback_invocations_; |
957 | 961 |
958 EXPECT_EQ(expected_error_, error); | 962 EXPECT_EQ(expected_error_, error); |
959 | 963 |
960 // Verify cache map. | 964 // Verify cache map. |
961 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 965 GDataCache::CacheEntry cache_entry; |
962 GetCacheEntryFromOriginThread(resource_id, md5); | |
963 if (GDataCache::IsCachePresent(expected_cache_state_) || | 966 if (GDataCache::IsCachePresent(expected_cache_state_) || |
964 GDataCache::IsCachePinned(expected_cache_state_)) { | 967 GDataCache::IsCachePinned(expected_cache_state_)) { |
965 ASSERT_TRUE(cache_entry.get()); | 968 ASSERT_TRUE(GetCacheEntryFromOriginThread( |
966 EXPECT_EQ(expected_cache_state_, cache_entry->cache_state); | 969 resource_id, md5, &cache_entry)); |
967 EXPECT_EQ(expected_sub_dir_type_, cache_entry->sub_dir_type); | 970 EXPECT_EQ(expected_cache_state_, cache_entry.cache_state); |
| 971 EXPECT_EQ(expected_sub_dir_type_, cache_entry.sub_dir_type); |
968 } else { | 972 } else { |
969 EXPECT_FALSE(cache_entry.get()); | 973 EXPECT_FALSE(GetCacheEntryFromOriginThread( |
| 974 resource_id, md5, &cache_entry)); |
970 } | 975 } |
971 | 976 |
972 // Verify actual cache file. | 977 // Verify actual cache file. |
973 FilePath dest_path = cache_->GetCacheFilePath( | 978 FilePath dest_path = cache_->GetCacheFilePath( |
974 resource_id, | 979 resource_id, |
975 md5, | 980 md5, |
976 GDataCache::IsCachePinned(expected_cache_state_) || | 981 GDataCache::IsCachePinned(expected_cache_state_) || |
977 GDataCache::IsCacheDirty(expected_cache_state_) ? | 982 GDataCache::IsCacheDirty(expected_cache_state_) ? |
978 GDataCache::CACHE_TYPE_PERSISTENT : | 983 GDataCache::CACHE_TYPE_PERSISTENT : |
979 GDataCache::CACHE_TYPE_TMP, | 984 GDataCache::CACHE_TYPE_TMP, |
(...skipping 2511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3491 | 3496 |
3492 // Try to close the same file twice. | 3497 // Try to close the same file twice. |
3493 file_system_->CloseFile(kFileInRoot, close_file_callback); | 3498 file_system_->CloseFile(kFileInRoot, close_file_callback); |
3494 message_loop_.Run(); | 3499 message_loop_.Run(); |
3495 | 3500 |
3496 // It must fail. | 3501 // It must fail. |
3497 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, callback_helper_->last_error_); | 3502 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, callback_helper_->last_error_); |
3498 } | 3503 } |
3499 | 3504 |
3500 } // namespace gdata | 3505 } // namespace gdata |
OLD | NEW |