mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS 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 "shill/result_aggregator.h" |
| 6 | |
Samuel Tan | 7c1aacb | 2014-09-15 17:13:40 -0700 | [diff] [blame] | 7 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 8 | #include "shill/logging.h" |
| 9 | |
| 10 | namespace shill { |
| 11 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 12 | ResultAggregator::ResultAggregator(const ResultCallback& callback) |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 13 | : ResultAggregator(callback, nullptr, -1) {} |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 14 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 15 | ResultAggregator::ResultAggregator(const ResultCallback& callback, |
| 16 | EventDispatcher* dispatcher, |
Samuel Tan | 7c1aacb | 2014-09-15 17:13:40 -0700 | [diff] [blame] | 17 | int timeout_milliseconds) |
| 18 | : weak_ptr_factory_(this), |
| 19 | callback_(callback), |
| 20 | timeout_callback_(base::Bind(&ResultAggregator::Timeout, |
| 21 | weak_ptr_factory_.GetWeakPtr())), |
| 22 | got_result_(false), |
| 23 | timed_out_(false) { |
| 24 | CHECK(!callback.is_null()); |
| 25 | if (dispatcher && timeout_milliseconds >= 0) { |
| 26 | dispatcher->PostDelayedTask(timeout_callback_.callback(), |
| 27 | timeout_milliseconds); |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
Samuel Tan | 7c1aacb | 2014-09-15 17:13:40 -0700 | [diff] [blame] | 31 | ResultAggregator::~ResultAggregator() { |
| 32 | if (got_result_ && !timed_out_) { |
| 33 | callback_.Run(error_); |
| 34 | } |
| 35 | // timeout_callback_ will automatically be canceled when its destructor |
| 36 | // is invoked. |
| 37 | } |
| 38 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 39 | void ResultAggregator::ReportResult(const Error& error) { |
Samuel Tan | 7c1aacb | 2014-09-15 17:13:40 -0700 | [diff] [blame] | 40 | LOG(INFO) << "Error type " << error << " reported"; |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 41 | CHECK(!error.IsOngoing()); // We want the final result. |
| 42 | got_result_ = true; |
| 43 | if (error_.IsSuccess()) { // Only copy first |error|. |
| 44 | error_.CopyFrom(error); |
Samuel Tan | 7c1aacb | 2014-09-15 17:13:40 -0700 | [diff] [blame] | 45 | } else { |
| 46 | LOG(WARNING) << "Dropping error type " << error; |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Samuel Tan | 7c1aacb | 2014-09-15 17:13:40 -0700 | [diff] [blame] | 50 | void ResultAggregator::Timeout() { |
| 51 | LOG(WARNING) << "Results aggregator timed out"; |
| 52 | timed_out_ = true; |
| 53 | error_.Populate(Error::kOperationTimeout); |
| 54 | callback_.Run(error_); |
| 55 | } |
| 56 | |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 57 | } // namespace shill |