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