blob: 6dee2d0a5c8bb2db1a4740bc02357f6e6a3e04c8 [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>
23#include <vector>
24
Tom Cherryb7349902015-08-26 11:43:36 -070025#include "builtins.h"
26#include "init_parser.h"
27#include "keyword_map.h"
28
29class Command {
30public:
31 Command(BuiltinFunction f, const std::vector<std::string>& args,
32 const std::string& filename, int line);
33
34 int InvokeFunc() const;
35 std::string BuildCommandString() const;
36 std::string BuildSourceString() const;
37
38private:
39 BuiltinFunction func_;
40 std::vector<std::string> args_;
41 std::string filename_;
42 int line_;
43};
44
Tom Cherryfa0c21c2015-07-23 17:53:11 -070045class Action {
46public:
Tom Cherryb7349902015-08-26 11:43:36 -070047 Action(bool oneshot = false);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070048
Tom Cherryb7349902015-08-26 11:43:36 -070049 bool AddCommand(const std::vector<std::string>& args,
50 const std::string& filename, int line, std::string* err);
51 void AddCommand(BuiltinFunction f,
Tom Cherryfa0c21c2015-07-23 17:53:11 -070052 const std::vector<std::string>& args,
53 const std::string& filename = "", int line = 0);
Tom Cherryb7349902015-08-26 11:43:36 -070054 void CombineAction(const Action& action);
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;
60 bool CheckEventTrigger(const std::string& trigger) const;
61 bool CheckPropertyTrigger(const std::string& name,
62 const std::string& value) const;
Tom Cherryb7349902015-08-26 11:43:36 -070063 bool TriggersEqual(const Action& other) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070064 std::string BuildTriggersString() const;
65 void DumpState() const;
66
Tom Cherryb7349902015-08-26 11:43:36 -070067 bool oneshot() const { return oneshot_; }
68 static void set_function_map(const KeywordMap<BuiltinFunction>* function_map) {
69 function_map_ = function_map;
70 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070071
Tom Cherryb7349902015-08-26 11:43:36 -070072
73private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070074 void ExecuteCommand(const Command& command) const;
75 bool CheckPropertyTriggers(const std::string& name = "",
76 const std::string& value = "") const;
77 bool ParsePropertyTrigger(const std::string& trigger, std::string* err);
78
79 std::map<std::string, std::string> property_triggers_;
80 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070081 std::vector<Command> commands_;
82 bool oneshot_;
83 static const KeywordMap<BuiltinFunction>* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070084};
85
Tom Cherrycb716f92015-08-11 12:34:22 -070086class Trigger {
87public:
88 virtual ~Trigger() { }
Tom Cherryb7349902015-08-26 11:43:36 -070089 virtual bool CheckTriggers(const Action& action) const = 0;
Tom Cherrycb716f92015-08-11 12:34:22 -070090};
91
Tom Cherryfa0c21c2015-07-23 17:53:11 -070092class ActionManager {
93public:
94 static ActionManager& GetInstance();
Tom Cherryb7349902015-08-26 11:43:36 -070095
96 void AddAction(std::unique_ptr<Action> action);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070097 void QueueEventTrigger(const std::string& trigger);
98 void QueuePropertyTrigger(const std::string& name, const std::string& value);
99 void QueueAllPropertyTriggers();
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
105private:
106 ActionManager();
107
108 ActionManager(ActionManager const&) = delete;
109 void operator=(ActionManager const&) = delete;
110
Tom Cherryb7349902015-08-26 11:43:36 -0700111 std::vector<std::unique_ptr<Action>> actions_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700112 std::queue<std::unique_ptr<Trigger>> trigger_queue_;
Tom Cherryb7349902015-08-26 11:43:36 -0700113 std::queue<const Action*> current_executing_actions_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700114 std::size_t current_command_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700115};
116
Tom Cherryb7349902015-08-26 11:43:36 -0700117class ActionParser : public SectionParser {
118public:
119 ActionParser() : action_(nullptr) {
120 }
121 bool ParseSection(const std::vector<std::string>& args,
122 std::string* err) override;
123 bool ParseLineSection(const std::vector<std::string>& args,
124 const std::string& filename, int line,
125 std::string* err) const override;
126 void EndSection() override;
127 void EndFile(const std::string&) override {
128 }
129private:
130 std::unique_ptr<Action> action_;
131};
132
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700133#endif