1 /*
2  * Copyright (c) 2023-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 "sr_samgr_helper.h"
17 
18 #include "bundle_constants.h"
19 #include "bundle_mgr_proxy.h"
20 #include "hilog_tag_wrapper.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #ifdef ACCOUNT_ENABLE
25 #include "os_account_manager.h"
26 #endif
27 #include "system_ability_definition.h"
28 
29 namespace OHOS {
30 namespace AbilityRuntime {
SrSamgrHelper()31 SrSamgrHelper::SrSamgrHelper()
32 {}
33 
~SrSamgrHelper()34 SrSamgrHelper::~SrSamgrHelper()
35 {}
36 
GetBundleMgr()37 sptr<IBundleMgr> SrSamgrHelper::GetBundleMgr()
38 {
39     TAG_LOGI(AAFwkTag::SER_ROUTER, "called");
40     std::lock_guard<std::mutex> lock(bundleMgrMutex_);
41     if (iBundleMgr_ == nullptr) {
42         ConnectBundleMgrLocked();
43     }
44     return iBundleMgr_;
45 }
46 
GetCurrentActiveUserId()47 int32_t SrSamgrHelper::GetCurrentActiveUserId()
48 {
49 #ifdef ACCOUNT_ENABLE
50     std::vector<int32_t> activeIds;
51     int ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds);
52     if (ret != 0) {
53         TAG_LOGE(AAFwkTag::SER_ROUTER, "Query error:%{public}d", ret);
54         return Constants::INVALID_USERID;
55     }
56     if (activeIds.empty()) {
57         TAG_LOGE(AAFwkTag::SER_ROUTER, "activeIds empty");
58         return Constants::INVALID_USERID;
59     }
60     TAG_LOGE(AAFwkTag::SER_ROUTER, "activeId:%{public}d", activeIds[0]);
61     return activeIds[0];
62 #else
63     TAG_LOGI(AAFwkTag::SER_ROUTER, "ACCOUNT_ENABLE is false");
64     return 0;
65 #endif
66 }
67 
ConnectBundleMgrLocked()68 void SrSamgrHelper::ConnectBundleMgrLocked()
69 {
70     if (iBundleMgr_ != nullptr) {
71         return;
72     }
73     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
74     if (saManager == nullptr) {
75         TAG_LOGE(AAFwkTag::SER_ROUTER, "Get saManager failed");
76         return;
77     }
78 
79     sptr<IRemoteObject> remoteObj = saManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
80     if (remoteObj == nullptr) {
81         TAG_LOGE(AAFwkTag::SER_ROUTER, "Get remoteObj failed");
82         return;
83     }
84 
85     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) BmsDeathRecipient());
86     if (deathRecipient_ == nullptr) {
87         TAG_LOGE(AAFwkTag::SER_ROUTER, "Create deathRecipient_ failed");
88         return;
89     }
90     if ((remoteObj->IsProxyObject()) && (!remoteObj->AddDeathRecipient(deathRecipient_))) {
91         TAG_LOGE(AAFwkTag::SER_ROUTER, "Add death recipient failed");
92         return;
93     }
94     iBundleMgr_ = iface_cast<IBundleMgr>(remoteObj);
95     if (iBundleMgr_ == nullptr) {
96         TAG_LOGE(AAFwkTag::SER_ROUTER, "null iBundleMgr_");
97     }
98 }
99 
ResetProxy(const wptr<IRemoteObject> & remote)100 void SrSamgrHelper::ResetProxy(const wptr<IRemoteObject> &remote)
101 {
102     std::lock_guard<std::mutex> lock(bundleMgrMutex_);
103     if (iBundleMgr_ == nullptr) {
104         return;
105     }
106 
107     auto serviceRemote = iBundleMgr_->AsObject();
108     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
109         TAG_LOGD(AAFwkTag::SER_ROUTER, "Remove death recipient");
110         serviceRemote->RemoveDeathRecipient(deathRecipient_);
111         iBundleMgr_ = nullptr;
112     }
113 }
114 
OnRemoteDied(const wptr<IRemoteObject> & remote)115 void SrSamgrHelper::BmsDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
116 {
117     TAG_LOGI(AAFwkTag::SER_ROUTER, "Called");
118     SrSamgrHelper::GetInstance().ResetProxy(remote);
119 }
120 } // namespace AbilityRuntime
121 } // namespace OHOS
122