pending_callback_chain.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2018 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. #include "services/network/pending_callback_chain.h"
  5. #include "base/bind.h"
  6. namespace network {
  7. PendingCallbackChain::PendingCallbackChain(net::CompletionOnceCallback complete)
  8. : complete_(std::move(complete)) {}
  9. PendingCallbackChain::~PendingCallbackChain() {}
  10. net::CompletionOnceCallback PendingCallbackChain::CreateCallback() {
  11. return base::BindOnce(&PendingCallbackChain::CallbackComplete, this);
  12. }
  13. void PendingCallbackChain::AddResult(int result) {
  14. if (result == net::ERR_IO_PENDING)
  15. num_waiting_++;
  16. else
  17. SetResult(result);
  18. }
  19. int PendingCallbackChain::GetResult() const {
  20. if (num_waiting_ > 0)
  21. return net::ERR_IO_PENDING;
  22. return final_result_;
  23. }
  24. void PendingCallbackChain::CallbackComplete(int result) {
  25. DCHECK_GT(num_waiting_, 0);
  26. SetResult(result);
  27. num_waiting_--;
  28. if (num_waiting_ == 0)
  29. std::move(complete_).Run(final_result_);
  30. }
  31. void PendingCallbackChain::SetResult(int result) {
  32. DCHECK_NE(result, net::ERR_IO_PENDING);
  33. if (final_result_ == net::OK) {
  34. final_result_ = result;
  35. } else if (result != net::OK && result != final_result_) {
  36. // If we have two non-OK results, default to ERR_FAILED.
  37. final_result_ = net::ERR_FAILED;
  38. }
  39. }
  40. } // namespace network