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