!9732 wantAgent支持startService

Merge pull request !9732 from XKK/wantAgentStartService
This commit is contained in:
openharmony_ci 2024-08-08 14:38:40 +00:00 committed by Gitee
commit f2e30b37a4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 85 additions and 2 deletions

View File

@ -1363,6 +1363,8 @@ napi_value WantAgentOperationTypeInit(napi_env env)
CreateJsValue(env, static_cast<int32_t>(NUMBER_OF_PARAMETERS_FOUR)));
napi_set_named_property(env, objValue, "START_FOREGROUND_SERVICE",
CreateJsValue(env, static_cast<int32_t>(NUMBER_OF_PARAMETERS_FIVE)));
napi_set_named_property(env, objValue, "START_SERVICE_EXTENSION",
CreateJsValue(env, static_cast<int32_t>(NUMBER_OF_PARAMETERS_SIX)));
return objValue;
}

View File

@ -164,6 +164,11 @@ public:
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags,
std::shared_ptr<PendingWant> &pendingWant);
static ErrCode GetServiceExtension(
const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags,
std::shared_ptr<PendingWant> &pendingWant);
/**
* Retrieve a PendingWant that will start a foreground service.
*

View File

@ -122,7 +122,9 @@ public:
/// <summary>
/// Starts a foreground ability without a UI.
/// </summary>
START_FOREGROUND_SERVICE
START_FOREGROUND_SERVICE,
START_SERVICE_EXTENSION
};
private:

View File

@ -182,6 +182,15 @@ ErrCode PendingWant::GetService(
WantAgentConstant::OperationType::START_SERVICE, pendingWant);
}
ErrCode PendingWant::GetServiceExtension(
const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags,
std::shared_ptr<PendingWant> &pendingWant)
{
return BuildServicePendingWant(context, requestCode, want, flags,
WantAgentConstant::OperationType::START_SERVICE_EXTENSION, pendingWant);
}
ErrCode PendingWant::GetForegroundService(
const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, int requestCode,
const std::shared_ptr<Want> &want, unsigned int flags,

View File

@ -127,6 +127,9 @@ ErrCode WantAgentHelper::GetWantAgent(
case WantAgentConstant::OperationType::SEND_COMMON_EVENT:
result = PendingWant::GetCommonEvent(context, requestCode, wants[0], flags, pendingWant);
break;
case WantAgentConstant::OperationType::START_SERVICE_EXTENSION:
result = PendingWant::GetServiceExtension(context, requestCode, wants[0], flags, pendingWant);
break;
default:
TAG_LOGE(AAFwkTag::WANTAGENT, "operation type is error");
result = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;

View File

@ -64,7 +64,12 @@ enum class OperationType {
/**
* Starts a foreground ability without a UI.
*/
START_FOREGROUND_SERVICE
START_FOREGROUND_SERVICE,
/**
* Starts a service extension.
*/
START_SERVICE_EXTENSION
};
enum class Flags {
@ -149,6 +154,7 @@ public:
void CancelWantSenderLocked(PendingWantRecord &record, bool cleanAbility);
int32_t PendingWantStartAbility(const Want &want, const sptr<StartOptions> &startOptions,
const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId);
int32_t PendingWantStartServiceExtension(Want &want, const sptr<IRemoteObject> &callerToken);
int32_t PendingWantStartAbilitys(const std::vector<WantsInfo> &wantsInfo, const sptr<StartOptions> &startOptions,
const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId);
int32_t DeviceIdDetermine(const Want &want, const sptr<StartOptions> &startOptions,

View File

@ -48,6 +48,12 @@ sptr<IWantSender> PendingWantManager::GetWantSender(int32_t callingUid, int32_t
}
}
if (wantSenderInfo.type == static_cast<int32_t>(OperationType::START_SERVICE_EXTENSION) && !isSystemApp &&
!AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
TAG_LOGE(AAFwkTag::WANTAGENT, "non-system app called");
return nullptr;
}
WantSenderInfo info = wantSenderInfo;
return GetWantSenderLocked(callingUid, uid, wantSenderInfo.userId, info, callerToken, appIndex);
}
@ -288,6 +294,19 @@ int32_t PendingWantManager::PendingWantStartAbility(const Want &want, const sptr
return result;
}
int32_t PendingWantManager::PendingWantStartServiceExtension(Want &want, const sptr<IRemoteObject> &callerToken)
{
TAG_LOGI(AAFwkTag::WANTAGENT, "called");
if (!PermissionVerification::GetInstance()->IsSystemAppCall()
&& !PermissionVerification::GetInstance()->IsSACall()) {
TAG_LOGE(AAFwkTag::WANTAGENT, "non-system app called");
return ERR_INVALID_VALUE;
}
//reset flags
want.SetFlags(0);
return DelayedSingleton<AbilityManagerService>::GetInstance()->StartExtensionAbility(want, callerToken);
}
int32_t PendingWantManager::PendingWantStartAbilitys(const std::vector<WantsInfo> &wantsInfo,
const sptr<StartOptions> &startOptions, const sptr<IRemoteObject> &callerToken, int32_t requestCode,
const int32_t callerUid, int32_t callerTokenId)

View File

@ -106,6 +106,9 @@ int32_t PendingWantRecord::SenderInner(SenderInfo &senderInfo)
case static_cast<int32_t>(OperationType::SEND_COMMON_EVENT):
res = pendingWantManager->PendingWantPublishCommonEvent(want, senderInfo, callerUid_, callerTokenId_);
break;
case static_cast<int32_t>(OperationType::START_SERVICE_EXTENSION):
res = pendingWantManager->PendingWantStartServiceExtension(want, callerToken_);
break;
default:
break;
}

View File

@ -1398,4 +1398,38 @@ HWTEST_F(PendingWantTest, PendingWant_7300, Function | MediumTest | Level1)
EXPECT_EQ(callBackCancelListenerConnt, 0);
callBackCancelListenerConnt = 0;
}
/*
* @tc.number : PendingWant_7400
* @tc.name : PendingWant GetServiceExtension
* @tc.desc : 1.Get pendingWant (want is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_7400, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want;
unsigned int flags = 1;
flags |= FLAG_ONE_SHOT;
std::shared_ptr<PendingWant> pendingWant = nullptr;
PendingWant::GetServiceExtension(GetAppContext(), requestCode, want, flags, pendingWant);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_7500
* @tc.name : PendingWant GetServiceExtension
* @tc.desc : 1.Get pendingWant (context is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_7500, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = nullptr;
PendingWant::GetServiceExtension(nullptr, requestCode, want, flags, pendingWant);
EXPECT_EQ(pendingWant, nullptr);
}
} // namespace OHOS::AbilityRuntime::WantAgent