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