blob: 44ecd237bd5ef33d758c20f2a50db45496ddf40b [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 Cherry67dee622017-07-27 12:54:48 -070028#include "parser.h"
Tom Cherry557946e2017-08-01 13:50:23 -070029#include "result.h"
Tom Cherryb7349902015-08-26 11:43:36 -070030
Tom Cherry81f5d3e2017-06-22 12:53:17 -070031namespace android {
32namespace init {
33
Tom Cherryb7349902015-08-26 11:43:36 -070034class Command {
Tom Cherry012c5732017-04-18 13:21:54 -070035 public:
36 Command(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryb7349902015-08-26 11:43:36 -070037
Tom Cherry557946e2017-08-01 13:50:23 -070038 Result<Success> InvokeFunc() const;
Tom Cherryb7349902015-08-26 11:43:36 -070039 std::string BuildCommandString() const;
Tom Cherryb7349902015-08-26 11:43:36 -070040
Tom Cherry012c5732017-04-18 13:21:54 -070041 int line() const { return line_; }
42
43 private:
Tom Cherryb7349902015-08-26 11:43:36 -070044 BuiltinFunction func_;
45 std::vector<std::string> args_;
Tom Cherryb7349902015-08-26 11:43:36 -070046 int line_;
47};
48
Tom Cherry26ed9cb2017-04-17 13:25:29 -070049using EventTrigger = std::string;
50using PropertyChange = std::pair<std::string, std::string>;
51using BuiltinAction = class Action*;
52
Tom Cherryfa0c21c2015-07-23 17:53:11 -070053class Action {
Tom Cherry012c5732017-04-18 13:21:54 -070054 public:
55 explicit Action(bool oneshot, const std::string& filename, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070056
Tom Cherry012c5732017-04-18 13:21:54 -070057 bool AddCommand(const std::vector<std::string>& args, int line, std::string* err);
58 void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070059 bool InitTriggers(const std::vector<std::string>& args, std::string* err);
60 bool InitSingleTrigger(const std::string& trigger);
61 std::size_t NumCommands() const;
62 void ExecuteOneCommand(std::size_t command) const;
63 void ExecuteAllCommands() const;
Tom Cherry26ed9cb2017-04-17 13:25:29 -070064 bool CheckEvent(const EventTrigger& event_trigger) const;
65 bool CheckEvent(const PropertyChange& property_change) const;
66 bool CheckEvent(const BuiltinAction& builtin_action) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070067 std::string BuildTriggersString() const;
68 void DumpState() const;
69
Tom Cherryb7349902015-08-26 11:43:36 -070070 bool oneshot() const { return oneshot_; }
Tom Cherry012c5732017-04-18 13:21:54 -070071 const std::string& filename() const { return filename_; }
72 int line() const { return line_; }
Tom Cherryb7349902015-08-26 11:43:36 -070073 static void set_function_map(const KeywordMap<BuiltinFunction>* function_map) {
74 function_map_ = function_map;
75 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070076
Tom Cherryb7349902015-08-26 11:43:36 -070077
78private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070079 void ExecuteCommand(const Command& command) const;
80 bool CheckPropertyTriggers(const std::string& name = "",
81 const std::string& value = "") const;
82 bool ParsePropertyTrigger(const std::string& trigger, std::string* err);
83
84 std::map<std::string, std::string> property_triggers_;
85 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070086 std::vector<Command> commands_;
87 bool oneshot_;
Tom Cherry012c5732017-04-18 13:21:54 -070088 std::string filename_;
89 int line_;
Tom Cherryb7349902015-08-26 11:43:36 -070090 static const KeywordMap<BuiltinFunction>* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070091};
92
93class ActionManager {
Tom Cherryad54d092017-04-19 16:18:50 -070094 public:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070095 static ActionManager& GetInstance();
Tom Cherryb7349902015-08-26 11:43:36 -070096
Tom Cherryad54d092017-04-19 16:18:50 -070097 // Exposed for testing
98 ActionManager();
99
Tom Cherryb7349902015-08-26 11:43:36 -0700100 void AddAction(std::unique_ptr<Action> action);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700101 void QueueEventTrigger(const std::string& trigger);
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700102 void QueuePropertyChange(const std::string& name, const std::string& value);
103 void QueueAllPropertyActions();
Tom Cherryb7349902015-08-26 11:43:36 -0700104 void QueueBuiltinAction(BuiltinFunction func, const std::string& name);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700105 void ExecuteOneCommand();
106 bool HasMoreCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700107 void DumpState() const;
Wei Wangeeab4912017-06-27 22:08:45 -0700108 void ClearQueue();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700109
Tom Cherryad54d092017-04-19 16:18:50 -0700110 private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700111 ActionManager(ActionManager const&) = delete;
112 void operator=(ActionManager const&) = delete;
113
Tom Cherryb7349902015-08-26 11:43:36 -0700114 std::vector<std::unique_ptr<Action>> actions_;
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700115 std::queue<std::variant<EventTrigger, PropertyChange, BuiltinAction>> event_queue_;
Tom Cherryb7349902015-08-26 11:43:36 -0700116 std::queue<const Action*> current_executing_actions_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700117 std::size_t current_command_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700118};
119
Tom Cherryb7349902015-08-26 11:43:36 -0700120class ActionParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700121 public:
Tom Cherry30a6f272017-04-19 15:31:58 -0700122 ActionParser(ActionManager* action_manager)
123 : action_manager_(action_manager), action_(nullptr) {}
124 bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line,
Tom Cherryb7349902015-08-26 11:43:36 -0700125 std::string* err) override;
Tom Cherry30a6f272017-04-19 15:31:58 -0700126 bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override;
Tom Cherryb7349902015-08-26 11:43:36 -0700127 void EndSection() override;
Tom Cherry012c5732017-04-18 13:21:54 -0700128
129 private:
Tom Cherry30a6f272017-04-19 15:31:58 -0700130 ActionManager* action_manager_;
Tom Cherryb7349902015-08-26 11:43:36 -0700131 std::unique_ptr<Action> action_;
132};
133
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700134} // namespace init
135} // namespace android
136
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700137#endif