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
3 // found in the LICENSE file.
5 #include "net/base/mock_file_stream.h"
8 #include "base/message_loop/message_loop.h"
14 MockFileStream::MockFileStream(
15 const scoped_refptr
<base::TaskRunner
>& task_runner
)
16 : net::FileStream(task_runner
),
17 forced_error_(net::OK
),
23 MockFileStream::MockFileStream(
25 const scoped_refptr
<base::TaskRunner
>& task_runner
)
26 : net::FileStream(file
.Pass(), task_runner
),
27 forced_error_(net::OK
),
33 MockFileStream::~MockFileStream() {
36 int MockFileStream::Seek(base::File::Whence whence
, int64 offset
,
37 const Int64CompletionCallback
& callback
) {
38 Int64CompletionCallback wrapped_callback
=
39 base::Bind(&MockFileStream::DoCallback64
,
40 weak_factory_
.GetWeakPtr(), callback
);
41 if (forced_error_
== net::OK
)
42 return FileStream::Seek(whence
, offset
, wrapped_callback
);
43 return ErrorCallback64(wrapped_callback
);
46 int MockFileStream::Read(IOBuffer
* buf
,
48 const CompletionCallback
& callback
) {
49 CompletionCallback wrapped_callback
= base::Bind(&MockFileStream::DoCallback
,
50 weak_factory_
.GetWeakPtr(),
52 if (forced_error_
== net::OK
)
53 return FileStream::Read(buf
, buf_len
, wrapped_callback
);
54 return ErrorCallback(wrapped_callback
);
57 int MockFileStream::Write(IOBuffer
* buf
,
59 const CompletionCallback
& callback
) {
60 CompletionCallback wrapped_callback
= base::Bind(&MockFileStream::DoCallback
,
61 weak_factory_
.GetWeakPtr(),
63 if (forced_error_
== net::OK
)
64 return FileStream::Write(buf
, buf_len
, wrapped_callback
);
65 return ErrorCallback(wrapped_callback
);
68 int MockFileStream::Flush(const CompletionCallback
& callback
) {
69 CompletionCallback wrapped_callback
= base::Bind(&MockFileStream::DoCallback
,
70 weak_factory_
.GetWeakPtr(),
72 if (forced_error_
== net::OK
)
73 return FileStream::Flush(wrapped_callback
);
74 return ErrorCallback(wrapped_callback
);
77 void MockFileStream::ThrottleCallbacks() {
82 void MockFileStream::ReleaseCallbacks() {
86 if (!throttled_task_
.is_null()) {
87 base::Closure throttled_task
= throttled_task_
;
88 throttled_task_
.Reset();
89 base::MessageLoop::current()->PostTask(FROM_HERE
, throttled_task
);
93 void MockFileStream::DoCallback(const CompletionCallback
& callback
,
99 CHECK(throttled_task_
.is_null());
100 throttled_task_
= base::Bind(callback
, result
);
103 void MockFileStream::DoCallback64(const Int64CompletionCallback
& callback
,
106 callback
.Run(result
);
109 CHECK(throttled_task_
.is_null());
110 throttled_task_
= base::Bind(callback
, result
);
113 int MockFileStream::ErrorCallback(const CompletionCallback
& callback
) {
114 CHECK_NE(net::OK
, forced_error_
);
116 base::MessageLoop::current()->PostTask(
117 FROM_HERE
, base::Bind(callback
, forced_error_
));
118 clear_forced_error();
119 return net::ERR_IO_PENDING
;
121 int ret
= forced_error_
;
122 clear_forced_error();
126 int64
MockFileStream::ErrorCallback64(const Int64CompletionCallback
& callback
) {
127 CHECK_NE(net::OK
, forced_error_
);
129 base::MessageLoop::current()->PostTask(
130 FROM_HERE
, base::Bind(callback
, forced_error_
));
131 clear_forced_error();
132 return net::ERR_IO_PENDING
;
134 int64 ret
= forced_error_
;
135 clear_forced_error();
139 } // namespace testing