Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 23 | #include <variant> |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 26 | #include "builtins.h" |
| 27 | #include "init_parser.h" |
| 28 | #include "keyword_map.h" |
| 29 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace init { |
| 32 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 33 | class Command { |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 34 | public: |
| 35 | Command(BuiltinFunction f, const std::vector<std::string>& args, int line); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 36 | |
| 37 | int InvokeFunc() const; |
| 38 | std::string BuildCommandString() const; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 39 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 40 | int line() const { return line_; } |
| 41 | |
| 42 | private: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 43 | BuiltinFunction func_; |
| 44 | std::vector<std::string> args_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 45 | int line_; |
| 46 | }; |
| 47 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 48 | using EventTrigger = std::string; |
| 49 | using PropertyChange = std::pair<std::string, std::string>; |
| 50 | using BuiltinAction = class Action*; |
| 51 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 52 | class Action { |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 53 | public: |
| 54 | explicit Action(bool oneshot, const std::string& filename, int line); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 55 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 56 | 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 Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 58 | 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 Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 63 | bool CheckEvent(const EventTrigger& event_trigger) const; |
| 64 | bool CheckEvent(const PropertyChange& property_change) const; |
| 65 | bool CheckEvent(const BuiltinAction& builtin_action) const; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 66 | std::string BuildTriggersString() const; |
| 67 | void DumpState() const; |
| 68 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 69 | bool oneshot() const { return oneshot_; } |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 70 | const std::string& filename() const { return filename_; } |
| 71 | int line() const { return line_; } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 72 | static void set_function_map(const KeywordMap<BuiltinFunction>* function_map) { |
| 73 | function_map_ = function_map; |
| 74 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 75 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 78 | 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 Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 85 | std::vector<Command> commands_; |
| 86 | bool oneshot_; |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 87 | std::string filename_; |
| 88 | int line_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 89 | static const KeywordMap<BuiltinFunction>* function_map_; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class ActionManager { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 93 | public: |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 94 | static ActionManager& GetInstance(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 95 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 96 | // Exposed for testing |
| 97 | ActionManager(); |
| 98 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 99 | void AddAction(std::unique_ptr<Action> action); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 100 | void QueueEventTrigger(const std::string& trigger); |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 101 | void QueuePropertyChange(const std::string& name, const std::string& value); |
| 102 | void QueueAllPropertyActions(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 103 | void QueueBuiltinAction(BuiltinFunction func, const std::string& name); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 104 | void ExecuteOneCommand(); |
| 105 | bool HasMoreCommands() const; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 106 | void DumpState() const; |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 107 | void ClearQueue(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 108 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 109 | private: |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 110 | ActionManager(ActionManager const&) = delete; |
| 111 | void operator=(ActionManager const&) = delete; |
| 112 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 113 | std::vector<std::unique_ptr<Action>> actions_; |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 114 | std::queue<std::variant<EventTrigger, PropertyChange, BuiltinAction>> event_queue_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 115 | std::queue<const Action*> current_executing_actions_; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 116 | std::size_t current_command_; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 119 | class ActionParser : public SectionParser { |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 120 | public: |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 121 | 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 Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 124 | std::string* err) override; |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 125 | bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 126 | void EndSection() override; |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 127 | |
| 128 | private: |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 129 | ActionManager* action_manager_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 130 | std::unique_ptr<Action> action_; |
| 131 | }; |
| 132 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 133 | } // namespace init |
| 134 | } // namespace android |
| 135 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 136 | #endif |