| 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 | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 19 | #include <android-base/chrono_utils.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 20 | #include <android-base/logging.h> | 
| Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 21 | #include <android-base/properties.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; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 27 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 28 | namespace android { | 
|  | 29 | namespace init { | 
|  | 30 |  | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 31 | Command::Command(BuiltinFunction f, const std::vector<std::string>& args, int line) | 
|  | 32 | : func_(f), args_(args), line_(line) {} | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 33 |  | 
| Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 34 | Result<Success> 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])) { | 
| Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 40 | return Error() << "cannot expand '" << args_[i] << "'"; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 41 | } | 
|  | 42 | } | 
|  | 43 |  | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 44 | return func_(expanded_args); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 47 | std::string Command::BuildCommandString() const { | 
|  | 48 | return Join(args_, ' '); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 51 | Action::Action(bool oneshot, const std::string& filename, int line) | 
|  | 52 | : oneshot_(oneshot), filename_(filename), line_(line) {} | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 53 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 54 | const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr; | 
|  | 55 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 56 | Result<Success> Action::AddCommand(const std::vector<std::string>& args, int line) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 57 | if (!function_map_) { | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 58 | return Error() << "no function map available"; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 61 | auto function = function_map_->FindFunction(args); | 
|  | 62 | if (!function) return Error() << function.error(); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 63 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 64 | AddCommand(*function, args, line); | 
|  | 65 | return Success(); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 68 | void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) { | 
|  | 69 | commands_.emplace_back(f, args, line); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 72 | std::size_t Action::NumCommands() const { | 
|  | 73 | return commands_.size(); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | void Action::ExecuteOneCommand(std::size_t command) const { | 
| Wei Wang | d67a4ab | 2016-11-16 12:08:30 -0800 | [diff] [blame] | 77 | // We need a copy here since some Command execution may result in | 
|  | 78 | // changing commands_ vector by importing .rc files through parser | 
|  | 79 | Command cmd = commands_[command]; | 
|  | 80 | ExecuteCommand(cmd); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 83 | void Action::ExecuteAllCommands() const { | 
|  | 84 | for (const auto& c : commands_) { | 
|  | 85 | ExecuteCommand(c); | 
|  | 86 | } | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | void Action::ExecuteCommand(const Command& command) const { | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 90 | android::base::Timer t; | 
| Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 91 | auto result = command.InvokeFunc(); | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 92 | auto duration = t.duration(); | 
| Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 93 |  | 
| Tom Cherry | 68f2a46 | 2017-08-22 16:15:26 -0700 | [diff] [blame] | 94 | // There are many legacy paths in rootdir/init.rc that will virtually never exist on a new | 
|  | 95 | // device, such as '/sys/class/leds/jogball-backlight/brightness'.  As of this writing, there | 
|  | 96 | // are 198 such failures on bullhead.  Instead of spamming the log reporting them, we do not | 
|  | 97 | // report such failures unless we're running at the DEBUG log level. | 
|  | 98 | bool report_failure = !result.has_value(); | 
|  | 99 | if (report_failure && android::base::GetMinimumLogSeverity() > android::base::DEBUG && | 
|  | 100 | result.error_errno() == ENOENT) { | 
|  | 101 | report_failure = false; | 
|  | 102 | } | 
|  | 103 |  | 
| Wei Wang | 8b1d526 | 2016-11-15 23:58:55 -0800 | [diff] [blame] | 104 | // Any action longer than 50ms will be warned to user as slow operation | 
| Tom Cherry | 68f2a46 | 2017-08-22 16:15:26 -0700 | [diff] [blame] | 105 | if (report_failure || duration > 50ms || | 
|  | 106 | android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 107 | std::string trigger_name = BuildTriggersString(); | 
|  | 108 | std::string cmd_str = command.BuildCommandString(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 109 |  | 
| Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 110 | LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_ | 
| Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 111 | << ":" << command.line() << ") took " << duration.count() << "ms and " | 
| Tom Cherry | 130e3d7 | 2017-08-22 16:07:15 -0700 | [diff] [blame] | 112 | << (result ? "succeeded" : "failed: " + result.error_string()); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 113 | } | 
|  | 114 | } | 
|  | 115 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 116 | Result<Success> Action::ParsePropertyTrigger(const std::string& trigger) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 117 | const static std::string prop_str("property:"); | 
|  | 118 | std::string prop_name(trigger.substr(prop_str.length())); | 
|  | 119 | size_t equal_pos = prop_name.find('='); | 
|  | 120 | if (equal_pos == std::string::npos) { | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 121 | return Error() << "property trigger found without matching '='"; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
|  | 124 | std::string prop_value(prop_name.substr(equal_pos + 1)); | 
|  | 125 | prop_name.erase(equal_pos); | 
|  | 126 |  | 
| Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 127 | if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) { | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 128 | return Error() << "multiple property triggers found for same property"; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 129 | } | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 130 | return Success(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 133 | Result<Success> Action::InitTriggers(const std::vector<std::string>& args) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 134 | const static std::string prop_str("property:"); | 
|  | 135 | for (std::size_t i = 0; i < args.size(); ++i) { | 
| Wei Wang | 93df4e1 | 2016-11-16 19:19:49 -0800 | [diff] [blame] | 136 | if (args[i].empty()) { | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 137 | return Error() << "empty trigger is not valid"; | 
| Wei Wang | 93df4e1 | 2016-11-16 19:19:49 -0800 | [diff] [blame] | 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 | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 142 | return Error() << "&& is the only symbol allowed to concatenate actions"; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 143 | } else { | 
|  | 144 | continue; | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | if (!args[i].compare(0, prop_str.length(), prop_str)) { | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 149 | if (auto result = ParsePropertyTrigger(args[i]); !result) { | 
|  | 150 | return result; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 151 | } | 
|  | 152 | } else { | 
|  | 153 | if (!event_trigger_.empty()) { | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 154 | return Error() << "multiple event triggers are not allowed"; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
|  | 157 | event_trigger_ = args[i]; | 
|  | 158 | } | 
|  | 159 | } | 
|  | 160 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 161 | return Success(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 164 | Result<Success> Action::InitSingleTrigger(const std::string& trigger) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 165 | std::vector<std::string> name_vector{trigger}; | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 166 | if (auto result = InitTriggers(name_vector); !result) { | 
|  | 167 | return Error() << "InitTriggers() failed: " << result.error(); | 
| Wei Wang | 93df4e1 | 2016-11-16 19:19:49 -0800 | [diff] [blame] | 168 | } | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 169 | return Success(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 172 | // This function checks that all property triggers are satisfied, that is | 
|  | 173 | // for each (name, value) in property_triggers_, check that the current | 
|  | 174 | // value of the property 'name' == value. | 
|  | 175 | // | 
|  | 176 | // It takes an optional (name, value) pair, which if provided must | 
|  | 177 | // be present in property_triggers_; it skips the check of the current | 
|  | 178 | // property value for this pair. | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 179 | bool Action::CheckPropertyTriggers(const std::string& name, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 180 | const std::string& value) const { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 181 | if (property_triggers_.empty()) { | 
|  | 182 | return true; | 
|  | 183 | } | 
|  | 184 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 185 | bool found = name.empty(); | 
| Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 186 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { | 
| Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 187 | if (trigger_name == name) { | 
|  | 188 | if (trigger_value != "*" && trigger_value != value) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 189 | return false; | 
|  | 190 | } else { | 
|  | 191 | found = true; | 
|  | 192 | } | 
|  | 193 | } else { | 
| Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 194 | std::string prop_val = android::base::GetProperty(trigger_name, ""); | 
|  | 195 | if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 196 | return false; | 
|  | 197 | } | 
|  | 198 | } | 
|  | 199 | } | 
|  | 200 | return found; | 
|  | 201 | } | 
|  | 202 |  | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 203 | bool Action::CheckEvent(const EventTrigger& event_trigger) const { | 
|  | 204 | return event_trigger == event_trigger_ && CheckPropertyTriggers(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 205 | } | 
|  | 206 |  | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 207 | bool Action::CheckEvent(const PropertyChange& property_change) const { | 
|  | 208 | const auto& [name, value] = property_change; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 209 | return event_trigger_.empty() && CheckPropertyTriggers(name, value); | 
|  | 210 | } | 
|  | 211 |  | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 212 | bool Action::CheckEvent(const BuiltinAction& builtin_action) const { | 
|  | 213 | return this == builtin_action; | 
|  | 214 | } | 
|  | 215 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 216 | std::string Action::BuildTriggersString() const { | 
| Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 217 | std::vector<std::string> triggers; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 218 |  | 
| Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 219 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { | 
| Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 220 | triggers.emplace_back(trigger_name + '=' + trigger_value); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 221 | } | 
|  | 222 | if (!event_trigger_.empty()) { | 
| Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 223 | triggers.emplace_back(event_trigger_); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 224 | } | 
| Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 225 |  | 
|  | 226 | return Join(triggers, " && "); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 229 | void Action::DumpState() const { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 230 | std::string trigger_name = BuildTriggersString(); | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 231 | LOG(INFO) << "on " << trigger_name; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 232 |  | 
|  | 233 | for (const auto& c : commands_) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 234 | std::string cmd_str = c.BuildCommandString(); | 
| Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 235 | LOG(INFO) << "  " << cmd_str; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 236 | } | 
| 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 | ActionManager::ActionManager() : current_command_(0) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 240 | } | 
|  | 241 |  | 
|  | 242 | ActionManager& ActionManager::GetInstance() { | 
|  | 243 | static ActionManager instance; | 
|  | 244 | return instance; | 
|  | 245 | } | 
|  | 246 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 247 | void ActionManager::AddAction(std::unique_ptr<Action> action) { | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 248 | actions_.emplace_back(std::move(action)); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
|  | 251 | void ActionManager::QueueEventTrigger(const std::string& trigger) { | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 252 | event_queue_.emplace(trigger); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 253 | } | 
|  | 254 |  | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 255 | void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) { | 
|  | 256 | event_queue_.emplace(std::make_pair(name, value)); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 259 | void ActionManager::QueueAllPropertyActions() { | 
|  | 260 | QueuePropertyChange("", ""); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 263 | void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) { | 
|  | 264 | auto action = std::make_unique<Action>(true, "<Builtin Action>", 0); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 265 | std::vector<std::string> name_vector{name}; | 
|  | 266 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 267 | if (auto result = action->InitSingleTrigger(name); !result) { | 
|  | 268 | LOG(ERROR) << "Cannot queue BuiltinAction for " << name << ": " << result.error(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 269 | return; | 
|  | 270 | } | 
|  | 271 |  | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 272 | action->AddCommand(func, name_vector, 0); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 273 |  | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 274 | event_queue_.emplace(action.get()); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 275 | actions_.emplace_back(std::move(action)); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
|  | 278 | void ActionManager::ExecuteOneCommand() { | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 279 | // Loop through the event queue until we have an action to execute | 
|  | 280 | while (current_executing_actions_.empty() && !event_queue_.empty()) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 281 | for (const auto& action : actions_) { | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 282 | if (std::visit([&action](const auto& event) { return action->CheckEvent(event); }, | 
|  | 283 | event_queue_.front())) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 284 | current_executing_actions_.emplace(action.get()); | 
|  | 285 | } | 
|  | 286 | } | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 287 | event_queue_.pop(); | 
| Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 288 | } | 
|  | 289 |  | 
|  | 290 | if (current_executing_actions_.empty()) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 291 | return; | 
|  | 292 | } | 
|  | 293 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 294 | auto action = current_executing_actions_.front(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 295 |  | 
| Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 296 | if (current_command_ == 0) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 297 | std::string trigger_name = action->BuildTriggersString(); | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 298 | LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename() | 
|  | 299 | << ":" << action->line() << ")"; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 300 | } | 
|  | 301 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 302 | action->ExecuteOneCommand(current_command_); | 
|  | 303 |  | 
|  | 304 | // If this was the last command in the current action, then remove | 
|  | 305 | // the action from the executing list. | 
|  | 306 | // If this action was oneshot, then also remove it from actions_. | 
|  | 307 | ++current_command_; | 
| Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 308 | if (current_command_ == action->NumCommands()) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 309 | current_executing_actions_.pop(); | 
| Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 310 | current_command_ = 0; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 311 | if (action->oneshot()) { | 
|  | 312 | auto eraser = [&action] (std::unique_ptr<Action>& a) { | 
|  | 313 | return a.get() == action; | 
|  | 314 | }; | 
|  | 315 | actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser)); | 
|  | 316 | } | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 317 | } | 
|  | 318 | } | 
|  | 319 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 320 | bool ActionManager::HasMoreCommands() const { | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 321 | return !current_executing_actions_.empty() || !event_queue_.empty(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 322 | } | 
|  | 323 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 324 | void ActionManager::DumpState() const { | 
| Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 325 | for (const auto& a : actions_) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 326 | a->DumpState(); | 
|  | 327 | } | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 328 | } | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 329 |  | 
| Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 330 | void ActionManager::ClearQueue() { | 
|  | 331 | // We are shutting down so don't claim the oneshot builtin actions back | 
|  | 332 | current_executing_actions_ = {}; | 
|  | 333 | event_queue_ = {}; | 
|  | 334 | current_command_ = 0; | 
|  | 335 | } | 
|  | 336 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 337 | Result<Success> ActionParser::ParseSection(std::vector<std::string>&& args, | 
|  | 338 | const std::string& filename, int line) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 339 | std::vector<std::string> triggers(args.begin() + 1, args.end()); | 
|  | 340 | if (triggers.size() < 1) { | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 341 | return Error() << "Actions must have a trigger"; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 342 | } | 
|  | 343 |  | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 344 | auto action = std::make_unique<Action>(false, filename, line); | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 345 |  | 
|  | 346 | if (auto result = action->InitTriggers(triggers); !result) { | 
|  | 347 | return Error() << "InitTriggers() failed: " << result.error(); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 348 | } | 
|  | 349 |  | 
|  | 350 | action_ = std::move(action); | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 351 | return Success(); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 352 | } | 
|  | 353 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 354 | Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) { | 
|  | 355 | return action_ ? action_->AddCommand(std::move(args), line) : Success(); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
|  | 358 | void ActionParser::EndSection() { | 
|  | 359 | if (action_ && action_->NumCommands() > 0) { | 
| Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 360 | action_manager_->AddAction(std::move(action_)); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 361 | } | 
|  | 362 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 363 |  | 
|  | 364 | }  // namespace init | 
|  | 365 | }  // namespace android |