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