• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "net/base/test_completion_callback.h"
6 
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/message_loop/message_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 (waiting_for_result_)
20     base::MessageLoop::current()->Quit();
21 }
22 
WaitForResult()23 void TestCompletionCallbackBaseInternal::WaitForResult() {
24   DCHECK(!waiting_for_result_);
25   while (!have_result_) {
26     waiting_for_result_ = true;
27     base::MessageLoop::current()->Run();
28     waiting_for_result_ = false;
29   }
30   have_result_ = false;  // Auto-reset for next callback.
31 }
32 
TestCompletionCallbackBaseInternal()33 TestCompletionCallbackBaseInternal::TestCompletionCallbackBaseInternal()
34     : have_result_(false),
35       waiting_for_result_(false) {
36 }
37 
38 }  // namespace internal
39 
TestCompletionCallback()40 TestCompletionCallback::TestCompletionCallback()
41     : callback_(base::Bind(&TestCompletionCallback::SetResult,
42                 base::Unretained(this))) {
43 }
44 
~TestCompletionCallback()45 TestCompletionCallback::~TestCompletionCallback() {
46 }
47 
TestInt64CompletionCallback()48 TestInt64CompletionCallback::TestInt64CompletionCallback()
49     : callback_(base::Bind(&TestInt64CompletionCallback::SetResult,
50                 base::Unretained(this))) {
51 }
52 
~TestInt64CompletionCallback()53 TestInt64CompletionCallback::~TestInt64CompletionCallback() {
54 }
55 
ReleaseBufferCompletionCallback(IOBuffer * buffer)56 ReleaseBufferCompletionCallback::ReleaseBufferCompletionCallback(
57     IOBuffer* buffer) : buffer_(buffer) {
58 }
59 
~ReleaseBufferCompletionCallback()60 ReleaseBufferCompletionCallback::~ReleaseBufferCompletionCallback() {
61 }
62 
SetResult(int result)63 void ReleaseBufferCompletionCallback::SetResult(int result) {
64   if (!buffer_->HasOneRef())
65     result = net::ERR_FAILED;
66   TestCompletionCallback::SetResult(result);
67 }
68 
69 }  // namespace net
70