1 /* 2 * Copyright (C) 2021 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 #include <android-base/logging.h> 18 #include <android/binder_ibinder.h> 19 20 #include <health-impl/Health.h> 21 #include <utils/Errors.h> 22 23 #include "LinkedCallback.h" 24 25 namespace aidl::android::hardware::health { 26 Make(std::shared_ptr<Health> service,std::shared_ptr<IHealthInfoCallback> callback)27::android::base::Result<LinkedCallback*> LinkedCallback::Make( 28 std::shared_ptr<Health> service, std::shared_ptr<IHealthInfoCallback> callback) { 29 LinkedCallback* ret(new LinkedCallback()); 30 // pass ownership of this object to the death recipient 31 binder_status_t linkRet = 32 AIBinder_linkToDeath(callback->asBinder().get(), service->death_recipient_.get(), 33 reinterpret_cast<void*>(ret)); 34 if (linkRet != ::STATUS_OK) { 35 LOG(WARNING) << __func__ << "Cannot link to death: " << linkRet; 36 return ::android::base::Error(-linkRet); 37 } 38 ret->service_ = service; 39 ret->callback_ = callback; 40 return ret; 41 } 42 43 LinkedCallback::LinkedCallback() = default; 44 service()45std::shared_ptr<Health> LinkedCallback::service() { 46 auto service_sp = service_.lock(); 47 CHECK_NE(nullptr, service_sp); 48 return service_sp; 49 } 50 OnCallbackDied()51void LinkedCallback::OnCallbackDied() { 52 auto sCb = callback_.lock(); 53 if (sCb) { 54 service()->unregisterCallback(sCb); 55 } 56 } 57 58 } // namespace aidl::android::hardware::health 59