blob: 73c1acaa81a6e016518b0783a5501662fde4bf6e [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
Gary Morainf80ef062012-05-16 14:57:04 -070026HookTable::HookTable(EventDispatcher *event_dispatcher)
Gary Moraina9fb3252012-05-31 12:05:31 -070027 : event_dispatcher_(event_dispatcher) {}
Gary Morainf80ef062012-05-16 14:57:04 -070028
Ben Chan3fbf8bd2014-06-07 20:49:52 -070029void HookTable::Add(const string &name, const Closure &start_callback) {
30 SLOG(Manager, 2) << __func__ << ": " << name;
Gary Moraina9fb3252012-05-31 12:05:31 -070031 Remove(name);
Ben Chan3fbf8bd2014-06-07 20:49:52 -070032 hook_table_.emplace(name, HookAction(start_callback));
Gary Morainf80ef062012-05-16 14:57:04 -070033}
34
Gary Moraina9fb3252012-05-31 12:05:31 -070035HookTable::~HookTable() {
Ben Chan3fbf8bd2014-06-07 20:49:52 -070036 timeout_callback_.Cancel();
Gary Moraina9fb3252012-05-31 12:05:31 -070037}
38
39void HookTable::Remove(const std::string &name) {
Ben Chan3fbf8bd2014-06-07 20:49:52 -070040 SLOG(Manager, 2) << __func__ << ": " << name;
41 hook_table_.erase(name);
Gary Morainf80ef062012-05-16 14:57:04 -070042}
43
Gary Moraina9fb3252012-05-31 12:05:31 -070044void HookTable::ActionComplete(const std::string &name) {
mukesh agrawalb1136662013-10-14 16:39:38 -070045 SLOG(Manager, 2) << __func__ << ": " << name;
Gary Moraina9fb3252012-05-31 12:05:31 -070046 HookTableMap::iterator it = hook_table_.find(name);
47 if (it != hook_table_.end()) {
48 HookAction *action = &it->second;
49 if (action->started && !action->completed) {
50 action->completed = true;
Gary Morainf80ef062012-05-16 14:57:04 -070051 }
52 }
Ben Chan3fbf8bd2014-06-07 20:49:52 -070053 if (AllActionsComplete() && !done_callback_.is_null()) {
54 timeout_callback_.Cancel();
55 done_callback_.Run(Error(Error::kSuccess));
56 done_callback_.Reset();
Gary Moraina9fb3252012-05-31 12:05:31 -070057 }
58}
Gary Morainf80ef062012-05-16 14:57:04 -070059
Ben Chan3fbf8bd2014-06-07 20:49:52 -070060void HookTable::Run(int timeout_ms, const ResultCallback &done) {
mukesh agrawalb1136662013-10-14 16:39:38 -070061 SLOG(Manager, 2) << __func__;
Gary Moraina9fb3252012-05-31 12:05:31 -070062 if (hook_table_.empty()) {
Gary Morainf80ef062012-05-16 14:57:04 -070063 done.Run(Error(Error::kSuccess));
64 return;
65 }
Ben Chan3fbf8bd2014-06-07 20:49:52 -070066 done_callback_ = done;
67 timeout_callback_.Reset(Bind(&HookTable::ActionsTimedOut, Unretained(this)));
68 event_dispatcher_->PostDelayedTask(timeout_callback_.callback(), timeout_ms);
Gary Morainf80ef062012-05-16 14:57:04 -070069
Gary Moraina9fb3252012-05-31 12:05:31 -070070 // Mark all actions as having started before we execute any actions.
71 // Otherwise, if the first action completes inline, its call to
72 // ActionComplete() will cause the |done| callback to be invoked before the
73 // rest of the actions get started.
Ben Chan255f5652014-06-03 22:14:15 -070074 //
75 // An action that completes inline could call HookTable::Remove(), which
76 // modifies |hook_table_|. It is thus not safe to iterate through
77 // |hook_table_| to execute the actions. Instead, we keep a list of start
78 // callback of each action and iterate through that to invoke the callback.
79 list<Closure> action_start_callbacks;
Paul Stewart6db7b242014-05-02 15:34:21 -070080 for (auto &hook_entry : hook_table_) {
81 HookAction *action = &hook_entry.second;
Ben Chan3fbf8bd2014-06-07 20:49:52 -070082 action_start_callbacks.push_back(action->start_callback);
Gary Moraina9fb3252012-05-31 12:05:31 -070083 action->started = true;
84 action->completed = false;
Gary Morainf80ef062012-05-16 14:57:04 -070085 }
Gary Moraina9fb3252012-05-31 12:05:31 -070086 // Now start the actions.
Ben Chan255f5652014-06-03 22:14:15 -070087 for (auto &callback : action_start_callbacks) {
88 callback.Run();
Gary Moraina9fb3252012-05-31 12:05:31 -070089 }
90}
Gary Morainf80ef062012-05-16 14:57:04 -070091
Ben Chan3fbf8bd2014-06-07 20:49:52 -070092bool HookTable::AllActionsComplete() const {
mukesh agrawalb1136662013-10-14 16:39:38 -070093 SLOG(Manager, 2) << __func__;
Ben Chan3fbf8bd2014-06-07 20:49:52 -070094 for (const auto &hook_entry : hook_table_) {
95 const HookAction &action = hook_entry.second;
Gary Moraina9fb3252012-05-31 12:05:31 -070096 if (action.started && !action.completed) {
97 return false;
98 }
99 }
100 return true;
101}
102
103void HookTable::ActionsTimedOut() {
Ben Chan3fbf8bd2014-06-07 20:49:52 -0700104 done_callback_.Run(Error(Error::kOperationTimeout));
105 done_callback_.Reset();
Gary Morainf80ef062012-05-16 14:57:04 -0700106}
107
108} // namespace shill