diff --git a/frameworks/js/napi/wantagent/napi_want_agent.cpp b/frameworks/js/napi/wantagent/napi_want_agent.cpp index 445affef69fa79d91fac4d00398dcbf5b66aad50..0bedfb765ef4e15484177c881495e3f0af53b0bc 100644 --- a/frameworks/js/napi/wantagent/napi_want_agent.cpp +++ b/frameworks/js/napi/wantagent/napi_want_agent.cpp @@ -970,6 +970,16 @@ int32_t JsWantAgent::GetWantAgentParam(napi_env env, napi_callback_info info, Wa return PARAMETER_ERROR; } + bool hasUserId = false; + napi_has_named_property(env, argv[0], "userId", &hasUserId); + if (hasUserId) { + napi_value jsUserId = nullptr; + napi_get_named_property(env, argv[0], "userId", &jsUserId); + if (!ConvertFromJsValue(env, jsUserId, paras.userId)) { + TAG_LOGE(AAFwkTag::WANTAGENT, "Convert userId failed"); + return PARAMETER_ERROR; + } + } bool hasActionFlags = false; napi_has_named_property(env, argv[0], "actionFlags", &hasActionFlags); if (hasActionFlags) { @@ -1341,7 +1351,8 @@ void JsWantAgent::SetOnNapiGetWantAgentCallback(std::shared_ptr(parasobj->operationType), parasobj->wantAgentFlags, parasobj->wants, - extraInfo); + extraInfo, + parasobj->userId); auto context = OHOS::AbilityRuntime::Context::GetApplicationContext(); std::shared_ptr wantAgent = nullptr; diff --git a/frameworks/js/napi/wantagent/napi_want_agent.h b/frameworks/js/napi/wantagent/napi_want_agent.h index 6307419eb76813ff01fca4e43a15ee47f691ce0f..b9f2d6a43b2ecdc92d11e2de699726c8c1950a7b 100644 --- a/frameworks/js/napi/wantagent/napi_want_agent.h +++ b/frameworks/js/napi/wantagent/napi_want_agent.h @@ -72,6 +72,7 @@ struct WantAgentWantsParas { std::vector> wants = {}; int32_t operationType = -1; int32_t requestCode = -1; + int32_t userId = -1; std::vector wantAgentFlags = {}; AAFwk::WantParams extraInfo = {}; }; diff --git a/interfaces/inner_api/wantagent/include/pending_want.h b/interfaces/inner_api/wantagent/include/pending_want.h index 00ab6d4fe03eb80763b71ce028bdde0b52f0c5cb..4cdebd5a0c5faae8118d3340a16fd63733af6798 100644 --- a/interfaces/inner_api/wantagent/include/pending_want.h +++ b/interfaces/inner_api/wantagent/include/pending_want.h @@ -74,7 +74,7 @@ public: const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, const std::shared_ptr &options, - std::shared_ptr &pendingWant); + std::shared_ptr &pendingWant, int userId = -1); /** * Like GetAbility(Context, int, Want, int)}, but allows an diff --git a/interfaces/inner_api/wantagent/include/want_agent_info.h b/interfaces/inner_api/wantagent/include/want_agent_info.h index 326c73e7b68abc1b8300d01813ab39d5a8702571..03a718bc381c1e329621870104a69b621d222c3a 100644 --- a/interfaces/inner_api/wantagent/include/want_agent_info.h +++ b/interfaces/inner_api/wantagent/include/want_agent_info.h @@ -69,7 +69,7 @@ public: */ WantAgentInfo(int requestCode, const WantAgentConstant::OperationType &operationType, const std::vector &flags, std::vector> &Wants, - const std::shared_ptr &extraInfo); + const std::shared_ptr &extraInfo, int userId = -1); /** * A constructor used to create an WantAgentInfo instance by copying parameters from an existing one. @@ -113,8 +113,16 @@ public: */ std::shared_ptr GetExtraInfo() const; + /** + * Obtains the userId of the WantAgent object. + * + * @return Returns the userId of the WantAgent object. + */ + int GetUserId() const; + private: int requestCode_ = 0; + int userId_ = -1; WantAgentConstant::OperationType operationType_ = WantAgentConstant::OperationType::UNKNOWN_TYPE; std::vector flags_ = std::vector(); std::vector> wants_ = std::vector>(); diff --git a/interfaces/inner_api/wantagent/src/pending_want.cpp b/interfaces/inner_api/wantagent/src/pending_want.cpp index cccc8d57cd99d31654bd6f7c13f4e5391282eef2..770d949341c715d6377f9c44aceb8a416078a75c 100644 --- a/interfaces/inner_api/wantagent/src/pending_want.cpp +++ b/interfaces/inner_api/wantagent/src/pending_want.cpp @@ -57,7 +57,7 @@ ErrCode PendingWant::GetAbility( const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, const std::shared_ptr &options, - std::shared_ptr &pendingWant) + std::shared_ptr &pendingWant, int userId) { if (context == nullptr) { TAG_LOGE(AAFwkTag::WANTAGENT, "invalid input param"); @@ -74,9 +74,13 @@ ErrCode PendingWant::GetAbility( WantSenderInfo wantSenderInfo; wantSenderInfo.type = static_cast(WantAgentConstant::OperationType::START_ABILITY); wantSenderInfo.allWants.push_back(wantsInfo); - wantSenderInfo.bundleName = context->GetBundleName(); + if (userId >= 0) { + wantSenderInfo.bundleName = want->GetOperation().GetBundleName(); + } else { + wantSenderInfo.bundleName = context->GetBundleName(); + } wantSenderInfo.flags = flags; - wantSenderInfo.userId = -1; // -1 : invalid user id + wantSenderInfo.userId = userId; wantSenderInfo.requestCode = requestCode; sptr target = nullptr; ErrCode result = WantAgentClient::GetInstance().GetWantSender(wantSenderInfo, nullptr, target); diff --git a/interfaces/inner_api/wantagent/src/want_agent_helper.cpp b/interfaces/inner_api/wantagent/src/want_agent_helper.cpp index 5f7d6248e40ab92cecb6c9f7fd3e3fffec2fa3a0..06c7b6bda77d2003537e5aa29efca2065b68c92f 100644 --- a/interfaces/inner_api/wantagent/src/want_agent_helper.cpp +++ b/interfaces/inner_api/wantagent/src/want_agent_helper.cpp @@ -112,11 +112,12 @@ ErrCode WantAgentHelper::GetWantAgent( std::shared_ptr extraInfo = paramsInfo.GetExtraInfo(); std::shared_ptr pendingWant = nullptr; int requestCode = paramsInfo.GetRequestCode(); + int userId = paramsInfo.GetUserId(); WantAgentConstant::OperationType operationType = paramsInfo.GetOperationType(); ErrCode result; switch (operationType) { case WantAgentConstant::OperationType::START_ABILITY: - result = PendingWant::GetAbility(context, requestCode, wants[0], flags, extraInfo, pendingWant); + result = PendingWant::GetAbility(context, requestCode, wants[0], flags, extraInfo, pendingWant, userId); break; case WantAgentConstant::OperationType::START_ABILITIES: result = PendingWant::GetAbilities(context, requestCode, wants, flags, extraInfo, pendingWant); @@ -468,6 +469,7 @@ std::string WantAgentHelper::ToString(const std::shared_ptr &agent) jsonObject["requestCode"] = (*info.get()).requestCode; jsonObject["operationType"] = (*info.get()).type; jsonObject["flags"] = (*info.get()).flags; + jsonObject["userId"] = (*info.get()).userId; nlohmann::json wants = nlohmann::json::array(); for (auto &wantInfo : (*info.get()).allWants) { @@ -500,6 +502,11 @@ std::shared_ptr WantAgentHelper::FromString(const std::string &jsonSt requestCode = jsonObject.at("requestCode").get(); } + int userId = -1; + if (jsonObject.contains("userId") && jsonObject["userId"].is_number_integer()) { + userId = jsonObject.at("userId").get(); + } + WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::UNKNOWN_TYPE; if (jsonObject.contains("operationType") && jsonObject["operationType"].is_number_integer()) { operationType = static_cast(jsonObject.at("operationType").get()); @@ -530,7 +537,7 @@ std::shared_ptr WantAgentHelper::FromString(const std::string &jsonSt } WantAgentInfo info(requestCode, operationType, flagsVec, wants, extraInfo); - return GetWantAgent(info, INVLID_WANT_AGENT_USER_ID, uid); + return GetWantAgent(info, userId, uid); } std::vector WantAgentHelper::ParseFlags(nlohmann::json jsonObject) diff --git a/interfaces/inner_api/wantagent/src/want_agent_info.cpp b/interfaces/inner_api/wantagent/src/want_agent_info.cpp index f2f62d142478ad74643447c91461d17c323476a5..001b42fc72b0fbed23e0b0b898a93e6303527d28 100644 --- a/interfaces/inner_api/wantagent/src/want_agent_info.cpp +++ b/interfaces/inner_api/wantagent/src/want_agent_info.cpp @@ -44,10 +44,11 @@ WantAgentInfo::WantAgentInfo(int requestCode, const WantAgentConstant::Operation WantAgentInfo::WantAgentInfo(int requestCode, const WantAgentConstant::OperationType &operationType, const std::vector &flags, std::vector> &wants, - const std::shared_ptr &extraInfo) + const std::shared_ptr &extraInfo, int userId) { requestCode_ = requestCode; operationType_ = operationType; + userId_ = userId; if (!flags.empty()) { flags_.insert(flags_.end(), flags.begin(), flags.end()); } @@ -105,4 +106,9 @@ std::shared_ptr WantAgentInfo::GetExtraInfo() const { return extraInfo_; } + +int WantAgentInfo::GetUserId() const +{ + return userId_; +} } // namespace OHOS::AbilityRuntime::WantAgent diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index d41193fd98904202c237ff48985be63e732b01a7..5bf3c45fe8ab5a6d28afe0f4df7624d768cb1c7f 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -2333,6 +2333,9 @@ private: void PauseOldMissionListManager(int32_t userId); void PauseOldConnectManager(int32_t userId); bool IsSystemUI(const std::string &bundleName) const; + int32_t GetUidByCloneBundleInfo(std::string &bundleName, int32_t callerUid, int32_t appIndex, int32_t userId) const; + sptr GetWantSenderByUserId(const WantSenderInfo &wantSenderInfo, + const sptr &callerToken, int32_t uid, int32_t callerUid, int32_t callerUserId); bool VerificationAllToken(const sptr &token); std::shared_ptr GetCurrentDataAbilityManager(); diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 3cabdd18eed53b18da2c07bdcb3ee71554853326..46b139c3d87d79869f96fc184e150f3e7704464b 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -5594,27 +5594,97 @@ int AbilityManagerService::UnRegisterMissionListener(const std::string &deviceId return dmsClient.UnRegisterMissionListener(Str8ToStr16(deviceId), listener->AsObject()); } +int32_t AbilityManagerService::GetUidByCloneBundleInfo( + std::string &bundleName, int32_t callerUid, int32_t appIndex, int32_t userId) const +{ + auto bms = AbilityUtil::GetBundleManagerHelper(); + CHECK_POINTER_AND_RETURN(bms, -1); + AppExecFwk::BundleInfo bundleInfo; + MultiAppUtils::GetRunningMultiAppIndex(bundleName, callerUid, appIndex); + if (IN_PROCESS_CALL(bms->GetCloneBundleInfo( + bundleName, static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION), + appIndex, bundleInfo, userId)) != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "failed get bundle info for %{public}s", bundleName.c_str()); + return -1; + } + return bundleInfo.uid; +} + +sptr AbilityManagerService::GetWantSenderByUserId(const WantSenderInfo &wantSenderInfo, + const sptr &callerToken, int32_t uid, int32_t callerUid, int32_t callerUserId) +{ + bool isSpecifyUserId = wantSenderInfo.userId >= 0; + int32_t userId = -1, appUid = -1, appIndex = 0; + std::string bundleName = ""; + if (!wantSenderInfo.allWants.empty()) { + bundleName = wantSenderInfo.allWants.back().want.GetElement().GetBundleName(); + } + bool isSACall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); + bool isSystemApp = AAFwk::PermissionVerification::GetInstance()->IsSystemAppCall(); + if (isSpecifyUserId && (isSACall || callerUserId == U0_USER_ID)) { + userId = wantSenderInfo.userId; + if (uid >= 0) { + appUid = uid; + } else { + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } + } else if (!isSpecifyUserId && (isSACall || callerUserId == U0_USER_ID)) { + if (uid >= 0) { + if (DelayedSingleton::GetInstance()-> + GetOsAccountLocalIdFromUid(callerUid, userId) != 0) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid failed uid=%{public}d", callerUid); + return nullptr; + } + appUid = uid; + } else { + userId = callerUserId; + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } + } else if (!isSystemApp) { + userId = callerUserId; + appUid = callerUid; + } else if (isSystemApp) { + if (isSpecifyUserId) { + userId = wantSenderInfo.userId; + } else { + userId = callerUserId; + } + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } + std::shared_ptr pendingWantManager = GetPendingWantManagerByUserId(userId); + CHECK_POINTER_AND_RETURN(pendingWantManager, nullptr); + if (!CheckSenderWantInfo(appUid == -1 ? callerUid : appUid, wantSenderInfo)) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "check bundleName failed"); + return nullptr; + } + const_cast(wantSenderInfo).userId = userId; + TAG_LOGI(AAFwkTag::ABILITYMGR, "bundleName: %{public}s, appIndex: %{public}d, isSystemApp: %{public}d, " + "userId: %{public}d", bundleName.c_str(), appIndex, isSystemApp, userId); + return pendingWantManager->GetWantSender(callerUid, appUid, isSystemApp, wantSenderInfo, callerToken, appIndex); +} + sptr AbilityManagerService::GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken, int32_t uid) { TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); + int32_t userId = wantSenderInfo.userId; + int32_t callerUid = IPCSkeleton::GetCallingUid(); + int32_t callerUserId = callerUid / BASE_USER_RANGE; + if (userId >= 0 || callerUserId == U0_USER_ID) { + return GetWantSenderByUserId(wantSenderInfo, callerToken, uid, callerUid, callerUserId); + } auto pendingWantManager = GetCurrentPendingWantManager(); CHECK_POINTER_AND_RETURN(pendingWantManager, nullptr); auto bms = AbilityUtil::GetBundleManagerHelper(); CHECK_POINTER_AND_RETURN(bms, nullptr); - int32_t callerUid = IPCSkeleton::GetCallingUid(); - int32_t userId = wantSenderInfo.userId; int32_t bundleMgrResult = 0; - - if (userId < 0) { - if (DelayedSingleton::GetInstance()-> - GetOsAccountLocalIdFromUid(callerUid, userId) != 0) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid failed uid=%{public}d", callerUid); - return nullptr; - } + if (DelayedSingleton::GetInstance()-> + GetOsAccountLocalIdFromUid(callerUid, userId) != 0) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid failed uid=%{public}d", callerUid); + return nullptr; } TAG_LOGD(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid userId: %{public}d", userId); //sa caller and has uid,no need find from bms. @@ -5658,9 +5728,25 @@ sptr AbilityManagerService::GetWantSender( int AbilityManagerService::SendWantSender(sptr target, SenderInfo &senderInfo) { TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER_AND_RETURN(pendingWantManager, ERR_INVALID_VALUE); CHECK_POINTER_AND_RETURN(target, ERR_INVALID_VALUE); + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return ERR_INVALID_VALUE; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, ERR_INVALID_VALUE); + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId >= 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, ERR_INVALID_VALUE); return pendingWantManager->SendWantSender(target, senderInfo); } @@ -5680,8 +5766,6 @@ int AbilityManagerService::SendLocalWantSender(const SenderInfo &senderInfo) void AbilityManagerService::CancelWantSender(const sptr &sender) { TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER(pendingWantManager); CHECK_POINTER(sender); sptr obj = sender->AsObject(); @@ -5689,19 +5773,20 @@ void AbilityManagerService::CancelWantSender(const sptr &sender) TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); return; } - - auto bms = AbilityUtil::GetBundleManagerHelper(); - CHECK_POINTER(bms); - - int32_t callerUid = IPCSkeleton::GetCallingUid(); sptr record = iface_cast(obj); + CHECK_POINTER(record); - int userId = -1; - if (DelayedSingleton::GetInstance()-> - GetOsAccountLocalIdFromUid(callerUid, userId) != 0) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid failed uid=%{public}d", callerUid); - return; + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); } + std::shared_ptr pendingWantManager; + if (userId >= 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER(pendingWantManager); TAG_LOGD(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid userId: %{public}d", userId); bool isSystemAppCall = AAFwk::PermissionVerification::GetInstance()->IsSystemAppCall(); @@ -5743,12 +5828,29 @@ void AbilityManagerService::CancelWantSenderByFlags(const sptr &sen int AbilityManagerService::GetPendingWantUid(const sptr &target) { TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s:begin", __func__); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER_AND_RETURN(pendingWantManager, -1); if (target == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%s, target null", __func__); return -1; } + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return -1; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, -1); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId >= 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, -1); return pendingWantManager->GetPendingWantUid(target); } @@ -5768,21 +5870,56 @@ std::string AbilityManagerService::GetPendingWantBundleName(const sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return ""; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, ""); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId >= 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, ""); return pendingWantManager->GetPendingWantBundleName(target); } int AbilityManagerService::GetPendingWantCode(const sptr &target) { TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s:begin", __func__); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER_AND_RETURN(pendingWantManager, -1); if (target == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%s, target null", __func__); return -1; } + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return -1; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, -1); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId >= 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, -1); return pendingWantManager->GetPendingWantCode(target); } @@ -5790,12 +5927,29 @@ int AbilityManagerService::GetPendingWantType(const sptr &target) { TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); XCOLLIE_TIMER_DEFAULT(__PRETTY_FUNCTION__); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER_AND_RETURN(pendingWantManager, -1); if (target == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%s, target null", __func__); return -1; } + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return -1; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, -1); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId >= 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, -1); return pendingWantManager->GetPendingWantType(target); } @@ -5826,7 +5980,28 @@ int AbilityManagerService::GetPendingRequestWant(const sptr &target HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); TAG_LOGD(AAFwkTag::ABILITYMGR, "Get pending request want."); XCOLLIE_TIMER_DEFAULT(__PRETTY_FUNCTION__); - auto pendingWantManager = GetCurrentPendingWantManager(); + if (target == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "%s, target null", __func__); + return -1; + } + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return -1; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, -1); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId >= 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } CHECK_POINTER_AND_RETURN(pendingWantManager, ERR_INVALID_VALUE); CHECK_POINTER_AND_RETURN(target, ERR_INVALID_VALUE); CHECK_POINTER_AND_RETURN(want, ERR_INVALID_VALUE); diff --git a/test/unittest/ability_manager_service_eleven_test/BUILD.gn b/test/unittest/ability_manager_service_eleven_test/BUILD.gn index 81b99d53d7c242e28383540edc9056273a843ecc..18f8c549d46718feb3ddbc502b5a1a8dd7044687 100644 --- a/test/unittest/ability_manager_service_eleven_test/BUILD.gn +++ b/test/unittest/ability_manager_service_eleven_test/BUILD.gn @@ -35,6 +35,7 @@ ohos_unittest("ability_manager_service_eleven_test") { "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include", "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/aakit/include", "${ability_runtime_test_path}/unittest/app_debug_listener_stub_test", + "${ability_runtime_test_path}/unittest/multi_app_utils_test/include", "${ability_runtime_services_path}/abilitymgr/include/mission", "${ability_runtime_services_path}/abilitymgr/include/scene_board", "mock/include", @@ -48,6 +49,7 @@ ohos_unittest("ability_manager_service_eleven_test") { "${ability_runtime_services_path}/abilitymgr/src/utils/timeout_state_utils.cpp", "${ability_runtime_services_path}/abilitymgr/src/utils/window_options_utils.cpp", "${ability_runtime_services_path}/abilitymgr/src/utils/request_id_util.cpp", + "${ability_runtime_test_path}/unittest/multi_app_utils_test/src/mock_app_mgr_service.cpp", "ability_manager_service_eleven_test.cpp", "mock/src/mock_my_flag.cpp", "mock/src/mock_permission_verification.cpp", diff --git a/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp b/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp index 8a91e68bbd284bb2fce1b3c930af629383a1c4e0..bb0446c963f64fda6742d987da671844c43176bd 100644 --- a/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp +++ b/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp @@ -27,6 +27,7 @@ #include "mock_ability_controller.h" #include "mock_ability_manager_collaborator.h" #include "mock_ability_token.h" +#include "mock_app_mgr_service.h" #include "mock_mission_list_manager_interface.h" #include "mock_my_flag.h" #include "mock_permission_verification.h" @@ -254,8 +255,15 @@ HWTEST_F(AbilityManagerServiceElevenTest, GetWantSender_0002, TestSize.Level1) EXPECT_EQ(result, nullptr); MyFlag::flag_ = 1; - wantSenderInfo.userId = 1; - uid = 1; + wantSenderInfo.userId = 100; + bundleName = "com.ohos.settings"; + operation.SetBundleName(bundleName); + want.SetOperation(operation); + wantsInfo.want = want; + wantSenderInfo.allWants.clear(); + wantSenderInfo.allWants.emplace_back(wantsInfo); + abilityMs->subManagersHelper_->pendingWantManagers_.emplace( + wantSenderInfo.userId, std::make_shared(nullptr)); EXPECT_EQ(wantSenderInfo.allWants.size(), 1); result = abilityMs->GetWantSender(wantSenderInfo, nullptr, uid); EXPECT_NE(result, nullptr); @@ -263,6 +271,41 @@ HWTEST_F(AbilityManagerServiceElevenTest, GetWantSender_0002, TestSize.Level1) GTEST_LOG_(INFO) << "GetWantSender_0002 end"; } +/* + * Feature: GetWantSender_0003 + * Function: GetWantSender + * SubFunction: NA + * FunctionPoints: AbilityManagerService GetWantSender + */ +HWTEST_F(AbilityManagerServiceElevenTest, GetWantSender_0003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetWantSender_0003 start"; + auto abilityMs = std::make_shared(); + EXPECT_NE(abilityMs, nullptr); + abilityMs->subManagersHelper_ = std::make_shared(nullptr, nullptr); + EXPECT_NE(abilityMs->subManagersHelper_, nullptr); + + MyFlag::flag_ = 1; + WantSenderInfo wantSenderInfo; + wantSenderInfo.userId = 100; + AAFwk::Want want; + AAFwk::Operation operation; + std::string bundleName = "com.ohos.settings"; + operation.SetBundleName(bundleName); + want.SetOperation(operation); + WantsInfo wantsInfo; + wantsInfo.want = want; + wantsInfo.resolvedTypes = want.GetType(); + wantSenderInfo.allWants.clear(); + wantSenderInfo.allWants.emplace_back(wantsInfo); + abilityMs->subManagersHelper_->pendingWantManagers_.emplace( + wantSenderInfo.userId, std::make_shared(nullptr)); + EXPECT_EQ(wantSenderInfo.allWants.size(), 1); + auto result = abilityMs->GetWantSender(wantSenderInfo, nullptr, -1); + EXPECT_NE(result, nullptr); + GTEST_LOG_(INFO) << "GetWantSender_0003 end"; +} + /* * Feature: CancelWantSender_0003 * Function: CancelWantSender @@ -986,5 +1029,53 @@ HWTEST_F(AbilityManagerServiceElevenTest, VerificationToken_003, TestSize.Level1 EXPECT_FALSE(abilityMs->VerificationToken(token)); TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceElevenTest VerificationToken_003 end"); } + +/* + * Feature: AbilityManagerService + * Name: GetUidByCloneBundleInfo_001 + * Function: GetUidByCloneBundleInfo + * SubFunction: NA + * FunctionPoints: AbilityManagerService GetUidByCloneBundleInfo + */ +HWTEST_F(AbilityManagerServiceElevenTest, GetUidByCloneBundleInfo_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_001 start"; + auto abilityMs = std::make_shared(); + EXPECT_NE(abilityMs, nullptr); + abilityMs->subManagersHelper_ = std::make_shared(nullptr, nullptr); + EXPECT_NE(abilityMs->subManagersHelper_, nullptr); + + std::string bundleName = "com.ohos.settings"; + int mockRightUid = -1; + int userId = 100; + AppExecFwk::MockAppMgrService::retCode_ = mockRightUid; + auto result = abilityMs->GetUidByCloneBundleInfo(bundleName, mockRightUid, 0, userId); + EXPECT_NE(result, mockRightUid); + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_001 end"; +} + +/* + * Feature: AbilityManagerService + * Name: GetUidByCloneBundleInfo_002 + * Function: GetUidByCloneBundleInfo + * SubFunction: NA + * FunctionPoints: AbilityManagerService GetUidByCloneBundleInfo + */ +HWTEST_F(AbilityManagerServiceElevenTest, GetUidByCloneBundleInfo_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_002 start"; + auto abilityMs = std::make_shared(); + EXPECT_NE(abilityMs, nullptr); + abilityMs->subManagersHelper_ = std::make_shared(nullptr, nullptr); + EXPECT_NE(abilityMs->subManagersHelper_, nullptr); + + std::string bundleName = "com.applicaion.test"; + int mockErrorUid = -1; + int userId = 101; + AppExecFwk::MockAppMgrService::retCode_ = mockErrorUid; + auto result = abilityMs->GetUidByCloneBundleInfo(bundleName, mockErrorUid, 0, userId); + EXPECT_EQ(result, mockErrorUid); + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_002 end"; +} } // namespace AAFwk } // namespace OHOS