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 | |
| 61 | if (args.empty()) { |
| 62 | *err = "command needed, but not provided"; |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | auto function = function_map_->FindFunction(args[0], args.size() - 1, err); |
| 67 | if (!function) { |
| 68 | return false; |
| 69 | } |
| 70 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 71 | AddCommand(function, args, line); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 72 | return true; |
| 73 | } |
| 74 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 75 | void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) { |
| 76 | commands_.emplace_back(f, args, line); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 79 | std::size_t Action::NumCommands() const { |
| 80 | return commands_.size(); |
| 81 | } |
| 82 | |
| 83 | void Action::ExecuteOneCommand(std::size_t command) const { |
Wei Wang | d67a4ab | 2016-11-16 12:08:30 -0800 | [diff] [blame] | 84 | // We need a copy here since some Command execution may result in |
| 85 | // changing commands_ vector by importing .rc files through parser |
| 86 | Command cmd = commands_[command]; |
| 87 | ExecuteCommand(cmd); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void Action::ExecuteAllCommands() const { |
| 91 | for (const auto& c : commands_) { |
| 92 | ExecuteCommand(c); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void Action::ExecuteCommand(const Command& command) const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 97 | Timer t; |
| 98 | int result = command.InvokeFunc(); |
| 99 | |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 100 | double duration_ms = t.duration_s() * 1000; |
Wei Wang | 8b1d526 | 2016-11-15 23:58:55 -0800 | [diff] [blame] | 101 | // Any action longer than 50ms will be warned to user as slow operation |
| 102 | if (duration_ms > 50.0 || |
| 103 | android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 104 | std::string trigger_name = BuildTriggersString(); |
| 105 | std::string cmd_str = command.BuildCommandString(); |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 106 | std::string source = StringPrintf(" (%s:%d)", filename_.c_str(), command.line()); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 107 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 108 | LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << source |
Wei Wang | 8b1d526 | 2016-11-15 23:58:55 -0800 | [diff] [blame] | 109 | << " returned " << result << " took " << duration_ms << "ms."; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 113 | bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 114 | const static std::string prop_str("property:"); |
| 115 | std::string prop_name(trigger.substr(prop_str.length())); |
| 116 | size_t equal_pos = prop_name.find('='); |
| 117 | if (equal_pos == std::string::npos) { |
| 118 | *err = "property trigger found without matching '='"; |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | std::string prop_value(prop_name.substr(equal_pos + 1)); |
| 123 | prop_name.erase(equal_pos); |
| 124 | |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 125 | if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 126 | *err = "multiple property triggers found for same property"; |
| 127 | return false; |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 132 | bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 133 | const static std::string prop_str("property:"); |
| 134 | for (std::size_t i = 0; i < args.size(); ++i) { |
Wei Wang | 93df4e1 | 2016-11-16 19:19:49 -0800 | [diff] [blame] | 135 | if (args[i].empty()) { |
| 136 | *err = "empty trigger is not valid"; |
| 137 | return false; |
| 138 | } |
| 139 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 140 | if (i % 2) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 141 | if (args[i] != "&&") { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 142 | *err = "&& is the only symbol allowed to concatenate actions"; |
| 143 | return false; |
| 144 | } else { |
| 145 | continue; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (!args[i].compare(0, prop_str.length(), prop_str)) { |
| 150 | if (!ParsePropertyTrigger(args[i], err)) { |
| 151 | return false; |
| 152 | } |
| 153 | } else { |
| 154 | if (!event_trigger_.empty()) { |
| 155 | *err = "multiple event triggers are not allowed"; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | event_trigger_ = args[i]; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 166 | bool Action::InitSingleTrigger(const std::string& trigger) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 167 | std::vector<std::string> name_vector{trigger}; |
| 168 | std::string err; |
Wei Wang | 93df4e1 | 2016-11-16 19:19:49 -0800 | [diff] [blame] | 169 | bool ret = InitTriggers(name_vector, &err); |
| 170 | if (!ret) { |
| 171 | LOG(ERROR) << "InitSingleTrigger failed due to: " << err; |
| 172 | } |
| 173 | return ret; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 176 | // This function checks that all property triggers are satisfied, that is |
| 177 | // for each (name, value) in property_triggers_, check that the current |
| 178 | // value of the property 'name' == value. |
| 179 | // |
| 180 | // It takes an optional (name, value) pair, which if provided must |
| 181 | // be present in property_triggers_; it skips the check of the current |
| 182 | // property value for this pair. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 183 | bool Action::CheckPropertyTriggers(const std::string& name, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 184 | const std::string& value) const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 185 | if (property_triggers_.empty()) { |
| 186 | return true; |
| 187 | } |
| 188 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 189 | bool found = name.empty(); |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 190 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 191 | if (trigger_name == name) { |
| 192 | if (trigger_value != "*" && trigger_value != value) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 193 | return false; |
| 194 | } else { |
| 195 | found = true; |
| 196 | } |
| 197 | } else { |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 198 | std::string prop_val = android::base::GetProperty(trigger_name, ""); |
| 199 | if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | return found; |
| 205 | } |
| 206 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 207 | bool Action::CheckEventTrigger(const std::string& trigger) const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 208 | return !event_trigger_.empty() && |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 209 | trigger == event_trigger_ && |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 210 | CheckPropertyTriggers(); |
| 211 | } |
| 212 | |
| 213 | bool Action::CheckPropertyTrigger(const std::string& name, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 214 | const std::string& value) const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 215 | return event_trigger_.empty() && CheckPropertyTriggers(name, value); |
| 216 | } |
| 217 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 218 | std::string Action::BuildTriggersString() const { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 219 | std::vector<std::string> triggers; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 220 | |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 221 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 222 | triggers.emplace_back(trigger_name + '=' + trigger_value); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 223 | } |
| 224 | if (!event_trigger_.empty()) { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 225 | triggers.emplace_back(event_trigger_); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 226 | } |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 227 | |
| 228 | return Join(triggers, " && "); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 231 | void Action::DumpState() const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 232 | std::string trigger_name = BuildTriggersString(); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 233 | LOG(INFO) << "on " << trigger_name; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 234 | |
| 235 | for (const auto& c : commands_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 236 | std::string cmd_str = c.BuildCommandString(); |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 237 | LOG(INFO) << " " << cmd_str; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 238 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 241 | class EventTrigger : public Trigger { |
| 242 | public: |
Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 243 | explicit EventTrigger(const std::string& trigger) : trigger_(trigger) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 244 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 245 | bool CheckTriggers(const Action& action) const override { |
| 246 | return action.CheckEventTrigger(trigger_); |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 247 | } |
| 248 | private: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 249 | const std::string trigger_; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | class PropertyTrigger : public Trigger { |
| 253 | public: |
| 254 | PropertyTrigger(const std::string& name, const std::string& value) |
| 255 | : name_(name), value_(value) { |
| 256 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 257 | bool CheckTriggers(const Action& action) const override { |
| 258 | return action.CheckPropertyTrigger(name_, value_); |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 259 | } |
| 260 | private: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 261 | const std::string name_; |
| 262 | const std::string value_; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | class BuiltinTrigger : public Trigger { |
| 266 | public: |
Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 267 | explicit BuiltinTrigger(Action* action) : action_(action) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 268 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 269 | bool CheckTriggers(const Action& action) const override { |
| 270 | return action_ == &action; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 271 | } |
| 272 | private: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 273 | const Action* action_; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 276 | ActionManager::ActionManager() : current_command_(0) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | ActionManager& ActionManager::GetInstance() { |
| 280 | static ActionManager instance; |
| 281 | return instance; |
| 282 | } |
| 283 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 284 | void ActionManager::AddAction(std::unique_ptr<Action> action) { |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 285 | actions_.emplace_back(std::move(action)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void ActionManager::QueueEventTrigger(const std::string& trigger) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 289 | trigger_queue_.push(std::make_unique<EventTrigger>(trigger)); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void ActionManager::QueuePropertyTrigger(const std::string& name, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 293 | const std::string& value) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 294 | trigger_queue_.push(std::make_unique<PropertyTrigger>(name, value)); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 297 | void ActionManager::QueueAllPropertyTriggers() { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 298 | QueuePropertyTrigger("", ""); |
| 299 | } |
| 300 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 301 | void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) { |
| 302 | auto action = std::make_unique<Action>(true, "<Builtin Action>", 0); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 303 | std::vector<std::string> name_vector{name}; |
| 304 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 305 | if (!action->InitSingleTrigger(name)) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 306 | return; |
| 307 | } |
| 308 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 309 | action->AddCommand(func, name_vector, 0); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 310 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 311 | trigger_queue_.push(std::make_unique<BuiltinTrigger>(action.get())); |
| 312 | actions_.emplace_back(std::move(action)); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void ActionManager::ExecuteOneCommand() { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 316 | // Loop through the trigger queue until we have an action to execute |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 317 | while (current_executing_actions_.empty() && !trigger_queue_.empty()) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 318 | for (const auto& action : actions_) { |
| 319 | if (trigger_queue_.front()->CheckTriggers(*action)) { |
| 320 | current_executing_actions_.emplace(action.get()); |
| 321 | } |
| 322 | } |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 323 | trigger_queue_.pop(); |
| 324 | } |
| 325 | |
| 326 | if (current_executing_actions_.empty()) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 330 | auto action = current_executing_actions_.front(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 331 | |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 332 | if (current_command_ == 0) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 333 | std::string trigger_name = action->BuildTriggersString(); |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 334 | LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename() |
| 335 | << ":" << action->line() << ")"; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 338 | action->ExecuteOneCommand(current_command_); |
| 339 | |
| 340 | // If this was the last command in the current action, then remove |
| 341 | // the action from the executing list. |
| 342 | // If this action was oneshot, then also remove it from actions_. |
| 343 | ++current_command_; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 344 | if (current_command_ == action->NumCommands()) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 345 | current_executing_actions_.pop(); |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 346 | current_command_ = 0; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 347 | if (action->oneshot()) { |
| 348 | auto eraser = [&action] (std::unique_ptr<Action>& a) { |
| 349 | return a.get() == action; |
| 350 | }; |
| 351 | actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser)); |
| 352 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 356 | bool ActionManager::HasMoreCommands() const { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 357 | return !current_executing_actions_.empty() || !trigger_queue_.empty(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 360 | void ActionManager::DumpState() const { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 361 | for (const auto& a : actions_) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 362 | a->DumpState(); |
| 363 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 364 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 365 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 366 | bool ActionParser::ParseSection(const std::vector<std::string>& args, const std::string& filename, |
| 367 | int line, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 368 | std::vector<std::string> triggers(args.begin() + 1, args.end()); |
| 369 | if (triggers.size() < 1) { |
| 370 | *err = "actions must have a trigger"; |
| 371 | return false; |
| 372 | } |
| 373 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 374 | auto action = std::make_unique<Action>(false, filename, line); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 375 | if (!action->InitTriggers(triggers, err)) { |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | action_ = std::move(action); |
| 380 | return true; |
| 381 | } |
| 382 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame^] | 383 | bool ActionParser::ParseLineSection(const std::vector<std::string>& args, int line, |
| 384 | std::string* err) { |
| 385 | return action_ ? action_->AddCommand(args, line, err) : false; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void ActionParser::EndSection() { |
| 389 | if (action_ && action_->NumCommands() > 0) { |
| 390 | ActionManager::GetInstance().AddAction(std::move(action_)); |
| 391 | } |
| 392 | } |