• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <functional>
20 
21 #include <binder/IServiceManager.h>
22 #include <binder/Status.h>
23 #include <utils/StrongPointer.h>
24 
25 namespace android {
26 namespace binder {
27 namespace internal {
28 class ClientCounterCallback;
29 }  // namespace internal
30 
31 /**
32  * Exits when all services 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 in init .rc files
37  * - configures services as disabled in init.rc files, unless a client is
38  *   guaranteed early in boot, in which case, forcePersist should also be used
39  *   to avoid races.
40  * - uses 'interface' declarations in init .rc files
41  *
42  * For more information on init .rc configuration, see system/core/init/README.md
43  **/
44 class LazyServiceRegistrar {
45    public:
46      static LazyServiceRegistrar& getInstance();
47      status_t registerService(const sp<IBinder>& service,
48                               const std::string& name = "default",
49                               bool allowIsolated = false,
50                               int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
51      /**
52       * Force the service to persist, even when it has 0 clients.
53       * If setting this flag from the server side, make sure to do so before calling
54       * registerService, or there may be a race with the default dynamic shutdown.
55       *
56       * This should only be used if it is every eventually set to false. If a
57       * service needs to persist but doesn't need to dynamically shut down,
58       * prefer to control it with another mechanism such as ctl.start.
59       */
60      void forcePersist(bool persist);
61 
62      /**
63       * Set a callback that is invoked when the active service count (i.e. services with clients)
64       * registered with this process drops to zero (or becomes nonzero).
65       * The callback takes a boolean argument, which is 'true' if there is
66       * at least one service with clients.
67       *
68       * Callback return value:
69       * - false: Default behavior for lazy services (shut down the process if there
70       *          are no clients).
71       * - true:  Don't shut down the process even if there are no clients.
72       *
73       * This callback gives a chance to:
74       * 1 - Perform some additional operations before exiting;
75       * 2 - Prevent the process from exiting by returning "true" from the
76       *     callback.
77       *
78       * This method should be called before 'registerService' to avoid races.
79       */
80      void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
81 
82      /**
83       * Try to unregister all services previously registered with 'registerService'.
84       * Returns 'true' if successful. This should only be called within the callback registered by
85       * setActiveServicesCallback.
86       */
87      bool tryUnregister();
88 
89      /**
90       * Re-register services that were unregistered by 'tryUnregister'.
91       * This method should be called in the case 'tryUnregister' fails
92       * (and should be called on the same thread).
93       */
94      void reRegister();
95 
96    private:
97      std::shared_ptr<internal::ClientCounterCallback> mClientCC;
98      LazyServiceRegistrar();
99 };
100 
101 }  // namespace binder
102 }  // namespace android
103