xref: /aosp_15_r20/external/cronet/net/base/test_completion_callback.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // 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/test_completion_callback.h"
6 
7 #include "base/compiler_specific.h"
8 #include "base/functional/bind.h"
9 #include "base/functional/callback_helpers.h"
10 #include "base/run_loop.h"
11 #include "net/base/io_buffer.h"
12 
13 namespace net {
14 
15 namespace internal {
16 
DidSetResult()17 void TestCompletionCallbackBaseInternal::DidSetResult() {
18   have_result_ = true;
19   if (run_loop_)
20     run_loop_->Quit();
21 }
22 
WaitForResult()23 void TestCompletionCallbackBaseInternal::WaitForResult() {
24   DCHECK(!run_loop_);
25   if (!have_result_) {
26     run_loop_ = std::make_unique<base::RunLoop>(
27         base::RunLoop::Type::kNestableTasksAllowed);
28     run_loop_->Run();
29     run_loop_.reset();
30     DCHECK(have_result_);
31   }
32   have_result_ = false;  // Auto-reset for next callback.
33 }
34 
35 TestCompletionCallbackBaseInternal::TestCompletionCallbackBaseInternal() =
36     default;
37 
38 TestCompletionCallbackBaseInternal::~TestCompletionCallbackBaseInternal() =
39     default;
40 
41 }  // namespace internal
42 
43 TestClosure::~TestClosure() = default;
44 
45 TestCompletionCallback::~TestCompletionCallback() = default;
46 
47 TestInt64CompletionCallback::~TestInt64CompletionCallback() = default;
48 
ReleaseBufferCompletionCallback(IOBuffer * buffer)49 ReleaseBufferCompletionCallback::ReleaseBufferCompletionCallback(
50     IOBuffer* buffer) : buffer_(buffer) {
51 }
52 
53 ReleaseBufferCompletionCallback::~ReleaseBufferCompletionCallback() = default;
54 
SetResult(int result)55 void ReleaseBufferCompletionCallback::SetResult(int result) {
56   if (!buffer_->HasOneRef())
57     result = ERR_FAILED;
58   TestCompletionCallback::SetResult(result);
59 }
60 
61 }  // namespace net
62