blob: 8a4b518f5feb2c41d3b682ac67472cc89336e3c2 [file] [log] [blame]
Tom Cherry0f6417f2018-02-13 15:25:29 -08001/*
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
Tom Cherry9cbf5702018-02-13 16:24:51 -080019#include <android-base/properties.h>
Tom Cherry0f6417f2018-02-13 15:25:29 -080020#include <android-base/strings.h>
21
Tom Cherry9cbf5702018-02-13 16:24:51 -080022#include "stable_properties.h"
23
24using android::base::GetBoolProperty;
Tom Cherry0f6417f2018-02-13 15:25:29 -080025using android::base::StartsWith;
26
27namespace android {
28namespace init {
29
Tom Cherry9cbf5702018-02-13 16:24:51 -080030namespace {
31
32bool IsActionableProperty(Subcontext* subcontext, const std::string& prop_name) {
33 static bool enabled = GetBoolProperty("ro.actionable_compatible_property.enabled", false);
34
35 if (subcontext == nullptr || !enabled) {
36 return true;
37 }
38
39 if (kExportedActionableProperties.count(prop_name) == 1) {
40 return true;
41 }
42 for (const auto& prefix : kPartnerPrefixes) {
43 if (android::base::StartsWith(prop_name, prefix)) {
44 return true;
45 }
46 }
47 return false;
48}
49
50Result<Success> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext,
51 std::map<std::string, std::string>* property_triggers) {
52 const static std::string prop_str("property:");
53 std::string prop_name(trigger.substr(prop_str.length()));
54 size_t equal_pos = prop_name.find('=');
55 if (equal_pos == std::string::npos) {
56 return Error() << "property trigger found without matching '='";
57 }
58
59 std::string prop_value(prop_name.substr(equal_pos + 1));
60 prop_name.erase(equal_pos);
61
62 if (!IsActionableProperty(subcontext, prop_name)) {
63 return Error() << "unexported property tigger found: " << prop_name;
64 }
65
66 if (auto [it, inserted] = property_triggers->emplace(prop_name, prop_value); !inserted) {
67 return Error() << "multiple property triggers found for same property";
68 }
69 return Success();
70}
71
72Result<Success> ParseTriggers(const std::vector<std::string>& args, Subcontext* subcontext,
73 std::string* event_trigger,
74 std::map<std::string, std::string>* property_triggers) {
75 const static std::string prop_str("property:");
76 for (std::size_t i = 0; i < args.size(); ++i) {
77 if (args[i].empty()) {
78 return Error() << "empty trigger is not valid";
79 }
80
81 if (i % 2) {
82 if (args[i] != "&&") {
83 return Error() << "&& is the only symbol allowed to concatenate actions";
84 } else {
85 continue;
86 }
87 }
88
89 if (!args[i].compare(0, prop_str.length(), prop_str)) {
90 if (auto result = ParsePropertyTrigger(args[i], subcontext, property_triggers);
91 !result) {
92 return result;
93 }
94 } else {
95 if (!event_trigger->empty()) {
96 return Error() << "multiple event triggers are not allowed";
97 }
98
99 *event_trigger = args[i];
100 }
101 }
102
103 return Success();
104}
105
106} // namespace
107
Tom Cherry0f6417f2018-02-13 15:25:29 -0800108Result<Success> ActionParser::ParseSection(std::vector<std::string>&& args,
109 const std::string& filename, int line) {
110 std::vector<std::string> triggers(args.begin() + 1, args.end());
111 if (triggers.size() < 1) {
112 return Error() << "Actions must have a trigger";
113 }
114
115 Subcontext* action_subcontext = nullptr;
116 if (subcontexts_) {
117 for (auto& subcontext : *subcontexts_) {
118 if (StartsWith(filename, subcontext.path_prefix())) {
119 action_subcontext = &subcontext;
120 break;
121 }
122 }
123 }
124
Tom Cherry9cbf5702018-02-13 16:24:51 -0800125 std::string event_trigger;
126 std::map<std::string, std::string> property_triggers;
Tom Cherry0f6417f2018-02-13 15:25:29 -0800127
Tom Cherry9cbf5702018-02-13 16:24:51 -0800128 if (auto result = ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers);
129 !result) {
130 return Error() << "ParseTriggers() failed: " << result.error();
Tom Cherry0f6417f2018-02-13 15:25:29 -0800131 }
132
Tom Cherry9cbf5702018-02-13 16:24:51 -0800133 auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger,
134 property_triggers);
135
Tom Cherry0f6417f2018-02-13 15:25:29 -0800136 action_ = std::move(action);
137 return Success();
138}
139
140Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) {
141 return action_ ? action_->AddCommand(std::move(args), line) : Success();
142}
143
144Result<Success> ActionParser::EndSection() {
145 if (action_ && action_->NumCommands() > 0) {
146 action_manager_->AddAction(std::move(action_));
147 }
148
149 return Success();
150}
151
152} // namespace init
153} // namespace android