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; |
| 19 | using base::Closure; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 20 | using base::Unretained; |
Ben Chan | 255f565 | 2014-06-03 22:14:15 -0700 | [diff] [blame] | 21 | using std::list; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 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 | |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 29 | void HookTable::Add(const string &name, const Closure &start_callback) { |
| 30 | SLOG(Manager, 2) << __func__ << ": " << name; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 31 | Remove(name); |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 32 | hook_table_.emplace(name, HookAction(start_callback)); |
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() { |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 36 | timeout_callback_.Cancel(); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void HookTable::Remove(const std::string &name) { |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 40 | SLOG(Manager, 2) << __func__ << ": " << name; |
| 41 | hook_table_.erase(name); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 44 | void HookTable::ActionComplete(const std::string &name) { |
mukesh agrawal | b113666 | 2013-10-14 16:39:38 -0700 | [diff] [blame] | 45 | SLOG(Manager, 2) << __func__ << ": " << name; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 46 | 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 Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 53 | if (AllActionsComplete() && !done_callback_.is_null()) { |
| 54 | timeout_callback_.Cancel(); |
| 55 | done_callback_.Run(Error(Error::kSuccess)); |
| 56 | done_callback_.Reset(); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 59 | |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 60 | void HookTable::Run(int timeout_ms, const ResultCallback &done) { |
mukesh agrawal | b113666 | 2013-10-14 16:39:38 -0700 | [diff] [blame] | 61 | SLOG(Manager, 2) << __func__; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 62 | if (hook_table_.empty()) { |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 63 | done.Run(Error(Error::kSuccess)); |
| 64 | return; |
| 65 | } |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 66 | done_callback_ = done; |
| 67 | timeout_callback_.Reset(Bind(&HookTable::ActionsTimedOut, Unretained(this))); |
| 68 | event_dispatcher_->PostDelayedTask(timeout_callback_.callback(), timeout_ms); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 69 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 70 | // 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 Chan | 255f565 | 2014-06-03 22:14:15 -0700 | [diff] [blame] | 74 | // |
| 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 Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 80 | for (auto &hook_entry : hook_table_) { |
| 81 | HookAction *action = &hook_entry.second; |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 82 | action_start_callbacks.push_back(action->start_callback); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 83 | action->started = true; |
| 84 | action->completed = false; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 85 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 86 | // Now start the actions. |
Ben Chan | 255f565 | 2014-06-03 22:14:15 -0700 | [diff] [blame] | 87 | for (auto &callback : action_start_callbacks) { |
| 88 | callback.Run(); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 91 | |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 92 | bool HookTable::AllActionsComplete() const { |
mukesh agrawal | b113666 | 2013-10-14 16:39:38 -0700 | [diff] [blame] | 93 | SLOG(Manager, 2) << __func__; |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 94 | for (const auto &hook_entry : hook_table_) { |
| 95 | const HookAction &action = hook_entry.second; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 96 | if (action.started && !action.completed) { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | void HookTable::ActionsTimedOut() { |
Ben Chan | 3fbf8bd | 2014-06-07 20:49:52 -0700 | [diff] [blame] | 104 | done_callback_.Run(Error(Error::kOperationTimeout)); |
| 105 | done_callback_.Reset(); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace shill |