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 "net/base/mock_file_stream.h" | 5 #include "net/base/mock_file_stream.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 |
7 namespace net { | 10 namespace net { |
8 | 11 |
9 namespace testing { | 12 namespace testing { |
10 | 13 |
| 14 MockFileStream::MockFileStream(net::NetLog* net_log) |
| 15 : net::FileStream(net_log), |
| 16 forced_error_(net::OK), |
| 17 async_error_(false), |
| 18 throttled_(false), |
| 19 weak_factory_(this) { |
| 20 } |
| 21 |
| 22 MockFileStream::MockFileStream(base::PlatformFile file, |
| 23 int flags, |
| 24 net::NetLog* net_log) |
| 25 : net::FileStream(file, flags, net_log), |
| 26 forced_error_(net::OK), |
| 27 async_error_(false), |
| 28 throttled_(false), |
| 29 weak_factory_(this) { |
| 30 } |
| 31 |
| 32 MockFileStream::MockFileStream( |
| 33 base::PlatformFile file, |
| 34 int flags, |
| 35 net::NetLog* net_log, |
| 36 const scoped_refptr<base::TaskRunner>& task_runner) |
| 37 : net::FileStream(file, flags, net_log, task_runner), |
| 38 forced_error_(net::OK), |
| 39 async_error_(false), |
| 40 throttled_(false), |
| 41 weak_factory_(this) { |
| 42 } |
| 43 |
| 44 MockFileStream::~MockFileStream() { |
| 45 } |
| 46 |
11 int MockFileStream::OpenSync(const base::FilePath& path, int open_flags) { | 47 int MockFileStream::OpenSync(const base::FilePath& path, int open_flags) { |
12 path_ = path; | 48 path_ = path; |
13 return ReturnError(FileStream::OpenSync(path, open_flags)); | 49 return ReturnError(FileStream::OpenSync(path, open_flags)); |
14 } | 50 } |
15 | 51 |
16 int MockFileStream::Seek(Whence whence, int64 offset, | 52 int MockFileStream::Seek(Whence whence, int64 offset, |
17 const Int64CompletionCallback& callback) { | 53 const Int64CompletionCallback& callback) { |
18 return ReturnError(FileStream::Seek(whence, offset, callback)); | 54 Int64CompletionCallback wrapped_callback = |
| 55 base::Bind(&MockFileStream::DoCallback64, |
| 56 weak_factory_.GetWeakPtr(), callback); |
| 57 if (forced_error_ == net::OK) |
| 58 return FileStream::Seek(whence, offset, wrapped_callback); |
| 59 return ErrorCallback64(wrapped_callback); |
19 } | 60 } |
20 | 61 |
21 int64 MockFileStream::SeekSync(Whence whence, int64 offset) { | 62 int64 MockFileStream::SeekSync(Whence whence, int64 offset) { |
22 return ReturnError64(FileStream::SeekSync(whence, offset)); | 63 return ReturnError64(FileStream::SeekSync(whence, offset)); |
23 } | 64 } |
24 | 65 |
25 int64 MockFileStream::Available() { | 66 int64 MockFileStream::Available() { |
26 return ReturnError64(FileStream::Available()); | 67 return ReturnError64(FileStream::Available()); |
27 } | 68 } |
28 | 69 |
29 int MockFileStream::Read(IOBuffer* buf, | 70 int MockFileStream::Read(IOBuffer* buf, |
30 int buf_len, | 71 int buf_len, |
31 const CompletionCallback& callback) { | 72 const CompletionCallback& callback) { |
32 return ReturnError(FileStream::Read(buf, buf_len, callback)); | 73 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 74 weak_factory_.GetWeakPtr(), |
| 75 callback); |
| 76 if (forced_error_ == net::OK) |
| 77 return FileStream::Read(buf, buf_len, wrapped_callback); |
| 78 return ErrorCallback(wrapped_callback); |
33 } | 79 } |
34 | 80 |
35 int MockFileStream::ReadSync(char* buf, int buf_len) { | 81 int MockFileStream::ReadSync(char* buf, int buf_len) { |
36 return ReturnError(FileStream::ReadSync(buf, buf_len)); | 82 return ReturnError(FileStream::ReadSync(buf, buf_len)); |
37 } | 83 } |
38 | 84 |
39 int MockFileStream::ReadUntilComplete(char *buf, int buf_len) { | 85 int MockFileStream::ReadUntilComplete(char *buf, int buf_len) { |
40 return ReturnError(FileStream::ReadUntilComplete(buf, buf_len)); | 86 return ReturnError(FileStream::ReadUntilComplete(buf, buf_len)); |
41 } | 87 } |
42 | 88 |
43 int MockFileStream::Write(IOBuffer* buf, | 89 int MockFileStream::Write(IOBuffer* buf, |
44 int buf_len, | 90 int buf_len, |
45 const CompletionCallback& callback) { | 91 const CompletionCallback& callback) { |
46 return ReturnError(FileStream::Write(buf, buf_len, callback)); | 92 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 93 weak_factory_.GetWeakPtr(), |
| 94 callback); |
| 95 if (forced_error_ == net::OK) |
| 96 return FileStream::Write(buf, buf_len, wrapped_callback); |
| 97 return ErrorCallback(wrapped_callback); |
47 } | 98 } |
48 | 99 |
49 int MockFileStream::WriteSync(const char* buf, int buf_len) { | 100 int MockFileStream::WriteSync(const char* buf, int buf_len) { |
50 return ReturnError(FileStream::WriteSync(buf, buf_len)); | 101 return ReturnError(FileStream::WriteSync(buf, buf_len)); |
51 } | 102 } |
52 | 103 |
53 int64 MockFileStream::Truncate(int64 bytes) { | 104 int64 MockFileStream::Truncate(int64 bytes) { |
54 return ReturnError64(FileStream::Truncate(bytes)); | 105 return ReturnError64(FileStream::Truncate(bytes)); |
55 } | 106 } |
56 | 107 |
57 int MockFileStream::Flush(const CompletionCallback& callback) { | 108 int MockFileStream::Flush(const CompletionCallback& callback) { |
58 return ReturnError(FileStream::Flush(callback)); | 109 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 110 weak_factory_.GetWeakPtr(), |
| 111 callback); |
| 112 if (forced_error_ == net::OK) |
| 113 return FileStream::Flush(wrapped_callback); |
| 114 return ErrorCallback(wrapped_callback); |
59 } | 115 } |
60 | 116 |
61 int MockFileStream::FlushSync() { | 117 int MockFileStream::FlushSync() { |
62 return ReturnError(FileStream::FlushSync()); | 118 return ReturnError(FileStream::FlushSync()); |
63 } | 119 } |
64 | 120 |
| 121 void MockFileStream::ThrottleCallbacks() { |
| 122 CHECK(!throttled_); |
| 123 throttled_ = true; |
| 124 } |
| 125 |
| 126 void MockFileStream::ReleaseCallbacks() { |
| 127 CHECK(throttled_); |
| 128 throttled_ = false; |
| 129 |
| 130 if (!throttled_task_.is_null()) { |
| 131 base::Closure throttled_task = throttled_task_; |
| 132 throttled_task_.Reset(); |
| 133 base::MessageLoop::current()->PostTask(FROM_HERE, throttled_task); |
| 134 } |
| 135 } |
| 136 |
| 137 void MockFileStream::DoCallback(const CompletionCallback& callback, |
| 138 int result) { |
| 139 if (!throttled_) { |
| 140 callback.Run(result); |
| 141 return; |
| 142 } |
| 143 CHECK(throttled_task_.is_null()); |
| 144 throttled_task_ = base::Bind(callback, result); |
| 145 } |
| 146 |
| 147 void MockFileStream::DoCallback64(const Int64CompletionCallback& callback, |
| 148 int64 result) { |
| 149 if (!throttled_) { |
| 150 callback.Run(result); |
| 151 return; |
| 152 } |
| 153 CHECK(throttled_task_.is_null()); |
| 154 throttled_task_ = base::Bind(callback, result); |
| 155 } |
| 156 |
| 157 int MockFileStream::ErrorCallback(const CompletionCallback& callback) { |
| 158 CHECK_NE(net::OK, forced_error_); |
| 159 if (async_error_) { |
| 160 base::MessageLoop::current()->PostTask( |
| 161 FROM_HERE, base::Bind(callback, forced_error_)); |
| 162 clear_forced_error(); |
| 163 return net::ERR_IO_PENDING; |
| 164 } |
| 165 int ret = forced_error_; |
| 166 clear_forced_error(); |
| 167 return ret; |
| 168 } |
| 169 |
| 170 int64 MockFileStream::ErrorCallback64(const Int64CompletionCallback& callback) { |
| 171 CHECK_NE(net::OK, forced_error_); |
| 172 if (async_error_) { |
| 173 base::MessageLoop::current()->PostTask( |
| 174 FROM_HERE, base::Bind(callback, forced_error_)); |
| 175 clear_forced_error(); |
| 176 return net::ERR_IO_PENDING; |
| 177 } |
| 178 int64 ret = forced_error_; |
| 179 clear_forced_error(); |
| 180 return ret; |
| 181 } |
| 182 |
65 } // namespace testing | 183 } // namespace testing |
66 | 184 |
67 } // namespace net | 185 } // namespace net |
OLD | NEW |