1020a203aSopenharmony_ci/*
2020a203aSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3020a203aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4020a203aSopenharmony_ci * you may not use this file except in compliance with the License.
5020a203aSopenharmony_ci * You may obtain a copy of the License at
6020a203aSopenharmony_ci *
7020a203aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8020a203aSopenharmony_ci *
9020a203aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10020a203aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11020a203aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12020a203aSopenharmony_ci * See the License for the specific language governing permissions and
13020a203aSopenharmony_ci * limitations under the License.
14020a203aSopenharmony_ci */
15020a203aSopenharmony_ci
16020a203aSopenharmony_ci#include "hiview_remote_service.h"
17020a203aSopenharmony_ci
18020a203aSopenharmony_ci#include "hiview_logger.h"
19020a203aSopenharmony_ci#include "iservice_registry.h"
20020a203aSopenharmony_ci#include "system_ability_definition.h"
21020a203aSopenharmony_ci
22020a203aSopenharmony_cinamespace OHOS {
23020a203aSopenharmony_cinamespace HiviewDFX {
24020a203aSopenharmony_cinamespace RemoteService {
25020a203aSopenharmony_cinamespace {
26020a203aSopenharmony_ciDEFINE_LOG_TAG("HiViewRemoteService");
27020a203aSopenharmony_cisptr<IRemoteObject> g_hiviewServiceAbilityProxy = nullptr;
28020a203aSopenharmony_cisptr<IRemoteObject::DeathRecipient> g_deathRecipient = nullptr;
29020a203aSopenharmony_cistd::mutex g_proxyMutex;
30020a203aSopenharmony_ci}
31020a203aSopenharmony_ci
32020a203aSopenharmony_ciclass HiviewServiceDeathRecipient : public IRemoteObject::DeathRecipient {
33020a203aSopenharmony_cipublic:
34020a203aSopenharmony_ci    HiviewServiceDeathRecipient() {};
35020a203aSopenharmony_ci    ~HiviewServiceDeathRecipient() = default;
36020a203aSopenharmony_ci    DISALLOW_COPY_AND_MOVE(HiviewServiceDeathRecipient);
37020a203aSopenharmony_ci
38020a203aSopenharmony_ci    void OnRemoteDied(const wptr<IRemoteObject>& remote)
39020a203aSopenharmony_ci    {
40020a203aSopenharmony_ci        std::lock_guard<std::mutex> proxyGuard(g_proxyMutex);
41020a203aSopenharmony_ci        if (g_hiviewServiceAbilityProxy == nullptr) {
42020a203aSopenharmony_ci            HIVIEW_LOGW("hiview remote service died and local instance is null.");
43020a203aSopenharmony_ci            return;
44020a203aSopenharmony_ci        }
45020a203aSopenharmony_ci
46020a203aSopenharmony_ci        if (g_hiviewServiceAbilityProxy == remote.promote()) {
47020a203aSopenharmony_ci            g_hiviewServiceAbilityProxy->RemoveDeathRecipient(g_deathRecipient);
48020a203aSopenharmony_ci            g_hiviewServiceAbilityProxy = nullptr;
49020a203aSopenharmony_ci            g_deathRecipient = nullptr;
50020a203aSopenharmony_ci            HIVIEW_LOGW("hiview remote service died.");
51020a203aSopenharmony_ci        } else {
52020a203aSopenharmony_ci            HIVIEW_LOGW("unknown service died.");
53020a203aSopenharmony_ci        }
54020a203aSopenharmony_ci    }
55020a203aSopenharmony_ci};
56020a203aSopenharmony_ci
57020a203aSopenharmony_cisptr<IRemoteObject> GetHiViewRemoteService()
58020a203aSopenharmony_ci{
59020a203aSopenharmony_ci    std::lock_guard<std::mutex> proxyGuard(g_proxyMutex);
60020a203aSopenharmony_ci    if (g_hiviewServiceAbilityProxy != nullptr) {
61020a203aSopenharmony_ci        return g_hiviewServiceAbilityProxy;
62020a203aSopenharmony_ci    }
63020a203aSopenharmony_ci    HIVIEW_LOGI("refresh remote service instance.");
64020a203aSopenharmony_ci    auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65020a203aSopenharmony_ci    if (abilityManager == nullptr) {
66020a203aSopenharmony_ci        return nullptr;
67020a203aSopenharmony_ci    }
68020a203aSopenharmony_ci    g_hiviewServiceAbilityProxy = abilityManager->CheckSystemAbility(DFX_SYS_HIVIEW_ABILITY_ID);
69020a203aSopenharmony_ci    if (g_hiviewServiceAbilityProxy == nullptr) {
70020a203aSopenharmony_ci        HIVIEW_LOGE("get hiview ability failed.");
71020a203aSopenharmony_ci        return nullptr;
72020a203aSopenharmony_ci    }
73020a203aSopenharmony_ci    g_deathRecipient = sptr<IRemoteObject::DeathRecipient>(new HiviewServiceDeathRecipient());
74020a203aSopenharmony_ci    if (g_deathRecipient == nullptr) {
75020a203aSopenharmony_ci        HIVIEW_LOGE("create service deathrecipient failed.");
76020a203aSopenharmony_ci        g_hiviewServiceAbilityProxy = nullptr;
77020a203aSopenharmony_ci        return nullptr;
78020a203aSopenharmony_ci    }
79020a203aSopenharmony_ci    g_hiviewServiceAbilityProxy->AddDeathRecipient(g_deathRecipient);
80020a203aSopenharmony_ci    return g_hiviewServiceAbilityProxy;
81020a203aSopenharmony_ci}
82020a203aSopenharmony_ci} // namespace RemoteService
83020a203aSopenharmony_ci} // namespace HiviewDFX
84020a203aSopenharmony_ci} // namespace OHOS