1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "adapter/dnetwork_adapter.h"
17686862fbSopenharmony_ci#include "bundle/bundle_manager_internal.h"
18686862fbSopenharmony_ci#include "bundle/bundle_manager_callback_stub.h"
19686862fbSopenharmony_ci#include "distributed_sched_adapter.h"
20686862fbSopenharmony_ci#include "distributed_sched_utils.h"
21686862fbSopenharmony_ci#include "dtbschedmgr_log.h"
22686862fbSopenharmony_ci#include "mission/dsched_sync_e2e.h"
23686862fbSopenharmony_ci#include "ipc_skeleton.h"
24686862fbSopenharmony_ci#include "iservice_registry.h"
25686862fbSopenharmony_ci#include "os_account_manager.h"
26686862fbSopenharmony_ci#include "system_ability_definition.h"
27686862fbSopenharmony_ci
28686862fbSopenharmony_cinamespace OHOS {
29686862fbSopenharmony_cinamespace DistributedSchedule {
30686862fbSopenharmony_ciusing namespace AccountSA;
31686862fbSopenharmony_cinamespace {
32686862fbSopenharmony_ciconst std::string TAG = "BundleManagerInternal";
33686862fbSopenharmony_ci}
34686862fbSopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(BundleManagerInternal);
35686862fbSopenharmony_cibool BundleManagerInternal::GetCallerAppIdFromBms(int32_t callingUid, std::string& appId)
36686862fbSopenharmony_ci{
37686862fbSopenharmony_ci    std::vector<std::string> bundleNameList;
38686862fbSopenharmony_ci    if (!GetBundleNameListFromBms(callingUid, bundleNameList)) {
39686862fbSopenharmony_ci        HILOGE("GetBundleNameListFromBms failed");
40686862fbSopenharmony_ci        return false;
41686862fbSopenharmony_ci    }
42686862fbSopenharmony_ci    if (bundleNameList.empty()) {
43686862fbSopenharmony_ci        HILOGE("bundleNameList empty");
44686862fbSopenharmony_ci        return false;
45686862fbSopenharmony_ci    }
46686862fbSopenharmony_ci    // getting an arbitrary bundlename for they sharing a same appId, here we get the first one
47686862fbSopenharmony_ci    return GetCallerAppIdFromBms(bundleNameList.front(), appId);
48686862fbSopenharmony_ci}
49686862fbSopenharmony_ci
50686862fbSopenharmony_cibool BundleManagerInternal::GetCallerAppIdFromBms(const std::string& bundleName, std::string& appId)
51686862fbSopenharmony_ci{
52686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
53686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
54686862fbSopenharmony_ci        HILOGE("failed to get bms");
55686862fbSopenharmony_ci        return false;
56686862fbSopenharmony_ci    }
57686862fbSopenharmony_ci    int32_t activeAccountId = 0;
58686862fbSopenharmony_ci    ErrCode err = QueryOsAccount(activeAccountId);
59686862fbSopenharmony_ci    if (err != ERR_OK) {
60686862fbSopenharmony_ci        return false;
61686862fbSopenharmony_ci    }
62686862fbSopenharmony_ci    appId = bundleMgr->GetAppIdByBundleName(bundleName, activeAccountId);
63686862fbSopenharmony_ci    HILOGD("appId:%s", GetAnonymStr(appId).c_str());
64686862fbSopenharmony_ci    return true;
65686862fbSopenharmony_ci}
66686862fbSopenharmony_ci
67686862fbSopenharmony_cibool BundleManagerInternal::GetSpecifyBundleNameFromBms(int32_t callingUid, std::string& bundleName)
68686862fbSopenharmony_ci{
69686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
70686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
71686862fbSopenharmony_ci        HILOGE("failed to get bms");
72686862fbSopenharmony_ci        return false;
73686862fbSopenharmony_ci    }
74686862fbSopenharmony_ci    bool result = bundleMgr->GetBundleNameForUid(callingUid, bundleName);
75686862fbSopenharmony_ci    if (!result) {
76686862fbSopenharmony_ci        HILOGE("Get specify bundle name for uid failed, result: %{public}d", result);
77686862fbSopenharmony_ci        return false;
78686862fbSopenharmony_ci    }
79686862fbSopenharmony_ci    return result;
80686862fbSopenharmony_ci}
81686862fbSopenharmony_ci
82686862fbSopenharmony_cibool BundleManagerInternal::GetBundleNameListFromBms(int32_t callingUid, std::vector<std::string>& bundleNameList)
83686862fbSopenharmony_ci{
84686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
85686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
86686862fbSopenharmony_ci        HILOGE("failed to get bms");
87686862fbSopenharmony_ci        return false;
88686862fbSopenharmony_ci    }
89686862fbSopenharmony_ci    bool result = bundleMgr->GetBundlesForUid(callingUid, bundleNameList);
90686862fbSopenharmony_ci    if (!result) {
91686862fbSopenharmony_ci        HILOGE("Get bundle name list for userId which the uid belongs failed, result: %{public}d", result);
92686862fbSopenharmony_ci        return false;
93686862fbSopenharmony_ci    }
94686862fbSopenharmony_ci    return result;
95686862fbSopenharmony_ci}
96686862fbSopenharmony_ci
97686862fbSopenharmony_cibool BundleManagerInternal::GetBundleNameListFromBms(int32_t callingUid,
98686862fbSopenharmony_ci    std::vector<std::u16string>& u16BundleNameList)
99686862fbSopenharmony_ci{
100686862fbSopenharmony_ci    std::vector<std::string> bundleNameList;
101686862fbSopenharmony_ci    if (!GetBundleNameListFromBms(callingUid, bundleNameList)) {
102686862fbSopenharmony_ci        HILOGE("Get bundle name list for userId which the uid belongs failed.");
103686862fbSopenharmony_ci        return false;
104686862fbSopenharmony_ci    }
105686862fbSopenharmony_ci    for (const std::string& bundleName : bundleNameList) {
106686862fbSopenharmony_ci        u16BundleNameList.emplace_back(Str8ToStr16(bundleName));
107686862fbSopenharmony_ci    }
108686862fbSopenharmony_ci    return true;
109686862fbSopenharmony_ci}
110686862fbSopenharmony_ci
111686862fbSopenharmony_cibool BundleManagerInternal::QueryAbilityInfo(const AAFwk::Want& want, AppExecFwk::AbilityInfo& abilityInfo)
112686862fbSopenharmony_ci{
113686862fbSopenharmony_ci    int32_t activeAccountId = 0;
114686862fbSopenharmony_ci    ErrCode err = QueryOsAccount(activeAccountId);
115686862fbSopenharmony_ci    if (err != ERR_OK) {
116686862fbSopenharmony_ci        return false;
117686862fbSopenharmony_ci    }
118686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
119686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
120686862fbSopenharmony_ci        HILOGE("failed to get bms");
121686862fbSopenharmony_ci        return false;
122686862fbSopenharmony_ci    }
123686862fbSopenharmony_ci    bool result = bundleMgr->QueryAbilityInfo(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
124686862fbSopenharmony_ci        | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION, activeAccountId, abilityInfo);
125686862fbSopenharmony_ci    if (!result) {
126686862fbSopenharmony_ci        HILOGW("QueryAbilityInfo failed");
127686862fbSopenharmony_ci        return false;
128686862fbSopenharmony_ci    }
129686862fbSopenharmony_ci    return true;
130686862fbSopenharmony_ci}
131686862fbSopenharmony_ci
132686862fbSopenharmony_cibool BundleManagerInternal::QueryExtensionAbilityInfo(const AAFwk::Want& want,
133686862fbSopenharmony_ci    AppExecFwk::ExtensionAbilityInfo& extensionInfo)
134686862fbSopenharmony_ci{
135686862fbSopenharmony_ci    int32_t activeAccountId = 0;
136686862fbSopenharmony_ci    ErrCode err = QueryOsAccount(activeAccountId);
137686862fbSopenharmony_ci    if (err != ERR_OK) {
138686862fbSopenharmony_ci        return false;
139686862fbSopenharmony_ci    }
140686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
141686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
142686862fbSopenharmony_ci        HILOGE("failed to get bms");
143686862fbSopenharmony_ci        return false;
144686862fbSopenharmony_ci    }
145686862fbSopenharmony_ci    std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
146686862fbSopenharmony_ci    bundleMgr->QueryExtensionAbilityInfos(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
147686862fbSopenharmony_ci        | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION, activeAccountId, extensionInfos);
148686862fbSopenharmony_ci    if (extensionInfos.empty()) {
149686862fbSopenharmony_ci        HILOGE("QueryExtensionAbilityInfo failed.");
150686862fbSopenharmony_ci        return false;
151686862fbSopenharmony_ci    }
152686862fbSopenharmony_ci    extensionInfo = extensionInfos.front();
153686862fbSopenharmony_ci    if (extensionInfo.bundleName.empty() || extensionInfo.name.empty()) {
154686862fbSopenharmony_ci        HILOGE("ExtensionAbilityInfo is empty.");
155686862fbSopenharmony_ci        return false;
156686862fbSopenharmony_ci    }
157686862fbSopenharmony_ci    HILOGD("ExtensionAbilityInfo found, name=%{public}s.", extensionInfo.name.c_str());
158686862fbSopenharmony_ci    return true;
159686862fbSopenharmony_ci}
160686862fbSopenharmony_ci
161686862fbSopenharmony_civoid BundleManagerInternal::InitAbilityInfoFromExtension(const AppExecFwk::ExtensionAbilityInfo &extensionAbilityInfo,
162686862fbSopenharmony_ci    AppExecFwk::AbilityInfo &abilityInfo)
163686862fbSopenharmony_ci{
164686862fbSopenharmony_ci    abilityInfo.bundleName = extensionAbilityInfo.bundleName;
165686862fbSopenharmony_ci    abilityInfo.name = extensionAbilityInfo.name;
166686862fbSopenharmony_ci    abilityInfo.permissions = extensionAbilityInfo.permissions;
167686862fbSopenharmony_ci    abilityInfo.visible = extensionAbilityInfo.visible;
168686862fbSopenharmony_ci}
169686862fbSopenharmony_ci
170686862fbSopenharmony_cibool BundleManagerInternal::IsSameAppId(const std::string& callerAppId, const std::string& targetBundleName)
171686862fbSopenharmony_ci{
172686862fbSopenharmony_ci    if (targetBundleName.empty() || callerAppId.empty()) {
173686862fbSopenharmony_ci        HILOGE("targetBundleName:%{public}s or callerAppId:%s is empty",
174686862fbSopenharmony_ci            targetBundleName.c_str(), GetAnonymStr(callerAppId).c_str());
175686862fbSopenharmony_ci        return false;
176686862fbSopenharmony_ci    }
177686862fbSopenharmony_ci    HILOGD("callerAppId:%s", GetAnonymStr(callerAppId).c_str());
178686862fbSopenharmony_ci    std::string calleeAppId;
179686862fbSopenharmony_ci    if (!GetCallerAppIdFromBms(targetBundleName, calleeAppId)) {
180686862fbSopenharmony_ci        HILOGE("GetCallerAppIdFromBms failed");
181686862fbSopenharmony_ci        return false;
182686862fbSopenharmony_ci    }
183686862fbSopenharmony_ci    HILOGD("calleeAppId:%s", GetAnonymStr(calleeAppId).c_str());
184686862fbSopenharmony_ci    return callerAppId == calleeAppId;
185686862fbSopenharmony_ci}
186686862fbSopenharmony_ci
187686862fbSopenharmony_cibool BundleManagerInternal::IsSameDeveloperId(const std::string &bundleNameInCurrentSide,
188686862fbSopenharmony_ci    const std::string &developerId4OtherSide)
189686862fbSopenharmony_ci{
190686862fbSopenharmony_ci    if (bundleNameInCurrentSide.empty() || developerId4OtherSide.empty()) {
191686862fbSopenharmony_ci        HILOGE("bundleNameInCurrentSide: %{public}s or developerId4OtherSide: %{public}s is empty",
192686862fbSopenharmony_ci            bundleNameInCurrentSide.c_str(), developerId4OtherSide.c_str());
193686862fbSopenharmony_ci        return false;
194686862fbSopenharmony_ci    }
195686862fbSopenharmony_ci
196686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
197686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
198686862fbSopenharmony_ci        HILOGE("get bundle manager failed");
199686862fbSopenharmony_ci        return false;
200686862fbSopenharmony_ci    }
201686862fbSopenharmony_ci    AppExecFwk::AppProvisionInfo targetAppProvisionInfo;
202686862fbSopenharmony_ci    std::vector<int32_t> ids;
203686862fbSopenharmony_ci    ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
204686862fbSopenharmony_ci    if (ret != ERR_OK || ids.empty()) {
205686862fbSopenharmony_ci        HILOGE("Get userId from active Os AccountIds fail, ret : %{public}d", ret);
206686862fbSopenharmony_ci        return false;
207686862fbSopenharmony_ci    }
208686862fbSopenharmony_ci    bundleMgr->GetAppProvisionInfo(bundleNameInCurrentSide, ids[0], targetAppProvisionInfo);
209686862fbSopenharmony_ci    return developerId4OtherSide == targetAppProvisionInfo.developerId;
210686862fbSopenharmony_ci}
211686862fbSopenharmony_ci
212686862fbSopenharmony_ciint32_t BundleManagerInternal::GetLocalBundleInfo(const std::string& bundleName,
213686862fbSopenharmony_ci    AppExecFwk::BundleInfo &localBundleInfo)
214686862fbSopenharmony_ci{
215686862fbSopenharmony_ci    auto bms = GetBundleManager();
216686862fbSopenharmony_ci    if (bms == nullptr) {
217686862fbSopenharmony_ci        HILOGE("get bundle manager failed");
218686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
219686862fbSopenharmony_ci    }
220686862fbSopenharmony_ci
221686862fbSopenharmony_ci    int32_t activeAccountId = 0;
222686862fbSopenharmony_ci    ErrCode err = QueryOsAccount(activeAccountId);
223686862fbSopenharmony_ci    if (err != ERR_OK) {
224686862fbSopenharmony_ci        return err;
225686862fbSopenharmony_ci    }
226686862fbSopenharmony_ci    if (!bms->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES,
227686862fbSopenharmony_ci        localBundleInfo, activeAccountId)) {
228686862fbSopenharmony_ci        HILOGE("get local bundle info failed");
229686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
230686862fbSopenharmony_ci    }
231686862fbSopenharmony_ci    return ERR_OK;
232686862fbSopenharmony_ci}
233686862fbSopenharmony_ci
234686862fbSopenharmony_ciint32_t BundleManagerInternal::GetLocalBundleInfoV9(const std::string& bundleName,
235686862fbSopenharmony_ci    AppExecFwk::BundleInfo &bundleInfo)
236686862fbSopenharmony_ci{
237686862fbSopenharmony_ci    auto bms = GetBundleManager();
238686862fbSopenharmony_ci    if (bms == nullptr) {
239686862fbSopenharmony_ci        HILOGE("get bundle manager failed");
240686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
241686862fbSopenharmony_ci    }
242686862fbSopenharmony_ci
243686862fbSopenharmony_ci    int32_t activeAccountId = 0;
244686862fbSopenharmony_ci    ErrCode ret = QueryOsAccount(activeAccountId);
245686862fbSopenharmony_ci    if (ret != ERR_OK) {
246686862fbSopenharmony_ci        return ret;
247686862fbSopenharmony_ci    }
248686862fbSopenharmony_ci    ret = bms->GetBundleInfoV9(bundleName,
249686862fbSopenharmony_ci        static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION),
250686862fbSopenharmony_ci        bundleInfo, activeAccountId);
251686862fbSopenharmony_ci    if (ret != ERR_OK) {
252686862fbSopenharmony_ci        HILOGE("get local bundle info failed, ret: %{public}d", ret);
253686862fbSopenharmony_ci    }
254686862fbSopenharmony_ci    return ret;
255686862fbSopenharmony_ci}
256686862fbSopenharmony_ci
257686862fbSopenharmony_cibool BundleManagerInternal::GetContinueBundle4Src(const std::string &srcBundleName,
258686862fbSopenharmony_ci    std::vector<std::string> &bundleNameList)
259686862fbSopenharmony_ci{
260686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
261686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
262686862fbSopenharmony_ci        HILOGE("get bundle manager failed");
263686862fbSopenharmony_ci        return false;
264686862fbSopenharmony_ci    }
265686862fbSopenharmony_ci
266686862fbSopenharmony_ci    int32_t activeAccountId = 0;
267686862fbSopenharmony_ci    ErrCode ret = QueryOsAccount(activeAccountId);
268686862fbSopenharmony_ci    if (ret != ERR_OK) {
269686862fbSopenharmony_ci        HILOGE("get os account id failed");
270686862fbSopenharmony_ci        return false;
271686862fbSopenharmony_ci    }
272686862fbSopenharmony_ci    ret = bundleMgr->GetContinueBundleNames(srcBundleName, bundleNameList, activeAccountId);
273686862fbSopenharmony_ci    if (ret != ERR_OK || bundleNameList.empty()) {
274686862fbSopenharmony_ci        HILOGW("No APP with specified bundle name(%{public}s) configured in continue Bundle; ret: %{public}d",
275686862fbSopenharmony_ci            srcBundleName.c_str(), ret);
276686862fbSopenharmony_ci        return false;
277686862fbSopenharmony_ci    }
278686862fbSopenharmony_ci    return true;
279686862fbSopenharmony_ci}
280686862fbSopenharmony_ci
281686862fbSopenharmony_cibool BundleManagerInternal::GetAppProvisionInfo4CurrentUser(const std::string &bundleName,
282686862fbSopenharmony_ci    AppExecFwk::AppProvisionInfo &appProvisionInfo)
283686862fbSopenharmony_ci{
284686862fbSopenharmony_ci    sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleManager();
285686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
286686862fbSopenharmony_ci        HILOGE("get bundle manager failed");
287686862fbSopenharmony_ci        return false;
288686862fbSopenharmony_ci    }
289686862fbSopenharmony_ci    std::vector<int32_t> ids;
290686862fbSopenharmony_ci    ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
291686862fbSopenharmony_ci    if (ret != ERR_OK || ids.empty()) {
292686862fbSopenharmony_ci        HILOGE("Get userId from active Os AccountIds fail, ret : %{public}d", ret);
293686862fbSopenharmony_ci        return false;
294686862fbSopenharmony_ci    }
295686862fbSopenharmony_ci    ErrCode result = bundleMgr->GetAppProvisionInfo(bundleName, ids[0], appProvisionInfo);
296686862fbSopenharmony_ci    HILOGI("find dst bundle name for diff bundle continue. dst developer id: %{public}s; ",
297686862fbSopenharmony_ci        GetAnonymStr(appProvisionInfo.developerId).c_str());
298686862fbSopenharmony_ci    return result == ERR_OK;
299686862fbSopenharmony_ci}
300686862fbSopenharmony_ci
301686862fbSopenharmony_ciint32_t BundleManagerInternal::CheckRemoteBundleInfoForContinuation(const std::string& dstDeviceId,
302686862fbSopenharmony_ci    const std::string& bundleName, AppExecFwk::DistributedBundleInfo& remoteBundleInfo)
303686862fbSopenharmony_ci{
304686862fbSopenharmony_ci    if (bundleName.empty()) {
305686862fbSopenharmony_ci        HILOGE("bundle name empty");
306686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
307686862fbSopenharmony_ci    }
308686862fbSopenharmony_ci    HILOGI("bundleName: %{public}s", bundleName.c_str());
309686862fbSopenharmony_ci
310686862fbSopenharmony_ci    auto bms = GetBundleManager();
311686862fbSopenharmony_ci    if (bms == nullptr) {
312686862fbSopenharmony_ci        HILOGE("get bundle manager failed");
313686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
314686862fbSopenharmony_ci    }
315686862fbSopenharmony_ci
316686862fbSopenharmony_ci    bool isInstalled = bms->GetDistributedBundleInfo(dstDeviceId, bundleName, remoteBundleInfo);
317686862fbSopenharmony_ci    if (isInstalled) {
318686862fbSopenharmony_ci        return ERR_OK;
319686862fbSopenharmony_ci    }
320686862fbSopenharmony_ci
321686862fbSopenharmony_ci    AppExecFwk::BundleInfo localBundleInfo;
322686862fbSopenharmony_ci    if (GetLocalBundleInfo(bundleName, localBundleInfo) != ERR_OK) {
323686862fbSopenharmony_ci        HILOGE("get local bundle info failed");
324686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
325686862fbSopenharmony_ci    }
326686862fbSopenharmony_ci    if (localBundleInfo.entryInstallationFree) {
327686862fbSopenharmony_ci        return CONTINUE_REMOTE_UNINSTALLED_SUPPORT_FREEINSTALL;
328686862fbSopenharmony_ci    }
329686862fbSopenharmony_ci    return CONTINUE_REMOTE_UNINSTALLED_UNSUPPORT_FREEINSTALL;
330686862fbSopenharmony_ci}
331686862fbSopenharmony_ci
332686862fbSopenharmony_cibool BundleManagerInternal::CheckIfRemoteCanInstall(const AAFwk::Want& want, int32_t missionId)
333686862fbSopenharmony_ci{
334686862fbSopenharmony_ci    std::string bundleName = want.GetElement().GetBundleName();
335686862fbSopenharmony_ci    std::string moduleName = want.GetElement().GetModuleName();
336686862fbSopenharmony_ci    std::string abilityName = want.GetElement().GetAbilityName();
337686862fbSopenharmony_ci    std::string deviceId = want.GetElement().GetDeviceID();
338686862fbSopenharmony_ci    std::string udid = DnetworkAdapter::GetInstance()->GetUdidByNetworkId(deviceId);
339686862fbSopenharmony_ci    HILOGD("bundleName = %{public}s, moduleName = %{public}s, abilityName = %{public}s, udid = %{public}s",
340686862fbSopenharmony_ci        bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), GetAnonymStr(udid).c_str());
341686862fbSopenharmony_ci
342686862fbSopenharmony_ci    if (bundleName.empty() || moduleName.empty() || abilityName.empty() || udid.empty()) {
343686862fbSopenharmony_ci        HILOGE("udid or bundle or module or ability name is empty");
344686862fbSopenharmony_ci        return false;
345686862fbSopenharmony_ci    }
346686862fbSopenharmony_ci    auto bms = GetBundleManager();
347686862fbSopenharmony_ci    if (bms == nullptr) {
348686862fbSopenharmony_ci        HILOGE("get bundle manager failed");
349686862fbSopenharmony_ci        return false;
350686862fbSopenharmony_ci    }
351686862fbSopenharmony_ci
352686862fbSopenharmony_ci    AAFwk::Want newWant;
353686862fbSopenharmony_ci    newWant.SetElementName(udid, bundleName, abilityName, moduleName);
354686862fbSopenharmony_ci    int32_t activeAccountId = 0;
355686862fbSopenharmony_ci    ErrCode err = QueryOsAccount(activeAccountId);
356686862fbSopenharmony_ci    if (err != ERR_OK) {
357686862fbSopenharmony_ci        return false;
358686862fbSopenharmony_ci    }
359686862fbSopenharmony_ci    bool ret = bms->CheckAbilityEnableInstall(newWant, missionId, activeAccountId, new DmsBundleManagerCallbackStub());
360686862fbSopenharmony_ci    if (ret != true) {
361686862fbSopenharmony_ci        HILOGE("CheckAbilityEnableInstall from bms failed");
362686862fbSopenharmony_ci    }
363686862fbSopenharmony_ci    return ret;
364686862fbSopenharmony_ci}
365686862fbSopenharmony_ci
366686862fbSopenharmony_cisptr<AppExecFwk::IBundleMgr> BundleManagerInternal::GetBundleManager()
367686862fbSopenharmony_ci{
368686862fbSopenharmony_ci    sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
369686862fbSopenharmony_ci    if (samgrProxy == nullptr) {
370686862fbSopenharmony_ci        return nullptr;
371686862fbSopenharmony_ci    }
372686862fbSopenharmony_ci    sptr<IRemoteObject> bmsProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
373686862fbSopenharmony_ci    if (bmsProxy == nullptr) {
374686862fbSopenharmony_ci        HILOGE("failed to get bms from samgr");
375686862fbSopenharmony_ci        return nullptr;
376686862fbSopenharmony_ci    }
377686862fbSopenharmony_ci    return iface_cast<AppExecFwk::IBundleMgr>(bmsProxy);
378686862fbSopenharmony_ci}
379686862fbSopenharmony_ci
380686862fbSopenharmony_cisptr<AppExecFwk::IDistributedBms> BundleManagerInternal::GetDistributedBundleManager()
381686862fbSopenharmony_ci{
382686862fbSopenharmony_ci    sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
383686862fbSopenharmony_ci    if (samgrProxy == nullptr) {
384686862fbSopenharmony_ci        HILOGE("failed to get samgrProxy from dbms");
385686862fbSopenharmony_ci        return nullptr;
386686862fbSopenharmony_ci    }
387686862fbSopenharmony_ci    sptr<IRemoteObject> dbmsProxy = samgrProxy->GetSystemAbility(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
388686862fbSopenharmony_ci    if (dbmsProxy == nullptr) {
389686862fbSopenharmony_ci        HILOGE("failed to get dbmsProxy from dbms");
390686862fbSopenharmony_ci        return nullptr;
391686862fbSopenharmony_ci    }
392686862fbSopenharmony_ci    return iface_cast<AppExecFwk::IDistributedBms>(dbmsProxy);
393686862fbSopenharmony_ci}
394686862fbSopenharmony_ci
395686862fbSopenharmony_ciint32_t BundleManagerInternal::GetUidFromBms(const std::string& bundleName)
396686862fbSopenharmony_ci{
397686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
398686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
399686862fbSopenharmony_ci        HILOGE("failed to get bms");
400686862fbSopenharmony_ci        return -1;
401686862fbSopenharmony_ci    }
402686862fbSopenharmony_ci    int32_t activeAccountId = 0;
403686862fbSopenharmony_ci    ErrCode err = QueryOsAccount(activeAccountId);
404686862fbSopenharmony_ci    if (err != ERR_OK) {
405686862fbSopenharmony_ci        return err;
406686862fbSopenharmony_ci    }
407686862fbSopenharmony_ci    return bundleMgr->GetUidByBundleName(bundleName, activeAccountId);
408686862fbSopenharmony_ci}
409686862fbSopenharmony_ci
410686862fbSopenharmony_ciint32_t BundleManagerInternal::GetBundleNameId(const std::string& bundleName, uint16_t& bundleNameId)
411686862fbSopenharmony_ci{
412686862fbSopenharmony_ci    HILOGD("called.");
413686862fbSopenharmony_ci    bool ret = DmsBmStorage::GetInstance()->GetBundleNameId(bundleName, bundleNameId);
414686862fbSopenharmony_ci    HILOGI("bundleNameId: %{public}d end.", bundleNameId);
415686862fbSopenharmony_ci    if (!ret) {
416686862fbSopenharmony_ci        HILOGE("can not get bundleNameId by bundleName");
417686862fbSopenharmony_ci        return CAN_NOT_FOUND_ABILITY_ERR;
418686862fbSopenharmony_ci    }
419686862fbSopenharmony_ci    HILOGD("end.");
420686862fbSopenharmony_ci    return ERR_OK;
421686862fbSopenharmony_ci}
422686862fbSopenharmony_ci
423686862fbSopenharmony_cistd::string BundleManagerInternal::GetContinueType(const std::string &networkId,
424686862fbSopenharmony_ci    std::string &bundleName, uint8_t continueTypeId)
425686862fbSopenharmony_ci{
426686862fbSopenharmony_ci    HILOGD("called.");
427686862fbSopenharmony_ci    std::string continueType = DmsBmStorage::GetInstance()->GetContinueType(networkId, bundleName, continueTypeId);
428686862fbSopenharmony_ci    HILOGI("continueType: %{public}s end.", continueType.c_str());
429686862fbSopenharmony_ci    if (continueType == "") {
430686862fbSopenharmony_ci        HILOGE("can not get continueType!");
431686862fbSopenharmony_ci    }
432686862fbSopenharmony_ci    HILOGD("end.");
433686862fbSopenharmony_ci    return continueType;
434686862fbSopenharmony_ci}
435686862fbSopenharmony_ci
436686862fbSopenharmony_ciint32_t BundleManagerInternal::GetContinueTypeId(const std::string &bundleName,
437686862fbSopenharmony_ci    const std::string &abilityName, uint8_t &continueTypeId)
438686862fbSopenharmony_ci{
439686862fbSopenharmony_ci    HILOGD("called.");
440686862fbSopenharmony_ci    bool ret = DmsBmStorage::GetInstance()->GetContinueTypeId(bundleName, abilityName, continueTypeId);
441686862fbSopenharmony_ci    HILOGI("ContinueTypeId: %{public}d ", continueTypeId);
442686862fbSopenharmony_ci    if (!ret) {
443686862fbSopenharmony_ci        HILOGE("can not get ContinueTypeId!");
444686862fbSopenharmony_ci        return CAN_NOT_FOUND_ABILITY_ERR;
445686862fbSopenharmony_ci    }
446686862fbSopenharmony_ci    HILOGD("end.");
447686862fbSopenharmony_ci    return ERR_OK;
448686862fbSopenharmony_ci}
449686862fbSopenharmony_ci
450686862fbSopenharmony_cistd::string BundleManagerInternal::GetAbilityName(const std::string &networkId,
451686862fbSopenharmony_ci    std::string &bundleName, std::string &continueType)
452686862fbSopenharmony_ci{
453686862fbSopenharmony_ci    HILOGI("called. continueType: %{public}s ", continueType.c_str());
454686862fbSopenharmony_ci    std::string abilityName = DmsBmStorage::GetInstance()->GetAbilityName(networkId, bundleName, continueType);
455686862fbSopenharmony_ci    if (abilityName == "") {
456686862fbSopenharmony_ci        HILOGE("can not get abilityName!");
457686862fbSopenharmony_ci    }
458686862fbSopenharmony_ci    HILOGD("end.");
459686862fbSopenharmony_ci    return abilityName;
460686862fbSopenharmony_ci}
461686862fbSopenharmony_ci
462686862fbSopenharmony_ciint32_t BundleManagerInternal::GetBundleNameById(const std::string& networkId,
463686862fbSopenharmony_ci    const uint16_t& bundleNameId, std::string& bundleName)
464686862fbSopenharmony_ci{
465686862fbSopenharmony_ci    HILOGD("called.");
466686862fbSopenharmony_ci    bool result = DmsBmStorage::GetInstance()->GetDistributedBundleName(networkId, bundleNameId, bundleName);
467686862fbSopenharmony_ci    if (!result) {
468686862fbSopenharmony_ci        HILOGE("failed to get bundleName by bundleNameId, try syncing");
469686862fbSopenharmony_ci        DmsKvSyncE2E::GetInstance()->PushAndPullData(networkId);
470686862fbSopenharmony_ci        return CAN_NOT_FOUND_ABILITY_ERR;
471686862fbSopenharmony_ci    }
472686862fbSopenharmony_ci    HILOGD("end.");
473686862fbSopenharmony_ci    return ERR_OK;
474686862fbSopenharmony_ci}
475686862fbSopenharmony_ci
476686862fbSopenharmony_ciint32_t BundleManagerInternal::GetApplicationInfoFromBms(const std::string& bundleName,
477686862fbSopenharmony_ci    const AppExecFwk::BundleFlag flag, const int32_t userId, AppExecFwk::ApplicationInfo &appInfo)
478686862fbSopenharmony_ci{
479686862fbSopenharmony_ci    auto bundleMgr = GetBundleManager();
480686862fbSopenharmony_ci    if (bundleMgr == nullptr) {
481686862fbSopenharmony_ci        HILOGE("failed to get bms");
482686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
483686862fbSopenharmony_ci    }
484686862fbSopenharmony_ci    std::string identity = IPCSkeleton::ResetCallingIdentity();
485686862fbSopenharmony_ci    bundleMgr->GetApplicationInfo(bundleName, flag, userId, appInfo);
486686862fbSopenharmony_ci    IPCSkeleton::SetCallingIdentity(identity);
487686862fbSopenharmony_ci    return ERR_OK;
488686862fbSopenharmony_ci}
489686862fbSopenharmony_ci
490686862fbSopenharmony_ciErrCode BundleManagerInternal::QueryOsAccount(int32_t& activeAccountId)
491686862fbSopenharmony_ci{
492686862fbSopenharmony_ci#ifdef OS_ACCOUNT_PART
493686862fbSopenharmony_ci    std::vector<int32_t> ids;
494686862fbSopenharmony_ci    ErrCode err = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
495686862fbSopenharmony_ci    if (err != ERR_OK || ids.empty()) {
496686862fbSopenharmony_ci        HILOGE("QueryActiveOsAccountIds passing param invalid or return error!, err : %{public}d", err);
497686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
498686862fbSopenharmony_ci    }
499686862fbSopenharmony_ci    activeAccountId = ids[0];
500686862fbSopenharmony_ci#endif
501686862fbSopenharmony_ci    return ERR_OK;
502686862fbSopenharmony_ci}
503686862fbSopenharmony_ci} // namespace DistributedSchedule
504686862fbSopenharmony_ci} // namespace OHOS
505