1 /*
2 * Copyright (c) 2024-2025 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 "bundle_clone_installer.h"
17
18 #include <sstream>
19
20 #include "ability_manager_helper.h"
21 #include "account_helper.h"
22 #include "bms_extension_data_mgr.h"
23 #include "bundle_mgr_service.h"
24 #include "bundle_permission_mgr.h"
25 #include "bundle_resource_helper.h"
26 #include "bundle_service_constants.h"
27 #include "code_protect_bundle_info.h"
28 #include "datetime_ex.h"
29 #include "hitrace_meter.h"
30 #include "installd_client.h"
31 #include "inner_bundle_clone_common.h"
32 #include "parameters.h"
33 #include "perf_profile.h"
34 #include "scope_guard.h"
35 #include "ipc_skeleton.h"
36
37 namespace OHOS {
38 namespace AppExecFwk {
39 using namespace OHOS::Security;
40
41 std::mutex gCloneInstallerMutex;
42
BundleCloneInstaller()43 BundleCloneInstaller::BundleCloneInstaller()
44 {
45 APP_LOGD("bundle clone installer instance is created");
46 }
47
~BundleCloneInstaller()48 BundleCloneInstaller::~BundleCloneInstaller()
49 {
50 APP_LOGD("bundle clone installer instance is destroyed");
51 }
52
InstallCloneApp(const std::string & bundleName,const int32_t userId,int32_t & appIndex)53 ErrCode BundleCloneInstaller::InstallCloneApp(const std::string &bundleName,
54 const int32_t userId, int32_t &appIndex)
55 {
56 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
57 APP_LOGD("InstallCloneApp %{public}s begin", bundleName.c_str());
58
59 PerfProfile::GetInstance().SetBundleInstallStartTime(GetTickCount());
60
61 ErrCode result = ProcessCloneBundleInstall(bundleName, userId, appIndex);
62 NotifyBundleEvents installRes = {
63 .type = NotifyType::INSTALL,
64 .resultCode = result,
65 .accessTokenId = accessTokenId_,
66 .uid = uid_,
67 .appIndex = appIndex,
68 .bundleName = bundleName,
69 .appId = appId_,
70 .appIdentifier = appIdentifier_,
71 };
72 std::shared_ptr<BundleCommonEventMgr> commonEventMgr = std::make_shared<BundleCommonEventMgr>();
73 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
74 commonEventMgr->NotifyBundleStatus(installRes, dataMgr);
75
76 SendBundleSystemEvent(bundleName, BundleEventType::INSTALL, userId, appIndex,
77 false, false, InstallScene::NORMAL, result);
78
79 ResetInstallProperties();
80 PerfProfile::GetInstance().SetBundleInstallEndTime(GetTickCount());
81 return result;
82 }
83
UninstallCloneApp(const std::string & bundleName,const int32_t userId,const int32_t appIndex,bool sync)84 ErrCode BundleCloneInstaller::UninstallCloneApp(
85 const std::string &bundleName, const int32_t userId, const int32_t appIndex, bool sync)
86 {
87 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
88 APP_LOGD("UninstallCloneApp %{public}s _ %{public}d begin", bundleName.c_str(), appIndex);
89
90 PerfProfile::GetInstance().SetBundleUninstallStartTime(GetTickCount());
91
92 ErrCode result = ProcessCloneBundleUninstall(bundleName, userId, appIndex, sync);
93 NotifyBundleEvents installRes = {
94 .type = NotifyType::UNINSTALL_BUNDLE,
95 .resultCode = result,
96 .accessTokenId = accessTokenId_,
97 .bundleName = bundleName,
98 .uid = uid_,
99 .appIndex = appIndex,
100 .appId = appId_,
101 .appIdentifier = appIdentifier_,
102 .developerId = GetDeveloperId(bundleName),
103 .assetAccessGroups = GetAssetAccessGroups(bundleName)
104 };
105 std::shared_ptr<BundleCommonEventMgr> commonEventMgr = std::make_shared<BundleCommonEventMgr>();
106 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
107 commonEventMgr->NotifyBundleStatus(installRes, dataMgr);
108
109 SendBundleSystemEvent(bundleName, BundleEventType::UNINSTALL, userId, appIndex,
110 false, false, InstallScene::NORMAL, result);
111
112 ResetInstallProperties();
113
114 PerfProfile::GetInstance().SetBundleUninstallEndTime(GetTickCount());
115 return result;
116 }
117
UninstallAllCloneApps(const std::string & bundleName,bool sync,int32_t userId)118 ErrCode BundleCloneInstaller::UninstallAllCloneApps(const std::string &bundleName, bool sync, int32_t userId)
119 {
120 // All clone will be uninstalled when the original application is updated or uninstalled
121 APP_LOGI_NOFUNC("UninstallAllCloneApps begin");
122 if (bundleName.empty()) {
123 APP_LOGE("UninstallAllCloneApps failed due to empty bundle name");
124 return ERR_APPEXECFWK_CLONE_UNINSTALL_INVALID_BUNDLE_NAME;
125 }
126 if (GetDataMgr() != ERR_OK) {
127 APP_LOGE("Get dataMgr shared_ptr nullptr");
128 return ERR_APPEXECFWK_CLONE_UNINSTALL_INTERNAL_ERROR;
129 }
130 if (!dataMgr_->HasUserId(userId)) {
131 APP_LOGE("install clone app user %{public}d not exist", userId);
132 return ERR_APPEXECFWK_CLONE_UNINSTALL_USER_NOT_EXIST;
133 }
134 InnerBundleInfo info;
135 bool isExist = dataMgr_->FetchInnerBundleInfo(bundleName, info);
136 if (!isExist) {
137 APP_LOGE("the bundle is not installed");
138 return ERR_APPEXECFWK_CLONE_UNINSTALL_APP_NOT_EXISTED;
139 }
140 InnerBundleUserInfo userInfo;
141 if (!info.GetInnerBundleUserInfo(userId, userInfo)) {
142 APP_LOGE_NOFUNC("the origin application is not installed at current user");
143 return ERR_APPEXECFWK_CLONE_UNINSTALL_NOT_INSTALLED_AT_SPECIFIED_USERID;
144 }
145 ErrCode result = ERR_OK;
146 for (auto it = userInfo.cloneInfos.begin(); it != userInfo.cloneInfos.end(); it++) {
147 if (UninstallCloneApp(bundleName, userId, atoi(it->first.c_str()), sync) != ERR_OK) {
148 APP_LOGE("UninstallCloneApp failed, appIndex %{public}s", it->first.c_str());
149 result = ERR_APPEXECFWK_CLONE_UNINSTALL_INTERNAL_ERROR;
150 }
151 }
152 APP_LOGI_NOFUNC("UninstallAllCloneApps end");
153 return result;
154 }
155
ProcessCloneBundleInstall(const std::string & bundleName,const int32_t userId,int32_t & appIndex)156 ErrCode BundleCloneInstaller::ProcessCloneBundleInstall(const std::string &bundleName,
157 const int32_t userId, int32_t &appIndex)
158 {
159 if (bundleName.empty()) {
160 APP_LOGE("the bundle name is empty");
161 return ERR_APPEXECFWK_CLONE_INSTALL_PARAM_ERROR;
162 }
163
164 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
165
166 std::lock_guard<std::mutex> cloneGuard(gCloneInstallerMutex);
167 // 1. check whether original application installed or not
168 InnerBundleInfo info;
169 bool isExist = dataMgr->FetchInnerBundleInfo(bundleName, info);
170 if (!isExist) {
171 APP_LOGE("the bundle is not installed");
172 return ERR_APPEXECFWK_CLONE_INSTALL_APP_NOT_EXISTED;
173 }
174
175 // 2. obtain userId
176 if (userId < Constants::DEFAULT_USERID) {
177 APP_LOGE("userId(%{public}d) invalid", userId);
178 return ERR_APPEXECFWK_CLONE_INSTALL_USER_NOT_EXIST;
179 }
180 if (!dataMgr->HasUserId(userId)) {
181 APP_LOGE("install clone app user %{public}d not exist", userId);
182 return ERR_APPEXECFWK_CLONE_INSTALL_USER_NOT_EXIST;
183 }
184
185 // 3. check whether original application installed at current userId or not
186 InnerBundleUserInfo userInfo;
187 if (!info.GetInnerBundleUserInfo(userId, userInfo)) {
188 APP_LOGE("the origin application is not installed at current user");
189 return ERR_APPEXECFWK_CLONE_INSTALL_NOT_INSTALLED_AT_SPECIFIED_USERID;
190 }
191
192 ErrCode ackRes = info.VerifyAndAckCloneAppIndex(userId, appIndex);
193 if (ackRes != ERR_OK) {
194 APP_LOGE("installCloneApp fail for verifyAndAck res %{public}d", ackRes);
195 return ackRes;
196 }
197
198 // uid
199 std::string cloneBundleName = BundleCloneCommonHelper::GetCloneBundleIdKey(bundleName, appIndex);
200 InnerBundleUserInfo tmpUserInfo;
201 tmpUserInfo.bundleName = cloneBundleName;
202 tmpUserInfo.bundleUserInfo.userId = userId;
203 dataMgr->GenerateUidAndGid(tmpUserInfo);
204 int32_t uid = tmpUserInfo.uid;
205
206 // 4. generate the accesstoken id and inherit original permissions
207 info.SetAppIndex(appIndex);
208 Security::AccessToken::AccessTokenIDEx newTokenIdEx;
209 Security::AccessToken::HapInfoCheckResult checkResult;
210 AppProvisionInfo appProvisionInfo;
211 if (dataMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo) != ERR_OK) {
212 APP_LOGE("GetAppProvisionInfo failed bundleName:%{public}s", bundleName.c_str());
213 }
214 if (BundlePermissionMgr::InitHapToken(info, userId, 0, newTokenIdEx, checkResult,
215 appProvisionInfo.appServiceCapabilities) != ERR_OK) {
216 auto result = BundlePermissionMgr::GetCheckResultMsg(checkResult);
217 APP_LOGE("bundleName:%{public}s InitHapToken failed, %{public}s", bundleName.c_str(), result.c_str());
218 return ERR_APPEXECFWK_INSTALL_GRANT_REQUEST_PERMISSIONS_FAILED;
219 }
220 ScopeGuard applyAccessTokenGuard([&] {
221 BundlePermissionMgr::DeleteAccessTokenId(newTokenIdEx.tokenIdExStruct.tokenID);
222 });
223
224 InnerBundleCloneInfo attr = {
225 .userId = userId,
226 .appIndex = appIndex,
227 .uid = uid,
228 .accessTokenId = newTokenIdEx.tokenIdExStruct.tokenID,
229 .accessTokenIdEx = newTokenIdEx.tokenIDEx,
230 .gids = tmpUserInfo.gids,
231 };
232 uid_ = uid;
233 accessTokenId_ = newTokenIdEx.tokenIdExStruct.tokenID;
234 versionCode_ = info.GetVersionCode();
235 appId_ = info.GetAppId();
236 appIdentifier_ = info.GetAppIdentifier();
237
238 ScopeGuard createCloneDataDirGuard([&] { RemoveCloneDataDir(bundleName, userId, appIndex, true); });
239 ErrCode result = CreateCloneDataDir(info, userId, uid, appIndex);
240 if (result != ERR_OK) {
241 APP_LOGE("InstallCloneApp create clone dir failed");
242 return result;
243 }
244
245 ScopeGuard addCloneBundleGuard([&] { dataMgr->RemoveCloneBundle(bundleName, userId, appIndex); });
246 ErrCode addRes = dataMgr->AddCloneBundle(bundleName, attr);
247 if (addRes != ERR_OK) {
248 APP_LOGE("dataMgr add clone bundle fail, bundleName: %{public}s, userId: %{public}d, appIndex: %{public}d",
249 bundleName.c_str(), userId, appIndex);
250 return addRes;
251 }
252
253 ScopeGuard createEl5DirGuard([&] { RemoveEl5Dir(userInfo, uid, userId, appIndex); });
254 CreateEl5Dir(info, userId, uid, appIndex);
255
256 // process icon and label
257 {
258 auto appIndexes = info.GetCloneBundleAppIndexes();
259 // appIndex not exist, need parse
260 if (appIndexes.find(appIndex) == appIndexes.end()) {
261 BundleResourceHelper::AddCloneBundleResourceInfo(bundleName, appIndex, userId);
262 }
263 }
264
265 // total to commit, avoid rollback
266 applyAccessTokenGuard.Dismiss();
267 createCloneDataDirGuard.Dismiss();
268 addCloneBundleGuard.Dismiss();
269 createEl5DirGuard.Dismiss();
270 APP_LOGI("InstallCloneApp %{public}s appIndex:%{public}d succesfully", bundleName.c_str(), appIndex);
271 return ERR_OK;
272 }
273
ProcessCloneBundleUninstall(const std::string & bundleName,int32_t userId,int32_t appIndex,bool sync)274 ErrCode BundleCloneInstaller::ProcessCloneBundleUninstall(const std::string &bundleName,
275 int32_t userId, int32_t appIndex, bool sync)
276 {
277 if (bundleName.empty()) {
278 APP_LOGE("UninstallCloneApp failed due to empty bundle name");
279 return ERR_APPEXECFWK_CLONE_UNINSTALL_INVALID_BUNDLE_NAME;
280 }
281 if (appIndex < ServiceConstants::CLONE_APP_INDEX_MIN || appIndex > ServiceConstants::CLONE_APP_INDEX_MAX) {
282 APP_LOGE("Add Clone Bundle Fail, appIndex: %{public}d not in valid range", appIndex);
283 return ERR_APPEXECFWK_CLONE_UNINSTALL_INVALID_APP_INDEX;
284 }
285 if (GetDataMgr() != ERR_OK) {
286 APP_LOGE("Get dataMgr shared_ptr nullptr");
287 return ERR_APPEXECFWK_CLONE_UNINSTALL_INTERNAL_ERROR;
288 }
289 std::lock_guard<std::mutex> cloneGuard(gCloneInstallerMutex);
290 if (!dataMgr_->HasUserId(userId)) {
291 APP_LOGE("install clone app user %{public}d not exist", userId);
292 return ERR_APPEXECFWK_CLONE_UNINSTALL_USER_NOT_EXIST;
293 }
294 InnerBundleInfo info;
295 bool isExist = dataMgr_->FetchInnerBundleInfo(bundleName, info);
296 if (!isExist) {
297 APP_LOGE("the bundle is not installed");
298 return ERR_APPEXECFWK_CLONE_UNINSTALL_APP_NOT_EXISTED;
299 }
300 InnerBundleUserInfo userInfo;
301 if (!info.GetInnerBundleUserInfo(userId, userInfo)) {
302 APP_LOGE("the origin application is not installed at current user");
303 return ERR_APPEXECFWK_CLONE_UNINSTALL_NOT_INSTALLED_AT_SPECIFIED_USERID;
304 }
305 auto it = userInfo.cloneInfos.find(std::to_string(appIndex));
306 if (it == userInfo.cloneInfos.end()) {
307 APP_LOGE("the clone app is not installed");
308 return ERR_APPEXECFWK_CLONE_UNINSTALL_APP_NOT_CLONED;
309 }
310 uid_ = it->second.uid;
311 accessTokenId_ = it->second.accessTokenId;
312 versionCode_ = info.GetVersionCode();
313 appId_ = info.GetAppId();
314 appIdentifier_ = info.GetAppIdentifier();
315 if (!AbilityManagerHelper::UninstallApplicationProcesses(bundleName, uid_, false, appIndex)) {
316 APP_LOGE("fail to kill running application");
317 }
318 if (BundlePermissionMgr::DeleteAccessTokenId(accessTokenId_) !=
319 AccessToken::AccessTokenKitRet::RET_SUCCESS) {
320 APP_LOGE("delete AT failed clone");
321 }
322 if (dataMgr_->RemoveCloneBundle(bundleName, userId, appIndex)) {
323 APP_LOGE("RemoveCloneBundle failed");
324 return ERR_APPEXECFWK_CLONE_UNINSTALL_INTERNAL_ERROR;
325 }
326 if (RemoveCloneDataDir(bundleName, userId, appIndex, sync) != ERR_OK) {
327 APP_LOGW("RemoveCloneDataDir failed");
328 }
329 RemoveEl5Dir(userInfo, uid_, userId, appIndex);
330 // process icon and label
331 {
332 InnerBundleInfo info;
333 if (dataMgr_->FetchInnerBundleInfo(bundleName, info)) {
334 auto appIndexes = info.GetCloneBundleAppIndexes();
335 if (appIndexes.find(appIndex) == appIndexes.end()) {
336 BundleResourceHelper::DeleteCloneBundleResourceInfo(bundleName, appIndex, userId);
337 }
338 }
339 }
340 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
341 std::shared_ptr<AppControlManager> appControlMgr = DelayedSingleton<AppControlManager>::GetInstance();
342 if (appControlMgr != nullptr) {
343 APP_LOGD("Delete disposed rule when bundleName :%{public}s uninstall", bundleName.c_str());
344 appControlMgr->DeleteAllDisposedRuleByBundle(info, appIndex, userId);
345 }
346 #endif
347 UninstallDebugAppSandbox(bundleName, uid_, appIndex, info);
348 APP_LOGI("UninstallCloneApp %{public}s _ %{public}d succesfully", bundleName.c_str(), appIndex);
349 return ERR_OK;
350 }
351
UninstallDebugAppSandbox(const std::string & bundleName,const int32_t uid,int32_t appIndex,const InnerBundleInfo & innerBundleInfo)352 void BundleCloneInstaller::UninstallDebugAppSandbox(const std::string &bundleName, const int32_t uid,
353 int32_t appIndex, const InnerBundleInfo& innerBundleInfo)
354 {
355 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
356 APP_LOGD("call UninstallDebugAppSandbox start");
357 bool isDebugApp = innerBundleInfo.GetBaseApplicationInfo().appProvisionType == Constants::APP_PROVISION_TYPE_DEBUG;
358 bool isDeveloperMode = OHOS::system::GetBoolParameter(ServiceConstants::DEVELOPERMODE_STATE, false);
359 if (isDeveloperMode && isDebugApp) {
360 AppSpawnRemoveSandboxDirMsg removeSandboxDirMsg;
361 removeSandboxDirMsg.code = MSG_UNINSTALL_DEBUG_HAP;
362 removeSandboxDirMsg.bundleName = bundleName;
363 removeSandboxDirMsg.bundleIndex = appIndex;
364 removeSandboxDirMsg.uid = uid;
365 removeSandboxDirMsg.flags = APP_FLAGS_CLONE_ENABLE;
366 if (BundleAppSpawnClient::GetInstance().RemoveSandboxDir(removeSandboxDirMsg) != 0) {
367 APP_LOGE("RemoveSandboxDir failed");
368 }
369 }
370 APP_LOGD("call UninstallDebugAppSandbox end");
371 }
372
373
CreateCloneDataDir(InnerBundleInfo & info,const int32_t userId,const int32_t & uid,const int32_t & appIndex) const374 ErrCode BundleCloneInstaller::CreateCloneDataDir(InnerBundleInfo &info,
375 const int32_t userId, const int32_t &uid, const int32_t &appIndex) const
376 {
377 APP_LOGD("CreateCloneDataDir %{public}s _ %{public}d begin", info.GetBundleName().c_str(), appIndex);
378 std::string innerDataDir = BundleCloneCommonHelper::GetCloneDataDir(info.GetBundleName(), appIndex);
379 CreateDirParam createDirParam;
380 createDirParam.bundleName = innerDataDir;
381 createDirParam.userId = userId;
382 createDirParam.uid = uid;
383 createDirParam.gid = uid;
384 createDirParam.apl = info.GetAppPrivilegeLevel();
385 createDirParam.isPreInstallApp = info.IsPreInstallApp();
386 createDirParam.debug = info.GetBaseApplicationInfo().appProvisionType == Constants::APP_PROVISION_TYPE_DEBUG;
387 auto result = InstalldClient::GetInstance()->CreateBundleDataDir(createDirParam);
388 if (result != ERR_OK) {
389 // if user is not activated, access el2-el4 may return ok but dir cannot be created
390 if (AccountHelper::IsOsAccountVerified(userId)) {
391 APP_LOGE("fail to create data dir, error is %{public}d", result);
392 return result;
393 } else {
394 APP_LOGW("user %{public}d is not activated", userId);
395 }
396 }
397 APP_LOGI("CreateCloneDataDir successfully");
398 return result;
399 }
400
RemoveCloneDataDir(const std::string bundleName,int32_t userId,int32_t appIndex,bool sync)401 ErrCode BundleCloneInstaller::RemoveCloneDataDir(
402 const std::string bundleName, int32_t userId, int32_t appIndex, bool sync)
403 {
404 std::string key = BundleCloneCommonHelper::GetCloneDataDir(bundleName, appIndex);
405 if (InstalldClient::GetInstance()->RemoveBundleDataDir(key, userId, false, !sync) != ERR_OK) {
406 APP_LOGW("CloneApp cannot remove the data dir");
407 return ERR_APPEXECFWK_CLONE_INSTALL_INTERNAL_ERROR;
408 }
409 return ERR_OK;
410 }
411
CreateEl5Dir(InnerBundleInfo & info,const int32_t userId,const int32_t uid,const int32_t appIndex)412 void BundleCloneInstaller::CreateEl5Dir(InnerBundleInfo &info, const int32_t userId,
413 const int32_t uid, const int32_t appIndex)
414 {
415 std::vector<RequestPermission> reqPermissions = info.GetAllRequestPermissions();
416 auto it = std::find_if(reqPermissions.begin(), reqPermissions.end(), [](const RequestPermission& permission) {
417 return permission.name == ServiceConstants::PERMISSION_PROTECT_SCREEN_LOCK_DATA;
418 });
419 if (it == reqPermissions.end()) {
420 APP_LOGD("no el5 permission");
421 return;
422 }
423 APP_LOGI("el5 -n %{public}s -i %{public}d", info.GetBundleName().c_str(), appIndex);
424 CreateDirParam el5Param;
425 el5Param.bundleName = info.GetBundleName();
426 el5Param.userId = userId;
427 el5Param.uid = uid;
428 el5Param.gid = uid;
429 el5Param.apl = info.GetAppPrivilegeLevel();
430 el5Param.isPreInstallApp = info.IsPreInstallApp();
431 el5Param.debug = info.GetBaseApplicationInfo().appProvisionType == Constants::APP_PROVISION_TYPE_DEBUG;
432 el5Param.appIndex = appIndex;
433 if (GetDataMgr() != ERR_OK) {
434 return;
435 }
436 dataMgr_->CreateEl5Dir(std::vector<CreateDirParam> {el5Param}, true);
437 }
438
RemoveEl5Dir(InnerBundleUserInfo & userInfo,const int32_t uid,int32_t userId,const int32_t appIndex)439 void BundleCloneInstaller::RemoveEl5Dir(InnerBundleUserInfo &userInfo, const int32_t uid,
440 int32_t userId, const int32_t appIndex)
441 {
442 APP_LOGI("el5 -n %{public}s -i %{public}d", userInfo.bundleName.c_str(), appIndex);
443 std::string key = BundleCloneCommonHelper::GetCloneDataDir(userInfo.bundleName, appIndex);
444 std::vector<std::string> dirs;
445 dirs.emplace_back(std::string(ServiceConstants::SCREEN_LOCK_FILE_DATA_PATH) + ServiceConstants::PATH_SEPARATOR +
446 std::to_string(userId) + ServiceConstants::BASE + key);
447 dirs.emplace_back(std::string(ServiceConstants::SCREEN_LOCK_FILE_DATA_PATH) + ServiceConstants::PATH_SEPARATOR +
448 std::to_string(userId) + ServiceConstants::DATABASE + key);
449 for (const std::string &dir : dirs) {
450 if (InstalldClient::GetInstance()->RemoveDir(dir) != ERR_OK) {
451 APP_LOGW("remove el5 dir %{public}s failed", dir.c_str());
452 }
453 }
454 auto it = userInfo.cloneInfos.find(std::to_string(appIndex));
455 if (it == userInfo.cloneInfos.end()) {
456 APP_LOGE("find cloneInfo failed");
457 return;
458 }
459 if (it->second.keyId.empty()) {
460 return;
461 }
462 EncryptionParam encryptionParam(key, "", 0, userId, EncryptionDirType::APP);
463 if (InstalldClient::GetInstance()->DeleteEncryptionKeyId(encryptionParam) != ERR_OK) {
464 APP_LOGW("delete encryption key id failed");
465 }
466 }
467
GetDataMgr()468 ErrCode BundleCloneInstaller::GetDataMgr()
469 {
470 if (dataMgr_ == nullptr) {
471 dataMgr_ = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
472 if (dataMgr_ == nullptr) {
473 APP_LOGE("Get dataMgr shared_ptr nullptr");
474 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
475 }
476 }
477 return ERR_OK;
478 }
479
SendBundleSystemEvent(const std::string & bundleName,BundleEventType bundleEventType,int32_t userId,int32_t appIndex,bool isPreInstallApp,bool isFreeInstallMode,InstallScene preBundleScene,ErrCode errCode)480 void BundleCloneInstaller::SendBundleSystemEvent(const std::string &bundleName, BundleEventType bundleEventType,
481 int32_t userId, int32_t appIndex, bool isPreInstallApp, bool isFreeInstallMode,
482 InstallScene preBundleScene, ErrCode errCode)
483 {
484 EventInfo sysEventInfo;
485 sysEventInfo.bundleName = bundleName;
486 sysEventInfo.isPreInstallApp = isPreInstallApp;
487 sysEventInfo.errCode = errCode;
488 sysEventInfo.isFreeInstallMode = isFreeInstallMode;
489 sysEventInfo.userId = userId;
490 sysEventInfo.appIndex = appIndex;
491 sysEventInfo.callingUid = IPCSkeleton::GetCallingUid();
492 sysEventInfo.versionCode = versionCode_;
493 sysEventInfo.preBundleScene = preBundleScene;
494 GetCallingEventInfo(sysEventInfo);
495 EventReport::SendBundleSystemEvent(bundleEventType, sysEventInfo);
496 }
497
GetCallingEventInfo(EventInfo & eventInfo)498 void BundleCloneInstaller::GetCallingEventInfo(EventInfo &eventInfo)
499 {
500 APP_LOGD("GetCallingEventInfo start, bundleName:%{public}s", eventInfo.callingBundleName.c_str());
501 if (dataMgr_ == nullptr) {
502 APP_LOGE("Get dataMgr shared_ptr nullptr");
503 return;
504 }
505 if (!dataMgr_->GetBundleNameForUid(eventInfo.callingUid, eventInfo.callingBundleName)) {
506 APP_LOGE("CallingUid %{public}d is not hap, no bundleName", eventInfo.callingUid);
507 eventInfo.callingBundleName = Constants::EMPTY_STRING;
508 return;
509 }
510 BundleInfo bundleInfo;
511 if (!dataMgr_->GetBundleInfo(eventInfo.callingBundleName, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo,
512 eventInfo.callingUid / Constants::BASE_USER_RANGE)) {
513 APP_LOGE("GetBundleInfo failed, bundleName: %{public}s", eventInfo.callingBundleName.c_str());
514 return;
515 }
516 eventInfo.callingAppId = bundleInfo.appId;
517 }
518
ResetInstallProperties()519 void BundleCloneInstaller::ResetInstallProperties()
520 {
521 uid_ = 0;
522 accessTokenId_ = 0;
523 versionCode_ = 0;
524 appId_ = "";
525 appIdentifier_ = "";
526 }
527
GetAssetAccessGroups(const std::string & bundleName)528 std::string BundleCloneInstaller::GetAssetAccessGroups(const std::string &bundleName)
529 {
530 if (GetDataMgr() != ERR_OK) {
531 APP_LOGE("DataMgr null");
532 return Constants::EMPTY_STRING;
533 }
534 std::vector<std::string> assetAccessGroups;
535 ErrCode ret = dataMgr_->GetAssetAccessGroups(bundleName, assetAccessGroups);
536 if (ret != ERR_OK) {
537 APP_LOGE("GetAssetAccessGroups failed, ret=%{public}d", ret);
538 return Constants::EMPTY_STRING;
539 }
540 std::string assetAccessGroupsStr;
541 if (!assetAccessGroups.empty()) {
542 std::stringstream assetAccessGroupsStream;
543 std::copy(assetAccessGroups.begin(), assetAccessGroups.end(),
544 std::ostream_iterator<std::string>(assetAccessGroupsStream, ","));
545 assetAccessGroupsStr = assetAccessGroupsStream.str();
546 if (!assetAccessGroupsStr.empty()) {
547 assetAccessGroupsStr.pop_back();
548 }
549 }
550 return assetAccessGroupsStr;
551 }
552
GetDeveloperId(const std::string & bundleName)553 std::string BundleCloneInstaller::GetDeveloperId(const std::string &bundleName)
554 {
555 if (GetDataMgr() != ERR_OK) {
556 APP_LOGE("DataMgr null");
557 return Constants::EMPTY_STRING;
558 }
559 std::string developerId;
560 ErrCode ret = dataMgr_->GetDeveloperId(bundleName, developerId);
561 if (ret != ERR_OK) {
562 APP_LOGE("GetDeveloperId failed, ret=%{public}d", ret);
563 }
564 return developerId;
565 }
566 } // AppExecFwk
567 } // OHOS
568