首次SetMissionContinueState时走同步处理

Signed-off-by: m30043719 <maxiaodong25@huawei.com>
This commit is contained in:
m30043719 2024-07-30 15:57:49 +08:00
parent 23e779750b
commit 66cbd2459e
3 changed files with 66 additions and 11 deletions

View File

@ -70,6 +70,7 @@ static std::map<ConnectionKey, sptr<JSAbilityConnection>, KeyCompare> g_connects
std::recursive_mutex gConnectsLock_;
int64_t g_serialNumber = 0;
const std::string ATOMIC_SERVICE_PREFIX = "com.atomicservice.";
std::atomic<bool> g_hasSetContinueState = false;
// This function has to be called from engine thread
void RemoveConnection(int64_t connectId)
@ -2151,29 +2152,62 @@ napi_value JsAbilityContext::OnSetMissionContinueState(napi_env env, NapiCallbac
return CreateJsUndefined(env);
}
ErrCode errcode = ERR_OK;
auto context = context_.lock();
if (context) {
errcode = context->SetMissionContinueState(state);
} else {
TAG_LOGW(AAFwkTag::CONTEXT, "context is released");
errcode = static_cast<int>(AbilityErrorCode::ERROR_CODE_INVALID_CONTEXT);
if (!g_hasSetContinueState) {
g_hasSetContinueState = true;
return SyncSetMissionContinueState(env, info, state);
}
NapiAsyncTask::CompleteCallback complete = [errcode](napi_env env, NapiAsyncTask& task, int32_t status) {
if (errcode == 0) {
auto innerErrorCode = std::make_shared<int32_t>(ERR_OK);
NapiAsyncTask::ExecuteCallback execute = [weak = context_, state, innerErrorCode]() {
auto context = weak.lock();
if (!context) {
TAG_LOGW(AAFwkTag::CONTEXT, "context is released");
*innerErrorCode = static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_CONTEXT);
return;
}
*innerErrorCode = context->SetMissionContinueState(state);
};
NapiAsyncTask::CompleteCallback complete = [innerErrorCode](napi_env env, NapiAsyncTask& task, int32_t status) {
if (*innerErrorCode == ERR_OK) {
task.Resolve(env, CreateJsUndefined(env));
} else {
task.Reject(env, CreateJsErrorByNativeErr(env, errcode));
task.Reject(env, CreateJsErrorByNativeErr(env, *innerErrorCode));
}
};
napi_value lastParam = (info.argc > ARGC_ONE) ? info.argv[INDEX_ONE] : nullptr;
napi_value result = nullptr;
NapiAsyncTask::ScheduleHighQos("JsAbilityContext::OnSetMissionContinueState",
env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
TAG_LOGI(AAFwkTag::CONTEXT, "end");
return result;
}
napi_value JsAbilityContext::SyncSetMissionContinueState(napi_env env, NapiCallbackInfo& info,
const AAFwk::ContinueState& state)
{
TAG_LOGI(AAFwkTag::CONTEXT, "called");
auto innerErrorCode = std::make_shared<int32_t>(ERR_OK);
auto context = context_.lock();
if (context) {
*innerErrorCode = context->SetMissionContinueState(state);
} else {
TAG_LOGW(AAFwkTag::CONTEXT, "context is released");
*innerErrorCode = static_cast<int>(AbilityErrorCode::ERROR_CODE_INVALID_CONTEXT);
}
NapiAsyncTask::CompleteCallback complete = [innerErrorCode](napi_env env, NapiAsyncTask& task, int32_t status) {
if (*innerErrorCode == ERR_OK) {
task.Resolve(env, CreateJsUndefined(env));
} else {
task.Reject(env, CreateJsErrorByNativeErr(env, *innerErrorCode));
}
};
napi_value lastParam = (info.argc > ARGC_ONE) ? info.argv[INDEX_ONE] : nullptr;
napi_value result = nullptr;
NapiAsyncTask::ScheduleHighQos("JsAbilityContext::OnSetMissionContinueState",
env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
TAG_LOGI(AAFwkTag::CONTEXT, "end");
return result;
}
#ifdef SUPPORT_SCREEN
napi_value JsAbilityContext::SetMissionLabel(napi_env env, napi_callback_info info)
{

View File

@ -149,6 +149,7 @@ private:
bool isAbilityResult = false, bool isOpenLink = false);
bool CheckStartAbilityByCallParams(napi_env env, NapiCallbackInfo& info, AAFwk::Want &want,
int32_t &userId, napi_value &lastParam);
napi_value SyncSetMissionContinueState(napi_env env, NapiCallbackInfo& info, const AAFwk::ContinueState& state);
std::weak_ptr<AbilityContext> context_;
int curRequestCode_ = 0;

View File

@ -1597,6 +1597,26 @@ HWTEST_F(AbilityManagerProxyTest, AbilityManagerProxy_SetMissionContinueState_00
EXPECT_EQ(res, NO_ERROR);
}
/*
* Feature: AbilityManagerService
* Function: SetMissionContinueState
* SubFunction: NA
* FunctionPoints: AbilityManagerService SetMissionContinueState
* EnvConditions: NA
* CaseDescription: Verify the normal process of SetMissionContinueState
*/
HWTEST_F(AbilityManagerProxyTest, AbilityManagerProxy_SetMissionContinueState_002, TestSize.Level1)
{
EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
.Times(1)
.WillOnce(Invoke(mock_.GetRefPtr(), &AbilityManagerStubMock::InvokeSendRequest));
OHOS::sptr<IRemoteObject> token = sptr<IRemoteObject>(new (std::nothrow) MockAbilityToken());
AAFwk::ContinueState state = AAFwk::ContinueState::CONTINUESTATE_INACTIVE;
auto res = proxy_->SetMissionContinueState(token, state);
EXPECT_EQ(static_cast<uint32_t>(AbilityManagerInterfaceCode::SET_MISSION_CONTINUE_STATE), mock_->code_);
EXPECT_EQ(res, NO_ERROR);
}
/*
* Feature: AbilityManagerService
* Function: SetMissionLabel