OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/test_completion_callback.h" | 5 #include "net/base/test_completion_callback.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 | 11 |
12 void TestCompletionCallbackBase::SetResult(int result) { | 12 TestOldCompletionCallback::TestOldCompletionCallback() |
13 result_ = result; | |
14 have_result_ = true; | |
15 if (waiting_for_result_) | |
16 MessageLoop::current()->Quit(); | |
17 } | |
18 | |
19 int TestCompletionCallbackBase::WaitForResult() { | |
20 DCHECK(!waiting_for_result_); | |
21 | |
22 while (!have_result_) { | |
23 printf("waiting\n"); | |
24 waiting_for_result_ = true; | |
25 MessageLoop::current()->Run(); | |
26 waiting_for_result_ = false; | |
27 } | |
28 | |
29 have_result_ = false; // Auto-reset for next callback. | |
30 return result_; | |
31 } | |
32 | |
33 int TestCompletionCallbackBase::GetResult(int result) { | |
34 if (net::ERR_IO_PENDING != result) | |
35 return result; | |
36 | |
37 return WaitForResult(); | |
38 } | |
39 | |
40 TestCompletionCallbackBase::TestCompletionCallbackBase() | |
41 : result_(0), | 13 : result_(0), |
42 have_result_(false), | 14 have_result_(false), |
43 waiting_for_result_(false) { | 15 waiting_for_result_(false) { |
44 } | 16 } |
45 | 17 |
| 18 TestOldCompletionCallback::~TestOldCompletionCallback() {} |
| 19 |
| 20 int TestOldCompletionCallback::WaitForResult() { |
| 21 DCHECK(!waiting_for_result_); |
| 22 while (!have_result_) { |
| 23 waiting_for_result_ = true; |
| 24 MessageLoop::current()->Run(); |
| 25 waiting_for_result_ = false; |
| 26 } |
| 27 have_result_ = false; // auto-reset for next callback |
| 28 return result_; |
| 29 } |
| 30 |
| 31 int TestOldCompletionCallback::GetResult(int result) { |
| 32 if (net::ERR_IO_PENDING != result) |
| 33 return result; |
| 34 return WaitForResult(); |
| 35 } |
| 36 |
46 void TestOldCompletionCallback::RunWithParams(const Tuple1<int>& params) { | 37 void TestOldCompletionCallback::RunWithParams(const Tuple1<int>& params) { |
47 SetResult(params.a); | 38 result_ = params.a; |
| 39 have_result_ = true; |
| 40 if (waiting_for_result_) |
| 41 MessageLoop::current()->Quit(); |
48 } | 42 } |
49 | 43 |
50 namespace net { | 44 namespace net { |
51 | 45 |
52 TestCompletionCallback::TestCompletionCallback() | 46 TestCompletionCallback::TestCompletionCallback() |
53 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_( | 47 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_( |
54 base::Bind(&TestCompletionCallback::SetResult, | 48 base::Bind(&TestCompletionCallback::OnComplete, |
55 base::Unretained(this)))) { | 49 base::Unretained(this)))) { |
56 } | 50 } |
57 | 51 |
58 TestCompletionCallback::~TestCompletionCallback() {} | 52 TestCompletionCallback::~TestCompletionCallback() {} |
59 | 53 |
| 54 void TestCompletionCallback::OnComplete(int result) { |
| 55 old_callback_impl_.RunWithParams(Tuple1<int>(result)); |
| 56 } |
60 | 57 |
61 } // namespace net | 58 } // namespace net |
OLD | NEW |