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