blob: 376b6abaf56e91bef7005ba0d98e9a8ab01ca16d [file] [log] [blame]
Peter Kalauskas3373cd32018-10-24 15:37:00 -07001/*
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#pragma once
18
Amos Bianchi57341bb2020-12-23 11:11:37 -080019#include <functional>
20
Peter Kalauskas3373cd32018-10-24 15:37:00 -070021#include <android/hidl/base/1.0/IBase.h>
22#include <utils/RefBase.h>
23#include <utils/StrongPointer.h>
24
25namespace android {
26namespace hardware {
27namespace details {
28class LazyServiceRegistrarImpl;
29} // namespace details
30
Steven Morelandf38e3d72020-08-05 19:48:07 +000031/**
32 * Exits when all HALs registered through this object have 0 clients
33 *
34 * In order to use this class, it's expected that your service:
35 * - registers all services in the process with this API
36 * - configures services as oneshot + disabled in init .rc files
37 * - uses 'interface' declarations in init .rc files
38 *
39 * For more information on init .rc configuration, see system/core/init/README.md
40 **/
Peter Kalauskas3373cd32018-10-24 15:37:00 -070041class LazyServiceRegistrar {
42 public:
Peter Kalauskas1e969222019-08-14 12:13:11 -070043 static LazyServiceRegistrar& getInstance();
44 status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
45 const std::string& name = "default");
Amos Bianchi57341bb2020-12-23 11:11:37 -080046 /**
Amos Bianchifb918632021-01-20 17:41:02 -080047 * Set a callback that is invoked when the active HAL count (i.e. HALs with clients)
48 * registered with this process drops to zero (or becomes nonzero).
49 * The callback takes a boolean argument, which is 'true' if there is
50 * at least one HAL with clients.
Amos Bianchi57341bb2020-12-23 11:11:37 -080051 *
52 * Callback return value:
53 * - false: Default behavior for lazy HALs (shut down the process if there
54 * are no clients).
55 * - true: Don't shut down the process even if there are no clients.
56 *
57 * This callback gives a chance to:
58 * 1 - Perform some additional operations before exiting;
59 * 2 - Prevent the process from exiting by returning "true" from the
60 * callback.
61 *
62 * This method should be called before 'registerService' to avoid races.
63 */
Amos Bianchifb918632021-01-20 17:41:02 -080064 void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
Amos Bianchi57341bb2020-12-23 11:11:37 -080065
66 /**
67 * Try to unregister all services previously registered with 'registerService'.
Steven Moreland8578d502021-06-22 18:40:27 +000068 * Returns 'true' if successful. This should only be called within
69 * the callback registered by setActiveServicesCallback.
Amos Bianchi57341bb2020-12-23 11:11:37 -080070 */
71 bool tryUnregister();
72
73 /**
74 * Re-register services that were unregistered by 'tryUnregister'.
75 * This method should be called in the case 'tryUnregister' fails
76 * (and should be called on the same thread).
77 */
78 void reRegister();
Peter Kalauskas3373cd32018-10-24 15:37:00 -070079
80 private:
Peter Kalauskas1e969222019-08-14 12:13:11 -070081 std::shared_ptr<details::LazyServiceRegistrarImpl> mImpl;
Peter Kalauskasb8b35202019-08-26 08:18:10 -070082 LazyServiceRegistrar();
Peter Kalauskas3373cd32018-10-24 15:37:00 -070083};
84
85} // namespace hardware
86} // namespace android