New default IClientCallback implementation

Add a default IClientCallback implementation which exits once all
watched services have no more clients. This is useful for lazy HALs.

Test: Use DefaultCounterExitCallback to register a HAL that implements
      two interfaces and check that it exits when both interfaces are
      unused.
Change-Id: Ifcc53631e284375a436d618e84e9546a5e53844a
diff --git a/transport/Android.bp b/transport/Android.bp
index f783022..6518177 100644
--- a/transport/Android.bp
+++ b/transport/Android.bp
@@ -65,6 +65,7 @@
 
     srcs: [
         "HidlBinderSupport.cpp",
+        "HidlLazyUtils.cpp",
         "HidlPassthroughSupport.cpp",
         "HidlTransportSupport.cpp",
         "HidlTransportUtils.cpp",
diff --git a/transport/HidlLazyUtils.cpp b/transport/HidlLazyUtils.cpp
new file mode 100644
index 0000000..0656318
--- /dev/null
+++ b/transport/HidlLazyUtils.cpp
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hidl/HidlLazyUtils.h>
+#include <hidl/HidlTransportSupport.h>
+
+#include <android-base/logging.h>
+
+#include <android/hidl/manager/1.2/IClientCallback.h>
+#include <android/hidl/manager/1.2/IServiceManager.h>
+
+namespace android {
+namespace hardware {
+namespace details {
+
+class ClientCounterCallback : public ::android::hidl::manager::V1_2::IClientCallback {
+   public:
+    ClientCounterCallback() : mNumConnectedServices(0), mNumRegisteredServices(0) {}
+
+    void incServiceCounter();
+
+   protected:
+    Return<void> onClients(const sp<::android::hidl::base::V1_0::IBase>& service,
+                           bool clients) override;
+
+   private:
+    /**
+     * Counter of the number of services that currently have at least one client.
+     */
+    size_t mNumConnectedServices;
+
+    /**
+     * Number of services that have been registered.
+     */
+    size_t mNumRegisteredServices;
+};
+
+class LazyServiceRegistrarImpl {
+   public:
+    LazyServiceRegistrarImpl() : mClientCallback(new ClientCounterCallback) {}
+
+    status_t registerServiceWithCallback(const sp<::android::hidl::base::V1_0::IBase>& service,
+                                         const std::string& name);
+
+   private:
+    sp<ClientCounterCallback> mClientCallback;
+};
+
+void ClientCounterCallback::incServiceCounter() {
+    mNumRegisteredServices++;
+}
+
+/**
+ * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
+ * invocations could occur on different threads however.
+ */
+Return<void> ClientCounterCallback::onClients(const sp<::android::hidl::base::V1_0::IBase>& service,
+                                              bool clients) {
+    if (clients) {
+        LOG(INFO) << "HAL " << service->descriptor << " connected.";
+        mNumConnectedServices++;
+    } else {
+        LOG(INFO) << "HAL " << service->descriptor << " disconnected.";
+        mNumConnectedServices--;
+    }
+    LOG(INFO) << "HAL has " << mNumConnectedServices << " (of " << mNumRegisteredServices
+              << " available) clients in use.";
+
+    if (mNumConnectedServices == 0) {
+        LOG(INFO) << "Exiting HAL. No clients in use for any service in process.";
+        exit(EXIT_SUCCESS);
+    }
+    return Status::ok();
+}
+
+status_t LazyServiceRegistrarImpl::registerServiceWithCallback(
+    const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
+    static auto manager = hardware::defaultServiceManager1_2();
+    LOG(INFO) << "Registering HAL: " << service->descriptor << " with name: " << name;
+    // TODO(b/118394906): Convert to service->registerAsService(name) when possible
+    status_t res = android::hardware::details::registerAsServiceInternal(service, name);
+    if (res == android::OK) {
+        mClientCallback->incServiceCounter();
+        bool ret = manager->registerClientCallback(service, mClientCallback);
+        if (!ret) {
+            res = android::INVALID_OPERATION;
+            LOG(ERROR) << "Failed to add client callback";
+        }
+    } else {
+        LOG(ERROR) << "Failed to register as service";
+    }
+    return res;
+}
+
+}  // namespace details
+
+LazyServiceRegistrar::LazyServiceRegistrar() {
+    mImpl = std::make_shared<details::LazyServiceRegistrarImpl>();
+}
+
+status_t LazyServiceRegistrar::registerServiceWithCallback(
+    const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
+    return mImpl->registerServiceWithCallback(service, name);
+}
+
+}  // namespace hardware
+}  // namespace android
diff --git a/transport/include/hidl/HidlLazyUtils.h b/transport/include/hidl/HidlLazyUtils.h
new file mode 100644
index 0000000..fc9c268
--- /dev/null
+++ b/transport/include/hidl/HidlLazyUtils.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <android/hidl/base/1.0/IBase.h>
+#include <utils/RefBase.h>
+#include <utils/StrongPointer.h>
+
+namespace android {
+namespace hardware {
+namespace details {
+class LazyServiceRegistrarImpl;
+}  // namespace details
+
+/** Exits when all HALs registered through this object have 0 clients */
+class LazyServiceRegistrar {
+   public:
+    LazyServiceRegistrar();
+    status_t registerServiceWithCallback(const sp<::android::hidl::base::V1_0::IBase>& service,
+                                         const std::string& name);
+
+   private:
+    std::shared_ptr<details::LazyServiceRegistrarImpl> mImpl;
+};
+
+}  // namespace hardware
+}  // namespace android