Peter Kalauskas | 3373cd3 | 2018-10-24 15:37:00 -0700 | [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 | #pragma once |
| 18 | |
Amos Bianchi | 57341bb | 2020-12-23 11:11:37 -0800 | [diff] [blame] | 19 | #include <functional> |
| 20 | |
Peter Kalauskas | 3373cd3 | 2018-10-24 15:37:00 -0700 | [diff] [blame] | 21 | #include <android/hidl/base/1.0/IBase.h> |
| 22 | #include <utils/RefBase.h> |
| 23 | #include <utils/StrongPointer.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace details { |
| 28 | class LazyServiceRegistrarImpl; |
| 29 | } // namespace details |
| 30 | |
Steven Moreland | f38e3d7 | 2020-08-05 19:48:07 +0000 | [diff] [blame] | 31 | /** |
| 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 Kalauskas | 3373cd3 | 2018-10-24 15:37:00 -0700 | [diff] [blame] | 41 | class LazyServiceRegistrar { |
| 42 | public: |
Peter Kalauskas | 1e96922 | 2019-08-14 12:13:11 -0700 | [diff] [blame] | 43 | static LazyServiceRegistrar& getInstance(); |
| 44 | status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service, |
| 45 | const std::string& name = "default"); |
Amos Bianchi | 57341bb | 2020-12-23 11:11:37 -0800 | [diff] [blame] | 46 | /** |
Amos Bianchi | fb91863 | 2021-01-20 17:41:02 -0800 | [diff] [blame] | 47 | * 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 Bianchi | 57341bb | 2020-12-23 11:11:37 -0800 | [diff] [blame] | 51 | * |
| 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 Bianchi | fb91863 | 2021-01-20 17:41:02 -0800 | [diff] [blame] | 64 | void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback); |
Amos Bianchi | 57341bb | 2020-12-23 11:11:37 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Try to unregister all services previously registered with 'registerService'. |
Steven Moreland | 8578d50 | 2021-06-22 18:40:27 +0000 | [diff] [blame] | 68 | * Returns 'true' if successful. This should only be called within |
| 69 | * the callback registered by setActiveServicesCallback. |
Amos Bianchi | 57341bb | 2020-12-23 11:11:37 -0800 | [diff] [blame] | 70 | */ |
| 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 Kalauskas | 3373cd3 | 2018-10-24 15:37:00 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
Peter Kalauskas | 1e96922 | 2019-08-14 12:13:11 -0700 | [diff] [blame] | 81 | std::shared_ptr<details::LazyServiceRegistrarImpl> mImpl; |
Peter Kalauskas | b8b3520 | 2019-08-26 08:18:10 -0700 | [diff] [blame] | 82 | LazyServiceRegistrar(); |
Peter Kalauskas | 3373cd3 | 2018-10-24 15:37:00 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace hardware |
| 86 | } // namespace android |