1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "hiview_remote_service.h" 17 18 #include "hiview_logger.h" 19 #include "iservice_registry.h" 20 #include "system_ability_definition.h" 21 22 namespace OHOS { 23 namespace HiviewDFX { 24 namespace RemoteService { 25 namespace { 26 DEFINE_LOG_TAG("HiViewRemoteService"); 27 sptr<IRemoteObject> g_hiviewServiceAbilityProxy = nullptr; 28 sptr<IRemoteObject::DeathRecipient> g_deathRecipient = nullptr; 29 std::mutex g_proxyMutex; 30 } 31 32 class HiviewServiceDeathRecipient : public IRemoteObject::DeathRecipient { 33 public: HiviewServiceDeathRecipient()34 HiviewServiceDeathRecipient() {}; 35 ~HiviewServiceDeathRecipient() = default; 36 DISALLOW_COPY_AND_MOVE(HiviewServiceDeathRecipient); 37 OnRemoteDied(const wptr<IRemoteObject> & remote)38 void OnRemoteDied(const wptr<IRemoteObject>& remote) 39 { 40 std::lock_guard<std::mutex> proxyGuard(g_proxyMutex); 41 if (g_hiviewServiceAbilityProxy == nullptr) { 42 HIVIEW_LOGW("hiview remote service died and local instance is null."); 43 return; 44 } 45 46 if (g_hiviewServiceAbilityProxy == remote.promote()) { 47 g_hiviewServiceAbilityProxy->RemoveDeathRecipient(g_deathRecipient); 48 g_hiviewServiceAbilityProxy = nullptr; 49 g_deathRecipient = nullptr; 50 HIVIEW_LOGW("hiview remote service died."); 51 } else { 52 HIVIEW_LOGW("unknown service died."); 53 } 54 } 55 }; 56 GetHiViewRemoteService()57sptr<IRemoteObject> GetHiViewRemoteService() 58 { 59 std::lock_guard<std::mutex> proxyGuard(g_proxyMutex); 60 if (g_hiviewServiceAbilityProxy != nullptr) { 61 return g_hiviewServiceAbilityProxy; 62 } 63 HIVIEW_LOGI("refresh remote service instance."); 64 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 65 if (abilityManager == nullptr) { 66 return nullptr; 67 } 68 g_hiviewServiceAbilityProxy = abilityManager->CheckSystemAbility(DFX_SYS_HIVIEW_ABILITY_ID); 69 if (g_hiviewServiceAbilityProxy == nullptr) { 70 HIVIEW_LOGE("get hiview ability failed."); 71 return nullptr; 72 } 73 g_deathRecipient = sptr<IRemoteObject::DeathRecipient>(new HiviewServiceDeathRecipient()); 74 if (g_deathRecipient == nullptr) { 75 HIVIEW_LOGE("create service deathrecipient failed."); 76 g_hiviewServiceAbilityProxy = nullptr; 77 return nullptr; 78 } 79 g_hiviewServiceAbilityProxy->AddDeathRecipient(g_deathRecipient); 80 return g_hiviewServiceAbilityProxy; 81 } 82 } // namespace RemoteService 83 } // namespace HiviewDFX 84 } // namespace OHOS