OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/string_util.h" | 9 #include "base/string_util.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 virtual Time GetLastModified() const { | 59 virtual Time GetLastModified() const { |
60 return Time::FromInternalValue(0); | 60 return Time::FromInternalValue(0); |
61 } | 61 } |
62 | 62 |
63 virtual int32 GetDataSize(int index) const { | 63 virtual int32 GetDataSize(int index) const { |
64 DCHECK(index >= 0 && index < 2); | 64 DCHECK(index >= 0 && index < 2); |
65 return static_cast<int32>(data_[index].size()); | 65 return static_cast<int32>(data_[index].size()); |
66 } | 66 } |
67 | 67 |
68 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, | 68 virtual int ReadData(int index, int offset, char* buf, int buf_len, |
69 net::CompletionCallback* callback) { | 69 net::CompletionCallback* callback) { |
70 DCHECK(index >= 0 && index < 2); | 70 DCHECK(index >= 0 && index < 2); |
71 | 71 |
72 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 72 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
73 return net::ERR_FAILED; | 73 return net::ERR_FAILED; |
74 if (static_cast<size_t>(offset) == data_[index].size()) | 74 if (static_cast<size_t>(offset) == data_[index].size()) |
75 return 0; | 75 return 0; |
76 | 76 |
77 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); | 77 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
78 memcpy(buf->data(), &data_[index][offset], num); | 78 memcpy(buf, &data_[index][offset], num); |
79 | 79 |
80 if (!callback || (test_mode_ & TEST_MODE_SYNC_CACHE_READ)) | 80 if (!callback || (test_mode_ & TEST_MODE_SYNC_CACHE_READ)) |
81 return num; | 81 return num; |
82 | 82 |
83 CallbackLater(callback, num); | 83 CallbackLater(callback, num); |
84 return net::ERR_IO_PENDING; | 84 return net::ERR_IO_PENDING; |
85 } | 85 } |
86 | 86 |
87 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 87 virtual int WriteData(int index, int offset, const char* buf, int buf_len, |
88 net::CompletionCallback* callback, bool truncate) { | 88 net::CompletionCallback* callback, bool truncate) { |
89 DCHECK(index >= 0 && index < 2); | 89 DCHECK(index >= 0 && index < 2); |
90 DCHECK(truncate); | 90 DCHECK(truncate); |
91 | 91 |
92 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 92 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
93 return net::ERR_FAILED; | 93 return net::ERR_FAILED; |
94 | 94 |
95 data_[index].resize(offset + buf_len); | 95 data_[index].resize(offset + buf_len); |
96 if (buf_len) | 96 if (buf_len) |
97 memcpy(&data_[index][offset], buf->data(), buf_len); | 97 memcpy(&data_[index][offset], buf, buf_len); |
98 return buf_len; | 98 return buf_len; |
99 } | 99 } |
100 | 100 |
101 private: | 101 private: |
102 // Unlike the callbacks for MockHttpTransaction, we want this one to run even | 102 // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
103 // if the consumer called Close on the MockDiskEntry. We achieve that by | 103 // if the consumer called Close on the MockDiskEntry. We achieve that by |
104 // leveraging the fact that this class is reference counted. | 104 // leveraging the fact that this class is reference counted. |
105 void CallbackLater(net::CompletionCallback* callback, int result) { | 105 void CallbackLater(net::CompletionCallback* callback, int result) { |
106 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, | 106 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, |
107 &MockDiskEntry::RunCallback, callback, result)); | 107 &MockDiskEntry::RunCallback, callback, result)); |
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1081 | 1081 |
1082 scoped_ptr<net::HttpTransaction> trans( | 1082 scoped_ptr<net::HttpTransaction> trans( |
1083 cache.http_cache()->CreateTransaction()); | 1083 cache.http_cache()->CreateTransaction()); |
1084 ASSERT_TRUE(trans.get()); | 1084 ASSERT_TRUE(trans.get()); |
1085 | 1085 |
1086 int rv = trans->Start(&request, &callback); | 1086 int rv = trans->Start(&request, &callback); |
1087 if (rv == net::ERR_IO_PENDING) | 1087 if (rv == net::ERR_IO_PENDING) |
1088 rv = callback.WaitForResult(); | 1088 rv = callback.WaitForResult(); |
1089 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | 1089 ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
1090 } | 1090 } |
OLD | NEW |