Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 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 | |
Devin Moore | 92f705c | 2024-01-19 22:22:01 +0000 | [diff] [blame] | 27 | ::android::base::Result<LinkedCallback*> LinkedCallback::Make( |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 28 | std::shared_ptr<Health> service, std::shared_ptr<IHealthInfoCallback> callback) { |
Devin Moore | 92f705c | 2024-01-19 22:22:01 +0000 | [diff] [blame] | 29 | LinkedCallback* ret(new LinkedCallback()); |
| 30 | // pass ownership of this object to the death recipient |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 31 | binder_status_t linkRet = |
| 32 | AIBinder_linkToDeath(callback->asBinder().get(), service->death_recipient_.get(), |
Devin Moore | 92f705c | 2024-01-19 22:22:01 +0000 | [diff] [blame] | 33 | reinterpret_cast<void*>(ret)); |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 34 | if (linkRet != ::STATUS_OK) { |
| 35 | LOG(WARNING) << __func__ << "Cannot link to death: " << linkRet; |
Yifan Hong | 7019726 | 2023-07-06 17:08:41 -0700 | [diff] [blame] | 36 | return ::android::base::Error(-linkRet); |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 37 | } |
| 38 | ret->service_ = service; |
Devin Moore | 92f705c | 2024-01-19 22:22:01 +0000 | [diff] [blame] | 39 | ret->callback_ = callback; |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | LinkedCallback::LinkedCallback() = default; |
| 44 | |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 45 | std::shared_ptr<Health> LinkedCallback::service() { |
| 46 | auto service_sp = service_.lock(); |
| 47 | CHECK_NE(nullptr, service_sp); |
| 48 | return service_sp; |
| 49 | } |
| 50 | |
| 51 | void LinkedCallback::OnCallbackDied() { |
Devin Moore | 92f705c | 2024-01-19 22:22:01 +0000 | [diff] [blame] | 52 | auto sCb = callback_.lock(); |
| 53 | if (sCb) { |
| 54 | service()->unregisterCallback(sCb); |
| 55 | } |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | } // namespace aidl::android::hardware::health |