blob: d006c50c6b5a7c7077ec0d06d8dc9a7fcfb04e09 [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
30class Command {
Tom Cherry012c5732017-04-18 13:21:54 -070031 public:
32 Command(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryb7349902015-08-26 11:43:36 -070033
34 int InvokeFunc() const;
35 std::string BuildCommandString() const;
Tom Cherryb7349902015-08-26 11:43:36 -070036
Tom Cherry012c5732017-04-18 13:21:54 -070037 int line() const { return line_; }
38
39 private:
Tom Cherryb7349902015-08-26 11:43:36 -070040 BuiltinFunction func_;
41 std::vector<std::string> args_;
Tom Cherryb7349902015-08-26 11:43:36 -070042 int line_;
43};
44
Tom Cherry26ed9cb2017-04-17 13:25:29 -070045using EventTrigger = std::string;
46using PropertyChange = std::pair<std::string, std::string>;
47using BuiltinAction = class Action*;
48
Tom Cherryfa0c21c2015-07-23 17:53:11 -070049class Action {
Tom Cherry012c5732017-04-18 13:21:54 -070050 public:
51 explicit Action(bool oneshot, const std::string& filename, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070052
Tom Cherry012c5732017-04-18 13:21:54 -070053 bool AddCommand(const std::vector<std::string>& args, int line, std::string* err);
54 void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070055 bool InitTriggers(const std::vector<std::string>& args, std::string* err);
56 bool InitSingleTrigger(const std::string& trigger);
57 std::size_t NumCommands() const;
58 void ExecuteOneCommand(std::size_t command) const;
59 void ExecuteAllCommands() const;
Tom Cherry26ed9cb2017-04-17 13:25:29 -070060 bool CheckEvent(const EventTrigger& event_trigger) const;
61 bool CheckEvent(const PropertyChange& property_change) const;
62 bool CheckEvent(const BuiltinAction& builtin_action) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070063 std::string BuildTriggersString() const;
64 void DumpState() const;
65
Tom Cherryb7349902015-08-26 11:43:36 -070066 bool oneshot() const { return oneshot_; }
Tom Cherry012c5732017-04-18 13:21:54 -070067 const std::string& filename() const { return filename_; }
68 int line() const { return line_; }
Tom Cherryb7349902015-08-26 11:43:36 -070069 static void set_function_map(const KeywordMap<BuiltinFunction>* function_map) {
70 function_map_ = function_map;
71 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070072
Tom Cherryb7349902015-08-26 11:43:36 -070073
74private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070075 void ExecuteCommand(const Command& command) const;
76 bool CheckPropertyTriggers(const std::string& name = "",
77 const std::string& value = "") const;
78 bool ParsePropertyTrigger(const std::string& trigger, std::string* err);
79
80 std::map<std::string, std::string> property_triggers_;
81 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070082 std::vector<Command> commands_;
83 bool oneshot_;
Tom Cherry012c5732017-04-18 13:21:54 -070084 std::string filename_;
85 int line_;
Tom Cherryb7349902015-08-26 11:43:36 -070086 static const KeywordMap<BuiltinFunction>* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070087};
88
89class ActionManager {
Tom Cherryad54d092017-04-19 16:18:50 -070090 public:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070091 static ActionManager& GetInstance();
Tom Cherryb7349902015-08-26 11:43:36 -070092
Tom Cherryad54d092017-04-19 16:18:50 -070093 // Exposed for testing
94 ActionManager();
95
Tom Cherryb7349902015-08-26 11:43:36 -070096 void AddAction(std::unique_ptr<Action> action);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070097 void QueueEventTrigger(const std::string& trigger);
Tom Cherry26ed9cb2017-04-17 13:25:29 -070098 void QueuePropertyChange(const std::string& name, const std::string& value);
99 void QueueAllPropertyActions();
Tom Cherryb7349902015-08-26 11:43:36 -0700100 void QueueBuiltinAction(BuiltinFunction func, const std::string& name);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700101 void ExecuteOneCommand();
102 bool HasMoreCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700103 void DumpState() const;
104
Tom Cherryad54d092017-04-19 16:18:50 -0700105 private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700106 ActionManager(ActionManager const&) = delete;
107 void operator=(ActionManager const&) = delete;
108
Tom Cherryb7349902015-08-26 11:43:36 -0700109 std::vector<std::unique_ptr<Action>> actions_;
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700110 std::queue<std::variant<EventTrigger, PropertyChange, BuiltinAction>> event_queue_;
Tom Cherryb7349902015-08-26 11:43:36 -0700111 std::queue<const Action*> current_executing_actions_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700112 std::size_t current_command_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700113};
114
Tom Cherryb7349902015-08-26 11:43:36 -0700115class ActionParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700116 public:
Tom Cherry30a6f272017-04-19 15:31:58 -0700117 ActionParser(ActionManager* action_manager)
118 : action_manager_(action_manager), action_(nullptr) {}
119 bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line,
Tom Cherryb7349902015-08-26 11:43:36 -0700120 std::string* err) override;
Tom Cherry30a6f272017-04-19 15:31:58 -0700121 bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override;
Tom Cherryb7349902015-08-26 11:43:36 -0700122 void EndSection() override;
Tom Cherry012c5732017-04-18 13:21:54 -0700123
124 private:
Tom Cherry30a6f272017-04-19 15:31:58 -0700125 ActionManager* action_manager_;
Tom Cherryb7349902015-08-26 11:43:36 -0700126 std::unique_ptr<Action> action_;
127};
128
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700129#endif