1 /* 2 * Copyright (C) 2023, 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 #include <android/binder_manager.h> 18 #include <stats_provider.h> 19 20 #include "utils.h" 21 22 using aidl::android::os::IStatsd; 23 StatsProvider(StatsProviderBinderDiedCallback callback)24StatsProvider::StatsProvider(StatsProviderBinderDiedCallback callback) 25 : mDeathRecipient(AIBinder_DeathRecipient_new(binderDied)), mCallback(callback) { 26 } 27 ~StatsProvider()28StatsProvider::~StatsProvider() { 29 resetStatsService(); 30 } 31 getStatsService()32std::shared_ptr<IStatsd> StatsProvider::getStatsService() { 33 std::lock_guard<std::mutex> lock(mMutex); 34 if (!mStatsd) { 35 // Fetch statsd 36 ::ndk::SpAIBinder binder(getStatsdBinder()); 37 mStatsd = IStatsd::fromBinder(binder); 38 if (mStatsd) { 39 AIBinder_linkToDeath(binder.get(), mDeathRecipient.get(), this); 40 } 41 } 42 return mStatsd; 43 } 44 resetStatsService()45void StatsProvider::resetStatsService() { 46 std::lock_guard<std::mutex> lock(mMutex); 47 mStatsd = nullptr; 48 } 49 binderDied(void * cookie)50void StatsProvider::binderDied(void* cookie) { 51 StatsProvider* statsProvider = static_cast<StatsProvider*>(cookie); 52 statsProvider->resetStatsService(); 53 statsProvider->mCallback(); 54 } 55