Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 1 | // 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 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/bind.h> |
| 10 | #include <base/callback.h> |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 11 | #include <base/cancelable_callback.h> |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 12 | |
| 13 | #include "shill/error.h" |
| 14 | #include "shill/event_dispatcher.h" |
mukesh agrawal | b113666 | 2013-10-14 16:39:38 -0700 | [diff] [blame] | 15 | #include "shill/logging.h" |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 16 | |
| 17 | using base::Bind; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 18 | using base::Callback; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 19 | using base::Closure; |
| 20 | using base::ConstRef; |
| 21 | using base::Unretained; |
| 22 | using std::string; |
| 23 | |
| 24 | namespace shill { |
| 25 | |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 26 | HookTable::HookTable(EventDispatcher *event_dispatcher) |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 27 | : event_dispatcher_(event_dispatcher) {} |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 28 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 29 | void HookTable::Add(const string &name, const base::Closure &start) { |
| 30 | Remove(name); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 31 | hook_table_.insert( |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 32 | HookTableMap::value_type(name, HookAction(start))); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 35 | HookTable::~HookTable() { |
| 36 | timeout_cb_.Cancel(); |
| 37 | } |
| 38 | |
| 39 | void HookTable::Remove(const std::string &name) { |
| 40 | HookTableMap::iterator it = hook_table_.find(name); |
| 41 | if (it != hook_table_.end()) { |
| 42 | hook_table_.erase(it); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 43 | } |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 46 | void HookTable::ActionComplete(const std::string &name) { |
mukesh agrawal | b113666 | 2013-10-14 16:39:38 -0700 | [diff] [blame] | 47 | SLOG(Manager, 2) << __func__ << ": " << name; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 48 | HookTableMap::iterator it = hook_table_.find(name); |
| 49 | if (it != hook_table_.end()) { |
| 50 | HookAction *action = &it->second; |
| 51 | if (action->started && !action->completed) { |
| 52 | action->completed = true; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 55 | if (AllActionsComplete() && !done_cb_.is_null()) { |
| 56 | timeout_cb_.Cancel(); |
| 57 | done_cb_.Run(Error(Error::kSuccess)); |
| 58 | done_cb_.Reset(); |
| 59 | } |
| 60 | } |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 61 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 62 | void HookTable::Run(int timeout_ms, |
| 63 | const Callback<void(const Error &)> &done) { |
mukesh agrawal | b113666 | 2013-10-14 16:39:38 -0700 | [diff] [blame] | 64 | SLOG(Manager, 2) << __func__; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 65 | if (hook_table_.empty()) { |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 66 | done.Run(Error(Error::kSuccess)); |
| 67 | return; |
| 68 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 69 | done_cb_ = done; |
| 70 | timeout_cb_.Reset(Bind(&HookTable::ActionsTimedOut, Unretained(this))); |
| 71 | event_dispatcher_->PostDelayedTask(timeout_cb_.callback(), timeout_ms); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 72 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 73 | // Mark all actions as having started before we execute any actions. |
| 74 | // Otherwise, if the first action completes inline, its call to |
| 75 | // ActionComplete() will cause the |done| callback to be invoked before the |
| 76 | // rest of the actions get started. |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame^] | 77 | for (auto &hook_entry : hook_table_) { |
| 78 | HookAction *action = &hook_entry.second; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 79 | action->started = true; |
| 80 | action->completed = false; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 81 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 82 | // Now start the actions. |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame^] | 83 | for (auto &hook_entry : hook_table_) { |
| 84 | hook_entry.second.start.Run(); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 87 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 88 | bool HookTable::AllActionsComplete() { |
mukesh agrawal | b113666 | 2013-10-14 16:39:38 -0700 | [diff] [blame] | 89 | SLOG(Manager, 2) << __func__; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 90 | for (HookTableMap::const_iterator it = hook_table_.begin(); |
| 91 | it != hook_table_.end(); ++it) { |
| 92 | const HookAction &action = it->second; |
| 93 | if (action.started && !action.completed) { |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | void HookTable::ActionsTimedOut() { |
| 101 | done_cb_.Run(Error(Error::kOperationTimeout)); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 102 | done_cb_.Reset(); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace shill |