blob: 792b23b0344888ebb64c4dac10470c93bf7ebe05 [file] [log] [blame]
[email protected]fe57eb22012-02-09 05:59:401// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]47a881b2011-08-29 22:59:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/base/mock_file_stream.h"
6
[email protected]52c41b42014-03-14 17:56:487#include "base/bind.h"
8#include "base/message_loop/message_loop.h"
9
[email protected]47a881b2011-08-29 22:59:2110namespace net {
11
12namespace testing {
13
[email protected]671e95fd2014-04-30 11:21:3614MockFileStream::MockFileStream(
15 const scoped_refptr<base::TaskRunner>& task_runner)
ttuttle859dc7a2015-04-23 19:42:2916 : FileStream(task_runner),
17 forced_error_(OK),
[email protected]52c41b42014-03-14 17:56:4818 async_error_(false),
19 throttled_(false),
20 weak_factory_(this) {
21}
22
23MockFileStream::MockFileStream(
[email protected]be6fb60b2014-04-03 18:33:5824 base::File file,
[email protected]52c41b42014-03-14 17:56:4825 const scoped_refptr<base::TaskRunner>& task_runner)
ttuttle859dc7a2015-04-23 19:42:2926 : FileStream(file.Pass(), task_runner),
27 forced_error_(OK),
[email protected]52c41b42014-03-14 17:56:4828 async_error_(false),
29 throttled_(false),
30 weak_factory_(this) {
31}
32
33MockFileStream::~MockFileStream() {
34}
35
[email protected]633ff3b12014-06-20 23:30:1836int MockFileStream::Seek(base::File::Whence whence, int64 offset,
[email protected]cf02541b2012-04-11 08:02:1737 const Int64CompletionCallback& callback) {
[email protected]52c41b42014-03-14 17:56:4838 Int64CompletionCallback wrapped_callback =
39 base::Bind(&MockFileStream::DoCallback64,
40 weak_factory_.GetWeakPtr(), callback);
ttuttle859dc7a2015-04-23 19:42:2941 if (forced_error_ == OK)
[email protected]52c41b42014-03-14 17:56:4842 return FileStream::Seek(whence, offset, wrapped_callback);
43 return ErrorCallback64(wrapped_callback);
[email protected]cf02541b2012-04-11 08:02:1744}
45
[email protected]9f49afb2012-02-16 09:59:2046int MockFileStream::Read(IOBuffer* buf,
[email protected]5eb431e22011-10-12 08:51:3847 int buf_len,
48 const CompletionCallback& callback) {
[email protected]52c41b42014-03-14 17:56:4849 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
50 weak_factory_.GetWeakPtr(),
51 callback);
ttuttle859dc7a2015-04-23 19:42:2952 if (forced_error_ == OK)
[email protected]52c41b42014-03-14 17:56:4853 return FileStream::Read(buf, buf_len, wrapped_callback);
54 return ErrorCallback(wrapped_callback);
[email protected]47a881b2011-08-29 22:59:2155}
56
[email protected]9f49afb2012-02-16 09:59:2057int MockFileStream::Write(IOBuffer* buf,
[email protected]5eb431e22011-10-12 08:51:3858 int buf_len,
59 const CompletionCallback& callback) {
[email protected]52c41b42014-03-14 17:56:4860 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
61 weak_factory_.GetWeakPtr(),
62 callback);
ttuttle859dc7a2015-04-23 19:42:2963 if (forced_error_ == OK)
[email protected]52c41b42014-03-14 17:56:4864 return FileStream::Write(buf, buf_len, wrapped_callback);
65 return ErrorCallback(wrapped_callback);
[email protected]47a881b2011-08-29 22:59:2166}
67
[email protected]a95ce0762012-10-01 05:54:2668int MockFileStream::Flush(const CompletionCallback& callback) {
[email protected]52c41b42014-03-14 17:56:4869 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
70 weak_factory_.GetWeakPtr(),
71 callback);
ttuttle859dc7a2015-04-23 19:42:2972 if (forced_error_ == OK)
[email protected]52c41b42014-03-14 17:56:4873 return FileStream::Flush(wrapped_callback);
74 return ErrorCallback(wrapped_callback);
[email protected]a95ce0762012-10-01 05:54:2675}
76
[email protected]52c41b42014-03-14 17:56:4877void MockFileStream::ThrottleCallbacks() {
78 CHECK(!throttled_);
79 throttled_ = true;
80}
81
82void MockFileStream::ReleaseCallbacks() {
83 CHECK(throttled_);
84 throttled_ = false;
85
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);
90 }
91}
92
93void MockFileStream::DoCallback(const CompletionCallback& callback,
94 int result) {
95 if (!throttled_) {
96 callback.Run(result);
97 return;
98 }
99 CHECK(throttled_task_.is_null());
100 throttled_task_ = base::Bind(callback, result);
101}
102
103void MockFileStream::DoCallback64(const Int64CompletionCallback& callback,
104 int64 result) {
105 if (!throttled_) {
106 callback.Run(result);
107 return;
108 }
109 CHECK(throttled_task_.is_null());
110 throttled_task_ = base::Bind(callback, result);
111}
112
113int MockFileStream::ErrorCallback(const CompletionCallback& callback) {
ttuttle859dc7a2015-04-23 19:42:29114 CHECK_NE(OK, forced_error_);
[email protected]52c41b42014-03-14 17:56:48115 if (async_error_) {
116 base::MessageLoop::current()->PostTask(
117 FROM_HERE, base::Bind(callback, forced_error_));
118 clear_forced_error();
ttuttle859dc7a2015-04-23 19:42:29119 return ERR_IO_PENDING;
[email protected]52c41b42014-03-14 17:56:48120 }
121 int ret = forced_error_;
122 clear_forced_error();
123 return ret;
124}
125
126int64 MockFileStream::ErrorCallback64(const Int64CompletionCallback& callback) {
ttuttle859dc7a2015-04-23 19:42:29127 CHECK_NE(OK, forced_error_);
[email protected]52c41b42014-03-14 17:56:48128 if (async_error_) {
129 base::MessageLoop::current()->PostTask(
130 FROM_HERE, base::Bind(callback, forced_error_));
131 clear_forced_error();
ttuttle859dc7a2015-04-23 19:42:29132 return ERR_IO_PENDING;
[email protected]52c41b42014-03-14 17:56:48133 }
134 int64 ret = forced_error_;
135 clear_forced_error();
136 return ret;
137}
138
[email protected]47a881b2011-08-29 22:59:21139} // namespace testing
140
141} // namespace net