blob: 6b17be097e731df103470c116159ef49d9bb03f8 [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#pragma once
18
19#include <memory>
20
21#include <aidl/android/hardware/health/IHealthInfoCallback.h>
22#include <android-base/macros.h>
Yifan Hong70197262023-07-06 17:08:41 -070023#include <android-base/result.h>
Yifan Hong830cdb12021-01-11 20:47:23 -080024#include <android/binder_auto_utils.h>
25
26#include <health-impl/Health.h>
27
28namespace aidl::android::hardware::health {
29
30// Type of the cookie pointer in linkToDeath.
31// A (Health, IHealthInfoCallback) tuple.
32class LinkedCallback {
33 public:
34 // Automatically linkToDeath upon construction with the returned object as the cookie.
Devin Moore92f705c2024-01-19 22:22:01 +000035 // 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 Hong70197262023-07-06 17:08:41 -070038 std::shared_ptr<Health> service, std::shared_ptr<IHealthInfoCallback> callback);
Daniel Zhengd8ed80d2024-12-17 14:19:17 -080039 // On callback died, unregister it from the service.
Yifan Hong830cdb12021-01-11 20:47:23 -080040 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 Moore92f705c2024-01-19 22:22:01 +000049 std::weak_ptr<IHealthInfoCallback> callback_;
Yifan Hong830cdb12021-01-11 20:47:23 -080050};
51
52} // namespace aidl::android::hardware::health