| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/hash_tables.h" | 7 #include "base/hash_tables.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 //----------------------------------------------------------------------------- | 66 //----------------------------------------------------------------------------- |
| 67 // mock disk cache (a very basic memory cache implementation) | 67 // mock disk cache (a very basic memory cache implementation) |
| 68 | 68 |
| 69 static const int kNumCacheEntryDataIndices = 3; | 69 static const int kNumCacheEntryDataIndices = 3; |
| 70 | 70 |
| 71 class MockDiskEntry : public disk_cache::Entry, | 71 class MockDiskEntry : public disk_cache::Entry, |
| 72 public base::RefCounted<MockDiskEntry> { | 72 public base::RefCounted<MockDiskEntry> { |
| 73 public: | 73 public: |
| 74 MockDiskEntry() | 74 MockDiskEntry() |
| 75 : test_mode_(0), doomed_(false), sparse_(false), fail_requests_(false), | 75 : test_mode_(0), rank_(0), doomed_(false), sparse_(false), |
| 76 busy_(false), delayed_(false) { | 76 fail_requests_(false), busy_(false), delayed_(false) { |
| 77 } | 77 } |
| 78 | 78 |
| 79 explicit MockDiskEntry(const std::string& key) | 79 explicit MockDiskEntry(const std::string& key) |
| 80 : key_(key), doomed_(false), sparse_(false), fail_requests_(false), | 80 : key_(key), rank_(0), doomed_(false), sparse_(false), |
| 81 busy_(false), delayed_(false) { | 81 fail_requests_(false), busy_(false), delayed_(false) { |
| 82 test_mode_ = GetTestModeForEntry(key); | 82 test_mode_ = GetTestModeForEntry(key); |
| 83 } | 83 } |
| 84 | 84 |
| 85 bool is_doomed() const { return doomed_; } | 85 bool is_doomed() const { return doomed_; } |
| 86 | 86 |
| 87 virtual void Doom() { | 87 virtual void Doom() { |
| 88 doomed_ = true; | 88 doomed_ = true; |
| 89 } | 89 } |
| 90 | 90 |
| 91 virtual void Close() { | 91 virtual void Close() { |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 DCHECK(completion_callback); | 272 DCHECK(completion_callback); |
| 273 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) | 273 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
| 274 return net::OK; | 274 return net::OK; |
| 275 | 275 |
| 276 // The pending operation is already in the message loop (and hopefuly | 276 // The pending operation is already in the message loop (and hopefuly |
| 277 // already in the second pass). Just notify the caller that it finished. | 277 // already in the second pass). Just notify the caller that it finished. |
| 278 CallbackLater(completion_callback, 0); | 278 CallbackLater(completion_callback, 0); |
| 279 return net::ERR_IO_PENDING; | 279 return net::ERR_IO_PENDING; |
| 280 } | 280 } |
| 281 | 281 |
| 282 void UpdateRankForExternalCacheHit() { |
| 283 ++rank_; |
| 284 } |
| 285 |
| 286 int rank() const { return rank_; } |
| 287 |
| 282 // Fail most subsequent requests. | 288 // Fail most subsequent requests. |
| 283 void set_fail_requests() { fail_requests_ = true; } | 289 void set_fail_requests() { fail_requests_ = true; } |
| 284 | 290 |
| 285 // If |value| is true, don't deliver any completion callbacks until called | 291 // If |value| is true, don't deliver any completion callbacks until called |
| 286 // again with |value| set to false. Caution: remember to enable callbacks | 292 // again with |value| set to false. Caution: remember to enable callbacks |
| 287 // again or all subsequent tests will fail. | 293 // again or all subsequent tests will fail. |
| 288 static void IgnoreCallbacks(bool value) { | 294 static void IgnoreCallbacks(bool value) { |
| 289 if (ignore_callbacks_ == value) | 295 if (ignore_callbacks_ == value) |
| 290 return; | 296 return; |
| 291 ignore_callbacks_ = value; | 297 ignore_callbacks_ = value; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 CallbackInfo& c = callback_list[i]; | 352 CallbackInfo& c = callback_list[i]; |
| 347 c.entry->CallbackLater(c.callback, c.result); | 353 c.entry->CallbackLater(c.callback, c.result); |
| 348 } | 354 } |
| 349 callback_list.clear(); | 355 callback_list.clear(); |
| 350 } | 356 } |
| 351 } | 357 } |
| 352 | 358 |
| 353 std::string key_; | 359 std::string key_; |
| 354 std::vector<char> data_[kNumCacheEntryDataIndices]; | 360 std::vector<char> data_[kNumCacheEntryDataIndices]; |
| 355 int test_mode_; | 361 int test_mode_; |
| 362 int rank_; |
| 356 bool doomed_; | 363 bool doomed_; |
| 357 bool sparse_; | 364 bool sparse_; |
| 358 bool fail_requests_; | 365 bool fail_requests_; |
| 359 bool busy_; | 366 bool busy_; |
| 360 bool delayed_; | 367 bool delayed_; |
| 361 static bool cancel_; | 368 static bool cancel_; |
| 362 static bool ignore_callbacks_; | 369 static bool ignore_callbacks_; |
| 363 }; | 370 }; |
| 364 | 371 |
| 365 // Statics. | 372 // Statics. |
| (...skipping 4726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5092 // Now return 200 when validating the entry so the metadata will be lost. | 5099 // Now return 200 when validating the entry so the metadata will be lost. |
| 5093 MockTransaction trans2(kTypicalGET_Transaction); | 5100 MockTransaction trans2(kTypicalGET_Transaction); |
| 5094 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 5101 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
| 5095 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 5102 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
| 5096 EXPECT_TRUE(response.metadata.get() == NULL); | 5103 EXPECT_TRUE(response.metadata.get() == NULL); |
| 5097 | 5104 |
| 5098 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 5105 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 5099 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 5106 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| 5100 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5107 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5101 } | 5108 } |
| 5109 |
| 5110 TEST(HttpCache, ExternalCacheHit_Success) { |
| 5111 MockHttpCache cache; |
| 5112 |
| 5113 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5114 |
| 5115 disk_cache::Entry* entry; |
| 5116 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| 5117 MockDiskEntry* mock_entry = static_cast<MockDiskEntry*>(entry); |
| 5118 EXPECT_EQ(0, mock_entry->rank()); |
| 5119 |
| 5120 cache.http_cache()->ReportExternalCacheHit(GURL(kSimpleGET_Transaction.url), |
| 5121 kSimpleGET_Transaction.method); |
| 5122 MessageLoop::current()->RunAllPending(); |
| 5123 |
| 5124 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5125 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 5126 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5127 EXPECT_EQ(1, mock_entry->rank()); |
| 5128 |
| 5129 entry->Close(); |
| 5130 } |
| 5131 |
| 5132 TEST(HttpCache, ExternalCacheHit_NotInCache) { |
| 5133 MockHttpCache cache; |
| 5134 |
| 5135 cache.http_cache()->ReportExternalCacheHit(GURL(kSimpleGET_Transaction.url), |
| 5136 kSimpleGET_Transaction.method); |
| 5137 MessageLoop::current()->RunAllPending(); |
| 5138 |
| 5139 disk_cache::Entry* entry; |
| 5140 EXPECT_FALSE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| 5141 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 5142 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5143 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 5144 } |
| OLD | NEW |