blob: ad15f3f42e12319b47ed1aae0cdfe661f663e928 [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"
27#include "init_parser.h"
28#include "keyword_map.h"
29
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030namespace android {
31namespace init {
32
Tom Cherryb7349902015-08-26 11:43:36 -070033class Command {
Tom Cherry012c5732017-04-18 13:21:54 -070034 public:
35 Command(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryb7349902015-08-26 11:43:36 -070036
37 int InvokeFunc() const;
38 std::string BuildCommandString() const;
Tom Cherryb7349902015-08-26 11:43:36 -070039
Tom Cherry012c5732017-04-18 13:21:54 -070040 int line() const { return line_; }
41
42 private:
Tom Cherryb7349902015-08-26 11:43:36 -070043 BuiltinFunction func_;
44 std::vector<std::string> args_;
Tom Cherryb7349902015-08-26 11:43:36 -070045 int line_;
46};
47
Tom Cherry26ed9cb2017-04-17 13:25:29 -070048using EventTrigger = std::string;
49using PropertyChange = std::pair<std::string, std::string>;
50using BuiltinAction = class Action*;
51
Tom Cherryfa0c21c2015-07-23 17:53:11 -070052class Action {
Tom Cherry012c5732017-04-18 13:21:54 -070053 public:
54 explicit Action(bool oneshot, const std::string& filename, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070055
Tom Cherry012c5732017-04-18 13:21:54 -070056 bool AddCommand(const std::vector<std::string>& args, int line, std::string* err);
57 void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070058 bool InitTriggers(const std::vector<std::string>& args, std::string* err);
59 bool InitSingleTrigger(const std::string& trigger);
60 std::size_t NumCommands() const;
61 void ExecuteOneCommand(std::size_t command) const;
62 void ExecuteAllCommands() const;
Tom Cherry26ed9cb2017-04-17 13:25:29 -070063 bool CheckEvent(const EventTrigger& event_trigger) const;
64 bool CheckEvent(const PropertyChange& property_change) const;
65 bool CheckEvent(const BuiltinAction& builtin_action) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070066 std::string BuildTriggersString() const;
67 void DumpState() const;
68
Tom Cherryb7349902015-08-26 11:43:36 -070069 bool oneshot() const { return oneshot_; }
Tom Cherry012c5732017-04-18 13:21:54 -070070 const std::string& filename() const { return filename_; }
71 int line() const { return line_; }
Tom Cherryb7349902015-08-26 11:43:36 -070072 static void set_function_map(const KeywordMap<BuiltinFunction>* function_map) {
73 function_map_ = function_map;
74 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070075
Tom Cherryb7349902015-08-26 11:43:36 -070076
77private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070078 void ExecuteCommand(const Command& command) const;
79 bool CheckPropertyTriggers(const std::string& name = "",
80 const std::string& value = "") const;
81 bool ParsePropertyTrigger(const std::string& trigger, std::string* err);
82
83 std::map<std::string, std::string> property_triggers_;
84 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070085 std::vector<Command> commands_;
86 bool oneshot_;
Tom Cherry012c5732017-04-18 13:21:54 -070087 std::string filename_;
88 int line_;
Tom Cherryb7349902015-08-26 11:43:36 -070089 static const KeywordMap<BuiltinFunction>* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070090};
91
92class ActionManager {
Tom Cherryad54d092017-04-19 16:18:50 -070093 public:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070094 static ActionManager& GetInstance();
Tom Cherryb7349902015-08-26 11:43:36 -070095
Tom Cherryad54d092017-04-19 16:18:50 -070096 // Exposed for testing
97 ActionManager();
98
Tom Cherryb7349902015-08-26 11:43:36 -070099 void AddAction(std::unique_ptr<Action> action);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700100 void QueueEventTrigger(const std::string& trigger);
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700101 void QueuePropertyChange(const std::string& name, const std::string& value);
102 void QueueAllPropertyActions();
Tom Cherryb7349902015-08-26 11:43:36 -0700103 void QueueBuiltinAction(BuiltinFunction func, const std::string& name);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700104 void ExecuteOneCommand();
105 bool HasMoreCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700106 void DumpState() const;
Wei Wangeeab4912017-06-27 22:08:45 -0700107 void ClearQueue();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700108
Tom Cherryad54d092017-04-19 16:18:50 -0700109 private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700110 ActionManager(ActionManager const&) = delete;
111 void operator=(ActionManager const&) = delete;
112
Tom Cherryb7349902015-08-26 11:43:36 -0700113 std::vector<std::unique_ptr<Action>> actions_;
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700114 std::queue<std::variant<EventTrigger, PropertyChange, BuiltinAction>> event_queue_;
Tom Cherryb7349902015-08-26 11:43:36 -0700115 std::queue<const Action*> current_executing_actions_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700116 std::size_t current_command_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700117};
118
Tom Cherryb7349902015-08-26 11:43:36 -0700119class ActionParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700120 public:
Tom Cherry30a6f272017-04-19 15:31:58 -0700121 ActionParser(ActionManager* action_manager)
122 : action_manager_(action_manager), action_(nullptr) {}
123 bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line,
Tom Cherryb7349902015-08-26 11:43:36 -0700124 std::string* err) override;
Tom Cherry30a6f272017-04-19 15:31:58 -0700125 bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override;
Tom Cherryb7349902015-08-26 11:43:36 -0700126 void EndSection() override;
Tom Cherry012c5732017-04-18 13:21:54 -0700127
128 private:
Tom Cherry30a6f272017-04-19 15:31:58 -0700129 ActionManager* action_manager_;
Tom Cherryb7349902015-08-26 11:43:36 -0700130 std::unique_ptr<Action> action_;
131};
132
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700133} // namespace init
134} // namespace android
135
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700136#endif