Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #pragma once |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "service.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace init { |
| 26 | |
| 27 | class ServiceList { |
| 28 | public: |
| 29 | static ServiceList& GetInstance(); |
| 30 | |
| 31 | // Exposed for testing |
| 32 | ServiceList(); |
Tom Cherry | 6737a6b | 2019-08-05 15:03:58 -0700 | [diff] [blame] | 33 | size_t CheckAllCommands(); |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 34 | |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 35 | void AddService(std::unique_ptr<Service> service); |
| 36 | void RemoveService(const Service& svc); |
Nikita Ioffe | 091c4d1 | 2019-12-05 12:35:19 +0000 | [diff] [blame] | 37 | template <class UnaryPredicate> |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 38 | void RemoveServiceIf(UnaryPredicate predicate) { |
Nikita Ioffe | 091c4d1 | 2019-12-05 12:35:19 +0000 | [diff] [blame] | 39 | services_.erase(std::remove_if(services_.begin(), services_.end(), predicate), |
| 40 | services_.end()); |
| 41 | } |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 42 | |
| 43 | template <typename T, typename F = decltype(&Service::name)> |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 44 | Service* FindService(T value, F function = &Service::name) const { |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 45 | auto svc = std::find_if(services_.begin(), services_.end(), |
| 46 | [&function, &value](const std::unique_ptr<Service>& s) { |
| 47 | return std::invoke(function, s) == value; |
| 48 | }); |
| 49 | if (svc != services_.end()) { |
| 50 | return svc->get(); |
| 51 | } |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 55 | Service* FindInterface(const std::string& interface_name) { |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 56 | for (const auto& svc : services_) { |
| 57 | if (svc->interfaces().count(interface_name) > 0) { |
| 58 | return svc.get(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 65 | void DumpState() const; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 66 | |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 67 | auto begin() const { return services_.begin(); } |
| 68 | auto end() const { return services_.end(); } |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 69 | const std::vector<Service*> services_in_shutdown_order() const; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 70 | |
| 71 | void MarkPostData(); |
| 72 | bool IsPostData(); |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 73 | void MarkServicesUpdate(); |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 74 | bool IsServicesUpdated() const { return services_update_finished_; } |
Tom Cherry | 832f9f1 | 2020-03-10 11:47:24 -0700 | [diff] [blame] | 75 | void DelayService(const Service& service); |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 76 | |
Nikita Ioffe | 3ad2920 | 2020-02-27 20:46:27 +0000 | [diff] [blame] | 77 | void ResetState() { |
| 78 | post_data_ = false; |
| 79 | services_update_finished_ = false; |
| 80 | } |
| 81 | |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 82 | private: |
| 83 | std::vector<std::unique_ptr<Service>> services_; |
| 84 | |
| 85 | bool post_data_ = false; |
| 86 | bool services_update_finished_ = false; |
| 87 | std::vector<std::string> delayed_service_names_; |
| 88 | }; |
| 89 | |
| 90 | } // namespace init |
| 91 | } // namespace android |