blob: f1d7f5b83f5fbcf816058348dd8163430f591aa5 [file] [log] [blame]
Gary Morainf80ef062012-05-16 14:57:04 -07001// Copyright (c) 2012 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/hook_table.h"
6
Ben Chan255f5652014-06-03 22:14:15 -07007#include <list>
Gary Morainf80ef062012-05-16 14:57:04 -07008#include <string>
9
10#include <base/bind.h>
11#include <base/callback.h>
Gary Moraina9fb3252012-05-31 12:05:31 -070012#include <base/cancelable_callback.h>
Gary Morainf80ef062012-05-16 14:57:04 -070013
14#include "shill/error.h"
15#include "shill/event_dispatcher.h"
mukesh agrawalb1136662013-10-14 16:39:38 -070016#include "shill/logging.h"
Gary Morainf80ef062012-05-16 14:57:04 -070017
18using base::Bind;
19using base::Closure;
Gary Morainf80ef062012-05-16 14:57:04 -070020using base::Unretained;
Ben Chan255f5652014-06-03 22:14:15 -070021using std::list;
Gary Morainf80ef062012-05-16 14:57:04 -070022using std::string;
23
24namespace shill {
25
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070026namespace Logging {
27static auto kModuleLogScope = ScopeLogger::kManager;
Paul Stewart8ae18742015-06-16 13:13:10 -070028static string ObjectID(const HookTable* h) { return "(hook_table)"; }
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070029}
30
Paul Stewart8ae18742015-06-16 13:13:10 -070031HookTable::HookTable(EventDispatcher* event_dispatcher)
Gary Moraina9fb3252012-05-31 12:05:31 -070032 : event_dispatcher_(event_dispatcher) {}
Gary Morainf80ef062012-05-16 14:57:04 -070033
Paul Stewart8ae18742015-06-16 13:13:10 -070034void HookTable::Add(const string& name, const Closure& start_callback) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070035 SLOG(this, 2) << __func__ << ": " << name;
Gary Moraina9fb3252012-05-31 12:05:31 -070036 Remove(name);
Ben Chan3fbf8bd2014-06-07 20:49:52 -070037 hook_table_.emplace(name, HookAction(start_callback));
Gary Morainf80ef062012-05-16 14:57:04 -070038}
39
Gary Moraina9fb3252012-05-31 12:05:31 -070040HookTable::~HookTable() {
Ben Chan3fbf8bd2014-06-07 20:49:52 -070041 timeout_callback_.Cancel();
Gary Moraina9fb3252012-05-31 12:05:31 -070042}
43
Paul Stewart8ae18742015-06-16 13:13:10 -070044void HookTable::Remove(const std::string& name) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070045 SLOG(this, 2) << __func__ << ": " << name;
Ben Chan3fbf8bd2014-06-07 20:49:52 -070046 hook_table_.erase(name);
Gary Morainf80ef062012-05-16 14:57:04 -070047}
48
Paul Stewart8ae18742015-06-16 13:13:10 -070049void HookTable::ActionComplete(const std::string& name) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070050 SLOG(this, 2) << __func__ << ": " << name;
Gary Moraina9fb3252012-05-31 12:05:31 -070051 HookTableMap::iterator it = hook_table_.find(name);
52 if (it != hook_table_.end()) {
Paul Stewart8ae18742015-06-16 13:13:10 -070053 HookAction* action = &it->second;
Gary Moraina9fb3252012-05-31 12:05:31 -070054 if (action->started && !action->completed) {
55 action->completed = true;
Gary Morainf80ef062012-05-16 14:57:04 -070056 }
57 }
Ben Chan3fbf8bd2014-06-07 20:49:52 -070058 if (AllActionsComplete() && !done_callback_.is_null()) {
59 timeout_callback_.Cancel();
60 done_callback_.Run(Error(Error::kSuccess));
61 done_callback_.Reset();
Gary Moraina9fb3252012-05-31 12:05:31 -070062 }
63}
Gary Morainf80ef062012-05-16 14:57:04 -070064
Paul Stewart8ae18742015-06-16 13:13:10 -070065void HookTable::Run(int timeout_ms, const ResultCallback& done) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070066 SLOG(this, 2) << __func__;
Gary Moraina9fb3252012-05-31 12:05:31 -070067 if (hook_table_.empty()) {
Gary Morainf80ef062012-05-16 14:57:04 -070068 done.Run(Error(Error::kSuccess));
69 return;
70 }
Ben Chan3fbf8bd2014-06-07 20:49:52 -070071 done_callback_ = done;
72 timeout_callback_.Reset(Bind(&HookTable::ActionsTimedOut, Unretained(this)));
73 event_dispatcher_->PostDelayedTask(timeout_callback_.callback(), timeout_ms);
Gary Morainf80ef062012-05-16 14:57:04 -070074
Gary Moraina9fb3252012-05-31 12:05:31 -070075 // Mark all actions as having started before we execute any actions.
76 // Otherwise, if the first action completes inline, its call to
77 // ActionComplete() will cause the |done| callback to be invoked before the
78 // rest of the actions get started.
Ben Chan255f5652014-06-03 22:14:15 -070079 //
80 // An action that completes inline could call HookTable::Remove(), which
81 // modifies |hook_table_|. It is thus not safe to iterate through
82 // |hook_table_| to execute the actions. Instead, we keep a list of start
83 // callback of each action and iterate through that to invoke the callback.
84 list<Closure> action_start_callbacks;
Paul Stewart8ae18742015-06-16 13:13:10 -070085 for (auto& hook_entry : hook_table_) {
86 HookAction* action = &hook_entry.second;
Ben Chan3fbf8bd2014-06-07 20:49:52 -070087 action_start_callbacks.push_back(action->start_callback);
Gary Moraina9fb3252012-05-31 12:05:31 -070088 action->started = true;
89 action->completed = false;
Gary Morainf80ef062012-05-16 14:57:04 -070090 }
Gary Moraina9fb3252012-05-31 12:05:31 -070091 // Now start the actions.
Paul Stewart8ae18742015-06-16 13:13:10 -070092 for (auto& callback : action_start_callbacks) {
Ben Chan255f5652014-06-03 22:14:15 -070093 callback.Run();
Gary Moraina9fb3252012-05-31 12:05:31 -070094 }
95}
Gary Morainf80ef062012-05-16 14:57:04 -070096
Ben Chan3fbf8bd2014-06-07 20:49:52 -070097bool HookTable::AllActionsComplete() const {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070098 SLOG(this, 2) << __func__;
Paul Stewart8ae18742015-06-16 13:13:10 -070099 for (const auto& hook_entry : hook_table_) {
100 const HookAction& action = hook_entry.second;
Gary Moraina9fb3252012-05-31 12:05:31 -0700101 if (action.started && !action.completed) {
102 return false;
103 }
104 }
105 return true;
106}
107
108void HookTable::ActionsTimedOut() {
Ben Chan3fbf8bd2014-06-07 20:49:52 -0700109 done_callback_.Run(Error(Error::kOperationTimeout));
110 done_callback_.Reset();
Gary Morainf80ef062012-05-16 14:57:04 -0700111}
112
113} // namespace shill