blob: 2136a217ae43a1afa1a32904c481bff95ac6784b [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();
33
34 void AddService(std::unique_ptr<Service> service);
35 void RemoveService(const Service& svc);
36
37 template <typename T, typename F = decltype(&Service::name)>
38 Service* FindService(T value, F function = &Service::name) const {
39 auto svc = std::find_if(services_.begin(), services_.end(),
40 [&function, &value](const std::unique_ptr<Service>& s) {
41 return std::invoke(function, s) == value;
42 });
43 if (svc != services_.end()) {
44 return svc->get();
45 }
46 return nullptr;
47 }
48
49 Service* FindInterface(const std::string& interface_name) {
50 for (const auto& svc : services_) {
51 if (svc->interfaces().count(interface_name) > 0) {
52 return svc.get();
53 }
54 }
55
56 return nullptr;
57 }
58
59 void DumpState() const;
60
61 auto begin() const { return services_.begin(); }
62 auto end() const { return services_.end(); }
63 const std::vector<std::unique_ptr<Service>>& services() const { return services_; }
64 const std::vector<Service*> services_in_shutdown_order() const;
65
66 void MarkPostData();
67 bool IsPostData();
68 void MarkServicesUpdate();
69 bool IsServicesUpdated() const { return services_update_finished_; }
70 void DelayService(const Service& service);
71
72 private:
73 std::vector<std::unique_ptr<Service>> services_;
74
75 bool post_data_ = false;
76 bool services_update_finished_ = false;
77 std::vector<std::string> delayed_service_names_;
78};
79
80} // namespace init
81} // namespace android