Chromium Code Reviews| 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" |
|
csilv
2011/12/14 20:10:19
add #include base/bind_helpers.h
James Hawkins
2011/12/14 20:59:05
Done.
| |
| 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 TestOldCompletionCallback::TestOldCompletionCallback() | 12 void TestCompletionCallbackBase::SetResult(int result) { |
| 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() | |
| 13 : result_(0), | 41 : result_(0), |
| 14 have_result_(false), | 42 have_result_(false), |
| 15 waiting_for_result_(false) { | 43 waiting_for_result_(false) { |
| 16 } | 44 } |
| 17 | 45 |
| 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 | |
| 37 void TestOldCompletionCallback::RunWithParams(const Tuple1<int>& params) { | 46 void TestOldCompletionCallback::RunWithParams(const Tuple1<int>& params) { |
| 38 result_ = params.a; | 47 SetResult(params.a); |
| 39 have_result_ = true; | |
| 40 if (waiting_for_result_) | |
| 41 MessageLoop::current()->Quit(); | |
| 42 } | 48 } |
| 43 | 49 |
| 44 namespace net { | 50 namespace net { |
| 45 | 51 |
| 46 TestCompletionCallback::TestCompletionCallback() | 52 TestCompletionCallback::TestCompletionCallback() |
| 47 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_( | 53 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_( |
| 48 base::Bind(&TestCompletionCallback::OnComplete, | 54 base::Bind(&TestCompletionCallback::SetResult, |
| 49 base::Unretained(this)))) { | 55 base::Unretained(this)))) { |
| 50 } | 56 } |
| 51 | 57 |
| 52 TestCompletionCallback::~TestCompletionCallback() {} | 58 TestCompletionCallback::~TestCompletionCallback() {} |
| 53 | 59 |
| 54 void TestCompletionCallback::OnComplete(int result) { | |
| 55 old_callback_impl_.RunWithParams(Tuple1<int>(result)); | |
| 56 } | |
| 57 | 60 |
| 58 } // namespace net | 61 } // namespace net |
| OLD | NEW |