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