blob: cdfc6a053d0ad80aedad2d9bc8dc24187e9ee1f3 [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 Cherrycb0f9bb2017-09-12 15:58:47 -070030#include "subcontext.h"
Tom Cherryb7349902015-08-26 11:43:36 -070031
Tom Cherry81f5d3e2017-06-22 12:53:17 -070032namespace android {
33namespace init {
34
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070035Result<Success> RunBuiltinFunction(const BuiltinFunction& function,
36 const std::vector<std::string>& args, const std::string& context);
37
Tom Cherryb7349902015-08-26 11:43:36 -070038class Command {
Tom Cherry012c5732017-04-18 13:21:54 -070039 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070040 Command(BuiltinFunction f, bool execute_in_subcontext, const std::vector<std::string>& args,
41 int line);
Tom Cherryb7349902015-08-26 11:43:36 -070042
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070043 Result<Success> InvokeFunc(Subcontext* subcontext) const;
Tom Cherryb7349902015-08-26 11:43:36 -070044 std::string BuildCommandString() const;
Tom Cherryb7349902015-08-26 11:43:36 -070045
Tom Cherry012c5732017-04-18 13:21:54 -070046 int line() const { return line_; }
47
48 private:
Tom Cherryb7349902015-08-26 11:43:36 -070049 BuiltinFunction func_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070050 bool execute_in_subcontext_;
Tom Cherryb7349902015-08-26 11:43:36 -070051 std::vector<std::string> args_;
Tom Cherryb7349902015-08-26 11:43:36 -070052 int line_;
53};
54
Tom Cherry26ed9cb2017-04-17 13:25:29 -070055using EventTrigger = std::string;
56using PropertyChange = std::pair<std::string, std::string>;
57using BuiltinAction = class Action*;
58
Tom Cherryfa0c21c2015-07-23 17:53:11 -070059class Action {
Tom Cherry012c5732017-04-18 13:21:54 -070060 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070061 Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070062
Tom Cherry89bcc852017-08-02 17:01:36 -070063 Result<Success> AddCommand(const std::vector<std::string>& args, int line);
Tom Cherry012c5732017-04-18 13:21:54 -070064 void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherry89bcc852017-08-02 17:01:36 -070065 Result<Success> InitTriggers(const std::vector<std::string>& args);
66 Result<Success> InitSingleTrigger(const std::string& trigger);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070067 std::size_t NumCommands() const;
68 void ExecuteOneCommand(std::size_t command) const;
69 void ExecuteAllCommands() const;
Tom Cherry26ed9cb2017-04-17 13:25:29 -070070 bool CheckEvent(const EventTrigger& event_trigger) const;
71 bool CheckEvent(const PropertyChange& property_change) const;
72 bool CheckEvent(const BuiltinAction& builtin_action) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070073 std::string BuildTriggersString() const;
74 void DumpState() const;
75
Tom Cherryb7349902015-08-26 11:43:36 -070076 bool oneshot() const { return oneshot_; }
Tom Cherry012c5732017-04-18 13:21:54 -070077 const std::string& filename() const { return filename_; }
78 int line() const { return line_; }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070079 static void set_function_map(const KeywordFunctionMap* function_map) {
Tom Cherryb7349902015-08-26 11:43:36 -070080 function_map_ = function_map;
81 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070082
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070083 private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070084 void ExecuteCommand(const Command& command) const;
85 bool CheckPropertyTriggers(const std::string& name = "",
86 const std::string& value = "") const;
Tom Cherry89bcc852017-08-02 17:01:36 -070087 Result<Success> ParsePropertyTrigger(const std::string& trigger);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070088
89 std::map<std::string, std::string> property_triggers_;
90 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070091 std::vector<Command> commands_;
92 bool oneshot_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070093 Subcontext* subcontext_;
Tom Cherry012c5732017-04-18 13:21:54 -070094 std::string filename_;
95 int line_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070096 static const KeywordFunctionMap* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070097};
98
99class ActionManager {
Tom Cherryad54d092017-04-19 16:18:50 -0700100 public:
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700101 static ActionManager& GetInstance();
Tom Cherryb7349902015-08-26 11:43:36 -0700102
Tom Cherryad54d092017-04-19 16:18:50 -0700103 // Exposed for testing
104 ActionManager();
105
Tom Cherryb7349902015-08-26 11:43:36 -0700106 void AddAction(std::unique_ptr<Action> action);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700107 void QueueEventTrigger(const std::string& trigger);
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700108 void QueuePropertyChange(const std::string& name, const std::string& value);
109 void QueueAllPropertyActions();
Tom Cherryb7349902015-08-26 11:43:36 -0700110 void QueueBuiltinAction(BuiltinFunction func, const std::string& name);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700111 void ExecuteOneCommand();
112 bool HasMoreCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700113 void DumpState() const;
Wei Wangeeab4912017-06-27 22:08:45 -0700114 void ClearQueue();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700115
Tom Cherryad54d092017-04-19 16:18:50 -0700116 private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700117 ActionManager(ActionManager const&) = delete;
118 void operator=(ActionManager const&) = delete;
119
Tom Cherryb7349902015-08-26 11:43:36 -0700120 std::vector<std::unique_ptr<Action>> actions_;
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700121 std::queue<std::variant<EventTrigger, PropertyChange, BuiltinAction>> event_queue_;
Tom Cherryb7349902015-08-26 11:43:36 -0700122 std::queue<const Action*> current_executing_actions_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700123 std::size_t current_command_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700124};
125
Tom Cherryb7349902015-08-26 11:43:36 -0700126class ActionParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700127 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700128 ActionParser(ActionManager* action_manager, std::vector<Subcontext>* subcontexts)
129 : action_manager_(action_manager), subcontexts_(subcontexts), action_(nullptr) {}
Tom Cherry89bcc852017-08-02 17:01:36 -0700130 Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
131 int line) override;
132 Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
Tom Cherryb7349902015-08-26 11:43:36 -0700133 void EndSection() override;
Tom Cherry012c5732017-04-18 13:21:54 -0700134
135 private:
Tom Cherry30a6f272017-04-19 15:31:58 -0700136 ActionManager* action_manager_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700137 std::vector<Subcontext>* subcontexts_;
Tom Cherryb7349902015-08-26 11:43:36 -0700138 std::unique_ptr<Action> action_;
139};
140
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700141} // namespace init
142} // namespace android
143
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700144#endif