Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "action.h" |
| 18 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 20 | #include <android-base/properties.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 22 | #include <android-base/strings.h> |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 23 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 24 | #include "util.h" |
| 25 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 26 | using android::base::Join; |
| 27 | using android::base::StringPrintf; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 28 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 29 | Command::Command(BuiltinFunction f, const std::vector<std::string>& args, int line) |
| 30 | : func_(f), args_(args), line_(line) {} |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 31 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 32 | int Command::InvokeFunc() const { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 33 | std::vector<std::string> expanded_args; |
| 34 | expanded_args.resize(args_.size()); |
| 35 | expanded_args[0] = args_[0]; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 36 | for (std::size_t i = 1; i < args_.size(); ++i) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 37 | if (!expand_props(args_[i], &expanded_args[i])) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 38 | LOG(ERROR) << args_[0] << ": cannot expand '" << args_[i] << "'"; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 39 | return -EINVAL; |
| 40 | } |
| 41 | } |
| 42 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 43 | return func_(expanded_args); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 46 | std::string Command::BuildCommandString() const { |
| 47 | return Join(args_, ' '); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 50 | Action::Action(bool oneshot, const std::string& filename, int line) |
| 51 | : oneshot_(oneshot), filename_(filename), line_(line) {} |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 52 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 53 | const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr; |
| 54 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 55 | bool Action::AddCommand(const std::vector<std::string>& args, int line, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 56 | if (!function_map_) { |
| 57 | *err = "no function map available"; |
| 58 | return false; |
| 59 | } |
| 60 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 61 | auto function = function_map_->FindFunction(args, err); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 62 | if (!function) { |
| 63 | return false; |
| 64 | } |
| 65 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 66 | AddCommand(function, args, line); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 70 | void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) { |
| 71 | commands_.emplace_back(f, args, line); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 74 | std::size_t Action::NumCommands() const { |
| 75 | return commands_.size(); |
| 76 | } |
| 77 | |
| 78 | void Action::ExecuteOneCommand(std::size_t command) const { |
Wei Wang | d67a4ab | 2016-11-16 12:08:30 -0800 | [diff] [blame] | 79 | // We need a copy here since some Command execution may result in |
| 80 | // changing commands_ vector by importing .rc files through parser |
| 81 | Command cmd = commands_[command]; |
| 82 | ExecuteCommand(cmd); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Action::ExecuteAllCommands() const { |
| 86 | for (const auto& c : commands_) { |
| 87 | ExecuteCommand(c); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void Action::ExecuteCommand(const Command& command) const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 92 | Timer t; |
| 93 | int result = command.InvokeFunc(); |
| 94 | |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 95 | double duration_ms = t.duration_s() * 1000; |
Wei Wang | 8b1d526 | 2016-11-15 23:58:55 -0800 | [diff] [blame] | 96 | // Any action longer than 50ms will be warned to user as slow operation |
| 97 | if (duration_ms > 50.0 || |
| 98 | android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 99 | std::string trigger_name = BuildTriggersString(); |
| 100 | std::string cmd_str = command.BuildCommandString(); |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 101 | std::string source = StringPrintf(" (%s:%d)", filename_.c_str(), command.line()); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 102 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 103 | LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << source |
Wei Wang | 8b1d526 | 2016-11-15 23:58:55 -0800 | [diff] [blame] | 104 | << " returned " << result << " took " << duration_ms << "ms."; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 108 | bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 109 | const static std::string prop_str("property:"); |
| 110 | std::string prop_name(trigger.substr(prop_str.length())); |
| 111 | size_t equal_pos = prop_name.find('='); |
| 112 | if (equal_pos == std::string::npos) { |
| 113 | *err = "property trigger found without matching '='"; |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | std::string prop_value(prop_name.substr(equal_pos + 1)); |
| 118 | prop_name.erase(equal_pos); |
| 119 | |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 120 | if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 121 | *err = "multiple property triggers found for same property"; |
| 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 127 | bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 128 | const static std::string prop_str("property:"); |
| 129 | for (std::size_t i = 0; i < args.size(); ++i) { |
Wei Wang | 93df4e1 | 2016-11-16 19:19:49 -0800 | [diff] [blame] | 130 | if (args[i].empty()) { |
| 131 | *err = "empty trigger is not valid"; |
| 132 | return false; |
| 133 | } |
| 134 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 135 | if (i % 2) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 136 | if (args[i] != "&&") { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 137 | *err = "&& is the only symbol allowed to concatenate actions"; |
| 138 | return false; |
| 139 | } else { |
| 140 | continue; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (!args[i].compare(0, prop_str.length(), prop_str)) { |
| 145 | if (!ParsePropertyTrigger(args[i], err)) { |
| 146 | return false; |
| 147 | } |
| 148 | } else { |
| 149 | if (!event_trigger_.empty()) { |
| 150 | *err = "multiple event triggers are not allowed"; |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | event_trigger_ = args[i]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 161 | bool Action::InitSingleTrigger(const std::string& trigger) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 162 | std::vector<std::string> name_vector{trigger}; |
| 163 | std::string err; |
Wei Wang | 93df4e1 | 2016-11-16 19:19:49 -0800 | [diff] [blame] | 164 | bool ret = InitTriggers(name_vector, &err); |
| 165 | if (!ret) { |
| 166 | LOG(ERROR) << "InitSingleTrigger failed due to: " << err; |
| 167 | } |
| 168 | return ret; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 171 | // This function checks that all property triggers are satisfied, that is |
| 172 | // for each (name, value) in property_triggers_, check that the current |
| 173 | // value of the property 'name' == value. |
| 174 | // |
| 175 | // It takes an optional (name, value) pair, which if provided must |
| 176 | // be present in property_triggers_; it skips the check of the current |
| 177 | // property value for this pair. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 178 | bool Action::CheckPropertyTriggers(const std::string& name, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 179 | const std::string& value) const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 180 | if (property_triggers_.empty()) { |
| 181 | return true; |
| 182 | } |
| 183 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 184 | bool found = name.empty(); |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 185 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 186 | if (trigger_name == name) { |
| 187 | if (trigger_value != "*" && trigger_value != value) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 188 | return false; |
| 189 | } else { |
| 190 | found = true; |
| 191 | } |
| 192 | } else { |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 193 | std::string prop_val = android::base::GetProperty(trigger_name, ""); |
| 194 | if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | return found; |
| 200 | } |
| 201 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 202 | bool Action::CheckEvent(const EventTrigger& event_trigger) const { |
| 203 | return event_trigger == event_trigger_ && CheckPropertyTriggers(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 206 | bool Action::CheckEvent(const PropertyChange& property_change) const { |
| 207 | const auto& [name, value] = property_change; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 208 | return event_trigger_.empty() && CheckPropertyTriggers(name, value); |
| 209 | } |
| 210 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 211 | bool Action::CheckEvent(const BuiltinAction& builtin_action) const { |
| 212 | return this == builtin_action; |
| 213 | } |
| 214 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 215 | std::string Action::BuildTriggersString() const { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 216 | std::vector<std::string> triggers; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 217 | |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 218 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 219 | triggers.emplace_back(trigger_name + '=' + trigger_value); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 220 | } |
| 221 | if (!event_trigger_.empty()) { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 222 | triggers.emplace_back(event_trigger_); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 223 | } |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 224 | |
| 225 | return Join(triggers, " && "); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 228 | void Action::DumpState() const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 229 | std::string trigger_name = BuildTriggersString(); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 230 | LOG(INFO) << "on " << trigger_name; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 231 | |
| 232 | for (const auto& c : commands_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 233 | std::string cmd_str = c.BuildCommandString(); |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 234 | LOG(INFO) << " " << cmd_str; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 235 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 238 | ActionManager::ActionManager() : current_command_(0) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | ActionManager& ActionManager::GetInstance() { |
| 242 | static ActionManager instance; |
| 243 | return instance; |
| 244 | } |
| 245 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 246 | void ActionManager::AddAction(std::unique_ptr<Action> action) { |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 247 | actions_.emplace_back(std::move(action)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void ActionManager::QueueEventTrigger(const std::string& trigger) { |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 251 | event_queue_.emplace(trigger); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 254 | void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) { |
| 255 | event_queue_.emplace(std::make_pair(name, value)); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 258 | void ActionManager::QueueAllPropertyActions() { |
| 259 | QueuePropertyChange("", ""); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 262 | void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) { |
| 263 | auto action = std::make_unique<Action>(true, "<Builtin Action>", 0); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 264 | std::vector<std::string> name_vector{name}; |
| 265 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 266 | if (!action->InitSingleTrigger(name)) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 270 | action->AddCommand(func, name_vector, 0); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 271 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 272 | event_queue_.emplace(action.get()); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 273 | actions_.emplace_back(std::move(action)); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void ActionManager::ExecuteOneCommand() { |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 277 | // Loop through the event queue until we have an action to execute |
| 278 | while (current_executing_actions_.empty() && !event_queue_.empty()) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 279 | for (const auto& action : actions_) { |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 280 | if (std::visit([&action](const auto& event) { return action->CheckEvent(event); }, |
| 281 | event_queue_.front())) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 282 | current_executing_actions_.emplace(action.get()); |
| 283 | } |
| 284 | } |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 285 | event_queue_.pop(); |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (current_executing_actions_.empty()) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 289 | return; |
| 290 | } |
| 291 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 292 | auto action = current_executing_actions_.front(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 293 | |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 294 | if (current_command_ == 0) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 295 | std::string trigger_name = action->BuildTriggersString(); |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 296 | LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename() |
| 297 | << ":" << action->line() << ")"; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 300 | action->ExecuteOneCommand(current_command_); |
| 301 | |
| 302 | // If this was the last command in the current action, then remove |
| 303 | // the action from the executing list. |
| 304 | // If this action was oneshot, then also remove it from actions_. |
| 305 | ++current_command_; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 306 | if (current_command_ == action->NumCommands()) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 307 | current_executing_actions_.pop(); |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 308 | current_command_ = 0; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 309 | if (action->oneshot()) { |
| 310 | auto eraser = [&action] (std::unique_ptr<Action>& a) { |
| 311 | return a.get() == action; |
| 312 | }; |
| 313 | actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser)); |
| 314 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 318 | bool ActionManager::HasMoreCommands() const { |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 319 | return !current_executing_actions_.empty() || !event_queue_.empty(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 322 | void ActionManager::DumpState() const { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 323 | for (const auto& a : actions_) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 324 | a->DumpState(); |
| 325 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 326 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 327 | |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 328 | bool ActionParser::ParseSection(std::vector<std::string>&& args, const std::string& filename, |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 329 | int line, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 330 | std::vector<std::string> triggers(args.begin() + 1, args.end()); |
| 331 | if (triggers.size() < 1) { |
| 332 | *err = "actions must have a trigger"; |
| 333 | return false; |
| 334 | } |
| 335 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 336 | auto action = std::make_unique<Action>(false, filename, line); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 337 | if (!action->InitTriggers(triggers, err)) { |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | action_ = std::move(action); |
| 342 | return true; |
| 343 | } |
| 344 | |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 345 | bool ActionParser::ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) { |
| 346 | return action_ ? action_->AddCommand(std::move(args), line, err) : false; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void ActionParser::EndSection() { |
| 350 | if (action_ && action_->NumCommands() > 0) { |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 351 | action_manager_->AddAction(std::move(action_)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 352 | } |
| 353 | } |