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