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" |
| 15 | |
| 16 | using base::Bind; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 17 | using base::Callback; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 18 | using base::Closure; |
| 19 | using base::ConstRef; |
| 20 | using base::Unretained; |
| 21 | using std::string; |
| 22 | |
| 23 | namespace shill { |
| 24 | |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 25 | HookTable::HookTable(EventDispatcher *event_dispatcher) |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 26 | : event_dispatcher_(event_dispatcher) {} |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 27 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 28 | void HookTable::Add(const string &name, const base::Closure &start) { |
| 29 | Remove(name); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 30 | hook_table_.insert( |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 31 | HookTableMap::value_type(name, HookAction(start))); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 34 | HookTable::~HookTable() { |
| 35 | timeout_cb_.Cancel(); |
| 36 | } |
| 37 | |
| 38 | void HookTable::Remove(const std::string &name) { |
| 39 | HookTableMap::iterator it = hook_table_.find(name); |
| 40 | if (it != hook_table_.end()) { |
| 41 | hook_table_.erase(it); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 42 | } |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 45 | void HookTable::ActionComplete(const std::string &name) { |
| 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 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 53 | if (AllActionsComplete() && !done_cb_.is_null()) { |
| 54 | timeout_cb_.Cancel(); |
| 55 | done_cb_.Run(Error(Error::kSuccess)); |
| 56 | done_cb_.Reset(); |
| 57 | } |
| 58 | } |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 59 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 60 | void HookTable::Run(int timeout_ms, |
| 61 | const Callback<void(const Error &)> &done) { |
| 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 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 66 | done_cb_ = done; |
| 67 | timeout_cb_.Reset(Bind(&HookTable::ActionsTimedOut, Unretained(this))); |
| 68 | event_dispatcher_->PostDelayedTask(timeout_cb_.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. |
| 74 | for (HookTableMap::iterator it = hook_table_.begin(); |
| 75 | it != hook_table_.end(); ++it) { |
| 76 | HookAction *action = &it->second; |
| 77 | action->started = true; |
| 78 | action->completed = false; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 79 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 80 | // Now start the actions. |
| 81 | for (HookTableMap::iterator it = hook_table_.begin(); |
| 82 | it != hook_table_.end(); ++it) { |
| 83 | it->second.start.Run(); |
| 84 | } |
| 85 | } |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 86 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 87 | bool HookTable::AllActionsComplete() { |
| 88 | for (HookTableMap::const_iterator it = hook_table_.begin(); |
| 89 | it != hook_table_.end(); ++it) { |
| 90 | const HookAction &action = it->second; |
| 91 | if (action.started && !action.completed) { |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void HookTable::ActionsTimedOut() { |
| 99 | done_cb_.Run(Error(Error::kOperationTimeout)); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // namespace shill |