blob: f9b96eb5e83f145b03deadcc89d9528716d0a71e [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
Elliott Hughesdc803122018-05-24 18:00:39 -070019#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
Elliott Hughesdc803122018-05-24 18:00:39 -070024#if !defined(__ANDROID__)
Tom Cherryde6bd502018-02-13 16:50:08 -080025#include "host_init_stubs.h"
26#endif
27
Tom Cherry9cbf5702018-02-13 16:24:51 -080028using android::base::GetBoolProperty;
Tom Cherry0f6417f2018-02-13 15:25:29 -080029using android::base::StartsWith;
30
31namespace android {
32namespace init {
33
Tom Cherry9cbf5702018-02-13 16:24:51 -080034namespace {
35
36bool 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
43 if (kExportedActionableProperties.count(prop_name) == 1) {
44 return true;
45 }
46 for (const auto& prefix : kPartnerPrefixes) {
47 if (android::base::StartsWith(prop_name, prefix)) {
48 return true;
49 }
50 }
51 return false;
52}
53
54Result<Success> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext,
55 std::map<std::string, std::string>* property_triggers) {
56 const static std::string prop_str("property:");
57 std::string prop_name(trigger.substr(prop_str.length()));
58 size_t equal_pos = prop_name.find('=');
59 if (equal_pos == std::string::npos) {
60 return Error() << "property trigger found without matching '='";
61 }
62
63 std::string prop_value(prop_name.substr(equal_pos + 1));
64 prop_name.erase(equal_pos);
65
66 if (!IsActionableProperty(subcontext, prop_name)) {
67 return Error() << "unexported property tigger found: " << prop_name;
68 }
69
70 if (auto [it, inserted] = property_triggers->emplace(prop_name, prop_value); !inserted) {
71 return Error() << "multiple property triggers found for same property";
72 }
73 return Success();
74}
75
76Result<Success> ParseTriggers(const std::vector<std::string>& args, Subcontext* subcontext,
77 std::string* event_trigger,
78 std::map<std::string, std::string>* property_triggers) {
79 const static std::string prop_str("property:");
80 for (std::size_t i = 0; i < args.size(); ++i) {
81 if (args[i].empty()) {
82 return Error() << "empty trigger is not valid";
83 }
84
85 if (i % 2) {
86 if (args[i] != "&&") {
87 return Error() << "&& is the only symbol allowed to concatenate actions";
88 } else {
89 continue;
90 }
91 }
92
93 if (!args[i].compare(0, prop_str.length(), prop_str)) {
94 if (auto result = ParsePropertyTrigger(args[i], subcontext, property_triggers);
95 !result) {
96 return result;
97 }
98 } else {
99 if (!event_trigger->empty()) {
100 return Error() << "multiple event triggers are not allowed";
101 }
102
103 *event_trigger = args[i];
104 }
105 }
106
107 return Success();
108}
109
110} // namespace
111
Tom Cherry0f6417f2018-02-13 15:25:29 -0800112Result<Success> ActionParser::ParseSection(std::vector<std::string>&& args,
113 const std::string& filename, int line) {
114 std::vector<std::string> triggers(args.begin() + 1, args.end());
115 if (triggers.size() < 1) {
116 return Error() << "Actions must have a trigger";
117 }
118
119 Subcontext* action_subcontext = nullptr;
120 if (subcontexts_) {
121 for (auto& subcontext : *subcontexts_) {
122 if (StartsWith(filename, subcontext.path_prefix())) {
123 action_subcontext = &subcontext;
124 break;
125 }
126 }
127 }
128
Tom Cherry9cbf5702018-02-13 16:24:51 -0800129 std::string event_trigger;
130 std::map<std::string, std::string> property_triggers;
Tom Cherry0f6417f2018-02-13 15:25:29 -0800131
Tom Cherry9cbf5702018-02-13 16:24:51 -0800132 if (auto result = ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers);
133 !result) {
134 return Error() << "ParseTriggers() failed: " << result.error();
Tom Cherry0f6417f2018-02-13 15:25:29 -0800135 }
136
Tom Cherry9cbf5702018-02-13 16:24:51 -0800137 auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger,
138 property_triggers);
139
Tom Cherry0f6417f2018-02-13 15:25:29 -0800140 action_ = std::move(action);
141 return Success();
142}
143
144Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) {
145 return action_ ? action_->AddCommand(std::move(args), line) : Success();
146}
147
148Result<Success> ActionParser::EndSection() {
149 if (action_ && action_->NumCommands() > 0) {
150 action_manager_->AddAction(std::move(action_));
151 }
152
153 return Success();
154}
155
156} // namespace init
157} // namespace android