• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <functional>
20 
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 
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  **/
41 class LazyServiceRegistrar {
42    public:
43      static LazyServiceRegistrar& getInstance();
44      status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
45                               const std::string& name = "default");
46      /**
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.
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       */
64      void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
65 
66      /**
67       * Try to unregister all services previously registered with 'registerService'.
68       * Returns 'true' if successful. This should only be called within
69       * the callback registered by setActiveServicesCallback.
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();
79 
80    private:
81      std::shared_ptr<details::LazyServiceRegistrarImpl> mImpl;
82      LazyServiceRegistrar();
83 };
84 
85 }  // namespace hardware
86 }  // namespace android
87