1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "free_install_manager.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include "ability_manager_service.h" 19eace7efcSopenharmony_ci#include "ability_util.h" 20eace7efcSopenharmony_ci#include "atomic_service_status_callback.h" 21eace7efcSopenharmony_ci#include "distributed_client.h" 22eace7efcSopenharmony_ci#include "hitrace_meter.h" 23eace7efcSopenharmony_ci#include "insight_intent_execute_manager.h" 24eace7efcSopenharmony_ci#include "insight_intent_utils.h" 25eace7efcSopenharmony_ci#include "permission_constants.h" 26eace7efcSopenharmony_ci#include "utils/app_mgr_util.h" 27eace7efcSopenharmony_ci#include "uri_utils.h" 28eace7efcSopenharmony_ci 29eace7efcSopenharmony_cinamespace OHOS { 30eace7efcSopenharmony_cinamespace AAFwk { 31eace7efcSopenharmony_ciconst std::u16string DMS_FREE_INSTALL_CALLBACK_TOKEN = u"ohos.DistributedSchedule.IDmsFreeInstallCallback"; 32eace7efcSopenharmony_ciconst std::string DMS_MISSION_ID = "dmsMissionId"; 33eace7efcSopenharmony_ciconst std::string PARAM_FREEINSTALL_APPID = "ohos.freeinstall.params.callingAppId"; 34eace7efcSopenharmony_ciconst std::string PARAM_FREEINSTALL_BUNDLENAMES = "ohos.freeinstall.params.callingBundleNames"; 35eace7efcSopenharmony_ciconst std::string PARAM_FREEINSTALL_UID = "ohos.freeinstall.params.callingUid"; 36eace7efcSopenharmony_ciconstexpr uint32_t IDMS_CALLBACK_ON_FREE_INSTALL_DONE = 0; 37eace7efcSopenharmony_ciconstexpr uint32_t UPDATE_ATOMOIC_SERVICE_TASK_TIMER = 24 * 60 * 60 * 1000; /* 24h */ 38eace7efcSopenharmony_ciconstexpr const char* KEY_IS_APP_RUNNING = "com.ohos.param.isAppRunning"; 39eace7efcSopenharmony_ci 40eace7efcSopenharmony_ciFreeInstallManager::FreeInstallManager(const std::weak_ptr<AbilityManagerService> &server) 41eace7efcSopenharmony_ci : server_(server) 42eace7efcSopenharmony_ci{ 43eace7efcSopenharmony_ci} 44eace7efcSopenharmony_ci 45eace7efcSopenharmony_cibool FreeInstallManager::IsTopAbility(const sptr<IRemoteObject> &callerToken) 46eace7efcSopenharmony_ci{ 47eace7efcSopenharmony_ci auto server = server_.lock(); 48eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN_LOG(server, false, "Get server failed!"); 49eace7efcSopenharmony_ci AppExecFwk::ElementName elementName = IN_PROCESS_CALL(server->GetTopAbility()); 50eace7efcSopenharmony_ci if (elementName.GetBundleName().empty() || elementName.GetAbilityName().empty()) { 51eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "GetBundleName or GetAbilityName empty"); 52eace7efcSopenharmony_ci return false; 53eace7efcSopenharmony_ci } 54eace7efcSopenharmony_ci 55eace7efcSopenharmony_ci auto caller = Token::GetAbilityRecordByToken(callerToken); 56eace7efcSopenharmony_ci if (caller == nullptr) { 57eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "null caller"); 58eace7efcSopenharmony_ci return false; 59eace7efcSopenharmony_ci } 60eace7efcSopenharmony_ci 61eace7efcSopenharmony_ci auto type = caller->GetAbilityInfo().type; 62eace7efcSopenharmony_ci if (type == AppExecFwk::AbilityType::SERVICE || type == AppExecFwk::AbilityType::EXTENSION) { 63eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "service or extension"); 64eace7efcSopenharmony_ci return true; 65eace7efcSopenharmony_ci } 66eace7efcSopenharmony_ci 67eace7efcSopenharmony_ci AppExecFwk::ElementName callerElementName = caller->GetElementName(); 68eace7efcSopenharmony_ci std::string callerBundleName = callerElementName.GetBundleName(); 69eace7efcSopenharmony_ci std::string callerAbilityName = callerElementName.GetAbilityName(); 70eace7efcSopenharmony_ci std::string callerModuleName = callerElementName.GetModuleName(); 71eace7efcSopenharmony_ci if (elementName.GetBundleName().compare(callerBundleName) == 0 && 72eace7efcSopenharmony_ci elementName.GetAbilityName().compare(callerAbilityName) == 0 && 73eace7efcSopenharmony_ci elementName.GetModuleName().compare(callerModuleName) == 0) { 74eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "top ability"); 75eace7efcSopenharmony_ci return true; 76eace7efcSopenharmony_ci } 77eace7efcSopenharmony_ci 78eace7efcSopenharmony_ci return false; 79eace7efcSopenharmony_ci} 80eace7efcSopenharmony_ci 81eace7efcSopenharmony_ciint FreeInstallManager::StartFreeInstall(const Want &want, int32_t userId, int requestCode, 82eace7efcSopenharmony_ci const sptr<IRemoteObject> &callerToken, bool isAsync, uint32_t specifyTokenId, bool isOpenAtomicServiceShortUrl, 83eace7efcSopenharmony_ci std::shared_ptr<Want> originalWant) 84eace7efcSopenharmony_ci{ 85eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 86eace7efcSopenharmony_ci if (!VerifyStartFreeInstallPermission(callerToken)) { 87eace7efcSopenharmony_ci return NOT_TOP_ABILITY; 88eace7efcSopenharmony_ci } 89eace7efcSopenharmony_ci FreeInstallInfo info = BuildFreeInstallInfo(want, userId, requestCode, callerToken, 90eace7efcSopenharmony_ci isAsync, specifyTokenId, isOpenAtomicServiceShortUrl, originalWant); 91eace7efcSopenharmony_ci { 92eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 93eace7efcSopenharmony_ci freeInstallList_.push_back(info); 94eace7efcSopenharmony_ci } 95eace7efcSopenharmony_ci int32_t recordId = GetRecordIdByToken(callerToken); 96eace7efcSopenharmony_ci sptr<AtomicServiceStatusCallback> callback = new AtomicServiceStatusCallback(weak_from_this(), isAsync, recordId); 97eace7efcSopenharmony_ci auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); 98eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(bundleMgrHelper, GET_ABILITY_SERVICE_FAILED); 99eace7efcSopenharmony_ci AppExecFwk::AbilityInfo abilityInfo = {}; 100eace7efcSopenharmony_ci constexpr auto flag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION; 101eace7efcSopenharmony_ci info.want.SetParam(PARAM_FREEINSTALL_UID, IPCSkeleton::GetCallingUid()); 102eace7efcSopenharmony_ci 103eace7efcSopenharmony_ci int result = SetAppRunningState(info.want); 104eace7efcSopenharmony_ci if (result != ERR_OK) { 105eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "setAppRunningState failed"); 106eace7efcSopenharmony_ci return result; 107eace7efcSopenharmony_ci } 108eace7efcSopenharmony_ci 109eace7efcSopenharmony_ci if (IN_PROCESS_CALL(bundleMgrHelper->QueryAbilityInfo(info.want, flag, info.userId, abilityInfo, callback))) { 110eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "app installed"); 111eace7efcSopenharmony_ci } 112eace7efcSopenharmony_ci std::string callingAppId = info.want.GetStringParam(PARAM_FREEINSTALL_APPID); 113eace7efcSopenharmony_ci std::vector<std::string> callingBundleNames = info.want.GetStringArrayParam(PARAM_FREEINSTALL_BUNDLENAMES); 114eace7efcSopenharmony_ci if (callingAppId.empty() && callingBundleNames.empty()) { 115eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "callingAppId and callingBundleNames empty"); 116eace7efcSopenharmony_ci } 117eace7efcSopenharmony_ci info.want.RemoveParam(PARAM_FREEINSTALL_APPID); 118eace7efcSopenharmony_ci info.want.RemoveParam(PARAM_FREEINSTALL_BUNDLENAMES); 119eace7efcSopenharmony_ci 120eace7efcSopenharmony_ci if (isAsync) { 121eace7efcSopenharmony_ci return ERR_OK; 122eace7efcSopenharmony_ci } else { 123eace7efcSopenharmony_ci auto future = info.promise->get_future(); 124eace7efcSopenharmony_ci std::future_status status = future.wait_for(std::chrono::milliseconds(DELAY_LOCAL_FREE_INSTALL_TIMEOUT)); 125eace7efcSopenharmony_ci if (status == std::future_status::timeout) { 126eace7efcSopenharmony_ci RemoveFreeInstallInfo(info.want.GetElement().GetBundleName(), info.want.GetElement().GetAbilityName(), 127eace7efcSopenharmony_ci info.want.GetStringParam(Want::PARAM_RESV_START_TIME)); 128eace7efcSopenharmony_ci return FREE_INSTALL_TIMEOUT; 129eace7efcSopenharmony_ci } 130eace7efcSopenharmony_ci return future.get(); 131eace7efcSopenharmony_ci } 132eace7efcSopenharmony_ci} 133eace7efcSopenharmony_ci 134eace7efcSopenharmony_ciint FreeInstallManager::RemoteFreeInstall(const Want &want, int32_t userId, int requestCode, 135eace7efcSopenharmony_ci const sptr<IRemoteObject> &callerToken) 136eace7efcSopenharmony_ci{ 137eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 138eace7efcSopenharmony_ci bool isFromRemote = want.GetBoolParam(FROM_REMOTE_KEY, false); 139eace7efcSopenharmony_ci auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); 140eace7efcSopenharmony_ci if (!isSaCall && !isFromRemote && !IsTopAbility(callerToken)) { 141eace7efcSopenharmony_ci return NOT_TOP_ABILITY; 142eace7efcSopenharmony_ci } 143eace7efcSopenharmony_ci FreeInstallInfo info = BuildFreeInstallInfo(want, userId, requestCode, callerToken, false); 144eace7efcSopenharmony_ci { 145eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 146eace7efcSopenharmony_ci freeInstallList_.push_back(info); 147eace7efcSopenharmony_ci } 148eace7efcSopenharmony_ci int32_t recordId = GetRecordIdByToken(callerToken); 149eace7efcSopenharmony_ci sptr<AtomicServiceStatusCallback> callback = new AtomicServiceStatusCallback(weak_from_this(), false, recordId); 150eace7efcSopenharmony_ci int32_t callerUid = IPCSkeleton::GetCallingUid(); 151eace7efcSopenharmony_ci uint32_t accessToken = IPCSkeleton::GetCallingTokenID(); 152eace7efcSopenharmony_ci UriUtils::GetInstance().FilterUriWithPermissionDms(info.want, accessToken); 153eace7efcSopenharmony_ci DistributedClient dmsClient; 154eace7efcSopenharmony_ci auto result = dmsClient.StartRemoteFreeInstall(info.want, callerUid, info.requestCode, accessToken, callback); 155eace7efcSopenharmony_ci if (result != ERR_NONE) { 156eace7efcSopenharmony_ci return result; 157eace7efcSopenharmony_ci } 158eace7efcSopenharmony_ci auto remoteFuture = info.promise->get_future(); 159eace7efcSopenharmony_ci std::future_status remoteStatus = remoteFuture.wait_for(std::chrono::milliseconds( 160eace7efcSopenharmony_ci DELAY_REMOTE_FREE_INSTALL_TIMEOUT)); 161eace7efcSopenharmony_ci if (remoteStatus == std::future_status::timeout) { 162eace7efcSopenharmony_ci return FREE_INSTALL_TIMEOUT; 163eace7efcSopenharmony_ci } 164eace7efcSopenharmony_ci return remoteFuture.get(); 165eace7efcSopenharmony_ci} 166eace7efcSopenharmony_ci 167eace7efcSopenharmony_ciFreeInstallInfo FreeInstallManager::BuildFreeInstallInfo(const Want &want, int32_t userId, int requestCode, 168eace7efcSopenharmony_ci const sptr<IRemoteObject> &callerToken, bool isAsync, uint32_t specifyTokenId, bool isOpenAtomicServiceShortUrl, 169eace7efcSopenharmony_ci std::shared_ptr<Want> originalWant) 170eace7efcSopenharmony_ci{ 171eace7efcSopenharmony_ci FreeInstallInfo info = { 172eace7efcSopenharmony_ci .want = want, 173eace7efcSopenharmony_ci .userId = userId, 174eace7efcSopenharmony_ci .requestCode = requestCode, 175eace7efcSopenharmony_ci .callerToken = callerToken, 176eace7efcSopenharmony_ci .specifyTokenId = specifyTokenId, 177eace7efcSopenharmony_ci .isOpenAtomicServiceShortUrl = isOpenAtomicServiceShortUrl, 178eace7efcSopenharmony_ci .originalWant = originalWant 179eace7efcSopenharmony_ci }; 180eace7efcSopenharmony_ci if (!isAsync) { 181eace7efcSopenharmony_ci auto promise = std::make_shared<std::promise<int32_t>>(); 182eace7efcSopenharmony_ci info.promise = promise; 183eace7efcSopenharmony_ci } 184eace7efcSopenharmony_ci auto identity = IPCSkeleton::ResetCallingIdentity(); 185eace7efcSopenharmony_ci info.identity = identity; 186eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(identity); 187eace7efcSopenharmony_ci return info; 188eace7efcSopenharmony_ci} 189eace7efcSopenharmony_ci 190eace7efcSopenharmony_ciint FreeInstallManager::StartRemoteFreeInstall(const Want &want, int requestCode, int32_t validUserId, 191eace7efcSopenharmony_ci const sptr<IRemoteObject> &callerToken) 192eace7efcSopenharmony_ci{ 193eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 194eace7efcSopenharmony_ci if (!want.GetBoolParam(Want::PARAM_RESV_FOR_RESULT, false)) { 195eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "StartAbility freeInstall"); 196eace7efcSopenharmony_ci return RemoteFreeInstall(want, validUserId, requestCode, callerToken); 197eace7efcSopenharmony_ci } 198eace7efcSopenharmony_ci int32_t missionId = DelayedSingleton<AbilityManagerService>::GetInstance()-> 199eace7efcSopenharmony_ci GetMissionIdByAbilityToken(callerToken); 200eace7efcSopenharmony_ci if (missionId < 0) { 201eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 202eace7efcSopenharmony_ci } 203eace7efcSopenharmony_ci Want* newWant = const_cast<Want*>(&want); 204eace7efcSopenharmony_ci newWant->SetParam(DMS_MISSION_ID, missionId); 205eace7efcSopenharmony_ci return RemoteFreeInstall(*newWant, validUserId, requestCode, callerToken); 206eace7efcSopenharmony_ci} 207eace7efcSopenharmony_ci 208eace7efcSopenharmony_ciint FreeInstallManager::NotifyDmsCallback(const Want &want, int resultCode) 209eace7efcSopenharmony_ci{ 210eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 211eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> autoLock(distributedFreeInstallLock_); 212eace7efcSopenharmony_ci if (dmsFreeInstallCbs_.empty()) { 213eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "null dms callback"); 214eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 215eace7efcSopenharmony_ci } 216eace7efcSopenharmony_ci 217eace7efcSopenharmony_ci MessageParcel reply; 218eace7efcSopenharmony_ci MessageOption option; 219eace7efcSopenharmony_ci 220eace7efcSopenharmony_ci for (auto it = dmsFreeInstallCbs_.begin(); it != dmsFreeInstallCbs_.end();) { 221eace7efcSopenharmony_ci std::string abilityName = (*it).want.GetElement().GetAbilityName(); 222eace7efcSopenharmony_ci if (want.GetElement().GetAbilityName().compare(abilityName) == 0) { 223eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "Handle DMS"); 224eace7efcSopenharmony_ci MessageParcel data; 225eace7efcSopenharmony_ci if (!data.WriteInterfaceToken(DMS_FREE_INSTALL_CALLBACK_TOKEN)) { 226eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "write interface token failed"); 227eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 228eace7efcSopenharmony_ci } 229eace7efcSopenharmony_ci 230eace7efcSopenharmony_ci if (!data.WriteInt32(resultCode)) { 231eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "write resultCode error"); 232eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 233eace7efcSopenharmony_ci } 234eace7efcSopenharmony_ci 235eace7efcSopenharmony_ci if (!data.WriteParcelable(&((*it).want))) { 236eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "want write failed"); 237eace7efcSopenharmony_ci return INNER_ERR; 238eace7efcSopenharmony_ci } 239eace7efcSopenharmony_ci 240eace7efcSopenharmony_ci if (!data.WriteInt32((*it).requestCode)) { 241eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "write resultCode error"); 242eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 243eace7efcSopenharmony_ci } 244eace7efcSopenharmony_ci 245eace7efcSopenharmony_ci (*it).dmsCallback->SendRequest(IDMS_CALLBACK_ON_FREE_INSTALL_DONE, data, reply, option); 246eace7efcSopenharmony_ci it = dmsFreeInstallCbs_.erase(it); 247eace7efcSopenharmony_ci } else { 248eace7efcSopenharmony_ci it++; 249eace7efcSopenharmony_ci } 250eace7efcSopenharmony_ci } 251eace7efcSopenharmony_ci 252eace7efcSopenharmony_ci return reply.ReadInt32(); 253eace7efcSopenharmony_ci} 254eace7efcSopenharmony_ci 255eace7efcSopenharmony_civoid FreeInstallManager::NotifyFreeInstallResult(int32_t recordId, const Want &want, int resultCode, bool isAsync) 256eace7efcSopenharmony_ci{ 257eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 258eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 259eace7efcSopenharmony_ci if (freeInstallList_.empty()) { 260eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "null app callback"); 261eace7efcSopenharmony_ci return; 262eace7efcSopenharmony_ci } 263eace7efcSopenharmony_ci 264eace7efcSopenharmony_ci bool isFromRemote = want.GetBoolParam(FROM_REMOTE_KEY, false); 265eace7efcSopenharmony_ci for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) { 266eace7efcSopenharmony_ci FreeInstallInfo &freeInstallInfo = *it; 267eace7efcSopenharmony_ci std::string bundleName = freeInstallInfo.want.GetElement().GetBundleName(); 268eace7efcSopenharmony_ci std::string abilityName = freeInstallInfo.want.GetElement().GetAbilityName(); 269eace7efcSopenharmony_ci std::string startTime = freeInstallInfo.want.GetStringParam(Want::PARAM_RESV_START_TIME); 270eace7efcSopenharmony_ci std::string url = freeInstallInfo.want.GetUriString(); 271eace7efcSopenharmony_ci if (want.GetElement().GetBundleName().compare(bundleName) != 0 || 272eace7efcSopenharmony_ci want.GetElement().GetAbilityName().compare(abilityName) != 0 || 273eace7efcSopenharmony_ci want.GetStringParam(Want::PARAM_RESV_START_TIME).compare(startTime) != 0 || 274eace7efcSopenharmony_ci want.GetUriString().compare(url) != 0) { 275eace7efcSopenharmony_ci it++; 276eace7efcSopenharmony_ci continue; 277eace7efcSopenharmony_ci } 278eace7efcSopenharmony_ci 279eace7efcSopenharmony_ci if (!isAsync && freeInstallInfo.promise == nullptr) { 280eace7efcSopenharmony_ci it++; 281eace7efcSopenharmony_ci continue; 282eace7efcSopenharmony_ci } 283eace7efcSopenharmony_ci freeInstallInfo.isFreeInstallFinished = true; 284eace7efcSopenharmony_ci freeInstallInfo.resultCode = resultCode; 285eace7efcSopenharmony_ci HandleFreeInstallResult(recordId, freeInstallInfo, resultCode, isAsync); 286eace7efcSopenharmony_ci it = freeInstallList_.erase(it); 287eace7efcSopenharmony_ci } 288eace7efcSopenharmony_ci} 289eace7efcSopenharmony_ci 290eace7efcSopenharmony_civoid FreeInstallManager::HandleOnFreeInstallSuccess(int32_t recordId, FreeInstallInfo &freeInstallInfo, bool isAsync) 291eace7efcSopenharmony_ci{ 292eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "install success"); 293eace7efcSopenharmony_ci freeInstallInfo.isInstalled = true; 294eace7efcSopenharmony_ci 295eace7efcSopenharmony_ci if (isAsync) { 296eace7efcSopenharmony_ci std::string startTime = freeInstallInfo.want.GetStringParam(Want::PARAM_RESV_START_TIME); 297eace7efcSopenharmony_ci std::string bundleName = freeInstallInfo.want.GetElement().GetBundleName(); 298eace7efcSopenharmony_ci std::string abilityName = freeInstallInfo.want.GetElement().GetAbilityName(); 299eace7efcSopenharmony_ci if (freeInstallInfo.isPreStartMissionCalled) { 300eace7efcSopenharmony_ci StartAbilityByPreInstall(recordId, freeInstallInfo, bundleName, abilityName, startTime); 301eace7efcSopenharmony_ci return; 302eace7efcSopenharmony_ci } 303eace7efcSopenharmony_ci if (freeInstallInfo.isOpenAtomicServiceShortUrl) { 304eace7efcSopenharmony_ci StartAbilityByConvertedWant(freeInstallInfo, startTime); 305eace7efcSopenharmony_ci return; 306eace7efcSopenharmony_ci } 307eace7efcSopenharmony_ci StartAbilityByFreeInstall(freeInstallInfo, bundleName, abilityName, startTime); 308eace7efcSopenharmony_ci return; 309eace7efcSopenharmony_ci } 310eace7efcSopenharmony_ci freeInstallInfo.promise->set_value(ERR_OK); 311eace7efcSopenharmony_ci} 312eace7efcSopenharmony_ci 313eace7efcSopenharmony_civoid FreeInstallManager::HandleOnFreeInstallFail(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, 314eace7efcSopenharmony_ci bool isAsync) 315eace7efcSopenharmony_ci{ 316eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "install failed"); 317eace7efcSopenharmony_ci freeInstallInfo.isInstalled = false; 318eace7efcSopenharmony_ci 319eace7efcSopenharmony_ci if (isAsync) { 320eace7efcSopenharmony_ci if (freeInstallInfo.isPreStartMissionCalled && 321eace7efcSopenharmony_ci freeInstallInfo.want.HasParameter(KEY_SESSION_ID) && 322eace7efcSopenharmony_ci !freeInstallInfo.want.GetStringParam(KEY_SESSION_ID).empty() && 323eace7efcSopenharmony_ci freeInstallInfo.isStartUIAbilityBySCBCalled) { 324eace7efcSopenharmony_ci DelayedSingleton<AbilityManagerService>::GetInstance()->NotifySCBToHandleAtomicServiceException( 325eace7efcSopenharmony_ci freeInstallInfo.want.GetStringParam(KEY_SESSION_ID), 326eace7efcSopenharmony_ci resultCode, "free install failed"); 327eace7efcSopenharmony_ci } 328eace7efcSopenharmony_ci std::string startTime = freeInstallInfo.want.GetStringParam(Want::PARAM_RESV_START_TIME); 329eace7efcSopenharmony_ci if (freeInstallInfo.isOpenAtomicServiceShortUrl 330eace7efcSopenharmony_ci && resultCode != CONCURRENT_TASKS_WAITING_FOR_RETRY) { 331eace7efcSopenharmony_ci StartAbilityByOriginalWant(freeInstallInfo, startTime); 332eace7efcSopenharmony_ci return; 333eace7efcSopenharmony_ci } 334eace7efcSopenharmony_ci 335eace7efcSopenharmony_ci std::string bundleName = freeInstallInfo.want.GetElement().GetBundleName(); 336eace7efcSopenharmony_ci std::string abilityName = freeInstallInfo.want.GetElement().GetAbilityName(); 337eace7efcSopenharmony_ci DelayedSingleton<FreeInstallObserverManager>::GetInstance()->OnInstallFinished( 338eace7efcSopenharmony_ci recordId, bundleName, abilityName, startTime, resultCode); 339eace7efcSopenharmony_ci return; 340eace7efcSopenharmony_ci } 341eace7efcSopenharmony_ci freeInstallInfo.promise->set_value(resultCode); 342eace7efcSopenharmony_ci} 343eace7efcSopenharmony_ci 344eace7efcSopenharmony_civoid FreeInstallManager::HandleFreeInstallResult(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, 345eace7efcSopenharmony_ci bool isAsync) 346eace7efcSopenharmony_ci{ 347eace7efcSopenharmony_ci if (resultCode == ERR_OK) { 348eace7efcSopenharmony_ci HandleOnFreeInstallSuccess(recordId, freeInstallInfo, isAsync); 349eace7efcSopenharmony_ci return; 350eace7efcSopenharmony_ci } 351eace7efcSopenharmony_ci HandleOnFreeInstallFail(recordId, freeInstallInfo, resultCode, isAsync); 352eace7efcSopenharmony_ci} 353eace7efcSopenharmony_ci 354eace7efcSopenharmony_civoid FreeInstallManager::StartAbilityByFreeInstall(FreeInstallInfo &info, std::string &bundleName, 355eace7efcSopenharmony_ci std::string &abilityName, std::string &startTime) 356eace7efcSopenharmony_ci{ 357eace7efcSopenharmony_ci info.want.SetFlags(info.want.GetFlags() ^ Want::FLAG_INSTALL_ON_DEMAND); 358eace7efcSopenharmony_ci auto identity = IPCSkeleton::ResetCallingIdentity(); 359eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(info.identity); 360eace7efcSopenharmony_ci int32_t result = ERR_OK; 361eace7efcSopenharmony_ci if (info.want.GetElement().GetAbilityName().empty()) { 362eace7efcSopenharmony_ci result = UpdateElementName(info.want, info.userId); 363eace7efcSopenharmony_ci } 364eace7efcSopenharmony_ci if (result == ERR_OK) { 365eace7efcSopenharmony_ci result = DelayedSingleton<AbilityManagerService>::GetInstance()->StartAbilityByFreeInstall(info.want, 366eace7efcSopenharmony_ci info.callerToken, info.userId, info.requestCode); 367eace7efcSopenharmony_ci } 368eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(identity); 369eace7efcSopenharmony_ci int32_t recordId = GetRecordIdByToken(info.callerToken); 370eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "result: %{public}d", result); 371eace7efcSopenharmony_ci DelayedSingleton<FreeInstallObserverManager>::GetInstance()->OnInstallFinished( 372eace7efcSopenharmony_ci recordId, bundleName, abilityName, startTime, result); 373eace7efcSopenharmony_ci} 374eace7efcSopenharmony_ci 375eace7efcSopenharmony_civoid FreeInstallManager::StartAbilityByPreInstall(int32_t recordId, FreeInstallInfo &info, std::string &bundleName, 376eace7efcSopenharmony_ci std::string &abilityName, std::string &startTime) 377eace7efcSopenharmony_ci{ 378eace7efcSopenharmony_ci info.want.SetFlags(info.want.GetFlags() ^ Want::FLAG_INSTALL_ON_DEMAND); 379eace7efcSopenharmony_ci auto identity = IPCSkeleton::ResetCallingIdentity(); 380eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(info.identity); 381eace7efcSopenharmony_ci int32_t result = ERR_OK; 382eace7efcSopenharmony_ci if (info.want.GetElement().GetAbilityName().empty()) { 383eace7efcSopenharmony_ci result = UpdateElementName(info.want, info.userId); 384eace7efcSopenharmony_ci } 385eace7efcSopenharmony_ci if (result == ERR_OK) { 386eace7efcSopenharmony_ci result = DelayedSingleton<AbilityManagerService>::GetInstance()->StartUIAbilityByPreInstall(info); 387eace7efcSopenharmony_ci } 388eace7efcSopenharmony_ci if (result != ERR_OK && info.isStartUIAbilityBySCBCalled) { 389eace7efcSopenharmony_ci DelayedSingleton<AbilityManagerService>::GetInstance()->NotifySCBToHandleAtomicServiceException( 390eace7efcSopenharmony_ci info.want.GetStringParam(KEY_SESSION_ID), 391eace7efcSopenharmony_ci result, "start ability failed"); 392eace7efcSopenharmony_ci } 393eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(identity); 394eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "preInstall result: %{public}d", result); 395eace7efcSopenharmony_ci DelayedSingleton<FreeInstallObserverManager>::GetInstance()->OnInstallFinished( 396eace7efcSopenharmony_ci recordId, bundleName, abilityName, startTime, result); 397eace7efcSopenharmony_ci} 398eace7efcSopenharmony_ci 399eace7efcSopenharmony_civoid FreeInstallManager::StartAbilityByConvertedWant(FreeInstallInfo &info, const std::string &startTime) 400eace7efcSopenharmony_ci{ 401eace7efcSopenharmony_ci info.want.SetFlags(info.want.GetFlags() ^ Want::FLAG_INSTALL_ON_DEMAND); 402eace7efcSopenharmony_ci auto identity = IPCSkeleton::ResetCallingIdentity(); 403eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(info.identity); 404eace7efcSopenharmony_ci int32_t result = ERR_OK; 405eace7efcSopenharmony_ci if (info.want.GetElement().GetAbilityName().empty()) { 406eace7efcSopenharmony_ci result = UpdateElementName(info.want, info.userId); 407eace7efcSopenharmony_ci } 408eace7efcSopenharmony_ci if (result == ERR_OK) { 409eace7efcSopenharmony_ci result = DelayedSingleton<AbilityManagerService>::GetInstance()->StartAbility(info.want, 410eace7efcSopenharmony_ci info.callerToken, info.userId, info.requestCode); 411eace7efcSopenharmony_ci } 412eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(identity); 413eace7efcSopenharmony_ci auto url = info.want.GetUriString(); 414eace7efcSopenharmony_ci int32_t recordId = GetRecordIdByToken(info.callerToken); 415eace7efcSopenharmony_ci DelayedSingleton<FreeInstallObserverManager>::GetInstance()->OnInstallFinishedByUrl(recordId, 416eace7efcSopenharmony_ci startTime, url, result); 417eace7efcSopenharmony_ci} 418eace7efcSopenharmony_ci 419eace7efcSopenharmony_civoid FreeInstallManager::StartAbilityByOriginalWant(FreeInstallInfo &info, const std::string &startTime) 420eace7efcSopenharmony_ci{ 421eace7efcSopenharmony_ci auto identity = IPCSkeleton::ResetCallingIdentity(); 422eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(info.identity); 423eace7efcSopenharmony_ci int result = ERR_INVALID_VALUE; 424eace7efcSopenharmony_ci if (info.originalWant) { 425eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "StartAbility by originalWant"); 426eace7efcSopenharmony_ci result = DelayedSingleton<AbilityManagerService>::GetInstance()->StartAbility(*(info.originalWant), 427eace7efcSopenharmony_ci info.callerToken, info.userId, info.requestCode); 428eace7efcSopenharmony_ci } else { 429eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "null original want"); 430eace7efcSopenharmony_ci } 431eace7efcSopenharmony_ci IPCSkeleton::SetCallingIdentity(identity); 432eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "startAbility result: %{public}d", result); 433eace7efcSopenharmony_ci auto url = info.want.GetUriString(); 434eace7efcSopenharmony_ci int32_t recordId = GetRecordIdByToken(info.callerToken); 435eace7efcSopenharmony_ci DelayedSingleton<FreeInstallObserverManager>::GetInstance()->OnInstallFinishedByUrl(recordId, 436eace7efcSopenharmony_ci startTime, url, result); 437eace7efcSopenharmony_ci} 438eace7efcSopenharmony_ci 439eace7efcSopenharmony_ciint32_t FreeInstallManager::UpdateElementName(Want &want, int32_t userId) const 440eace7efcSopenharmony_ci{ 441eace7efcSopenharmony_ci auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); 442eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(bundleMgrHelper, ERR_INVALID_VALUE); 443eace7efcSopenharmony_ci Want launchWant; 444eace7efcSopenharmony_ci auto errCode = IN_PROCESS_CALL(bundleMgrHelper->GetLaunchWantForBundle(want.GetBundle(), launchWant, userId)); 445eace7efcSopenharmony_ci if (errCode != ERR_OK) { 446eace7efcSopenharmony_ci return errCode; 447eace7efcSopenharmony_ci } 448eace7efcSopenharmony_ci want.SetElement(launchWant.GetElement()); 449eace7efcSopenharmony_ci return ERR_OK; 450eace7efcSopenharmony_ci} 451eace7efcSopenharmony_ci 452eace7efcSopenharmony_ciint FreeInstallManager::FreeInstallAbilityFromRemote(const Want &want, const sptr<IRemoteObject> &callback, 453eace7efcSopenharmony_ci int32_t userId, int requestCode) 454eace7efcSopenharmony_ci{ 455eace7efcSopenharmony_ci if (callback == nullptr) { 456eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "null callback"); 457eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 458eace7efcSopenharmony_ci } 459eace7efcSopenharmony_ci 460eace7efcSopenharmony_ci FreeInstallInfo info = { 461eace7efcSopenharmony_ci .want = want, 462eace7efcSopenharmony_ci .userId = userId, 463eace7efcSopenharmony_ci .requestCode = requestCode, 464eace7efcSopenharmony_ci .dmsCallback = callback 465eace7efcSopenharmony_ci }; 466eace7efcSopenharmony_ci 467eace7efcSopenharmony_ci { 468eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> autoLock(distributedFreeInstallLock_); 469eace7efcSopenharmony_ci dmsFreeInstallCbs_.push_back(info); 470eace7efcSopenharmony_ci } 471eace7efcSopenharmony_ci 472eace7efcSopenharmony_ci auto result = StartFreeInstall(info.want, info.userId, info.requestCode, nullptr); 473eace7efcSopenharmony_ci if (result != ERR_OK) { 474eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "StartFreeInstall code: %{public}d", result); 475eace7efcSopenharmony_ci NotifyDmsCallback(info.want, result); 476eace7efcSopenharmony_ci } 477eace7efcSopenharmony_ci return result; 478eace7efcSopenharmony_ci} 479eace7efcSopenharmony_ci 480eace7efcSopenharmony_ciint FreeInstallManager::ConnectFreeInstall(const Want &want, int32_t userId, 481eace7efcSopenharmony_ci const sptr<IRemoteObject> &callerToken, const std::string& localDeviceId) 482eace7efcSopenharmony_ci{ 483eace7efcSopenharmony_ci auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); 484eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(bundleMgrHelper, GET_ABILITY_SERVICE_FAILED); 485eace7efcSopenharmony_ci std::string wantDeviceId = want.GetElement().GetDeviceID(); 486eace7efcSopenharmony_ci if (!(localDeviceId == wantDeviceId || wantDeviceId.empty())) { 487eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "deviceID empty"); 488eace7efcSopenharmony_ci return INVALID_PARAMETERS_ERR; 489eace7efcSopenharmony_ci } 490eace7efcSopenharmony_ci 491eace7efcSopenharmony_ci auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); 492eace7efcSopenharmony_ci if (!isSaCall) { 493eace7efcSopenharmony_ci std::string wantAbilityName = want.GetElement().GetAbilityName(); 494eace7efcSopenharmony_ci std::string wantBundleName = want.GetElement().GetBundleName(); 495eace7efcSopenharmony_ci if (wantBundleName.empty() || wantAbilityName.empty()) { 496eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "wantBundleName or wantAbilityName empty."); 497eace7efcSopenharmony_ci return INVALID_PARAMETERS_ERR; 498eace7efcSopenharmony_ci } 499eace7efcSopenharmony_ci int callerUid = IPCSkeleton::GetCallingUid(); 500eace7efcSopenharmony_ci std::string localBundleName; 501eace7efcSopenharmony_ci auto res = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerUid, localBundleName)); 502eace7efcSopenharmony_ci if (res != ERR_OK || localBundleName != wantBundleName) { 503eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "not local BundleName"); 504eace7efcSopenharmony_ci return INVALID_PARAMETERS_ERR; 505eace7efcSopenharmony_ci } 506eace7efcSopenharmony_ci } 507eace7efcSopenharmony_ci 508eace7efcSopenharmony_ci AppExecFwk::AbilityInfo abilityInfo; 509eace7efcSopenharmony_ci std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos; 510eace7efcSopenharmony_ci if (!IN_PROCESS_CALL(bundleMgrHelper->QueryAbilityInfo( 511eace7efcSopenharmony_ci want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, userId, abilityInfo)) && 512eace7efcSopenharmony_ci !IN_PROCESS_CALL(bundleMgrHelper->QueryExtensionAbilityInfos( 513eace7efcSopenharmony_ci want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, userId, extensionInfos))) { 514eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "try to StartFreeInstall"); 515eace7efcSopenharmony_ci int result = StartFreeInstall(want, userId, DEFAULT_INVAL_VALUE, callerToken); 516eace7efcSopenharmony_ci if (result) { 517eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "startFreeInstall error"); 518eace7efcSopenharmony_ci return result; 519eace7efcSopenharmony_ci } 520eace7efcSopenharmony_ci } 521eace7efcSopenharmony_ci return ERR_OK; 522eace7efcSopenharmony_ci} 523eace7efcSopenharmony_ci 524eace7efcSopenharmony_cistd::time_t FreeInstallManager::GetTimeStamp() 525eace7efcSopenharmony_ci{ 526eace7efcSopenharmony_ci std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = 527eace7efcSopenharmony_ci std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()); 528eace7efcSopenharmony_ci std::time_t timestamp = tp.time_since_epoch().count(); 529eace7efcSopenharmony_ci return timestamp; 530eace7efcSopenharmony_ci} 531eace7efcSopenharmony_ci 532eace7efcSopenharmony_civoid FreeInstallManager::OnInstallFinished(int32_t recordId, int resultCode, const Want &want, 533eace7efcSopenharmony_ci int32_t userId, bool isAsync) 534eace7efcSopenharmony_ci{ 535eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 536eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "resultCode: %{public}d", resultCode); 537eace7efcSopenharmony_ci 538eace7efcSopenharmony_ci if (!InsightIntentExecuteParam::IsInsightIntentExecute(want)) { 539eace7efcSopenharmony_ci NotifyDmsCallback(want, resultCode); 540eace7efcSopenharmony_ci NotifyFreeInstallResult(recordId, want, resultCode, isAsync); 541eace7efcSopenharmony_ci } else { 542eace7efcSopenharmony_ci NotifyInsightIntentFreeInstallResult(want, resultCode); 543eace7efcSopenharmony_ci } 544eace7efcSopenharmony_ci 545eace7efcSopenharmony_ci PostUpgradeAtomicServiceTask(resultCode, want, userId); 546eace7efcSopenharmony_ci} 547eace7efcSopenharmony_ci 548eace7efcSopenharmony_civoid FreeInstallManager::PostUpgradeAtomicServiceTask(int resultCode, const Want &want, int32_t userId) 549eace7efcSopenharmony_ci{ 550eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "called"); 551eace7efcSopenharmony_ci std::weak_ptr<FreeInstallManager> thisWptr(shared_from_this()); 552eace7efcSopenharmony_ci if (resultCode == ERR_OK) { 553eace7efcSopenharmony_ci auto updateAtmoicServiceTask = [want, userId, thisWptr, &timeStampMap = timeStampMap_]() { 554eace7efcSopenharmony_ci auto sptr = thisWptr.lock(); 555eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::FREE_INSTALL, 556eace7efcSopenharmony_ci "bundleName: %{public}s, moduleName: %{public}s", want.GetElement().GetBundleName().c_str(), 557eace7efcSopenharmony_ci want.GetElement().GetModuleName().c_str()); 558eace7efcSopenharmony_ci std::string nameKey = want.GetElement().GetBundleName() + want.GetElement().GetModuleName(); 559eace7efcSopenharmony_ci if (timeStampMap.find(nameKey) == timeStampMap.end() || 560eace7efcSopenharmony_ci sptr->GetTimeStamp() - timeStampMap[nameKey] > UPDATE_ATOMOIC_SERVICE_TASK_TIMER) { 561eace7efcSopenharmony_ci auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); 562eace7efcSopenharmony_ci CHECK_POINTER(bundleMgrHelper); 563eace7efcSopenharmony_ci bundleMgrHelper->UpgradeAtomicService(want, userId); 564eace7efcSopenharmony_ci timeStampMap.emplace(nameKey, sptr->GetTimeStamp()); 565eace7efcSopenharmony_ci } 566eace7efcSopenharmony_ci }; 567eace7efcSopenharmony_ci 568eace7efcSopenharmony_ci auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler(); 569eace7efcSopenharmony_ci CHECK_POINTER_LOG(handler, "Fail to get Ability task handler."); 570eace7efcSopenharmony_ci handler->SubmitTask(updateAtmoicServiceTask, "UpdateAtmoicServiceTask"); 571eace7efcSopenharmony_ci } 572eace7efcSopenharmony_ci} 573eace7efcSopenharmony_ci 574eace7efcSopenharmony_civoid FreeInstallManager::OnRemoteInstallFinished(int32_t recordId, int resultCode, const Want &want, int32_t userId) 575eace7efcSopenharmony_ci{ 576eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "finished resultCode:%{public}d", resultCode); 577eace7efcSopenharmony_ci NotifyFreeInstallResult(recordId, want, resultCode); 578eace7efcSopenharmony_ci} 579eace7efcSopenharmony_ci 580eace7efcSopenharmony_ciint FreeInstallManager::AddFreeInstallObserver(const sptr<IRemoteObject> &callerToken, 581eace7efcSopenharmony_ci const sptr<AbilityRuntime::IFreeInstallObserver> &observer) 582eace7efcSopenharmony_ci{ 583eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "called"); 584eace7efcSopenharmony_ci auto abilityRecord = Token::GetAbilityRecordByToken(callerToken); 585eace7efcSopenharmony_ci if (abilityRecord != nullptr) { 586eace7efcSopenharmony_ci return DelayedSingleton<FreeInstallObserverManager>::GetInstance()->AddObserver(abilityRecord->GetRecordId(), 587eace7efcSopenharmony_ci observer); 588eace7efcSopenharmony_ci } 589eace7efcSopenharmony_ci if (AAFwk::PermissionVerification::GetInstance()->IsSACall()) { 590eace7efcSopenharmony_ci return DelayedSingleton<FreeInstallObserverManager>::GetInstance()->AddObserver(-1, observer); 591eace7efcSopenharmony_ci } 592eace7efcSopenharmony_ci return CHECK_PERMISSION_FAILED; 593eace7efcSopenharmony_ci} 594eace7efcSopenharmony_ci 595eace7efcSopenharmony_civoid FreeInstallManager::RemoveFreeInstallInfo(const std::string &bundleName, const std::string &abilityName, 596eace7efcSopenharmony_ci const std::string &startTime) 597eace7efcSopenharmony_ci{ 598eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 599eace7efcSopenharmony_ci for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) { 600eace7efcSopenharmony_ci if ((*it).want.GetElement().GetBundleName() == bundleName && 601eace7efcSopenharmony_ci (*it).want.GetElement().GetAbilityName() == abilityName && 602eace7efcSopenharmony_ci (*it).want.GetStringParam(Want::PARAM_RESV_START_TIME) == startTime) { 603eace7efcSopenharmony_ci it = freeInstallList_.erase(it); 604eace7efcSopenharmony_ci } else { 605eace7efcSopenharmony_ci it++; 606eace7efcSopenharmony_ci } 607eace7efcSopenharmony_ci } 608eace7efcSopenharmony_ci} 609eace7efcSopenharmony_ci 610eace7efcSopenharmony_ciint32_t FreeInstallManager::GetRecordIdByToken(const sptr<IRemoteObject> &callerToken) 611eace7efcSopenharmony_ci{ 612eace7efcSopenharmony_ci auto abilityRecord = Token::GetAbilityRecordByToken(callerToken); 613eace7efcSopenharmony_ci int recordId = -1; 614eace7efcSopenharmony_ci if (abilityRecord != nullptr) { 615eace7efcSopenharmony_ci recordId = abilityRecord->GetRecordId(); 616eace7efcSopenharmony_ci } 617eace7efcSopenharmony_ci return recordId; 618eace7efcSopenharmony_ci} 619eace7efcSopenharmony_ci 620eace7efcSopenharmony_cibool FreeInstallManager::VerifyStartFreeInstallPermission(const sptr<IRemoteObject> &callerToken) 621eace7efcSopenharmony_ci{ 622eace7efcSopenharmony_ci auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); 623eace7efcSopenharmony_ci if (isSaCall || IsTopAbility(callerToken)) { 624eace7efcSopenharmony_ci return true; 625eace7efcSopenharmony_ci } 626eace7efcSopenharmony_ci 627eace7efcSopenharmony_ci if (AAFwk::PermissionVerification::GetInstance()->VerifyCallingPermission( 628eace7efcSopenharmony_ci PermissionConstants::PERMISSION_START_ABILITIES_FROM_BACKGROUND)) { 629eace7efcSopenharmony_ci return true; 630eace7efcSopenharmony_ci } 631eace7efcSopenharmony_ci 632eace7efcSopenharmony_ci return false; 633eace7efcSopenharmony_ci} 634eace7efcSopenharmony_ci 635eace7efcSopenharmony_ciint FreeInstallManager::SetAppRunningState(Want &want) 636eace7efcSopenharmony_ci{ 637eace7efcSopenharmony_ci auto appMgr = AppMgrUtil::GetAppMgr(); 638eace7efcSopenharmony_ci if (appMgr == nullptr) { 639eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "null appMgr"); 640eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 641eace7efcSopenharmony_ci } 642eace7efcSopenharmony_ci 643eace7efcSopenharmony_ci bool isAppRunning = appMgr->GetAppRunningStateByBundleName(want.GetElement().GetBundleName()); 644eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "isAppRunning:%{public}d", static_cast<int>(isAppRunning)); 645eace7efcSopenharmony_ci want.SetParam(KEY_IS_APP_RUNNING, isAppRunning); 646eace7efcSopenharmony_ci return ERR_OK; 647eace7efcSopenharmony_ci} 648eace7efcSopenharmony_ci 649eace7efcSopenharmony_cibool FreeInstallManager::GetFreeInstallTaskInfo(const std::string& bundleName, const std::string& abilityName, 650eace7efcSopenharmony_ci const std::string& startTime, FreeInstallInfo& taskInfo) 651eace7efcSopenharmony_ci{ 652eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 653eace7efcSopenharmony_ci for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) { 654eace7efcSopenharmony_ci if ((*it).want.GetElement().GetBundleName() == bundleName && 655eace7efcSopenharmony_ci (*it).want.GetElement().GetAbilityName() == abilityName && 656eace7efcSopenharmony_ci (*it).want.GetStringParam(Want::PARAM_RESV_START_TIME) == startTime) { 657eace7efcSopenharmony_ci taskInfo = *it; 658eace7efcSopenharmony_ci return true; 659eace7efcSopenharmony_ci } 660eace7efcSopenharmony_ci it++; 661eace7efcSopenharmony_ci } 662eace7efcSopenharmony_ci return false; 663eace7efcSopenharmony_ci} 664eace7efcSopenharmony_ci 665eace7efcSopenharmony_cibool FreeInstallManager::GetFreeInstallTaskInfo(const std::string& sessionId, FreeInstallInfo& taskInfo) 666eace7efcSopenharmony_ci{ 667eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 668eace7efcSopenharmony_ci for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) { 669eace7efcSopenharmony_ci if ((*it).want.GetStringParam(KEY_SESSION_ID) == sessionId) { 670eace7efcSopenharmony_ci taskInfo = *it; 671eace7efcSopenharmony_ci return true; 672eace7efcSopenharmony_ci } 673eace7efcSopenharmony_ci it++; 674eace7efcSopenharmony_ci } 675eace7efcSopenharmony_ci return false; 676eace7efcSopenharmony_ci} 677eace7efcSopenharmony_ci 678eace7efcSopenharmony_civoid FreeInstallManager::SetSCBCallStatus(const std::string& bundleName, const std::string& abilityName, 679eace7efcSopenharmony_ci const std::string& startTime, bool scbCallStatus) 680eace7efcSopenharmony_ci{ 681eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 682eace7efcSopenharmony_ci for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) { 683eace7efcSopenharmony_ci if ((*it).want.GetElement().GetBundleName() == bundleName && 684eace7efcSopenharmony_ci (*it).want.GetElement().GetAbilityName() == abilityName && 685eace7efcSopenharmony_ci (*it).want.GetStringParam(Want::PARAM_RESV_START_TIME) == startTime) { 686eace7efcSopenharmony_ci (*it).isStartUIAbilityBySCBCalled = scbCallStatus; 687eace7efcSopenharmony_ci return; 688eace7efcSopenharmony_ci } 689eace7efcSopenharmony_ci it++; 690eace7efcSopenharmony_ci } 691eace7efcSopenharmony_ci} 692eace7efcSopenharmony_ci 693eace7efcSopenharmony_civoid FreeInstallManager::SetPreStartMissionCallStatus(const std::string& bundleName, const std::string& abilityName, 694eace7efcSopenharmony_ci const std::string& startTime, bool preStartMissionCallStatus) 695eace7efcSopenharmony_ci{ 696eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 697eace7efcSopenharmony_ci for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) { 698eace7efcSopenharmony_ci if ((*it).want.GetElement().GetBundleName() == bundleName && 699eace7efcSopenharmony_ci (*it).want.GetElement().GetAbilityName() == abilityName && 700eace7efcSopenharmony_ci (*it).want.GetStringParam(Want::PARAM_RESV_START_TIME) == startTime) { 701eace7efcSopenharmony_ci (*it).isPreStartMissionCalled = preStartMissionCallStatus; 702eace7efcSopenharmony_ci return; 703eace7efcSopenharmony_ci } 704eace7efcSopenharmony_ci it++; 705eace7efcSopenharmony_ci } 706eace7efcSopenharmony_ci} 707eace7efcSopenharmony_ci 708eace7efcSopenharmony_civoid FreeInstallManager::SetFreeInstallTaskSessionId(const std::string& bundleName, const std::string& abilityName, 709eace7efcSopenharmony_ci const std::string& startTime, const std::string& sessionId) 710eace7efcSopenharmony_ci{ 711eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 712eace7efcSopenharmony_ci for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) { 713eace7efcSopenharmony_ci if ((*it).want.GetElement().GetBundleName() == bundleName && 714eace7efcSopenharmony_ci (*it).want.GetElement().GetAbilityName() == abilityName && 715eace7efcSopenharmony_ci (*it).want.GetStringParam(Want::PARAM_RESV_START_TIME) == startTime) { 716eace7efcSopenharmony_ci (*it).want.SetParam(KEY_SESSION_ID, sessionId); 717eace7efcSopenharmony_ci return; 718eace7efcSopenharmony_ci } 719eace7efcSopenharmony_ci it++; 720eace7efcSopenharmony_ci } 721eace7efcSopenharmony_ci} 722eace7efcSopenharmony_ci 723eace7efcSopenharmony_civoid FreeInstallManager::NotifyInsightIntentFreeInstallResult(const Want &want, int resultCode) 724eace7efcSopenharmony_ci{ 725eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 726eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "insight install result:%{public}d", resultCode); 727eace7efcSopenharmony_ci if (resultCode != ERR_OK) { 728eace7efcSopenharmony_ci RemoveFreeInstallInfo(want.GetElement().GetBundleName(), want.GetElement().GetAbilityName(), 729eace7efcSopenharmony_ci want.GetStringParam(Want::PARAM_RESV_START_TIME)); 730eace7efcSopenharmony_ci NotifyInsightIntentExecuteDone(want, ERR_INVALID_VALUE); 731eace7efcSopenharmony_ci return; 732eace7efcSopenharmony_ci } 733eace7efcSopenharmony_ci 734eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(freeInstallListLock_); 735eace7efcSopenharmony_ci if (freeInstallList_.empty()) { 736eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::FREE_INSTALL, "list empty"); 737eace7efcSopenharmony_ci return; 738eace7efcSopenharmony_ci } 739eace7efcSopenharmony_ci 740eace7efcSopenharmony_ci for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) { 741eace7efcSopenharmony_ci std::string bundleName = (*it).want.GetElement().GetBundleName(); 742eace7efcSopenharmony_ci std::string abilityName = (*it).want.GetElement().GetAbilityName(); 743eace7efcSopenharmony_ci std::string startTime = (*it).want.GetStringParam(Want::PARAM_RESV_START_TIME); 744eace7efcSopenharmony_ci if (want.GetElement().GetBundleName().compare(bundleName) != 0 || 745eace7efcSopenharmony_ci want.GetElement().GetAbilityName().compare(abilityName) != 0 || 746eace7efcSopenharmony_ci want.GetStringParam(Want::PARAM_RESV_START_TIME).compare(startTime) != 0) { 747eace7efcSopenharmony_ci it++; 748eace7efcSopenharmony_ci continue; 749eace7efcSopenharmony_ci } 750eace7efcSopenharmony_ci 751eace7efcSopenharmony_ci auto moduleName = (*it).want.GetElement().GetModuleName(); 752eace7efcSopenharmony_ci auto insightIntentName = (*it).want.GetStringParam(AppExecFwk::INSIGHT_INTENT_EXECUTE_PARAM_NAME); 753eace7efcSopenharmony_ci auto executeMode = static_cast<AppExecFwk::ExecuteMode>( 754eace7efcSopenharmony_ci it->want.GetIntParam(AppExecFwk::INSIGHT_INTENT_EXECUTE_PARAM_MODE, 0)); 755eace7efcSopenharmony_ci std::string srcEntry; 756eace7efcSopenharmony_ci auto ret = AbilityRuntime::InsightIntentUtils::GetSrcEntry(it->want.GetElement(), insightIntentName, 757eace7efcSopenharmony_ci executeMode, srcEntry); 758eace7efcSopenharmony_ci if (ret != ERR_OK || srcEntry.empty()) { 759eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "failed. bundleName: %{public}s, " 760eace7efcSopenharmony_ci "moduleName: %{public}s, insightIntentName: %{public}s", bundleName.c_str(), moduleName.c_str(), 761eace7efcSopenharmony_ci insightIntentName.c_str()); 762eace7efcSopenharmony_ci NotifyInsightIntentExecuteDone(want, ERR_INVALID_VALUE); 763eace7efcSopenharmony_ci } else { 764eace7efcSopenharmony_ci (*it).want.SetParam(AppExecFwk::INSIGHT_INTENT_SRC_ENTRY, srcEntry); 765eace7efcSopenharmony_ci StartAbilityByFreeInstall(*it, bundleName, abilityName, startTime); 766eace7efcSopenharmony_ci } 767eace7efcSopenharmony_ci 768eace7efcSopenharmony_ci it = freeInstallList_.erase(it); 769eace7efcSopenharmony_ci } 770eace7efcSopenharmony_ci} 771eace7efcSopenharmony_ci 772eace7efcSopenharmony_civoid FreeInstallManager::NotifyInsightIntentExecuteDone(const Want &want, int resultCode) 773eace7efcSopenharmony_ci{ 774eace7efcSopenharmony_ci InsightIntentExecuteParam executeParam; 775eace7efcSopenharmony_ci InsightIntentExecuteParam::GenerateFromWant(want, executeParam); 776eace7efcSopenharmony_ci AppExecFwk::InsightIntentExecuteResult result; 777eace7efcSopenharmony_ci auto ret = DelayedSingleton<InsightIntentExecuteManager>::GetInstance()->ExecuteIntentDone( 778eace7efcSopenharmony_ci executeParam.insightIntentId_, resultCode, result); 779eace7efcSopenharmony_ci if (ret != ERR_OK) { 780eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::FREE_INSTALL, "failed with %{public}d", ret); 781eace7efcSopenharmony_ci } 782eace7efcSopenharmony_ci} 783eace7efcSopenharmony_ci} // namespace AAFwk 784eace7efcSopenharmony_ci} // namespace OHOS 785