Add active services count callback to lazy HALs.

Additionally, add methods to tryUnregister/reRegister services.

Bug: 176240491
Test: atest hidl_lazy_test

Change-Id: I9e67315e1308d0ce7370bf7d293d74f9c081a3c4
diff --git a/transport/include/hidl/HidlLazyUtils.h b/transport/include/hidl/HidlLazyUtils.h
index 97fe20e..44fbcb2 100644
--- a/transport/include/hidl/HidlLazyUtils.h
+++ b/transport/include/hidl/HidlLazyUtils.h
@@ -16,6 +16,8 @@
 
 #pragma once
 
+#include <functional>
+
 #include <android/hidl/base/1.0/IBase.h>
 #include <utils/RefBase.h>
 #include <utils/StrongPointer.h>
@@ -41,6 +43,39 @@
      static LazyServiceRegistrar& getInstance();
      status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
                               const std::string& name = "default");
+     /**
+      * Set a callback that is executed when the total number of services with
+      * clients changes.
+      * The callback takes an argument, which is the number of registered
+      * lazy HALs for this process which have clients.
+      *
+      * Callback return value:
+      * - false: Default behavior for lazy HALs (shut down the process if there
+      *          are no clients).
+      * - true:  Don't shut down the process even if there are no clients.
+      *
+      * This callback gives a chance to:
+      * 1 - Perform some additional operations before exiting;
+      * 2 - Prevent the process from exiting by returning "true" from the
+      *     callback.
+      *
+      * This method should be called before 'registerService' to avoid races.
+      */
+     void setActiveServicesCountCallback(
+             const std::function<bool(int)>& activeServicesCountCallback);
+
+     /**
+      * Try to unregister all services previously registered with 'registerService'.
+      * Returns 'true' if successful.
+      */
+     bool tryUnregister();
+
+     /**
+      * Re-register services that were unregistered by 'tryUnregister'.
+      * This method should be called in the case 'tryUnregister' fails
+      * (and should be called on the same thread).
+      */
+     void reRegister();
 
    private:
      std::shared_ptr<details::LazyServiceRegistrarImpl> mImpl;