blob: 555da258ad67abf54b8b95d00bf4246258c248f4 [file] [log] [blame]
Tom Cherry2aeb1ad2019-06-26 10:46:20 -07001/*
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
24namespace android {
25namespace init {
26
27class ServiceList {
28 public:
29 static ServiceList& GetInstance();
30
31 // Exposed for testing
32 ServiceList();
Tom Cherry6737a6b2019-08-05 15:03:58 -070033 size_t CheckAllCommands();
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070034
Tom Cherry832f9f12020-03-10 11:47:24 -070035 void AddService(std::unique_ptr<Service> service);
36 void RemoveService(const Service& svc);
Nikita Ioffe091c4d12019-12-05 12:35:19 +000037 template <class UnaryPredicate>
Tom Cherry832f9f12020-03-10 11:47:24 -070038 void RemoveServiceIf(UnaryPredicate predicate) {
Nikita Ioffe091c4d12019-12-05 12:35:19 +000039 services_.erase(std::remove_if(services_.begin(), services_.end(), predicate),
40 services_.end());
41 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070042
43 template <typename T, typename F = decltype(&Service::name)>
Tom Cherry832f9f12020-03-10 11:47:24 -070044 Service* FindService(T value, F function = &Service::name) const {
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070045 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 Cherry832f9f12020-03-10 11:47:24 -070055 Service* FindInterface(const std::string& interface_name) {
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070056 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 Cherry832f9f12020-03-10 11:47:24 -070065 void DumpState() const;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070066
Tom Cherry832f9f12020-03-10 11:47:24 -070067 auto begin() const { return services_.begin(); }
68 auto end() const { return services_.end(); }
Tom Cherry832f9f12020-03-10 11:47:24 -070069 const std::vector<Service*> services_in_shutdown_order() const;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070070
71 void MarkPostData();
72 bool IsPostData();
Tom Cherry832f9f12020-03-10 11:47:24 -070073 void MarkServicesUpdate();
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070074 bool IsServicesUpdated() const { return services_update_finished_; }
Tom Cherry832f9f12020-03-10 11:47:24 -070075 void DelayService(const Service& service);
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070076
Nikita Ioffe3ad29202020-02-27 20:46:27 +000077 void ResetState() {
78 post_data_ = false;
79 services_update_finished_ = false;
80 }
81
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070082 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