blob: 19ff24e1982740bd607927d6d25ccb16756b3c7a [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"
skyostil4891b25b2015-06-11 11:43:458#include "base/location.h"
9#include "base/single_thread_task_runner.h"
10#include "base/thread_task_runner_handle.h"
[email protected]52c41b42014-03-14 17:56:4811
[email protected]47a881b2011-08-29 22:59:2112namespace net {
13
14namespace testing {
15
[email protected]671e95fd2014-04-30 11:21:3616MockFileStream::MockFileStream(
17 const scoped_refptr<base::TaskRunner>& task_runner)
ttuttle859dc7a2015-04-23 19:42:2918 : FileStream(task_runner),
19 forced_error_(OK),
[email protected]52c41b42014-03-14 17:56:4820 async_error_(false),
21 throttled_(false),
22 weak_factory_(this) {
23}
24
25MockFileStream::MockFileStream(
[email protected]be6fb60b2014-04-03 18:33:5826 base::File file,
[email protected]52c41b42014-03-14 17:56:4827 const scoped_refptr<base::TaskRunner>& task_runner)
ttuttle859dc7a2015-04-23 19:42:2928 : FileStream(file.Pass(), task_runner),
29 forced_error_(OK),
[email protected]52c41b42014-03-14 17:56:4830 async_error_(false),
31 throttled_(false),
32 weak_factory_(this) {
33}
34
35MockFileStream::~MockFileStream() {
36}
37
wtc69f8ea82015-06-04 00:08:1338int MockFileStream::Seek(base::File::Whence whence,
39 int64_t offset,
[email protected]cf02541b2012-04-11 08:02:1740 const Int64CompletionCallback& callback) {
[email protected]52c41b42014-03-14 17:56:4841 Int64CompletionCallback wrapped_callback =
42 base::Bind(&MockFileStream::DoCallback64,
43 weak_factory_.GetWeakPtr(), callback);
ttuttle859dc7a2015-04-23 19:42:2944 if (forced_error_ == OK)
[email protected]52c41b42014-03-14 17:56:4845 return FileStream::Seek(whence, offset, wrapped_callback);
46 return ErrorCallback64(wrapped_callback);
[email protected]cf02541b2012-04-11 08:02:1747}
48
[email protected]9f49afb2012-02-16 09:59:2049int MockFileStream::Read(IOBuffer* buf,
[email protected]5eb431e22011-10-12 08:51:3850 int buf_len,
51 const CompletionCallback& callback) {
[email protected]52c41b42014-03-14 17:56:4852 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
53 weak_factory_.GetWeakPtr(),
54 callback);
ttuttle859dc7a2015-04-23 19:42:2955 if (forced_error_ == OK)
[email protected]52c41b42014-03-14 17:56:4856 return FileStream::Read(buf, buf_len, wrapped_callback);
57 return ErrorCallback(wrapped_callback);
[email protected]47a881b2011-08-29 22:59:2158}
59
[email protected]9f49afb2012-02-16 09:59:2060int MockFileStream::Write(IOBuffer* buf,
[email protected]5eb431e22011-10-12 08:51:3861 int buf_len,
62 const CompletionCallback& callback) {
[email protected]52c41b42014-03-14 17:56:4863 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
64 weak_factory_.GetWeakPtr(),
65 callback);
ttuttle859dc7a2015-04-23 19:42:2966 if (forced_error_ == OK)
[email protected]52c41b42014-03-14 17:56:4867 return FileStream::Write(buf, buf_len, wrapped_callback);
68 return ErrorCallback(wrapped_callback);
[email protected]47a881b2011-08-29 22:59:2169}
70
[email protected]a95ce0762012-10-01 05:54:2671int MockFileStream::Flush(const CompletionCallback& callback) {
[email protected]52c41b42014-03-14 17:56:4872 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
73 weak_factory_.GetWeakPtr(),
74 callback);
ttuttle859dc7a2015-04-23 19:42:2975 if (forced_error_ == OK)
[email protected]52c41b42014-03-14 17:56:4876 return FileStream::Flush(wrapped_callback);
77 return ErrorCallback(wrapped_callback);
[email protected]a95ce0762012-10-01 05:54:2678}
79
[email protected]52c41b42014-03-14 17:56:4880void MockFileStream::ThrottleCallbacks() {
81 CHECK(!throttled_);
82 throttled_ = true;
83}
84
85void MockFileStream::ReleaseCallbacks() {
86 CHECK(throttled_);
87 throttled_ = false;
88
89 if (!throttled_task_.is_null()) {
90 base::Closure throttled_task = throttled_task_;
91 throttled_task_.Reset();
skyostil4891b25b2015-06-11 11:43:4592 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, throttled_task);
[email protected]52c41b42014-03-14 17:56:4893 }
94}
95
96void MockFileStream::DoCallback(const CompletionCallback& callback,
97 int result) {
98 if (!throttled_) {
99 callback.Run(result);
100 return;
101 }
102 CHECK(throttled_task_.is_null());
103 throttled_task_ = base::Bind(callback, result);
104}
105
106void MockFileStream::DoCallback64(const Int64CompletionCallback& callback,
wtc69f8ea82015-06-04 00:08:13107 int64_t result) {
[email protected]52c41b42014-03-14 17:56:48108 if (!throttled_) {
109 callback.Run(result);
110 return;
111 }
112 CHECK(throttled_task_.is_null());
113 throttled_task_ = base::Bind(callback, result);
114}
115
116int MockFileStream::ErrorCallback(const CompletionCallback& callback) {
ttuttle859dc7a2015-04-23 19:42:29117 CHECK_NE(OK, forced_error_);
[email protected]52c41b42014-03-14 17:56:48118 if (async_error_) {
skyostil4891b25b2015-06-11 11:43:45119 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]52c41b42014-03-14 17:56:48120 FROM_HERE, base::Bind(callback, forced_error_));
121 clear_forced_error();
ttuttle859dc7a2015-04-23 19:42:29122 return ERR_IO_PENDING;
[email protected]52c41b42014-03-14 17:56:48123 }
124 int ret = forced_error_;
125 clear_forced_error();
126 return ret;
127}
128
wtc69f8ea82015-06-04 00:08:13129int64_t MockFileStream::ErrorCallback64(
130 const Int64CompletionCallback& callback) {
ttuttle859dc7a2015-04-23 19:42:29131 CHECK_NE(OK, forced_error_);
[email protected]52c41b42014-03-14 17:56:48132 if (async_error_) {
skyostil4891b25b2015-06-11 11:43:45133 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]52c41b42014-03-14 17:56:48134 FROM_HERE, base::Bind(callback, forced_error_));
135 clear_forced_error();
ttuttle859dc7a2015-04-23 19:42:29136 return ERR_IO_PENDING;
[email protected]52c41b42014-03-14 17:56:48137 }
wtc69f8ea82015-06-04 00:08:13138 int64_t ret = forced_error_;
[email protected]52c41b42014-03-14 17:56:48139 clear_forced_error();
140 return ret;
141}
142
[email protected]47a881b2011-08-29 22:59:21143} // namespace testing
144
145} // namespace net