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 | #pragma once |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include <aidl/android/hardware/health/IHealthInfoCallback.h> |
| 22 | #include <android-base/macros.h> |
Yifan Hong | 7019726 | 2023-07-06 17:08:41 -0700 | [diff] [blame] | 23 | #include <android-base/result.h> |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 24 | #include <android/binder_auto_utils.h> |
| 25 | |
| 26 | #include <health-impl/Health.h> |
| 27 | |
| 28 | namespace aidl::android::hardware::health { |
| 29 | |
| 30 | // Type of the cookie pointer in linkToDeath. |
| 31 | // A (Health, IHealthInfoCallback) tuple. |
| 32 | class LinkedCallback { |
| 33 | public: |
| 34 | // Automatically linkToDeath upon construction with the returned object as the cookie. |
Devin Moore | 92f705c | 2024-01-19 22:22:01 +0000 | [diff] [blame] | 35 | // The deathRecipient owns the LinkedCallback object and will delete it with |
| 36 | // cookie when it's unlinked. |
| 37 | static ::android::base::Result<LinkedCallback*> Make( |
Yifan Hong | 7019726 | 2023-07-06 17:08:41 -0700 | [diff] [blame] | 38 | std::shared_ptr<Health> service, std::shared_ptr<IHealthInfoCallback> callback); |
Daniel Zheng | d8ed80d | 2024-12-17 14:19:17 -0800 | [diff] [blame] | 39 | // On callback died, unregister it from the service. |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 40 | void OnCallbackDied(); |
| 41 | |
| 42 | private: |
| 43 | LinkedCallback(); |
| 44 | DISALLOW_COPY_AND_ASSIGN(LinkedCallback); |
| 45 | |
| 46 | std::shared_ptr<Health> service(); |
| 47 | |
| 48 | std::weak_ptr<Health> service_; |
Devin Moore | 92f705c | 2024-01-19 22:22:01 +0000 | [diff] [blame] | 49 | std::weak_ptr<IHealthInfoCallback> callback_; |
Yifan Hong | 830cdb1 | 2021-01-11 20:47:23 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | } // namespace aidl::android::hardware::health |