1 /*
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 
17 #ifndef _INIT_ACTION_H
18 #define _INIT_ACTION_H
19 
20 #include <map>
21 #include <queue>
22 #include <string>
23 #include <variant>
24 #include <vector>
25 
26 #include "builtins.h"
27 #include "keyword_map.h"
28 #include "result.h"
29 #include "subcontext.h"
30 
31 namespace android {
32 namespace init {
33 
34 Result<Success> RunBuiltinFunction(const BuiltinFunction& function,
35                                    const std::vector<std::string>& args, const std::string& context);
36 
37 class Command {
38   public:
39     Command(BuiltinFunction f, bool execute_in_subcontext, const std::vector<std::string>& args,
40             int line);
41 
42     Result<Success> InvokeFunc(Subcontext* subcontext) const;
43     std::string BuildCommandString() const;
44 
line()45     int line() const { return line_; }
46 
47   private:
48     BuiltinFunction func_;
49     bool execute_in_subcontext_;
50     std::vector<std::string> args_;
51     int line_;
52 };
53 
54 using EventTrigger = std::string;
55 using PropertyChange = std::pair<std::string, std::string>;
56 using BuiltinAction = class Action*;
57 
58 class Action {
59   public:
60     Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line,
61            const std::string& event_trigger,
62            const std::map<std::string, std::string>& property_triggers);
63 
64     Result<Success> AddCommand(const std::vector<std::string>& args, int line);
65     void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line);
66     std::size_t NumCommands() const;
67     void ExecuteOneCommand(std::size_t command) const;
68     void ExecuteAllCommands() const;
69     bool CheckEvent(const EventTrigger& event_trigger) const;
70     bool CheckEvent(const PropertyChange& property_change) const;
71     bool CheckEvent(const BuiltinAction& builtin_action) const;
72     std::string BuildTriggersString() const;
73     void DumpState() const;
74 
oneshot()75     bool oneshot() const { return oneshot_; }
filename()76     const std::string& filename() const { return filename_; }
line()77     int line() const { return line_; }
set_function_map(const KeywordFunctionMap * function_map)78     static void set_function_map(const KeywordFunctionMap* function_map) {
79         function_map_ = function_map;
80     }
81 
82   private:
83     void ExecuteCommand(const Command& command) const;
84     bool CheckPropertyTriggers(const std::string& name = "",
85                                const std::string& value = "") const;
86 
87     std::map<std::string, std::string> property_triggers_;
88     std::string event_trigger_;
89     std::vector<Command> commands_;
90     bool oneshot_;
91     Subcontext* subcontext_;
92     std::string filename_;
93     int line_;
94     static const KeywordFunctionMap* function_map_;
95 };
96 
97 }  // namespace init
98 }  // namespace android
99 
100 #endif
101