blob: 2d497b386432dba7eb7cadcefb0ec7a6911552ad [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 Cherryb35f8272018-10-22 14:50:52 -070022#if defined(__ANDROID__)
23#include "property_service.h"
24#else
25#include "host_init_stubs.h"
26#endif
Tom Cherry9cbf5702018-02-13 16:24:51 -080027
28using 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
Tom Cherryb35f8272018-10-22 14:50:52 -070043 return CanReadProperty(subcontext->context(), prop_name);
Tom Cherry9cbf5702018-02-13 16:24:51 -080044}
45
46Result<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 Cherry8c901dd2018-07-24 15:54:33 -070059 return Error() << "unexported property trigger found: " << prop_name;
Tom Cherry9cbf5702018-02-13 16:24:51 -080060 }
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
68Result<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 Cherry0f6417f2018-02-13 15:25:29 -0800104Result<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 Cherry9cbf5702018-02-13 16:24:51 -0800121 std::string event_trigger;
122 std::map<std::string, std::string> property_triggers;
Tom Cherry0f6417f2018-02-13 15:25:29 -0800123
Tom Cherry9cbf5702018-02-13 16:24:51 -0800124 if (auto result = ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers);
125 !result) {
126 return Error() << "ParseTriggers() failed: " << result.error();
Tom Cherry0f6417f2018-02-13 15:25:29 -0800127 }
128
Tom Cherry9cbf5702018-02-13 16:24:51 -0800129 auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger,
130 property_triggers);
131
Tom Cherry0f6417f2018-02-13 15:25:29 -0800132 action_ = std::move(action);
133 return Success();
134}
135
136Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) {
137 return action_ ? action_->AddCommand(std::move(args), line) : Success();
138}
139
140Result<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