diff --git a/services/abilitymgr/include/ecological_rule/ability_ecological_rule_mgr_service_param.h b/services/abilitymgr/include/ecological_rule/ability_ecological_rule_mgr_service_param.h index db5541ffe7..ea12333645 100644 --- a/services/abilitymgr/include/ecological_rule/ability_ecological_rule_mgr_service_param.h +++ b/services/abilitymgr/include/ecological_rule/ability_ecological_rule_mgr_service_param.h @@ -21,6 +21,7 @@ #include "parcel.h" #include "want.h" +#include "ability_info.h" namespace OHOS { namespace EcologicalRuleMgrService { @@ -69,10 +70,11 @@ struct AbilityCallerInfo : public Parcelable { std::string targetAppDistType = ""; std::string targetLinkFeature = ""; int32_t targetLinkType = LINK_TYPE_INVALID; - int32_t callerAbilityType = 0L; + AppExecFwk::AbilityType callerAbilityType = AppExecFwk::AbilityType::UNKNOWN; int32_t embedded = 0; std::string callerAppProvisionType; std::string targetAppProvisionType; + AppExecFwk::ExtensionAbilityType callerExtensionAbilityType = AppExecFwk::ExtensionAbilityType::UNSPECIFIED; bool ReadFromParcel(Parcel &parcel); diff --git a/services/abilitymgr/include/interceptor/ecological_rule_interceptor.h b/services/abilitymgr/include/interceptor/ecological_rule_interceptor.h index 8e3b693fd3..9f9bd8aecf 100644 --- a/services/abilitymgr/include/interceptor/ecological_rule_interceptor.h +++ b/services/abilitymgr/include/interceptor/ecological_rule_interceptor.h @@ -40,7 +40,8 @@ public: }; private: - void GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId); + void GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId, + const sptr &callerToken = nullptr); void InitErmsCallerInfo(Want &want, ErmsCallerInfo &callerInfo) const; }; } // namespace AAFwk diff --git a/services/abilitymgr/include/start_ability_utils.h b/services/abilitymgr/include/start_ability_utils.h index a94788af8c..287e54e2ba 100644 --- a/services/abilitymgr/include/start_ability_utils.h +++ b/services/abilitymgr/include/start_ability_utils.h @@ -30,6 +30,7 @@ struct StartAbilityInfo { AppExecFwk::AbilityInfo &abilityInfo); static std::shared_ptr CreateStartAbilityInfo(const Want &want, int32_t userId, int32_t appIndex); + static std::shared_ptr CreateCallerAbilityInfo(const sptr &callerToken); static std::shared_ptr CreateStartExtensionInfo(const Want &want, int32_t userId, int32_t appIndex); @@ -48,15 +49,18 @@ struct StartAbilityUtils { static int32_t GetAppIndex(const Want &want, sptr callerToken); static bool GetApplicationInfo(const std::string &bundleName, int32_t userId, AppExecFwk::ApplicationInfo &appInfo); + static bool GetCallerAbilityInfo(const sptr &callerToken, + AppExecFwk::AbilityInfo &abilityInfo); static thread_local std::shared_ptr startAbilityInfo; - + static thread_local std::shared_ptr callerAbilityInfo; static thread_local bool skipCrowTest; static thread_local bool skipStartOther; static thread_local bool skipErms; }; struct StartAbilityInfoWrap { - StartAbilityInfoWrap(const Want &want, int32_t validUserId, int32_t appIndex, bool isExtension = false); + StartAbilityInfoWrap(const Want &want, int32_t validUserId, int32_t appIndex, + const sptr &callerToken, bool isExtension = false); ~StartAbilityInfoWrap(); }; } diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index a6e1f553c7..48f79cd0de 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -877,7 +877,7 @@ int AbilityManagerService::StartAbilityInner(const Want &want, const sptr sessionInfo) auto requestCode = sessionInfo->requestCode; StartAbilityInfoWrap threadLocalInfo(sessionInfo->want, currentUserId, - StartAbilityUtils::GetAppIndex(sessionInfo->want, sessionInfo->callerToken)); + StartAbilityUtils::GetAppIndex(sessionInfo->want, sessionInfo->callerToken), sessionInfo->callerToken); if (sessionInfo->want.GetBoolParam(IS_CALL_BY_SCB, true)) { TAG_LOGD(AAFwkTag::ABILITYMGR, "interceptorExecuter_ called."); AbilityInterceptorParam interceptorParam = AbilityInterceptorParam(sessionInfo->want, requestCode, @@ -2388,7 +2388,7 @@ int AbilityManagerService::StartExtensionAbilityInner(const Want &want, const sp int32_t validUserId = GetValidUserId(userId); StartAbilityInfoWrap threadLocalInfo(want, validUserId, - StartAbilityUtils::GetAppIndex(want, callerToken), true); + StartAbilityUtils::GetAppIndex(want, callerToken), callerToken, true); AbilityInterceptorParam interceptorParam = AbilityInterceptorParam(want, 0, GetUserId(), false, nullptr); result = interceptorExecuter_ == nullptr ? ERR_INVALID_VALUE : interceptorExecuter_->DoProcess(interceptorParam); @@ -6428,7 +6428,7 @@ int AbilityManagerService::StartAbilityByCall(const Want &want, const sptr(want)); StartAbilityInfoWrap threadLocalInfo(want, GetUserId(), - StartAbilityUtils::GetAppIndex(want, callerToken)); + StartAbilityUtils::GetAppIndex(want, callerToken), callerToken); AbilityInterceptorParam interceptorParam = AbilityInterceptorParam(want, 0, GetUserId(), true, nullptr); auto result = interceptorExecuter_ == nullptr ? ERR_INVALID_VALUE : interceptorExecuter_->DoProcess(interceptorParam); diff --git a/services/abilitymgr/src/ecological_rule/ability_ecological_rule_mgr_service_param.cpp b/services/abilitymgr/src/ecological_rule/ability_ecological_rule_mgr_service_param.cpp index 60b12c2e91..704a24832a 100644 --- a/services/abilitymgr/src/ecological_rule/ability_ecological_rule_mgr_service_param.cpp +++ b/services/abilitymgr/src/ecological_rule/ability_ecological_rule_mgr_service_param.cpp @@ -125,15 +125,12 @@ AbilityCallerInfo *AbilityCallerInfo::Unmarshalling(Parcel &in) return nullptr; } - if (!in.ReadInt32(info->callerAbilityType)) { - TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "read callerAbilityType failed"); - delete info; - return nullptr; - } + info->callerAbilityType = static_cast(in.ReadInt32()); info->embedded = in.ReadInt32(); info->callerAppProvisionType = in.ReadString(); info->targetAppProvisionType = in.ReadString(); + info->callerExtensionAbilityType = static_cast(in.ReadInt32()); return info; } @@ -157,6 +154,11 @@ bool AbilityCallerInfo::Marshalling(Parcel &parcel) const TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "write targetAppProvisionType failed"); return false; } + + if (!parcel.WriteInt32(static_cast(callerExtensionAbilityType))) { + TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "write callerExtensionAbilityType failed"); + return false; + } return true; } @@ -207,10 +209,11 @@ bool AbilityCallerInfo::DoMarshallingOne(Parcel &parcel) const return false; } - if (!parcel.WriteInt32(callerAbilityType)) { + if (!parcel.WriteInt32(static_cast(callerAbilityType))) { TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "write callerAbilityType failed"); return false; } + return true; } @@ -220,7 +223,9 @@ std::string AbilityCallerInfo::ToString() const ",pid:" + std::to_string(pid) + ",callerAppType:" + std::to_string(callerAppType) + ",targetAppType:" + std::to_string(targetAppType) + ",callerModelType:" + std::to_string(callerModelType) + ",targetAppDistType:" + targetAppDistType + ",targetLinkFeature:" + targetLinkFeature + ",targetLinkType:" + - std::to_string(targetLinkType) + ",callerAbilityType:" + std::to_string(callerAbilityType) + ",embedded:" + + std::to_string(targetLinkType) + ",callerAbilityType:" + + std::to_string(static_cast(callerAbilityType)) + ",callerExtensionAbilityType:" + + std::to_string(static_cast(callerExtensionAbilityType)) + ",embedded:" + std::to_string(embedded) + ",callerAppProvisionType:" + callerAppProvisionType + ",targetAppProvisionType:" + targetAppProvisionType + "}"; return str; diff --git a/services/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp b/services/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp index 243fbb120e..e18970b521 100644 --- a/services/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp +++ b/services/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp @@ -54,7 +54,7 @@ ErrCode EcologicalRuleInterceptor::DoProcess(AbilityInterceptorParam param) } AAFwk::Want newWant = param.want; newWant.RemoveAllFd(); - GetEcologicalCallerInfo(newWant, callerInfo, param.userId); + GetEcologicalCallerInfo(newWant, callerInfo, param.userId, param.callerToken); std::string supportErms = OHOS::system::GetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); if (supportErms == "false") { TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "Abilityms not support Erms between applications."); @@ -128,30 +128,39 @@ bool EcologicalRuleInterceptor::DoProcess(Want &want, int32_t userId) return rule.isAllow; } -void EcologicalRuleInterceptor::GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId) +void EcologicalRuleInterceptor::GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId, + const sptr &callerToken) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); InitErmsCallerInfo(const_cast(want), callerInfo); - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); - if (bundleMgrHelper == nullptr) { - TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "The bundleMgrHelper is nullptr."); - return; - } - - std::string callerBundleName; - ErrCode err = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerInfo.uid, callerBundleName)); - if (err != ERR_OK) { - TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "Get callerBundleName failed,uid: %{public}d.", callerInfo.uid); - return; - } AppExecFwk::ApplicationInfo callerAppInfo; - bool getCallerResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(callerBundleName, - AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, callerAppInfo)); - if (!getCallerResult) { - TAG_LOGD(AAFwkTag::ECOLOGICAL_RULE, "Get callerAppInfo failed."); - return; + AppExecFwk::AbilityInfo callerAbilityInfo; + if (StartAbilityUtils::GetCallerAbilityInfo(callerToken, callerAbilityInfo)) { + callerAppInfo = callerAbilityInfo.applicationInfo; + callerInfo.callerAbilityType = callerAbilityInfo.type; + callerInfo.callerExtensionAbilityType = callerAbilityInfo.extensionAbilityType; + } else { + auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + if (bundleMgrHelper == nullptr) { + TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "The bundleMgrHelper is nullptr."); + return; + } + + std::string callerBundleName; + ErrCode err = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerInfo.uid, callerBundleName)); + if (err != ERR_OK) { + TAG_LOGE(AAFwkTag::ECOLOGICAL_RULE, "Get callerBundleName failed,uid: %{public}d.", callerInfo.uid); + return; + } + bool getCallerResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(callerBundleName, + AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, callerAppInfo)); + if (!getCallerResult) { + TAG_LOGD(AAFwkTag::ECOLOGICAL_RULE, "Get callerAppInfo failed."); + return; + } } + callerInfo.callerAppProvisionType = callerAppInfo.appProvisionType; if (callerAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) { TAG_LOGD(AAFwkTag::ECOLOGICAL_RULE, "the caller type is atomic service"); diff --git a/services/abilitymgr/src/start_ability_utils.cpp b/services/abilitymgr/src/start_ability_utils.cpp index eefc88da68..cc4b096b86 100644 --- a/services/abilitymgr/src/start_ability_utils.cpp +++ b/services/abilitymgr/src/start_ability_utils.cpp @@ -31,6 +31,7 @@ constexpr const char* SCREENSHOT_BUNDLE_NAME = "com.huawei.ohos.screenshot"; constexpr const char* SCREENSHOT_ABILITY_NAME = "com.huawei.ohos.screenshot.ServiceExtAbility"; } thread_local std::shared_ptr StartAbilityUtils::startAbilityInfo; +thread_local std::shared_ptr StartAbilityUtils::callerAbilityInfo; thread_local bool StartAbilityUtils::skipCrowTest = false; thread_local bool StartAbilityUtils::skipStartOther = false; thread_local bool StartAbilityUtils::skipErms = false; @@ -66,8 +67,26 @@ bool StartAbilityUtils::GetApplicationInfo(const std::string &bundleName, int32_ return true; } +bool StartAbilityUtils::GetCallerAbilityInfo(const sptr &callerToken, + AppExecFwk::AbilityInfo &abilityInfo) +{ + if (StartAbilityUtils::callerAbilityInfo) { + abilityInfo = StartAbilityUtils::callerAbilityInfo->abilityInfo; + } else { + if (callerToken == nullptr) { + return false; + } + auto abilityRecord = Token::GetAbilityRecordByToken(callerToken); + if (abilityRecord == nullptr) { + return false; + } + abilityInfo = abilityRecord->GetAbilityInfo(); + } + return true; +} + StartAbilityInfoWrap::StartAbilityInfoWrap(const Want &want, int32_t validUserId, int32_t appIndex, - bool isExtension) + const sptr &callerToken, bool isExtension) { if (StartAbilityUtils::startAbilityInfo != nullptr) { TAG_LOGW(AAFwkTag::ABILITYMGR, "startAbilityInfo has been created"); @@ -91,11 +110,17 @@ StartAbilityInfoWrap::StartAbilityInfoWrap(const Want &want, int32_t validUserId StartAbilityUtils::skipCrowTest = true; StartAbilityUtils::skipStartOther = true; } + + if (StartAbilityUtils::callerAbilityInfo != nullptr) { + TAG_LOGW(AAFwkTag::ABILITYMGR, "callerAbilityInfo has been created"); + } + StartAbilityUtils::callerAbilityInfo = StartAbilityInfo::CreateCallerAbilityInfo(callerToken); } StartAbilityInfoWrap::~StartAbilityInfoWrap() { StartAbilityUtils::startAbilityInfo.reset(); + StartAbilityUtils::callerAbilityInfo.reset(); StartAbilityUtils::skipCrowTest = false; StartAbilityUtils::skipStartOther = false; StartAbilityUtils::skipErms = false; @@ -220,5 +245,22 @@ std::shared_ptr StartAbilityInfo::CreateStartExtensionInfo(con return abilityInfo; } + +std::shared_ptr StartAbilityInfo::CreateCallerAbilityInfo(const sptr &callerToken) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + if (callerToken == nullptr) { + TAG_LOGD(AAFwkTag::ABILITYMGR, "not call from context."); + return nullptr; + } + auto abilityRecord = Token::GetAbilityRecordByToken(callerToken); + if (abilityRecord == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "can not find abilityRecord"); + return nullptr; + } + auto request = std::make_shared(); + request->abilityInfo = abilityRecord->GetAbilityInfo(); + return request; +} } } \ No newline at end of file