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/binder_auto_utils.h> 24 25 #include <health-impl/Health.h> 26 27 namespace aidl::android::hardware::health { 28 29 // Type of the cookie pointer in linkToDeath. 30 // A (Health, IHealthInfoCallback) tuple. 31 class LinkedCallback { 32 public: 33 // Automatically linkToDeath upon construction with the returned object as the cookie. 34 // service->death_reciepient() should be from CreateDeathRecipient(). 35 // Not using a strong reference to |service| to avoid circular reference. The lifetime 36 // of |service| must be longer than this LinkedCallback object. 37 static std::unique_ptr<LinkedCallback> Make(std::shared_ptr<Health> service, 38 std::shared_ptr<IHealthInfoCallback> callback); 39 40 // Automatically unlinkToDeath upon destruction. So, it is always safe to reinterpret_cast 41 // the cookie back to the LinkedCallback object. 42 ~LinkedCallback(); 43 44 // The wrapped IHealthInfoCallback object. callback()45 const std::shared_ptr<IHealthInfoCallback>& callback() const { return callback_; } 46 47 // On callback died, unreigster it from the service. 48 void OnCallbackDied(); 49 50 private: 51 LinkedCallback(); 52 DISALLOW_COPY_AND_ASSIGN(LinkedCallback); 53 54 std::shared_ptr<Health> service(); 55 56 std::weak_ptr<Health> service_; 57 std::shared_ptr<IHealthInfoCallback> callback_; 58 }; 59 60 } // namespace aidl::android::hardware::health 61