blob: 8973e84bd56cd2f190b3055d9f6f9711d483851b [file] [log] [blame]
mukesh agrawal46c27cc2013-07-10 16:39:10 -07001// 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 Tan7c1aacb2014-09-15 17:13:40 -07007#include "shill/event_dispatcher.h"
mukesh agrawal46c27cc2013-07-10 16:39:10 -07008#include "shill/logging.h"
9
10namespace shill {
11
Paul Stewart1a212a62015-06-16 13:13:10 -070012ResultAggregator::ResultAggregator(const ResultCallback& callback)
Ben Chancc225ef2014-09-30 13:26:51 -070013 : ResultAggregator(callback, nullptr, -1) {}
mukesh agrawal46c27cc2013-07-10 16:39:10 -070014
Paul Stewart1a212a62015-06-16 13:13:10 -070015ResultAggregator::ResultAggregator(const ResultCallback& callback,
16 EventDispatcher* dispatcher,
Samuel Tan7c1aacb2014-09-15 17:13:40 -070017 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 agrawal46c27cc2013-07-10 16:39:10 -070028 }
29}
30
Samuel Tan7c1aacb2014-09-15 17:13:40 -070031ResultAggregator::~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 Stewart1a212a62015-06-16 13:13:10 -070039void ResultAggregator::ReportResult(const Error& error) {
Samuel Tan7c1aacb2014-09-15 17:13:40 -070040 LOG(INFO) << "Error type " << error << " reported";
mukesh agrawal46c27cc2013-07-10 16:39:10 -070041 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 Tan7c1aacb2014-09-15 17:13:40 -070045 } else {
46 LOG(WARNING) << "Dropping error type " << error;
mukesh agrawal46c27cc2013-07-10 16:39:10 -070047 }
48}
49
Samuel Tan7c1aacb2014-09-15 17:13:40 -070050void 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 agrawal46c27cc2013-07-10 16:39:10 -070057} // namespace shill