Home | History | Annotate | Download | only in base
      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
      3 // found in the LICENSE file.
      4 
      5 #include "net/base/test_completion_callback.h"
      6 
      7 #include "base/message_loop.h"
      8 #include "net/base/net_errors.h"
      9 
     10 TestCompletionCallback::TestCompletionCallback()
     11     : result_(0),
     12       have_result_(false),
     13       waiting_for_result_(false) {
     14 }
     15 
     16 TestCompletionCallback::~TestCompletionCallback() {}
     17 
     18 int TestCompletionCallback::WaitForResult() {
     19   DCHECK(!waiting_for_result_);
     20   while (!have_result_) {
     21     waiting_for_result_ = true;
     22     MessageLoop::current()->Run();
     23     waiting_for_result_ = false;
     24   }
     25   have_result_ = false;  // auto-reset for next callback
     26   return result_;
     27 }
     28 
     29 int TestCompletionCallback::GetResult(int result) {
     30   if (net::ERR_IO_PENDING != result)
     31     return result;
     32   return WaitForResult();
     33 }
     34 
     35 void TestCompletionCallback::RunWithParams(const Tuple1<int>& params) {
     36   result_ = params.a;
     37   have_result_ = true;
     38   if (waiting_for_result_)
     39     MessageLoop::current()->Quit();
     40 }
     41