blob: da494c91910fc1d07788921aa628fb803d8fa8e4 [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.
35 // service->death_reciepient() should be from CreateDeathRecipient().
36 // Not using a strong reference to |service| to avoid circular reference. The lifetime
37 // of |service| must be longer than this LinkedCallback object.
Yifan Hong70197262023-07-06 17:08:41 -070038 static ::android::base::Result<std::unique_ptr<LinkedCallback>> Make(
39 std::shared_ptr<Health> service, std::shared_ptr<IHealthInfoCallback> callback);
Yifan Hong830cdb12021-01-11 20:47:23 -080040
41 // Automatically unlinkToDeath upon destruction. So, it is always safe to reinterpret_cast
42 // the cookie back to the LinkedCallback object.
43 ~LinkedCallback();
44
45 // The wrapped IHealthInfoCallback object.
46 const std::shared_ptr<IHealthInfoCallback>& callback() const { return callback_; }
47
48 // On callback died, unreigster it from the service.
49 void OnCallbackDied();
50
51 private:
52 LinkedCallback();
53 DISALLOW_COPY_AND_ASSIGN(LinkedCallback);
54
55 std::shared_ptr<Health> service();
56
57 std::weak_ptr<Health> service_;
58 std::shared_ptr<IHealthInfoCallback> callback_;
59};
60
61} // namespace aidl::android::hardware::health