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