| 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 | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 30 | #include "subcontext.h" | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 31 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 32 | namespace android { | 
|  | 33 | namespace init { | 
|  | 34 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 35 | Result<Success> RunBuiltinFunction(const BuiltinFunction& function, | 
|  | 36 | const std::vector<std::string>& args, const std::string& context); | 
|  | 37 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 38 | class Command { | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 39 | public: | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 40 | Command(BuiltinFunction f, bool execute_in_subcontext, const std::vector<std::string>& args, | 
|  | 41 | int line); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 42 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 43 | Result<Success> InvokeFunc(Subcontext* subcontext) const; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 44 | std::string BuildCommandString() const; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 45 |  | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 46 | int line() const { return line_; } | 
|  | 47 |  | 
|  | 48 | private: | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 49 | BuiltinFunction func_; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 50 | bool execute_in_subcontext_; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 51 | std::vector<std::string> args_; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 52 | int line_; | 
|  | 53 | }; | 
|  | 54 |  | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 55 | using EventTrigger = std::string; | 
|  | 56 | using PropertyChange = std::pair<std::string, std::string>; | 
|  | 57 | using BuiltinAction = class Action*; | 
|  | 58 |  | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 59 | class Action { | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 60 | public: | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 61 | Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 62 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 63 | Result<Success> AddCommand(const std::vector<std::string>& args, int line); | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 64 | void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line); | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 65 | Result<Success> InitTriggers(const std::vector<std::string>& args); | 
|  | 66 | Result<Success> InitSingleTrigger(const std::string& trigger); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 67 | std::size_t NumCommands() const; | 
|  | 68 | void ExecuteOneCommand(std::size_t command) const; | 
|  | 69 | void ExecuteAllCommands() const; | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 70 | bool CheckEvent(const EventTrigger& event_trigger) const; | 
|  | 71 | bool CheckEvent(const PropertyChange& property_change) const; | 
|  | 72 | bool CheckEvent(const BuiltinAction& builtin_action) const; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 73 | std::string BuildTriggersString() const; | 
|  | 74 | void DumpState() const; | 
|  | 75 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 76 | bool oneshot() const { return oneshot_; } | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 77 | const std::string& filename() const { return filename_; } | 
|  | 78 | int line() const { return line_; } | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 79 | static void set_function_map(const KeywordFunctionMap* function_map) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 80 | function_map_ = function_map; | 
|  | 81 | } | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 82 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 83 | private: | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 84 | void ExecuteCommand(const Command& command) const; | 
|  | 85 | bool CheckPropertyTriggers(const std::string& name = "", | 
|  | 86 | const std::string& value = "") const; | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 87 | Result<Success> ParsePropertyTrigger(const std::string& trigger); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 88 |  | 
|  | 89 | std::map<std::string, std::string> property_triggers_; | 
|  | 90 | std::string event_trigger_; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 91 | std::vector<Command> commands_; | 
|  | 92 | bool oneshot_; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 93 | Subcontext* subcontext_; | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 94 | std::string filename_; | 
|  | 95 | int line_; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 96 | static const KeywordFunctionMap* function_map_; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 97 | }; | 
|  | 98 |  | 
|  | 99 | class ActionManager { | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 100 | public: | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 101 | static ActionManager& GetInstance(); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 102 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 103 | // Exposed for testing | 
|  | 104 | ActionManager(); | 
|  | 105 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 106 | void AddAction(std::unique_ptr<Action> action); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 107 | void QueueEventTrigger(const std::string& trigger); | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 108 | void QueuePropertyChange(const std::string& name, const std::string& value); | 
|  | 109 | void QueueAllPropertyActions(); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 110 | void QueueBuiltinAction(BuiltinFunction func, const std::string& name); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 111 | void ExecuteOneCommand(); | 
|  | 112 | bool HasMoreCommands() const; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 113 | void DumpState() const; | 
| Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 114 | void ClearQueue(); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 115 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 116 | private: | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 117 | ActionManager(ActionManager const&) = delete; | 
|  | 118 | void operator=(ActionManager const&) = delete; | 
|  | 119 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 120 | std::vector<std::unique_ptr<Action>> actions_; | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 121 | std::queue<std::variant<EventTrigger, PropertyChange, BuiltinAction>> event_queue_; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 122 | std::queue<const Action*> current_executing_actions_; | 
| Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 123 | std::size_t current_command_; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 124 | }; | 
|  | 125 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 126 | class ActionParser : public SectionParser { | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 127 | public: | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 128 | ActionParser(ActionManager* action_manager, std::vector<Subcontext>* subcontexts) | 
|  | 129 | : action_manager_(action_manager), subcontexts_(subcontexts), action_(nullptr) {} | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 130 | Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename, | 
|  | 131 | int line) override; | 
|  | 132 | Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override; | 
| Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 133 | Result<Success> EndSection() override; | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 134 |  | 
|  | 135 | private: | 
| Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 136 | ActionManager* action_manager_; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 137 | std::vector<Subcontext>* subcontexts_; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 138 | std::unique_ptr<Action> action_; | 
|  | 139 | }; | 
|  | 140 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 141 | }  // namespace init | 
|  | 142 | }  // namespace android | 
|  | 143 |  | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 144 | #endif |