blob: df471a37bd2fb1254d4eaf3b5823ac1a01d48f21 [file] [log] [blame]
Yifan Hong830cdb12021-01-11 20:47:23 -08001/*
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
25namespace aidl::android::hardware::health {
26
Devin Moore92f705c2024-01-19 22:22:01 +000027::android::base::Result<LinkedCallback*> LinkedCallback::Make(
Yifan Hong830cdb12021-01-11 20:47:23 -080028 std::shared_ptr<Health> service, std::shared_ptr<IHealthInfoCallback> callback) {
Devin Moore92f705c2024-01-19 22:22:01 +000029 LinkedCallback* ret(new LinkedCallback());
30 // pass ownership of this object to the death recipient
Yifan Hong830cdb12021-01-11 20:47:23 -080031 binder_status_t linkRet =
32 AIBinder_linkToDeath(callback->asBinder().get(), service->death_recipient_.get(),
Devin Moore92f705c2024-01-19 22:22:01 +000033 reinterpret_cast<void*>(ret));
Yifan Hong830cdb12021-01-11 20:47:23 -080034 if (linkRet != ::STATUS_OK) {
35 LOG(WARNING) << __func__ << "Cannot link to death: " << linkRet;
Yifan Hong70197262023-07-06 17:08:41 -070036 return ::android::base::Error(-linkRet);
Yifan Hong830cdb12021-01-11 20:47:23 -080037 }
38 ret->service_ = service;
Devin Moore92f705c2024-01-19 22:22:01 +000039 ret->callback_ = callback;
Yifan Hong830cdb12021-01-11 20:47:23 -080040 return ret;
41}
42
43LinkedCallback::LinkedCallback() = default;
44
Yifan Hong830cdb12021-01-11 20:47:23 -080045std::shared_ptr<Health> LinkedCallback::service() {
46 auto service_sp = service_.lock();
47 CHECK_NE(nullptr, service_sp);
48 return service_sp;
49}
50
51void LinkedCallback::OnCallbackDied() {
Devin Moore92f705c2024-01-19 22:22:01 +000052 auto sCb = callback_.lock();
53 if (sCb) {
54 service()->unregisterCallback(sCb);
55 }
Yifan Hong830cdb12021-01-11 20:47:23 -080056}
57
58} // namespace aidl::android::hardware::health