OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_vector.h" | 9 #include "base/scoped_vector.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 int GetEffectiveTestMode(int test_mode) { | 55 int GetEffectiveTestMode(int test_mode) { |
56 if (!g_test_mode) | 56 if (!g_test_mode) |
57 return test_mode; | 57 return test_mode; |
58 | 58 |
59 return g_test_mode; | 59 return g_test_mode; |
60 } | 60 } |
61 | 61 |
62 //----------------------------------------------------------------------------- | 62 //----------------------------------------------------------------------------- |
63 // mock disk cache (a very basic memory cache implementation) | 63 // mock disk cache (a very basic memory cache implementation) |
64 | 64 |
| 65 static const int kNumCacheEntryDataIndices = 3; |
| 66 |
65 class MockDiskEntry : public disk_cache::Entry, | 67 class MockDiskEntry : public disk_cache::Entry, |
66 public base::RefCounted<MockDiskEntry> { | 68 public base::RefCounted<MockDiskEntry> { |
67 public: | 69 public: |
68 MockDiskEntry() | 70 MockDiskEntry() |
69 : test_mode_(0), doomed_(false), sparse_(false), fail_requests_(false), | 71 : test_mode_(0), doomed_(false), sparse_(false), fail_requests_(false), |
70 busy_(false), delayed_(false) { | 72 busy_(false), delayed_(false) { |
71 } | 73 } |
72 | 74 |
73 explicit MockDiskEntry(const std::string& key) | 75 explicit MockDiskEntry(const std::string& key) |
74 : key_(key), doomed_(false), sparse_(false), fail_requests_(false), | 76 : key_(key), doomed_(false), sparse_(false), fail_requests_(false), |
(...skipping 19 matching lines...) Expand all Loading... |
94 | 96 |
95 virtual Time GetLastUsed() const { | 97 virtual Time GetLastUsed() const { |
96 return Time::FromInternalValue(0); | 98 return Time::FromInternalValue(0); |
97 } | 99 } |
98 | 100 |
99 virtual Time GetLastModified() const { | 101 virtual Time GetLastModified() const { |
100 return Time::FromInternalValue(0); | 102 return Time::FromInternalValue(0); |
101 } | 103 } |
102 | 104 |
103 virtual int32 GetDataSize(int index) const { | 105 virtual int32 GetDataSize(int index) const { |
104 DCHECK(index >= 0 && index < 3); | 106 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
105 return static_cast<int32>(data_[index].size()); | 107 return static_cast<int32>(data_[index].size()); |
106 } | 108 } |
107 | 109 |
108 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, | 110 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, |
109 net::CompletionCallback* callback) { | 111 net::CompletionCallback* callback) { |
110 DCHECK(index >= 0 && index < 3); | 112 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
111 | 113 |
112 if (fail_requests_) | 114 if (fail_requests_) |
113 return net::ERR_CACHE_READ_FAILURE; | 115 return net::ERR_CACHE_READ_FAILURE; |
114 | 116 |
115 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 117 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
116 return net::ERR_FAILED; | 118 return net::ERR_FAILED; |
117 if (static_cast<size_t>(offset) == data_[index].size()) | 119 if (static_cast<size_t>(offset) == data_[index].size()) |
118 return 0; | 120 return 0; |
119 | 121 |
120 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); | 122 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
121 memcpy(buf->data(), &data_[index][offset], num); | 123 memcpy(buf->data(), &data_[index][offset], num); |
122 | 124 |
123 if (!callback || | 125 if (!callback || |
124 (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)) | 126 (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)) |
125 return num; | 127 return num; |
126 | 128 |
127 CallbackLater(callback, num); | 129 CallbackLater(callback, num); |
128 return net::ERR_IO_PENDING; | 130 return net::ERR_IO_PENDING; |
129 } | 131 } |
130 | 132 |
131 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 133 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, |
132 net::CompletionCallback* callback, bool truncate) { | 134 net::CompletionCallback* callback, bool truncate) { |
133 DCHECK(index >= 0 && index < 3); | 135 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
134 DCHECK(truncate); | 136 DCHECK(truncate); |
135 | 137 |
136 if (fail_requests_) | 138 if (fail_requests_) |
137 return net::ERR_CACHE_READ_FAILURE; | 139 return net::ERR_CACHE_READ_FAILURE; |
138 | 140 |
139 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 141 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
140 return net::ERR_FAILED; | 142 return net::ERR_FAILED; |
141 | 143 |
142 data_[index].resize(offset + buf_len); | 144 data_[index].resize(offset + buf_len); |
143 if (buf_len) | 145 if (buf_len) |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 } else { | 333 } else { |
332 for (size_t i = 0; i < callback_list.size(); i++) { | 334 for (size_t i = 0; i < callback_list.size(); i++) { |
333 CallbackInfo& c = callback_list[i]; | 335 CallbackInfo& c = callback_list[i]; |
334 c.entry->CallbackLater(c.callback, c.result); | 336 c.entry->CallbackLater(c.callback, c.result); |
335 } | 337 } |
336 callback_list.clear(); | 338 callback_list.clear(); |
337 } | 339 } |
338 } | 340 } |
339 | 341 |
340 std::string key_; | 342 std::string key_; |
341 std::vector<char> data_[3]; | 343 std::vector<char> data_[kNumCacheEntryDataIndices]; |
342 int test_mode_; | 344 int test_mode_; |
343 bool doomed_; | 345 bool doomed_; |
344 bool sparse_; | 346 bool sparse_; |
345 bool fail_requests_; | 347 bool fail_requests_; |
346 bool busy_; | 348 bool busy_; |
347 bool delayed_; | 349 bool delayed_; |
348 static bool cancel_; | 350 static bool cancel_; |
349 static bool ignore_callbacks_; | 351 static bool ignore_callbacks_; |
350 }; | 352 }; |
351 | 353 |
(...skipping 3992 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4344 // Now return 200 when validating the entry so the metadata will be lost. | 4346 // Now return 200 when validating the entry so the metadata will be lost. |
4345 MockTransaction trans2(kTypicalGET_Transaction); | 4347 MockTransaction trans2(kTypicalGET_Transaction); |
4346 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 4348 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
4347 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 4349 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
4348 EXPECT_TRUE(response.metadata.get() == NULL); | 4350 EXPECT_TRUE(response.metadata.get() == NULL); |
4349 | 4351 |
4350 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4352 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
4351 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 4353 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
4352 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4354 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4353 } | 4355 } |
OLD | NEW |