Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / mock_file_stream.cc
blobc699b463d593f169bc4f450958d83cfafd3a8625
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"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
12 namespace net {
14 namespace testing {
16 MockFileStream::MockFileStream(
17 const scoped_refptr<base::TaskRunner>& task_runner)
18 : FileStream(task_runner),
19 forced_error_(OK),
20 async_error_(false),
21 throttled_(false),
22 weak_factory_(this) {
25 MockFileStream::MockFileStream(
26 base::File file,
27 const scoped_refptr<base::TaskRunner>& task_runner)
28 : FileStream(file.Pass(), task_runner),
29 forced_error_(OK),
30 async_error_(false),
31 throttled_(false),
32 weak_factory_(this) {
35 MockFileStream::~MockFileStream() {
38 int MockFileStream::Seek(int64_t offset,
39 const Int64CompletionCallback& callback) {
40 Int64CompletionCallback wrapped_callback =
41 base::Bind(&MockFileStream::DoCallback64,
42 weak_factory_.GetWeakPtr(), callback);
43 if (forced_error_ == OK)
44 return FileStream::Seek(offset, wrapped_callback);
45 return ErrorCallback64(wrapped_callback);
48 int MockFileStream::Read(IOBuffer* buf,
49 int buf_len,
50 const CompletionCallback& callback) {
51 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
52 weak_factory_.GetWeakPtr(),
53 callback);
54 if (forced_error_ == OK)
55 return FileStream::Read(buf, buf_len, wrapped_callback);
56 return ErrorCallback(wrapped_callback);
59 int MockFileStream::Write(IOBuffer* buf,
60 int buf_len,
61 const CompletionCallback& callback) {
62 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
63 weak_factory_.GetWeakPtr(),
64 callback);
65 if (forced_error_ == OK)
66 return FileStream::Write(buf, buf_len, wrapped_callback);
67 return ErrorCallback(wrapped_callback);
70 int MockFileStream::Flush(const CompletionCallback& callback) {
71 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback,
72 weak_factory_.GetWeakPtr(),
73 callback);
74 if (forced_error_ == OK)
75 return FileStream::Flush(wrapped_callback);
76 return ErrorCallback(wrapped_callback);
79 void MockFileStream::ThrottleCallbacks() {
80 CHECK(!throttled_);
81 throttled_ = true;
84 void MockFileStream::ReleaseCallbacks() {
85 CHECK(throttled_);
86 throttled_ = false;
88 if (!throttled_task_.is_null()) {
89 base::Closure throttled_task = throttled_task_;
90 throttled_task_.Reset();
91 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, throttled_task);
95 void MockFileStream::DoCallback(const CompletionCallback& callback,
96 int result) {
97 if (!throttled_) {
98 callback.Run(result);
99 return;
101 CHECK(throttled_task_.is_null());
102 throttled_task_ = base::Bind(callback, result);
105 void MockFileStream::DoCallback64(const Int64CompletionCallback& callback,
106 int64_t result) {
107 if (!throttled_) {
108 callback.Run(result);
109 return;
111 CHECK(throttled_task_.is_null());
112 throttled_task_ = base::Bind(callback, result);
115 int MockFileStream::ErrorCallback(const CompletionCallback& callback) {
116 CHECK_NE(OK, forced_error_);
117 if (async_error_) {
118 base::ThreadTaskRunnerHandle::Get()->PostTask(
119 FROM_HERE, base::Bind(callback, forced_error_));
120 clear_forced_error();
121 return ERR_IO_PENDING;
123 int ret = forced_error_;
124 clear_forced_error();
125 return ret;
128 int64_t MockFileStream::ErrorCallback64(
129 const Int64CompletionCallback& callback) {
130 CHECK_NE(OK, forced_error_);
131 if (async_error_) {
132 base::ThreadTaskRunnerHandle::Get()->PostTask(
133 FROM_HERE, base::Bind(callback, forced_error_));
134 clear_forced_error();
135 return ERR_IO_PENDING;
137 int64_t ret = forced_error_;
138 clear_forced_error();
139 return ret;
142 } // namespace testing
144 } // namespace net