| 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 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 } | 419 } |
| 420 | 420 |
| 421 FilePath GetCacheFilePath(const std::string& resource_id, | 421 FilePath GetCacheFilePath(const std::string& resource_id, |
| 422 const std::string& md5, | 422 const std::string& md5, |
| 423 GDataCache::CacheSubDirectoryType sub_dir_type, | 423 GDataCache::CacheSubDirectoryType sub_dir_type, |
| 424 GDataCache::CachedFileOrigin file_origin) { | 424 GDataCache::CachedFileOrigin file_origin) { |
| 425 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, | 425 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, |
| 426 file_origin); | 426 file_origin); |
| 427 } | 427 } |
| 428 | 428 |
| 429 // Helper function to call GetCacheEntry from origin thread. |
| 430 // Note: This method calls RunAllPendingForIO. |
| 431 scoped_ptr<GDataCache::CacheEntry> GetCacheEntryFromOriginThread( |
| 432 const std::string& resource_id, |
| 433 const std::string& md5) { |
| 434 scoped_ptr<GDataCache::CacheEntry> cache_entry; |
| 435 content::BrowserThread::GetBlockingPool() |
| 436 ->GetSequencedTaskRunner(sequence_token_)->PostTask( |
| 437 FROM_HERE, |
| 438 base::Bind( |
| 439 &GDataFileSystemTest::GetCacheEntryFromOriginThreadInternal, |
| 440 base::Unretained(this), |
| 441 resource_id, |
| 442 md5, |
| 443 &cache_entry)); |
| 444 RunAllPendingForIO(); |
| 445 return cache_entry.Pass(); |
| 446 } |
| 447 |
| 448 // Used to implement GetCacheEntry. |
| 449 void GetCacheEntryFromOriginThreadInternal( |
| 450 const std::string& resource_id, |
| 451 const std::string& md5, |
| 452 scoped_ptr<GDataCache::CacheEntry>* cache_entry) { |
| 453 cache_entry->reset(cache_->GetCacheEntry(resource_id, md5).release()); |
| 454 } |
| 455 |
| 429 // Returns true if the cache entry exists for the given resource ID and MD5. | 456 // Returns true if the cache entry exists for the given resource ID and MD5. |
| 430 bool CacheEntryExists(const std::string& resource_id, | 457 bool CacheEntryExists(const std::string& resource_id, |
| 431 const std::string& md5) { | 458 const std::string& md5) { |
| 432 return file_system_->cache_->GetCacheEntry(resource_id, md5).get(); | 459 return GetCacheEntryFromOriginThread(resource_id, md5).get(); |
| 433 } | 460 } |
| 434 | 461 |
| 435 // Returns true if the cache file exists for the given resource ID and MD5. | 462 // Returns true if the cache file exists for the given resource ID and MD5. |
| 436 bool CacheFileExists(const std::string& resource_id, | 463 bool CacheFileExists(const std::string& resource_id, |
| 437 const std::string& md5) { | 464 const std::string& md5) { |
| 438 const FilePath file_path = cache_->GetCacheFilePath( | 465 const FilePath file_path = cache_->GetCacheFilePath( |
| 439 resource_id, | 466 resource_id, |
| 440 md5, | 467 md5, |
| 441 GDataCache::CACHE_TYPE_TMP, | 468 GDataCache::CACHE_TYPE_TMP, |
| 442 GDataCache::CACHED_FILE_FROM_SERVER); | 469 GDataCache::CACHED_FILE_FROM_SERVER); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 | 563 |
| 537 void VerifyRemoveFromCache(base::PlatformFileError error, | 564 void VerifyRemoveFromCache(base::PlatformFileError error, |
| 538 const std::string& resource_id, | 565 const std::string& resource_id, |
| 539 const std::string& md5) { | 566 const std::string& md5) { |
| 540 ++num_callback_invocations_; | 567 ++num_callback_invocations_; |
| 541 | 568 |
| 542 EXPECT_EQ(expected_error_, error); | 569 EXPECT_EQ(expected_error_, error); |
| 543 | 570 |
| 544 // Verify cache map. | 571 // Verify cache map. |
| 545 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 572 scoped_ptr<GDataCache::CacheEntry> cache_entry = |
| 546 file_system_->cache_->GetCacheEntry(resource_id, md5); | 573 GetCacheEntryFromOriginThread(resource_id, md5); |
| 547 if (cache_entry.get()) | 574 if (cache_entry.get()) |
| 548 EXPECT_TRUE(cache_entry->IsDirty()); | 575 EXPECT_TRUE(cache_entry->IsDirty()); |
| 549 | 576 |
| 550 // If entry doesn't exist, verify that: | 577 // If entry doesn't exist, verify that: |
| 551 // - no files with "<resource_id>.* exists in persistent and tmp dirs | 578 // - no files with "<resource_id>.* exists in persistent and tmp dirs |
| 552 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. | 579 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. |
| 553 std::vector<PathToVerify> paths_to_verify; | 580 std::vector<PathToVerify> paths_to_verify; |
| 554 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. | 581 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. |
| 555 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", | 582 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", |
| 556 GDataCache::CACHE_TYPE_TMP, | 583 GDataCache::CACHE_TYPE_TMP, |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 base::PLATFORM_FILE_OK : | 937 base::PLATFORM_FILE_OK : |
| 911 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 938 base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 912 resource.expected_file_extension); | 939 resource.expected_file_extension); |
| 913 EXPECT_EQ(1, num_callback_invocations_); | 940 EXPECT_EQ(1, num_callback_invocations_); |
| 914 | 941 |
| 915 // Verify cache state. | 942 // Verify cache state. |
| 916 std::string md5; | 943 std::string md5; |
| 917 if (GDataCache::IsCachePresent(resource.cache_state)) | 944 if (GDataCache::IsCachePresent(resource.cache_state)) |
| 918 md5 = resource.md5; | 945 md5 = resource.md5; |
| 919 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 946 scoped_ptr<GDataCache::CacheEntry> cache_entry = |
| 920 file_system_->cache_->GetCacheEntry(resource.resource_id, md5); | 947 GetCacheEntryFromOriginThread(resource.resource_id, md5); |
| 921 ASSERT_TRUE(cache_entry.get()); | 948 ASSERT_TRUE(cache_entry.get()); |
| 922 EXPECT_EQ(resource.cache_state, cache_entry->cache_state); | 949 EXPECT_EQ(resource.cache_state, cache_entry->cache_state); |
| 923 EXPECT_EQ(resource.expected_sub_dir_type, cache_entry->sub_dir_type); | 950 EXPECT_EQ(resource.expected_sub_dir_type, cache_entry->sub_dir_type); |
| 924 } | 951 } |
| 925 } | 952 } |
| 926 | 953 |
| 927 void VerifyCacheFileState(base::PlatformFileError error, | 954 void VerifyCacheFileState(base::PlatformFileError error, |
| 928 const std::string& resource_id, | 955 const std::string& resource_id, |
| 929 const std::string& md5) { | 956 const std::string& md5) { |
| 930 ++num_callback_invocations_; | 957 ++num_callback_invocations_; |
| 931 | 958 |
| 932 EXPECT_EQ(expected_error_, error); | 959 EXPECT_EQ(expected_error_, error); |
| 933 | 960 |
| 934 // Verify cache map. | 961 // Verify cache map. |
| 935 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 962 scoped_ptr<GDataCache::CacheEntry> cache_entry = |
| 936 file_system_->cache_->GetCacheEntry(resource_id, md5); | 963 GetCacheEntryFromOriginThread(resource_id, md5); |
| 937 if (GDataCache::IsCachePresent(expected_cache_state_) || | 964 if (GDataCache::IsCachePresent(expected_cache_state_) || |
| 938 GDataCache::IsCachePinned(expected_cache_state_)) { | 965 GDataCache::IsCachePinned(expected_cache_state_)) { |
| 939 ASSERT_TRUE(cache_entry.get()); | 966 ASSERT_TRUE(cache_entry.get()); |
| 940 EXPECT_EQ(expected_cache_state_, cache_entry->cache_state); | 967 EXPECT_EQ(expected_cache_state_, cache_entry->cache_state); |
| 941 EXPECT_EQ(expected_sub_dir_type_, cache_entry->sub_dir_type); | 968 EXPECT_EQ(expected_sub_dir_type_, cache_entry->sub_dir_type); |
| 942 } else { | 969 } else { |
| 943 EXPECT_FALSE(cache_entry.get()); | 970 EXPECT_FALSE(cache_entry.get()); |
| 944 } | 971 } |
| 945 | 972 |
| 946 // Verify actual cache file. | 973 // Verify actual cache file. |
| (...skipping 2875 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3822 // Verify that the file was properly closed. | 3849 // Verify that the file was properly closed. |
| 3823 EXPECT_EQ(base::PLATFORM_FILE_OK, callback_helper_->last_error_); | 3850 EXPECT_EQ(base::PLATFORM_FILE_OK, callback_helper_->last_error_); |
| 3824 | 3851 |
| 3825 // Verify that the cache state was changed as expected. | 3852 // Verify that the cache state was changed as expected. |
| 3826 VerifyCacheStateAfterCloseFile(callback_helper_->last_error_, | 3853 VerifyCacheStateAfterCloseFile(callback_helper_->last_error_, |
| 3827 file_resource_id, | 3854 file_resource_id, |
| 3828 file_md5); | 3855 file_md5); |
| 3829 } | 3856 } |
| 3830 | 3857 |
| 3831 } // namespace gdata | 3858 } // namespace gdata |
| OLD | NEW |