blob: eddc384cdee8ee250cfeea6230349adc7bf01e99 [file] [log] [blame]
Tom Cherryfa0c21c2015-07-23 17:53:11 -07001/*
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
Tom Cherry4772f1d2019-07-30 09:34:41 -070017#pragma once
Tom Cherryfa0c21c2015-07-23 17:53:11 -070018
19#include <map>
20#include <queue>
21#include <string>
Tom Cherry26ed9cb2017-04-17 13:25:29 -070022#include <variant>
Tom Cherryfa0c21c2015-07-23 17:53:11 -070023#include <vector>
24
Jooyung Hanbadb7de2022-05-10 03:16:51 +090025#include <android-base/strings.h>
26
Tom Cherryb7349902015-08-26 11:43:36 -070027#include "builtins.h"
Tom Cherryb7349902015-08-26 11:43:36 -070028#include "keyword_map.h"
Tom Cherry557946e2017-08-01 13:50:23 -070029#include "result.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070030#include "subcontext.h"
Tom Cherryb7349902015-08-26 11:43:36 -070031
Tom Cherry81f5d3e2017-06-22 12:53:17 -070032namespace android {
33namespace init {
34
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070035Result<void> RunBuiltinFunction(const BuiltinFunction& function,
36 const std::vector<std::string>& args, const std::string& context);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070037
Tom Cherryb7349902015-08-26 11:43:36 -070038class Command {
Tom Cherry012c5732017-04-18 13:21:54 -070039 public:
Tom Cherry018a4382018-10-17 11:11:23 -070040 Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args,
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070041 int line);
Tom Cherryb7349902015-08-26 11:43:36 -070042
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070043 Result<void> InvokeFunc(Subcontext* subcontext) const;
Tom Cherryb7349902015-08-26 11:43:36 -070044 std::string BuildCommandString() const;
Tom Cherry4772f1d2019-07-30 09:34:41 -070045 Result<void> CheckCommand() const;
Tom Cherryb7349902015-08-26 11:43:36 -070046
Tom Cherry012c5732017-04-18 13:21:54 -070047 int line() const { return line_; }
48
49 private:
Tom Cherryb7349902015-08-26 11:43:36 -070050 BuiltinFunction func_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070051 bool execute_in_subcontext_;
Tom Cherryb7349902015-08-26 11:43:36 -070052 std::vector<std::string> args_;
Tom Cherryb7349902015-08-26 11:43:36 -070053 int line_;
54};
55
Tom Cherry26ed9cb2017-04-17 13:25:29 -070056using EventTrigger = std::string;
57using PropertyChange = std::pair<std::string, std::string>;
58using BuiltinAction = class Action*;
59
Tom Cherryfa0c21c2015-07-23 17:53:11 -070060class Action {
Tom Cherry012c5732017-04-18 13:21:54 -070061 public:
Tom Cherry9cbf5702018-02-13 16:24:51 -080062 Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line,
63 const std::string& event_trigger,
64 const std::map<std::string, std::string>& property_triggers);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070065
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070066 Result<void> AddCommand(std::vector<std::string>&& args, int line);
Tom Cherry018a4382018-10-17 11:11:23 -070067 void AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line);
Tom Cherry4772f1d2019-07-30 09:34:41 -070068 size_t NumCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070069 void ExecuteOneCommand(std::size_t command) const;
70 void ExecuteAllCommands() const;
Tom Cherry26ed9cb2017-04-17 13:25:29 -070071 bool CheckEvent(const EventTrigger& event_trigger) const;
72 bool CheckEvent(const PropertyChange& property_change) const;
73 bool CheckEvent(const BuiltinAction& builtin_action) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070074 std::string BuildTriggersString() const;
75 void DumpState() const;
Tom Cherry4772f1d2019-07-30 09:34:41 -070076 size_t CheckAllCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070077
Tom Cherryb7349902015-08-26 11:43:36 -070078 bool oneshot() const { return oneshot_; }
Tom Cherry012c5732017-04-18 13:21:54 -070079 const std::string& filename() const { return filename_; }
80 int line() const { return line_; }
Tom Cherryd52a5b32019-07-22 16:05:36 -070081 static void set_function_map(const BuiltinFunctionMap* function_map) {
Tom Cherryb7349902015-08-26 11:43:36 -070082 function_map_ = function_map;
83 }
Jooyung Hanbadb7de2022-05-10 03:16:51 +090084 bool IsFromApex() const { return base::StartsWith(filename_, "/apex/"); }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070085
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070086 private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070087 void ExecuteCommand(const Command& command) const;
88 bool CheckPropertyTriggers(const std::string& name = "",
89 const std::string& value = "") const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070090
91 std::map<std::string, std::string> property_triggers_;
92 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070093 std::vector<Command> commands_;
94 bool oneshot_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070095 Subcontext* subcontext_;
Tom Cherry012c5732017-04-18 13:21:54 -070096 std::string filename_;
97 int line_;
Tom Cherryd52a5b32019-07-22 16:05:36 -070098 static const BuiltinFunctionMap* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070099};
100
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700101} // namespace init
102} // namespace android