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>
23 #include <android-base/result.h>
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.
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(
38             std::shared_ptr<Health> service, std::shared_ptr<IHealthInfoCallback> callback);
39     // On callback died, unreigster it from the service.
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_;
49     std::weak_ptr<IHealthInfoCallback> callback_;
50 };
51 
52 }  // namespace aidl::android::hardware::health
53