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