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