| 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/mock_http_cache.h" | 5 #include "net/http/mock_http_cache.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 class OldCallbackRunner : public Task { | |
| 15 public: | |
| 16 OldCallbackRunner(net::OldCompletionCallback* callback, int result) | |
| 17 : callback_(callback), result_(result) {} | |
| 18 virtual void Run() { | |
| 19 callback_->Run(result_); | |
| 20 } | |
| 21 | |
| 22 private: | |
| 23 net::OldCompletionCallback* callback_; | |
| 24 int result_; | |
| 25 | |
| 26 DISALLOW_COPY_AND_ASSIGN(OldCallbackRunner); | |
| 27 }; | |
| 28 | |
| 29 void CompletionCallbackRunner( | |
| 30 const net::CompletionCallback& callback, int result) { | |
| 31 callback.Run(result); | |
| 32 } | |
| 33 | |
| 34 int GetTestModeForEntry(const std::string& key) { | 14 int GetTestModeForEntry(const std::string& key) { |
| 35 // 'key' is prefixed with an identifier if it corresponds to a cached POST. | 15 // 'key' is prefixed with an identifier if it corresponds to a cached POST. |
| 36 // Skip past that to locate the actual URL. | 16 // Skip past that to locate the actual URL. |
| 37 // | 17 // |
| 38 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an | 18 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an |
| 39 // URL corresponding to a registered MockTransaction. It would be good to | 19 // URL corresponding to a registered MockTransaction. It would be good to |
| 40 // have another way to access the test_mode. | 20 // have another way to access the test_mode. |
| 41 GURL url; | 21 GURL url; |
| 42 if (isdigit(key[0])) { | 22 if (isdigit(key[0])) { |
| 43 size_t slash = key.find('/'); | 23 size_t slash = key.find('/'); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 54 // We can override the test mode for a given operation by setting this global | 34 // We can override the test mode for a given operation by setting this global |
| 55 // variable. | 35 // variable. |
| 56 int g_test_mode = 0; | 36 int g_test_mode = 0; |
| 57 | 37 |
| 58 } // namespace | 38 } // namespace |
| 59 | 39 |
| 60 //----------------------------------------------------------------------------- | 40 //----------------------------------------------------------------------------- |
| 61 | 41 |
| 62 struct MockDiskEntry::CallbackInfo { | 42 struct MockDiskEntry::CallbackInfo { |
| 63 scoped_refptr<MockDiskEntry> entry; | 43 scoped_refptr<MockDiskEntry> entry; |
| 64 net::OldCompletionCallback* old_callback; | 44 net::OldCompletionCallback* callback; |
| 65 net::CompletionCallback callback; | |
| 66 int result; | 45 int result; |
| 67 }; | 46 }; |
| 68 | 47 |
| 69 MockDiskEntry::MockDiskEntry() | 48 MockDiskEntry::MockDiskEntry() |
| 70 : test_mode_(0), doomed_(false), sparse_(false), | 49 : test_mode_(0), doomed_(false), sparse_(false), |
| 71 fail_requests_(false), busy_(false), delayed_(false) { | 50 fail_requests_(false), busy_(false), delayed_(false) { |
| 72 } | 51 } |
| 73 | 52 |
| 74 MockDiskEntry::MockDiskEntry(const std::string& key) | 53 MockDiskEntry::MockDiskEntry(const std::string& key) |
| 75 : key_(key), doomed_(false), sparse_(false), | 54 : key_(key), doomed_(false), sparse_(false), |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); | 99 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
| 121 memcpy(buf->data(), &data_[index][offset], num); | 100 memcpy(buf->data(), &data_[index][offset], num); |
| 122 | 101 |
| 123 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) | 102 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
| 124 return num; | 103 return num; |
| 125 | 104 |
| 126 CallbackLater(callback, num); | 105 CallbackLater(callback, num); |
| 127 return net::ERR_IO_PENDING; | 106 return net::ERR_IO_PENDING; |
| 128 } | 107 } |
| 129 | 108 |
| 130 int MockDiskEntry::ReadData( | |
| 131 int index, int offset, net::IOBuffer* buf, int buf_len, | |
| 132 const net::CompletionCallback& callback) { | |
| 133 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); | |
| 134 DCHECK(!callback.is_null()); | |
| 135 | |
| 136 if (fail_requests_) | |
| 137 return net::ERR_CACHE_READ_FAILURE; | |
| 138 | |
| 139 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | |
| 140 return net::ERR_FAILED; | |
| 141 if (static_cast<size_t>(offset) == data_[index].size()) | |
| 142 return 0; | |
| 143 | |
| 144 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); | |
| 145 memcpy(buf->data(), &data_[index][offset], num); | |
| 146 | |
| 147 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) | |
| 148 return num; | |
| 149 | |
| 150 CallbackLater(callback, num); | |
| 151 return net::ERR_IO_PENDING; | |
| 152 } | |
| 153 | |
| 154 int MockDiskEntry::WriteData(int index, int offset, net::IOBuffer* buf, | 109 int MockDiskEntry::WriteData(int index, int offset, net::IOBuffer* buf, |
| 155 int buf_len, net::OldCompletionCallback* callback, | 110 int buf_len, net::OldCompletionCallback* callback, |
| 156 bool truncate) { | 111 bool truncate) { |
| 157 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); | 112 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
| 158 DCHECK(callback); | 113 DCHECK(callback); |
| 159 DCHECK(truncate); | 114 DCHECK(truncate); |
| 160 | 115 |
| 161 if (fail_requests_) { | 116 if (fail_requests_) { |
| 162 CallbackLater(callback, net::ERR_CACHE_READ_FAILURE); | 117 CallbackLater(callback, net::ERR_CACHE_READ_FAILURE); |
| 163 return net::ERR_IO_PENDING; | 118 return net::ERR_IO_PENDING; |
| 164 } | 119 } |
| 165 | 120 |
| 166 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 121 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
| 167 return net::ERR_FAILED; | 122 return net::ERR_FAILED; |
| 168 | 123 |
| 169 data_[index].resize(offset + buf_len); | 124 data_[index].resize(offset + buf_len); |
| 170 if (buf_len) | 125 if (buf_len) |
| 171 memcpy(&data_[index][offset], buf->data(), buf_len); | 126 memcpy(&data_[index][offset], buf->data(), buf_len); |
| 172 | 127 |
| 173 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 128 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
| 174 return buf_len; | 129 return buf_len; |
| 175 | 130 |
| 176 CallbackLater(callback, buf_len); | 131 CallbackLater(callback, buf_len); |
| 177 return net::ERR_IO_PENDING; | 132 return net::ERR_IO_PENDING; |
| 178 } | 133 } |
| 179 | 134 |
| 180 int MockDiskEntry::WriteData( | |
| 181 int index, int offset, net::IOBuffer* buf, int buf_len, | |
| 182 const net::CompletionCallback& callback, bool truncate) { | |
| 183 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); | |
| 184 DCHECK(truncate); | |
| 185 DCHECK(!callback.is_null()); | |
| 186 | |
| 187 if (fail_requests_) { | |
| 188 CallbackLater(callback, net::ERR_CACHE_READ_FAILURE); | |
| 189 return net::ERR_IO_PENDING; | |
| 190 } | |
| 191 | |
| 192 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | |
| 193 return net::ERR_FAILED; | |
| 194 | |
| 195 data_[index].resize(offset + buf_len); | |
| 196 if (buf_len) | |
| 197 memcpy(&data_[index][offset], buf->data(), buf_len); | |
| 198 | |
| 199 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | |
| 200 return buf_len; | |
| 201 | |
| 202 CallbackLater(callback, buf_len); | |
| 203 return net::ERR_IO_PENDING; | |
| 204 } | |
| 205 | |
| 206 int MockDiskEntry::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 135 int MockDiskEntry::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
| 207 net::OldCompletionCallback* callback) { | 136 net::OldCompletionCallback* callback) { |
| 208 DCHECK(callback); | 137 DCHECK(callback); |
| 209 if (!sparse_ || busy_) | 138 if (!sparse_ || busy_) |
| 210 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 139 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 211 if (offset < 0) | 140 if (offset < 0) |
| 212 return net::ERR_FAILED; | 141 return net::ERR_FAILED; |
| 213 | 142 |
| 214 if (fail_requests_) | 143 if (fail_requests_) |
| 215 return net::ERR_CACHE_READ_FAILURE; | 144 return net::ERR_CACHE_READ_FAILURE; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 | 257 |
| 329 // If |value| is true, don't deliver any completion callbacks until called | 258 // If |value| is true, don't deliver any completion callbacks until called |
| 330 // again with |value| set to false. Caution: remember to enable callbacks | 259 // again with |value| set to false. Caution: remember to enable callbacks |
| 331 // again or all subsequent tests will fail. | 260 // again or all subsequent tests will fail. |
| 332 // Static. | 261 // Static. |
| 333 void MockDiskEntry::IgnoreCallbacks(bool value) { | 262 void MockDiskEntry::IgnoreCallbacks(bool value) { |
| 334 if (ignore_callbacks_ == value) | 263 if (ignore_callbacks_ == value) |
| 335 return; | 264 return; |
| 336 ignore_callbacks_ = value; | 265 ignore_callbacks_ = value; |
| 337 if (!value) | 266 if (!value) |
| 338 StoreAndDeliverCallbacks(false, NULL, NULL, net::CompletionCallback(), 0); | 267 StoreAndDeliverCallbacks(false, NULL, NULL, 0); |
| 339 } | 268 } |
| 340 | 269 |
| 341 MockDiskEntry::~MockDiskEntry() { | 270 MockDiskEntry::~MockDiskEntry() { |
| 342 } | 271 } |
| 343 | 272 |
| 344 // Unlike the callbacks for MockHttpTransaction, we want this one to run even | 273 // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
| 345 // if the consumer called Close on the MockDiskEntry. We achieve that by | 274 // if the consumer called Close on the MockDiskEntry. We achieve that by |
| 346 // leveraging the fact that this class is reference counted. | 275 // leveraging the fact that this class is reference counted. |
| 347 void MockDiskEntry::CallbackLater(net::OldCompletionCallback* callback, | 276 void MockDiskEntry::CallbackLater(net::OldCompletionCallback* callback, |
| 348 int result) { | 277 int result) { |
| 349 if (ignore_callbacks_) { | 278 if (ignore_callbacks_) |
| 350 StoreAndDeliverCallbacks(true, this, callback, | 279 return StoreAndDeliverCallbacks(true, this, callback, result); |
| 351 net::CompletionCallback(), result); | |
| 352 return; | |
| 353 } | |
| 354 | |
| 355 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
| 356 &MockDiskEntry::RunOldCallback, this, callback, result)); | |
| 357 } | |
| 358 | |
| 359 void MockDiskEntry::CallbackLater(const net::CompletionCallback& callback, | |
| 360 int result) { | |
| 361 if (ignore_callbacks_) { | |
| 362 StoreAndDeliverCallbacks(true, this, NULL, callback, result); | |
| 363 return; | |
| 364 } | |
| 365 | |
| 366 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 280 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 367 &MockDiskEntry::RunCallback, this, callback, result)); | 281 &MockDiskEntry::RunCallback, this, callback, result)); |
| 368 } | 282 } |
| 369 | 283 |
| 370 void MockDiskEntry::RunOldCallback( | 284 void MockDiskEntry::RunCallback(net::OldCompletionCallback* callback, int result
) { |
| 371 net::OldCompletionCallback* callback, int result) { | |
| 372 if (busy_) { | 285 if (busy_) { |
| 373 // This is kind of hacky, but controlling the behavior of just this entry | 286 // This is kind of hacky, but controlling the behavior of just this entry |
| 374 // from a test is sort of complicated. What we really want to do is | 287 // from a test is sort of complicated. What we really want to do is |
| 375 // delay the delivery of a sparse IO operation a little more so that the | 288 // delay the delivery of a sparse IO operation a little more so that the |
| 376 // request start operation (async) will finish without seeing the end of | 289 // request start operation (async) will finish without seeing the end of |
| 377 // this operation (already posted to the message loop)... and without | 290 // this operation (already posted to the message loop)... and without |
| 378 // just delaying for n mS (which may cause trouble with slow bots). So | 291 // just delaying for n mS (which may cause trouble with slow bots). So |
| 379 // we re-post this operation (all async sparse IO operations will take two | 292 // we re-post this operation (all async sparse IO operations will take two |
| 380 // trips through the message loop instead of one). | 293 // trips trhough the message loop instead of one). |
| 381 if (!delayed_) { | 294 if (!delayed_) { |
| 382 delayed_ = true; | 295 delayed_ = true; |
| 383 return CallbackLater(callback, result); | 296 return CallbackLater(callback, result); |
| 384 } | 297 } |
| 385 } | 298 } |
| 386 busy_ = false; | 299 busy_ = false; |
| 387 callback->Run(result); | 300 callback->Run(result); |
| 388 } | 301 } |
| 389 | 302 |
| 390 void MockDiskEntry::RunCallback( | |
| 391 const net::CompletionCallback& callback, int result) { | |
| 392 if (busy_) { | |
| 393 // This is kind of hacky, but controlling the behavior of just this entry | |
| 394 // from a test is sort of complicated. What we really want to do is | |
| 395 // delay the delivery of a sparse IO operation a little more so that the | |
| 396 // request start operation (async) will finish without seeing the end of | |
| 397 // this operation (already posted to the message loop)... and without | |
| 398 // just delaying for n mS (which may cause trouble with slow bots). So | |
| 399 // we re-post this operation (all async sparse IO operations will take two | |
| 400 // trips through the message loop instead of one). | |
| 401 if (!delayed_) { | |
| 402 delayed_ = true; | |
| 403 return CallbackLater(callback, result); | |
| 404 } | |
| 405 } | |
| 406 busy_ = false; | |
| 407 callback.Run(result); | |
| 408 } | |
| 409 | |
| 410 // When |store| is true, stores the callback to be delivered later; otherwise | 303 // When |store| is true, stores the callback to be delivered later; otherwise |
| 411 // delivers any callback previously stored. | 304 // delivers any callback previously stored. |
| 412 // Static. | 305 // Static. |
| 413 void MockDiskEntry::StoreAndDeliverCallbacks( | 306 void MockDiskEntry::StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry, |
| 414 bool store, MockDiskEntry* entry, | 307 net::OldCompletionCallback* callbac
k, |
| 415 net::OldCompletionCallback* old_callback, | 308 int result) { |
| 416 const net::CompletionCallback& callback, int result) { | |
| 417 static std::vector<CallbackInfo> callback_list; | 309 static std::vector<CallbackInfo> callback_list; |
| 418 if (store) { | 310 if (store) { |
| 419 CallbackInfo c = {entry, old_callback, callback, result}; | 311 CallbackInfo c = {entry, callback, result}; |
| 420 callback_list.push_back(c); | 312 callback_list.push_back(c); |
| 421 } else { | 313 } else { |
| 422 for (size_t i = 0; i < callback_list.size(); ++i) { | 314 for (size_t i = 0; i < callback_list.size(); i++) { |
| 423 CallbackInfo& c = callback_list[i]; | 315 CallbackInfo& c = callback_list[i]; |
| 424 if (c.old_callback) | 316 c.entry->CallbackLater(c.callback, c.result); |
| 425 c.entry->CallbackLater(c.old_callback, c.result); | |
| 426 else | |
| 427 c.entry->CallbackLater(c.callback, c.result); | |
| 428 } | 317 } |
| 429 callback_list.clear(); | 318 callback_list.clear(); |
| 430 } | 319 } |
| 431 } | 320 } |
| 432 | 321 |
| 433 // Statics. | 322 // Statics. |
| 434 bool MockDiskEntry::cancel_ = false; | 323 bool MockDiskEntry::cancel_ = false; |
| 435 bool MockDiskEntry::ignore_callbacks_ = false; | 324 bool MockDiskEntry::ignore_callbacks_ = false; |
| 436 | 325 |
| 437 //----------------------------------------------------------------------------- | 326 //----------------------------------------------------------------------------- |
| 438 | 327 |
| 328 class MockDiskCache::CallbackRunner : public Task { |
| 329 public: |
| 330 CallbackRunner(net::OldCompletionCallback* callback, int result) |
| 331 : callback_(callback), result_(result) {} |
| 332 virtual void Run() { |
| 333 callback_->Run(result_); |
| 334 } |
| 335 |
| 336 private: |
| 337 net::OldCompletionCallback* callback_; |
| 338 int result_; |
| 339 DISALLOW_COPY_AND_ASSIGN(CallbackRunner); |
| 340 }; |
| 341 |
| 439 MockDiskCache::MockDiskCache() | 342 MockDiskCache::MockDiskCache() |
| 440 : open_count_(0), create_count_(0), fail_requests_(false), | 343 : open_count_(0), create_count_(0), fail_requests_(false), |
| 441 soft_failures_(false), double_create_check_(true) { | 344 soft_failures_(false), double_create_check_(true) { |
| 442 } | 345 } |
| 443 | 346 |
| 444 MockDiskCache::~MockDiskCache() { | 347 MockDiskCache::~MockDiskCache() { |
| 445 ReleaseAll(); | 348 ReleaseAll(); |
| 446 } | 349 } |
| 447 | 350 |
| 448 int32 MockDiskCache::GetEntryCount() const { | 351 int32 MockDiskCache::GetEntryCount() const { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 473 if (soft_failures_) | 376 if (soft_failures_) |
| 474 it->second->set_fail_requests(); | 377 it->second->set_fail_requests(); |
| 475 | 378 |
| 476 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) | 379 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
| 477 return net::OK; | 380 return net::OK; |
| 478 | 381 |
| 479 CallbackLater(callback, net::OK); | 382 CallbackLater(callback, net::OK); |
| 480 return net::ERR_IO_PENDING; | 383 return net::ERR_IO_PENDING; |
| 481 } | 384 } |
| 482 | 385 |
| 483 int MockDiskCache::OpenEntry(const std::string& key, disk_cache::Entry** entry, | |
| 484 const net::CompletionCallback& callback) { | |
| 485 DCHECK(!callback.is_null()); | |
| 486 | |
| 487 if (fail_requests_) | |
| 488 return net::ERR_CACHE_OPEN_FAILURE; | |
| 489 | |
| 490 EntryMap::iterator it = entries_.find(key); | |
| 491 if (it == entries_.end()) | |
| 492 return net::ERR_CACHE_OPEN_FAILURE; | |
| 493 | |
| 494 if (it->second->is_doomed()) { | |
| 495 it->second->Release(); | |
| 496 entries_.erase(it); | |
| 497 return net::ERR_CACHE_OPEN_FAILURE; | |
| 498 } | |
| 499 | |
| 500 open_count_++; | |
| 501 | |
| 502 it->second->AddRef(); | |
| 503 *entry = it->second; | |
| 504 | |
| 505 if (soft_failures_) | |
| 506 it->second->set_fail_requests(); | |
| 507 | |
| 508 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) | |
| 509 return net::OK; | |
| 510 | |
| 511 CallbackLater(callback, net::OK); | |
| 512 return net::ERR_IO_PENDING; | |
| 513 } | |
| 514 | |
| 515 int MockDiskCache::CreateEntry(const std::string& key, | 386 int MockDiskCache::CreateEntry(const std::string& key, |
| 516 disk_cache::Entry** entry, | 387 disk_cache::Entry** entry, |
| 517 net::OldCompletionCallback* callback) { | 388 net::OldCompletionCallback* callback) { |
| 518 DCHECK(callback); | 389 DCHECK(callback); |
| 519 if (fail_requests_) | 390 if (fail_requests_) |
| 520 return net::ERR_CACHE_CREATE_FAILURE; | 391 return net::ERR_CACHE_CREATE_FAILURE; |
| 521 | 392 |
| 522 EntryMap::iterator it = entries_.find(key); | 393 EntryMap::iterator it = entries_.find(key); |
| 523 if (it != entries_.end()) { | 394 if (it != entries_.end()) { |
| 524 if (!it->second->is_doomed()) { | 395 if (!it->second->is_doomed()) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 544 if (soft_failures_) | 415 if (soft_failures_) |
| 545 new_entry->set_fail_requests(); | 416 new_entry->set_fail_requests(); |
| 546 | 417 |
| 547 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) | 418 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
| 548 return net::OK; | 419 return net::OK; |
| 549 | 420 |
| 550 CallbackLater(callback, net::OK); | 421 CallbackLater(callback, net::OK); |
| 551 return net::ERR_IO_PENDING; | 422 return net::ERR_IO_PENDING; |
| 552 } | 423 } |
| 553 | 424 |
| 554 int MockDiskCache::CreateEntry(const std::string& key, | |
| 555 disk_cache::Entry** entry, | |
| 556 const net::CompletionCallback& callback) { | |
| 557 DCHECK(!callback.is_null()); | |
| 558 | |
| 559 if (fail_requests_) | |
| 560 return net::ERR_CACHE_CREATE_FAILURE; | |
| 561 | |
| 562 EntryMap::iterator it = entries_.find(key); | |
| 563 if (it != entries_.end()) { | |
| 564 if (!it->second->is_doomed()) { | |
| 565 if (double_create_check_) | |
| 566 NOTREACHED(); | |
| 567 else | |
| 568 return net::ERR_CACHE_CREATE_FAILURE; | |
| 569 } | |
| 570 it->second->Release(); | |
| 571 entries_.erase(it); | |
| 572 } | |
| 573 | |
| 574 create_count_++; | |
| 575 | |
| 576 MockDiskEntry* new_entry = new MockDiskEntry(key); | |
| 577 | |
| 578 new_entry->AddRef(); | |
| 579 entries_[key] = new_entry; | |
| 580 | |
| 581 new_entry->AddRef(); | |
| 582 *entry = new_entry; | |
| 583 | |
| 584 if (soft_failures_) | |
| 585 new_entry->set_fail_requests(); | |
| 586 | |
| 587 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) | |
| 588 return net::OK; | |
| 589 | |
| 590 CallbackLater(callback, net::OK); | |
| 591 return net::ERR_IO_PENDING; | |
| 592 } | |
| 593 | |
| 594 int MockDiskCache::DoomEntry(const std::string& key, | 425 int MockDiskCache::DoomEntry(const std::string& key, |
| 595 net::OldCompletionCallback* callback) { | 426 net::OldCompletionCallback* callback) { |
| 596 DCHECK(callback); | 427 DCHECK(callback); |
| 597 EntryMap::iterator it = entries_.find(key); | 428 EntryMap::iterator it = entries_.find(key); |
| 598 if (it != entries_.end()) { | 429 if (it != entries_.end()) { |
| 599 it->second->Release(); | 430 it->second->Release(); |
| 600 entries_.erase(it); | 431 entries_.erase(it); |
| 601 } | 432 } |
| 602 | 433 |
| 603 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) | 434 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
| 604 return net::OK; | 435 return net::OK; |
| 605 | 436 |
| 606 CallbackLater(callback, net::OK); | 437 CallbackLater(callback, net::OK); |
| 607 return net::ERR_IO_PENDING; | 438 return net::ERR_IO_PENDING; |
| 608 } | 439 } |
| 609 | 440 |
| 610 int MockDiskCache::DoomAllEntries(net::OldCompletionCallback* callback) { | 441 int MockDiskCache::DoomAllEntries(net::OldCompletionCallback* callback) { |
| 611 return net::ERR_NOT_IMPLEMENTED; | 442 return net::ERR_NOT_IMPLEMENTED; |
| 612 } | 443 } |
| 613 | 444 |
| 614 int MockDiskCache::DoomAllEntries(const net::CompletionCallback& callback) { | |
| 615 return net::ERR_NOT_IMPLEMENTED; | |
| 616 } | |
| 617 | |
| 618 int MockDiskCache::DoomEntriesBetween(const base::Time initial_time, | 445 int MockDiskCache::DoomEntriesBetween(const base::Time initial_time, |
| 619 const base::Time end_time, | 446 const base::Time end_time, |
| 620 net::OldCompletionCallback* callback) { | 447 net::OldCompletionCallback* callback) { |
| 621 return net::ERR_NOT_IMPLEMENTED; | 448 return net::ERR_NOT_IMPLEMENTED; |
| 622 } | 449 } |
| 623 | 450 |
| 624 int MockDiskCache::DoomEntriesBetween(const base::Time initial_time, | |
| 625 const base::Time end_time, | |
| 626 const net::CompletionCallback& callback) { | |
| 627 return net::ERR_NOT_IMPLEMENTED; | |
| 628 } | |
| 629 | |
| 630 int MockDiskCache::DoomEntriesSince(const base::Time initial_time, | 451 int MockDiskCache::DoomEntriesSince(const base::Time initial_time, |
| 631 net::OldCompletionCallback* callback) { | 452 net::OldCompletionCallback* callback) { |
| 632 return net::ERR_NOT_IMPLEMENTED; | 453 return net::ERR_NOT_IMPLEMENTED; |
| 633 } | 454 } |
| 634 | 455 |
| 635 int MockDiskCache::OpenNextEntry(void** iter, disk_cache::Entry** next_entry, | 456 int MockDiskCache::OpenNextEntry(void** iter, disk_cache::Entry** next_entry, |
| 636 net::OldCompletionCallback* callback) { | 457 net::OldCompletionCallback* callback) { |
| 637 return net::ERR_NOT_IMPLEMENTED; | 458 return net::ERR_NOT_IMPLEMENTED; |
| 638 } | 459 } |
| 639 | 460 |
| 640 int MockDiskCache::OpenNextEntry(void** iter, disk_cache::Entry** next_entry, | |
| 641 const net::CompletionCallback& callback) { | |
| 642 return net::ERR_NOT_IMPLEMENTED; | |
| 643 } | |
| 644 | |
| 645 void MockDiskCache::EndEnumeration(void** iter) { | 461 void MockDiskCache::EndEnumeration(void** iter) { |
| 646 } | 462 } |
| 647 | 463 |
| 648 void MockDiskCache::GetStats( | 464 void MockDiskCache::GetStats( |
| 649 std::vector<std::pair<std::string, std::string> >* stats) { | 465 std::vector<std::pair<std::string, std::string> >* stats) { |
| 650 } | 466 } |
| 651 | 467 |
| 652 void MockDiskCache::OnExternalCacheHit(const std::string& key) { | 468 void MockDiskCache::OnExternalCacheHit(const std::string& key) { |
| 653 } | 469 } |
| 654 | 470 |
| 655 void MockDiskCache::ReleaseAll() { | 471 void MockDiskCache::ReleaseAll() { |
| 656 EntryMap::iterator it = entries_.begin(); | 472 EntryMap::iterator it = entries_.begin(); |
| 657 for (; it != entries_.end(); ++it) | 473 for (; it != entries_.end(); ++it) |
| 658 it->second->Release(); | 474 it->second->Release(); |
| 659 entries_.clear(); | 475 entries_.clear(); |
| 660 } | 476 } |
| 661 | 477 |
| 662 void MockDiskCache::CallbackLater(net::OldCompletionCallback* callback, | 478 void MockDiskCache::CallbackLater(net::OldCompletionCallback* callback, |
| 663 int result) { | 479 int result) { |
| 664 MessageLoop::current()->PostTask(FROM_HERE, | 480 MessageLoop::current()->PostTask(FROM_HERE, |
| 665 new OldCallbackRunner(callback, result)); | 481 new CallbackRunner(callback, result)); |
| 666 } | |
| 667 | |
| 668 void MockDiskCache::CallbackLater(const net::CompletionCallback& callback, | |
| 669 int result) { | |
| 670 MessageLoop::current()->PostTask( | |
| 671 FROM_HERE, base::Bind(&CompletionCallbackRunner, callback, result)); | |
| 672 } | 482 } |
| 673 | 483 |
| 674 //----------------------------------------------------------------------------- | 484 //----------------------------------------------------------------------------- |
| 675 | 485 |
| 676 int MockBackendFactory::CreateBackend(net::NetLog* net_log, | 486 int MockBackendFactory::CreateBackend(net::NetLog* net_log, |
| 677 disk_cache::Backend** backend, | 487 disk_cache::Backend** backend, |
| 678 net::OldCompletionCallback* callback) { | 488 net::OldCompletionCallback* callback) { |
| 679 *backend = new MockDiskCache(); | 489 *backend = new MockDiskCache(); |
| 680 return net::OK; | 490 return net::OK; |
| 681 } | 491 } |
| 682 | 492 |
| 683 //----------------------------------------------------------------------------- | 493 //----------------------------------------------------------------------------- |
| 684 | 494 |
| 685 MockHttpCache::MockHttpCache() | 495 MockHttpCache::MockHttpCache() |
| 686 : http_cache_(new MockNetworkLayer(), NULL, new MockBackendFactory()) { | 496 : http_cache_(new MockNetworkLayer(), NULL, new MockBackendFactory()) { |
| 687 } | 497 } |
| 688 | 498 |
| 689 MockHttpCache::MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory) | 499 MockHttpCache::MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory) |
| 690 : http_cache_(new MockNetworkLayer(), NULL, disk_cache_factory) { | 500 : http_cache_(new MockNetworkLayer(), NULL, disk_cache_factory) { |
| 691 } | 501 } |
| 692 | 502 |
| 693 MockDiskCache* MockHttpCache::disk_cache() { | 503 MockDiskCache* MockHttpCache::disk_cache() { |
| 694 net::TestCompletionCallback cb; | 504 TestOldCompletionCallback cb; |
| 695 disk_cache::Backend* backend; | 505 disk_cache::Backend* backend; |
| 696 int rv = http_cache_.GetBackend(&backend, cb.callback()); | 506 int rv = http_cache_.GetBackend(&backend, &cb); |
| 697 rv = cb.GetResult(rv); | 507 rv = cb.GetResult(rv); |
| 698 return (rv == net::OK) ? static_cast<MockDiskCache*>(backend) : NULL; | 508 return (rv == net::OK) ? static_cast<MockDiskCache*>(backend) : NULL; |
| 699 } | 509 } |
| 700 | 510 |
| 701 bool MockHttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry, | 511 bool MockHttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry, |
| 702 net::HttpResponseInfo* response_info, | 512 net::HttpResponseInfo* response_info, |
| 703 bool* response_truncated) { | 513 bool* response_truncated) { |
| 704 int size = disk_entry->GetDataSize(0); | 514 int size = disk_entry->GetDataSize(0); |
| 705 | 515 |
| 706 TestOldCompletionCallback cb; | 516 TestOldCompletionCallback cb; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 void MockBlockingBackendFactory::FinishCreation() { | 609 void MockBlockingBackendFactory::FinishCreation() { |
| 800 block_ = false; | 610 block_ = false; |
| 801 if (callback_) { | 611 if (callback_) { |
| 802 if (!fail_) | 612 if (!fail_) |
| 803 *backend_ = new MockDiskCache(); | 613 *backend_ = new MockDiskCache(); |
| 804 net::OldCompletionCallback* cb = callback_; | 614 net::OldCompletionCallback* cb = callback_; |
| 805 callback_ = NULL; | 615 callback_ = NULL; |
| 806 cb->Run(Result()); // This object can be deleted here. | 616 cb->Run(Result()); // This object can be deleted here. |
| 807 } | 617 } |
| 808 } | 618 } |
| OLD | NEW |