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