| 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 |  | 
| Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 17 | #pragma once | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include <map> | 
|  | 20 | #include <queue> | 
|  | 21 | #include <string> | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 22 | #include <variant> | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 23 | #include <vector> | 
|  | 24 |  | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 25 | #include <android-base/strings.h> | 
|  | 26 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 27 | #include "builtins.h" | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 28 | #include "keyword_map.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 | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 35 | Result<void> RunBuiltinFunction(const BuiltinFunction& function, | 
|  | 36 | const std::vector<std::string>& args, const std::string& context); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 40 | Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args, | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 41 | int line); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 42 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 43 | Result<void> InvokeFunc(Subcontext* subcontext) const; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 44 | std::string BuildCommandString() const; | 
| Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 45 | Result<void> CheckCommand() const; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 46 |  | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 47 | int line() const { return line_; } | 
|  | 48 |  | 
|  | 49 | private: | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 50 | BuiltinFunction func_; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 51 | bool execute_in_subcontext_; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 52 | std::vector<std::string> args_; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 53 | int line_; | 
|  | 54 | }; | 
|  | 55 |  | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 56 | using EventTrigger = std::string; | 
|  | 57 | using PropertyChange = std::pair<std::string, std::string>; | 
|  | 58 | using BuiltinAction = class Action*; | 
|  | 59 |  | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 60 | class Action { | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 61 | public: | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 62 | Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line, | 
|  | 63 | const std::string& event_trigger, | 
|  | 64 | const std::map<std::string, std::string>& property_triggers); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 65 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 66 | Result<void> AddCommand(std::vector<std::string>&& args, int line); | 
| Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 67 | void AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line); | 
| Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 68 | size_t NumCommands() const; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 69 | void ExecuteOneCommand(std::size_t command) const; | 
|  | 70 | void ExecuteAllCommands() const; | 
| Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 71 | bool CheckEvent(const EventTrigger& event_trigger) const; | 
|  | 72 | bool CheckEvent(const PropertyChange& property_change) const; | 
|  | 73 | bool CheckEvent(const BuiltinAction& builtin_action) const; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 74 | std::string BuildTriggersString() const; | 
|  | 75 | void DumpState() const; | 
| Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 76 | size_t CheckAllCommands() const; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 77 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 78 | bool oneshot() const { return oneshot_; } | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 79 | const std::string& filename() const { return filename_; } | 
|  | 80 | int line() const { return line_; } | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 81 | static void set_function_map(const BuiltinFunctionMap* function_map) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 82 | function_map_ = function_map; | 
|  | 83 | } | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 84 | bool IsFromApex() const { return base::StartsWith(filename_, "/apex/"); } | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 85 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 86 | private: | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 87 | void ExecuteCommand(const Command& command) const; | 
|  | 88 | bool CheckPropertyTriggers(const std::string& name = "", | 
|  | 89 | const std::string& value = "") const; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | std::map<std::string, std::string> property_triggers_; | 
|  | 92 | std::string event_trigger_; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 93 | std::vector<Command> commands_; | 
|  | 94 | bool oneshot_; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 95 | Subcontext* subcontext_; | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 96 | std::string filename_; | 
|  | 97 | int line_; | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 98 | static const BuiltinFunctionMap* function_map_; | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 99 | }; | 
|  | 100 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 101 | }  // namespace init | 
|  | 102 | }  // namespace android |