Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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_parser.h" |
| 18 | |
Elliott Hughes | dc80312 | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 19 | #include <android-base/properties.h> |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 20 | #include <android-base/strings.h> |
| 21 | |
Tom Cherry | b35f827 | 2018-10-22 14:50:52 -0700 | [diff] [blame^] | 22 | #if defined(__ANDROID__) |
| 23 | #include "property_service.h" |
| 24 | #else |
| 25 | #include "host_init_stubs.h" |
| 26 | #endif |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 27 | |
| 28 | using android::base::GetBoolProperty; |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 29 | using android::base::StartsWith; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace init { |
| 33 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | bool IsActionableProperty(Subcontext* subcontext, const std::string& prop_name) { |
| 37 | static bool enabled = GetBoolProperty("ro.actionable_compatible_property.enabled", false); |
| 38 | |
| 39 | if (subcontext == nullptr || !enabled) { |
| 40 | return true; |
| 41 | } |
| 42 | |
Tom Cherry | b35f827 | 2018-10-22 14:50:52 -0700 | [diff] [blame^] | 43 | return CanReadProperty(subcontext->context(), prop_name); |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | Result<Success> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext, |
| 47 | std::map<std::string, std::string>* property_triggers) { |
| 48 | const static std::string prop_str("property:"); |
| 49 | std::string prop_name(trigger.substr(prop_str.length())); |
| 50 | size_t equal_pos = prop_name.find('='); |
| 51 | if (equal_pos == std::string::npos) { |
| 52 | return Error() << "property trigger found without matching '='"; |
| 53 | } |
| 54 | |
| 55 | std::string prop_value(prop_name.substr(equal_pos + 1)); |
| 56 | prop_name.erase(equal_pos); |
| 57 | |
| 58 | if (!IsActionableProperty(subcontext, prop_name)) { |
Tom Cherry | 8c901dd | 2018-07-24 15:54:33 -0700 | [diff] [blame] | 59 | return Error() << "unexported property trigger found: " << prop_name; |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if (auto [it, inserted] = property_triggers->emplace(prop_name, prop_value); !inserted) { |
| 63 | return Error() << "multiple property triggers found for same property"; |
| 64 | } |
| 65 | return Success(); |
| 66 | } |
| 67 | |
| 68 | Result<Success> ParseTriggers(const std::vector<std::string>& args, Subcontext* subcontext, |
| 69 | std::string* event_trigger, |
| 70 | std::map<std::string, std::string>* property_triggers) { |
| 71 | const static std::string prop_str("property:"); |
| 72 | for (std::size_t i = 0; i < args.size(); ++i) { |
| 73 | if (args[i].empty()) { |
| 74 | return Error() << "empty trigger is not valid"; |
| 75 | } |
| 76 | |
| 77 | if (i % 2) { |
| 78 | if (args[i] != "&&") { |
| 79 | return Error() << "&& is the only symbol allowed to concatenate actions"; |
| 80 | } else { |
| 81 | continue; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (!args[i].compare(0, prop_str.length(), prop_str)) { |
| 86 | if (auto result = ParsePropertyTrigger(args[i], subcontext, property_triggers); |
| 87 | !result) { |
| 88 | return result; |
| 89 | } |
| 90 | } else { |
| 91 | if (!event_trigger->empty()) { |
| 92 | return Error() << "multiple event triggers are not allowed"; |
| 93 | } |
| 94 | |
| 95 | *event_trigger = args[i]; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return Success(); |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 104 | Result<Success> ActionParser::ParseSection(std::vector<std::string>&& args, |
| 105 | const std::string& filename, int line) { |
| 106 | std::vector<std::string> triggers(args.begin() + 1, args.end()); |
| 107 | if (triggers.size() < 1) { |
| 108 | return Error() << "Actions must have a trigger"; |
| 109 | } |
| 110 | |
| 111 | Subcontext* action_subcontext = nullptr; |
| 112 | if (subcontexts_) { |
| 113 | for (auto& subcontext : *subcontexts_) { |
| 114 | if (StartsWith(filename, subcontext.path_prefix())) { |
| 115 | action_subcontext = &subcontext; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 121 | std::string event_trigger; |
| 122 | std::map<std::string, std::string> property_triggers; |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 123 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 124 | if (auto result = ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers); |
| 125 | !result) { |
| 126 | return Error() << "ParseTriggers() failed: " << result.error(); |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 129 | auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger, |
| 130 | property_triggers); |
| 131 | |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 132 | action_ = std::move(action); |
| 133 | return Success(); |
| 134 | } |
| 135 | |
| 136 | Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) { |
| 137 | return action_ ? action_->AddCommand(std::move(args), line) : Success(); |
| 138 | } |
| 139 | |
| 140 | Result<Success> ActionParser::EndSection() { |
| 141 | if (action_ && action_->NumCommands() > 0) { |
| 142 | action_manager_->AddAction(std::move(action_)); |
| 143 | } |
| 144 | |
| 145 | return Success(); |
| 146 | } |
| 147 | |
| 148 | } // namespace init |
| 149 | } // namespace android |