blob: 4f063cc54908ce16058af072194e5b5d96818171 [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
17#ifndef _INIT_ACTION_H
18#define _INIT_ACTION_H
19
20#include <map>
21#include <queue>
22#include <string>
Tom Cherry26ed9cb2017-04-17 13:25:29 -070023#include <variant>
Tom Cherryfa0c21c2015-07-23 17:53:11 -070024#include <vector>
25
Tom Cherryb7349902015-08-26 11:43:36 -070026#include "builtins.h"
Tom Cherryb7349902015-08-26 11:43:36 -070027#include "keyword_map.h"
Tom Cherry557946e2017-08-01 13:50:23 -070028#include "result.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070029#include "subcontext.h"
Tom Cherryb7349902015-08-26 11:43:36 -070030
Tom Cherry81f5d3e2017-06-22 12:53:17 -070031namespace android {
32namespace init {
33
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070034Result<Success> RunBuiltinFunction(const BuiltinFunction& function,
35 const std::vector<std::string>& args, const std::string& context);
36
Tom Cherryb7349902015-08-26 11:43:36 -070037class Command {
Tom Cherry012c5732017-04-18 13:21:54 -070038 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070039 Command(BuiltinFunction f, bool execute_in_subcontext, const std::vector<std::string>& args,
40 int line);
Tom Cherryb7349902015-08-26 11:43:36 -070041
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070042 Result<Success> InvokeFunc(Subcontext* subcontext) const;
Tom Cherryb7349902015-08-26 11:43:36 -070043 std::string BuildCommandString() const;
Tom Cherryb7349902015-08-26 11:43:36 -070044
Tom Cherry012c5732017-04-18 13:21:54 -070045 int line() const { return line_; }
46
47 private:
Tom Cherryb7349902015-08-26 11:43:36 -070048 BuiltinFunction func_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070049 bool execute_in_subcontext_;
Tom Cherryb7349902015-08-26 11:43:36 -070050 std::vector<std::string> args_;
Tom Cherryb7349902015-08-26 11:43:36 -070051 int line_;
52};
53
Tom Cherry26ed9cb2017-04-17 13:25:29 -070054using EventTrigger = std::string;
55using PropertyChange = std::pair<std::string, std::string>;
56using BuiltinAction = class Action*;
57
Tom Cherryfa0c21c2015-07-23 17:53:11 -070058class Action {
Tom Cherry012c5732017-04-18 13:21:54 -070059 public:
Tom Cherry9cbf5702018-02-13 16:24:51 -080060 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);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070063
Tom Cherry89bcc852017-08-02 17:01:36 -070064 Result<Success> AddCommand(const std::vector<std::string>& args, int line);
Tom Cherry012c5732017-04-18 13:21:54 -070065 void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070066 std::size_t NumCommands() const;
67 void ExecuteOneCommand(std::size_t command) const;
68 void ExecuteAllCommands() const;
Tom Cherry26ed9cb2017-04-17 13:25:29 -070069 bool CheckEvent(const EventTrigger& event_trigger) const;
70 bool CheckEvent(const PropertyChange& property_change) const;
71 bool CheckEvent(const BuiltinAction& builtin_action) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070072 std::string BuildTriggersString() const;
73 void DumpState() const;
74
Tom Cherryb7349902015-08-26 11:43:36 -070075 bool oneshot() const { return oneshot_; }
Tom Cherry012c5732017-04-18 13:21:54 -070076 const std::string& filename() const { return filename_; }
77 int line() const { return line_; }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070078 static void set_function_map(const KeywordFunctionMap* function_map) {
Tom Cherryb7349902015-08-26 11:43:36 -070079 function_map_ = function_map;
80 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070081
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070082 private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070083 void ExecuteCommand(const Command& command) const;
84 bool CheckPropertyTriggers(const std::string& name = "",
85 const std::string& value = "") const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070086
87 std::map<std::string, std::string> property_triggers_;
88 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070089 std::vector<Command> commands_;
90 bool oneshot_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070091 Subcontext* subcontext_;
Tom Cherry012c5732017-04-18 13:21:54 -070092 std::string filename_;
93 int line_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070094 static const KeywordFunctionMap* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070095};
96
Tom Cherry81f5d3e2017-06-22 12:53:17 -070097} // namespace init
98} // namespace android
99
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700100#endif