!10523 稳定性:window实例创建子窗task 修改

Merge pull request !10523 from 万孝国/stability
This commit is contained in:
openharmony_ci 2024-11-06 09:21:42 +00:00 committed by Gitee
commit bb140a0def
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 62 additions and 48 deletions

View File

@ -7000,44 +7000,6 @@ napi_value JsWindow::OnGetGestureBackEnabled(napi_env env, napi_callback_info in
return CreateJsValue(env, enable);
}
static void CreateNewSubWindowTask(const sptr<Window>& windowToken, const std::string& windowName,
sptr<WindowOption>& windowOption, napi_env env, NapiAsyncTask& task)
{
if (windowToken == nullptr) {
TLOGE(WmsLogTag::WMS_SUB, "window is null");
task.Reject(env, CreateJsError(env,
static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "window is null"));
return;
}
if (windowOption == nullptr) {
TLOGE(WmsLogTag::WMS_SUB, "windowOption is null");
task.Reject(env, CreateJsError(env,
static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "windowOption is null"));
return;
}
if (!WindowHelper::IsSubWindow(windowToken->GetType()) &&
!WindowHelper::IsMainWindow(windowToken->GetType())) {
TLOGE(WmsLogTag::WMS_SUB, "This is not subWindow or mainWindow.");
task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_INVALID_CALLING,
"This is not subWindow or mainWindow"));
return;
}
windowOption->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
windowOption->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
windowOption->SetOnlySupportSceneBoard(true);
windowOption->SetParentId(windowToken->GetWindowId());
windowOption->SetWindowTag(WindowTag::SUB_WINDOW);
auto window = Window::Create(windowName, windowOption, windowToken->GetContext());
if (window == nullptr) {
TLOGE(WmsLogTag::WMS_SUB, "create sub window failed");
task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
"create sub window failed"));
return;
}
task.Resolve(env, CreateJsWindowObject(env, window));
TLOGI(WmsLogTag::WMS_SUB, "create sub window %{public}s end", windowName.c_str());
}
napi_value JsWindow::OnCreateSubWindowWithOptions(napi_env env, napi_callback_info info)
{
if (windowToken_ == nullptr) {
@ -7049,8 +7011,8 @@ napi_value JsWindow::OnCreateSubWindowWithOptions(napi_env env, napi_callback_in
TLOGE(WmsLogTag::WMS_SUB, "device not support");
return NapiGetUndefined(env);
}
size_t argc = 4;
napi_value argv[4] = {nullptr};
size_t argc = FOUR_PARAMS_SIZE;
napi_value argv[FOUR_PARAMS_SIZE] = {nullptr};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
if (argc < 2) { // 2: minimum params num
TLOGE(WmsLogTag::WMS_SUB, "Argc is invalid: %{public}zu", argc);
@ -7079,16 +7041,46 @@ napi_value JsWindow::OnCreateSubWindowWithOptions(napi_env env, napi_callback_in
napi_throw(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP));
return NapiGetUndefined(env);
}
NapiAsyncTask::CompleteCallback complete =
[windowToken = windowToken_, windowName = std::move(windowName), windowOption](napi_env env,
NapiAsyncTask& task, int32_t status) mutable {
CreateNewSubWindowTask(windowToken, windowName, windowOption, env, task);
};
napi_value callback = (argc > 2 && argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ?
argv[2] : nullptr;
napi_value result = nullptr;
NapiAsyncTask::Schedule("JsWindow::OnCreateSubWindowWithOptions",
env, CreateAsyncTaskWithLastParam(env, callback, nullptr, std::move(complete), &result));
std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, callback, &result);
const char* const where = __func__;
auto asyncTask = [windowToken = wptr<Window>(windowToken_), windowName = std::move(windowName),
windowOption, env, task = napiAsyncTask, where]() mutable {
auto window = windowToken.promote();
if (window == nullptr) {
TLOGNE(WmsLogTag::WMS_SUB, "%{public}s window is nullptr", where);
task->Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
"window is nullptr"));
return;
}
if (!WindowHelper::IsSubWindow(window->GetType()) &&
!WindowHelper::IsMainWindow(window->GetType())) {
TLOGNE(WmsLogTag::WMS_SUB, "%{public}s this is not subWindow or mainWindow.", where);
task->Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_INVALID_CALLING,
"This is not subWindow or mainWindow"));
return;
}
windowOption->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
windowOption->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
windowOption->SetOnlySupportSceneBoard(true);
windowOption->SetParentId(windowToken->GetWindowId());
windowOption->SetWindowTag(WindowTag::SUB_WINDOW);
auto subWindow = Window::Create(windowName, windowOption, window->GetContext());
if (subWindow == nullptr) {
TLOGNE(WmsLogTag::WMS_SUB, "%{public}s create sub window failed.", where);
task->Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
"create sub window failed"));
return;
}
task->Resolve(env, CreateJsWindowObject(env, subWindow));
TLOGNI(WmsLogTag::WMS_SUB, "%{public}s create sub window %{public}s end", where, windowName.c_str());
};
if (napi_status::napi_ok != napi_send_event(env, asyncTask, napi_eprio_vip)) {
napiAsyncTask->Reject(env, CreateJsError(env,
static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "send event failed"));
}
return result;
}

View File

@ -1077,6 +1077,27 @@ std::unique_ptr<NapiAsyncTask> CreateAsyncTask(napi_env env, napi_value lastPara
}
}
std::unique_ptr<NapiAsyncTask> CreateEmptyAsyncTask(napi_env env,
napi_value lastParam, napi_value* result)
{
napi_valuetype type = napi_undefined;
napi_typeof(env, lastParam, &type);
if (lastParam == nullptr || type != napi_function) {
napi_deferred nativeDeferred = nullptr;
napi_create_promise(env, &nativeDeferred, result);
return std::make_unique<NapiAsyncTask>(nativeDeferred,
std::unique_ptr<NapiAsyncTask::ExecuteCallback>(),
std::unique_ptr<NapiAsyncTask::CompleteCallback>());
} else {
napi_get_undefined(env, result);
napi_ref callbackRef = nullptr;
napi_create_reference(env, lastParam, 1, &callbackRef);
return std::make_unique<NapiAsyncTask>(callbackRef,
std::unique_ptr<NapiAsyncTask::ExecuteCallback>(),
std::unique_ptr<NapiAsyncTask::CompleteCallback>());
}
}
napi_value ModalityTypeInit(napi_env env)
{
CHECK_NAPI_ENV_RETURN_IF_NULL(env);
@ -1149,6 +1170,5 @@ bool ParseSubWindowOptions(napi_env env, napi_value jsObject, const sptr<WindowO
windowOption->SetSubWindowDecorEnable(decorEnabled);
return ParseModalityParam(env, jsObject, windowOption);
}
} // namespace Rosen
} // namespace OHOS

View File

@ -343,6 +343,8 @@ inline const std::map<ApiModalityType, ModalityType> JS_TO_NATIVE_MODALITY_TYPE_
std::unique_ptr<AbilityRuntime::NapiAsyncTask> CreateAsyncTask(napi_env env, napi_value lastParam,
std::unique_ptr<AbilityRuntime::NapiAsyncTask::ExecuteCallback>&& execute,
std::unique_ptr<AbilityRuntime::NapiAsyncTask::CompleteCallback>&& complete, napi_value* result);
std::unique_ptr<AbilityRuntime::NapiAsyncTask> CreateEmptyAsyncTask(
napi_env env, napi_value lastParam, napi_value* result);
bool ParseSubWindowOptions(napi_env env, napi_value JsObject, const sptr<WindowOption>& WindowOption);
template<class T>
bool ParseJsValue(napi_value jsObject, napi_env env, const std::string& name, T& data)