Tom Cherry | 7fd3bc2 | 2018-02-13 15:36:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "action_manager.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | namespace android { |
| 22 | namespace init { |
| 23 | |
| 24 | ActionManager::ActionManager() : current_command_(0) {} |
| 25 | |
| 26 | ActionManager& ActionManager::GetInstance() { |
| 27 | static ActionManager instance; |
| 28 | return instance; |
| 29 | } |
| 30 | |
| 31 | void ActionManager::AddAction(std::unique_ptr<Action> action) { |
| 32 | actions_.emplace_back(std::move(action)); |
| 33 | } |
| 34 | |
| 35 | void ActionManager::QueueEventTrigger(const std::string& trigger) { |
| 36 | event_queue_.emplace(trigger); |
| 37 | } |
| 38 | |
| 39 | void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) { |
| 40 | event_queue_.emplace(std::make_pair(name, value)); |
| 41 | } |
| 42 | |
| 43 | void ActionManager::QueueAllPropertyActions() { |
| 44 | QueuePropertyChange("", ""); |
| 45 | } |
| 46 | |
| 47 | void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) { |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 48 | auto action = std::make_unique<Action>(true, nullptr, "<Builtin Action>", 0, name, |
| 49 | std::map<std::string, std::string>{}); |
Tom Cherry | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame^] | 50 | action->AddCommand(std::move(func), {name}, 0); |
Tom Cherry | 7fd3bc2 | 2018-02-13 15:36:14 -0800 | [diff] [blame] | 51 | |
| 52 | event_queue_.emplace(action.get()); |
| 53 | actions_.emplace_back(std::move(action)); |
| 54 | } |
| 55 | |
| 56 | void ActionManager::ExecuteOneCommand() { |
| 57 | // Loop through the event queue until we have an action to execute |
| 58 | while (current_executing_actions_.empty() && !event_queue_.empty()) { |
| 59 | for (const auto& action : actions_) { |
| 60 | if (std::visit([&action](const auto& event) { return action->CheckEvent(event); }, |
| 61 | event_queue_.front())) { |
| 62 | current_executing_actions_.emplace(action.get()); |
| 63 | } |
| 64 | } |
| 65 | event_queue_.pop(); |
| 66 | } |
| 67 | |
| 68 | if (current_executing_actions_.empty()) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | auto action = current_executing_actions_.front(); |
| 73 | |
| 74 | if (current_command_ == 0) { |
| 75 | std::string trigger_name = action->BuildTriggersString(); |
| 76 | LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename() |
| 77 | << ":" << action->line() << ")"; |
| 78 | } |
| 79 | |
| 80 | action->ExecuteOneCommand(current_command_); |
| 81 | |
| 82 | // If this was the last command in the current action, then remove |
| 83 | // the action from the executing list. |
| 84 | // If this action was oneshot, then also remove it from actions_. |
| 85 | ++current_command_; |
| 86 | if (current_command_ == action->NumCommands()) { |
| 87 | current_executing_actions_.pop(); |
| 88 | current_command_ = 0; |
| 89 | if (action->oneshot()) { |
| 90 | auto eraser = [&action](std::unique_ptr<Action>& a) { return a.get() == action; }; |
Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 91 | actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser), |
| 92 | actions_.end()); |
Tom Cherry | 7fd3bc2 | 2018-02-13 15:36:14 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | bool ActionManager::HasMoreCommands() const { |
| 98 | return !current_executing_actions_.empty() || !event_queue_.empty(); |
| 99 | } |
| 100 | |
| 101 | void ActionManager::DumpState() const { |
| 102 | for (const auto& a : actions_) { |
| 103 | a->DumpState(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void ActionManager::ClearQueue() { |
| 108 | // We are shutting down so don't claim the oneshot builtin actions back |
| 109 | current_executing_actions_ = {}; |
| 110 | event_queue_ = {}; |
| 111 | current_command_ = 0; |
| 112 | } |
| 113 | |
| 114 | } // namespace init |
| 115 | } // namespace android |