update interfaces/kits/picker/src/picker_n_exporter.cpp.

Signed-off-by: BrainL <libuyan1@huawei.com>
This commit is contained in:
BrainL 2024-09-09 04:41:05 +00:00 committed by Gitee
parent b863e5dc51
commit 27accb949d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -47,12 +47,12 @@ string PickerNExporter::GetClassName()
static void StartModalPickerExecute(napi_env env, void *data) static void StartModalPickerExecute(napi_env env, void *data)
{ {
HILOG_INFO("modal picker: StartModalPickerExecute begin"); HILOG_INFO("[picker]: StartModalPickerExecute begin");
auto *context = static_cast<PickerAsyncContext*>(data); auto *context = static_cast<PickerAsyncContext*>(data);
while (!context->pickerCallBack->ready) { while (!context->pickerCallBack->ready) {
std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_MS)); std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_MS));
} }
HILOG_INFO("modal picker: StartModalPickerExecute is ready."); HILOG_INFO("[picker]: StartModalPickerExecute is ready.");
} }
static void MakeResultWithArr(napi_env env, std::string key, napi_value &result, static void MakeResultWithArr(napi_env env, std::string key, napi_value &result,
@ -63,7 +63,7 @@ static void MakeResultWithArr(napi_env env, std::string key, napi_value &result,
napi_status status = napi_generic_failure; napi_status status = napi_generic_failure;
if (pickerCallBack->want.GetParams().HasParam(key.c_str())) { if (pickerCallBack->want.GetParams().HasParam(key.c_str())) {
std::vector<std::string> list = pickerCallBack->want.GetStringArrayParam(key.c_str()); std::vector<std::string> list = pickerCallBack->want.GetStringArrayParam(key.c_str());
HILOG_INFO("modal picker: %{public}s size. %{public}zu ", key.c_str(), list.size()); HILOG_INFO("[picker]: %{public}s size. %{public}zu ", key.c_str(), list.size());
for (size_t i = 0; i < list.size(); i++) { for (size_t i = 0; i < list.size(); i++) {
napi_value uri = nullptr; napi_value uri = nullptr;
napi_create_string_utf8(env, list[i].c_str(), NAPI_AUTO_LENGTH, &uri); napi_create_string_utf8(env, list[i].c_str(), NAPI_AUTO_LENGTH, &uri);
@ -74,7 +74,7 @@ static void MakeResultWithArr(napi_env env, std::string key, napi_value &result,
} }
status = napi_set_named_property(env, result, key.c_str(), array); status = napi_set_named_property(env, result, key.c_str(), array);
if (status != napi_ok) { if (status != napi_ok) {
HILOG_ERROR("modal picker: napi_set_named_property uri failed"); HILOG_ERROR("[picker]: napi_set_named_property uri failed");
} }
} }
} }
@ -85,12 +85,12 @@ static void MakeResultWithInt(napi_env env, std::string key, napi_value &result,
napi_status status = napi_generic_failure; napi_status status = napi_generic_failure;
if (pickerCallBack->want.GetParams().HasParam(key.c_str())) { if (pickerCallBack->want.GetParams().HasParam(key.c_str())) {
const int32_t suffixindex = pickerCallBack->want.GetIntParam(key.c_str(), -1); const int32_t suffixindex = pickerCallBack->want.GetIntParam(key.c_str(), -1);
HILOG_INFO("Modal picker: %{public}s is %{public}d ", key.c_str(), suffixindex); HILOG_INFO("[picker]: %{public}s is %{public}d ", key.c_str(), suffixindex);
napi_value suffix = nullptr; napi_value suffix = nullptr;
napi_create_int32(env, suffixindex, &suffix); napi_create_int32(env, suffixindex, &suffix);
status = napi_set_named_property(env, result, key.c_str(), suffix); status = napi_set_named_property(env, result, key.c_str(), suffix);
if (status != napi_ok) { if (status != napi_ok) {
HILOG_ERROR("Modal picker: napi_set_named_property suffix failed"); HILOG_ERROR("[picker]: napi_set_named_property suffix failed");
} }
} }
} }
@ -106,12 +106,12 @@ static napi_value MakeResultWithPickerCallBack(napi_env env, std::shared_ptr<Pic
napi_create_object(env, &result); napi_create_object(env, &result);
const int32_t resCode = pickerCallBack->resultCode; const int32_t resCode = pickerCallBack->resultCode;
HILOG_INFO("modal picker: resCode is %{public}d.", resCode); HILOG_INFO("[picker]: resCode is %{public}d.", resCode);
napi_value resultCode = nullptr; napi_value resultCode = nullptr;
napi_create_int32(env, resCode, &resultCode); napi_create_int32(env, resCode, &resultCode);
status = napi_set_named_property(env, result, "resultCode", resultCode); status = napi_set_named_property(env, result, "resultCode", resultCode);
if (status != napi_ok) { if (status != napi_ok) {
HILOG_ERROR("modal picker: napi_set_named_property resultCode failed"); HILOG_ERROR("[picker]: napi_set_named_property resultCode failed");
} }
MakeResultWithArr(env, "ability.params.stream", result, pickerCallBack); MakeResultWithArr(env, "ability.params.stream", result, pickerCallBack);
MakeResultWithArr(env, "uriArr", result, pickerCallBack); MakeResultWithArr(env, "uriArr", result, pickerCallBack);
@ -121,7 +121,7 @@ static napi_value MakeResultWithPickerCallBack(napi_env env, std::shared_ptr<Pic
static void StartModalPickerAsyncCallbackComplete(napi_env env, napi_status status, void *data) static void StartModalPickerAsyncCallbackComplete(napi_env env, napi_status status, void *data)
{ {
HILOG_INFO("modal picker: StartModalPickerAsyncCallbackComplete begin."); HILOG_INFO("[picker]: StartModalPickerAsyncCallbackComplete begin.");
auto *context = static_cast<PickerAsyncContext*>(data); auto *context = static_cast<PickerAsyncContext*>(data);
if (context == nullptr) { if (context == nullptr) {
HILOG_ERROR("Async context is null"); HILOG_ERROR("Async context is null");
@ -131,11 +131,11 @@ static void StartModalPickerAsyncCallbackComplete(napi_env env, napi_status stat
jsContext->status = false; jsContext->status = false;
status = napi_get_undefined(env, &jsContext->data); status = napi_get_undefined(env, &jsContext->data);
if (status != napi_ok) { if (status != napi_ok) {
HILOG_ERROR("modal picker: napi_get_undefined jsContext->data failed"); HILOG_ERROR("[picker]: napi_get_undefined jsContext->data failed");
} }
status = napi_get_undefined(env, &jsContext->error); status = napi_get_undefined(env, &jsContext->error);
if (status != napi_ok) { if (status != napi_ok) {
HILOG_ERROR("modal picker: napi_get_undefined jsContext->error failed"); HILOG_ERROR("[picker]: napi_get_undefined jsContext->error failed");
} }
napi_value result = MakeResultWithPickerCallBack(env, context->pickerCallBack); napi_value result = MakeResultWithPickerCallBack(env, context->pickerCallBack);
if (result != nullptr) { if (result != nullptr) {
@ -152,25 +152,101 @@ static void StartModalPickerAsyncCallbackComplete(napi_env env, napi_status stat
delete context; delete context;
} }
static bool IsTypeRight(napi_env env, napi_value val, napi_value_type type)
{
napi_valuetype valueType;
napi_status status = napi_typeof(env, val, &valueType);
if (status != napi_ok || valueType != type) {
HILOG_ERROR("[picker] Type is not right, type: %{public}d", valueType);
return false;
}
return true;
}
static ErrCode GetWindowName(napi_env env, napi_value properties, sptr<Rosen::Window> &window)
{
HILOG_INFO("[picker] Begin GetWindowName.");
napi_value name;
napi_status status;
status = napi_get_named_property(env, properties, "name", &name);
if (status != napi_ok) {
HILOG_ERROR("Get name from properties fail.");
return ERR_INV;
}
size_t nameLen;
status = napi_get_value_string_utf8(env, name, NULL, 0, &nameLen);
if (status != napi_ok) {
HILOG_ERROR("[picker] Get window name length fail.")
return ERR_INV;
}
char *nameBuf = new char[nameLen + 1];
status = napi_get_value_string_utf8(env, name, nameBuf, nameLen + 1, &nameLen);
if (status != napi_ok) {
HILOG_ERROR("[picker] Get value string UTF8 fail.");
return ERR_INV;
}
HILOG_INFO("[picker] Get window name: %{public}s", nameBuf);
auto customWindow = Rosen::Window::Find(nameBuf);
if (!customWindow) {
HILOG_ERROR("[picker] Window find fail.");
return ERR_INV;
}
window = customWindow;
HILOG_INFO("[picker] Window found: %{public}s", nameBuf);
delete[] nameBuf;
return ERR_OK;
}
template <class AsyncContext>
static ErrCode GetCustomShowingWindow(napi_env, AsyncContext &asyncContext,
const napi_callback_info info, sptr<Rosen::Window> &window)
{
HILOG_INFO("[picker] GetCustomShowingWindow enter.");
napi_valuetype valueType;
napi_status status;
if (!IsTypeRight(env, asyncContext->argv[ARGS_TWO], napi_object)) {
HILOG_ERROR("[picker] The type of the parameter transferred to the window is not object.");
return ERR_INV;
}
auto windowObj = asyncContext->argv[ARGS_TWO];
napi_value getPropertiesFunc;
status = napi_get_name_property(env, windowObj, "getWindowProperties", &getPropertiesFunc);
if (status != napi_ok || !getPropertiesFunc) {
HILOG_ERROR("[picker] getWindowProperties fail.");
return ERR_INV;
}
if (!IsTypeRight(env, getPropertiesFunc, napi_function)) {
HILOG_ERROR("[picker] The type of the parameter transferred to the getPropertiesFunc is not function.");
return ERR_INV;
}
napi_value properties;
status = napi_call_function(env, windowObj, getPropertiesFunc, 0, nullptr, &properties);
if (status != napi_ok || !properties) {
HILOG_INFO("[picker] Call getPropertiesFunc fail.");
return ERR_INV;
}
return GetWindowName(env, properties, window);
}
Ace::UIContent *GetUIContent(napi_env env, napi_callback_info info, Ace::UIContent *GetUIContent(napi_env env, napi_callback_info info,
unique_ptr<PickerAsyncContext> &AsyncContext) unique_ptr<PickerAsyncContext> &AsyncContext)
{ {
bool isStageMode = false; bool isStageMode = false;
napi_status status = AbilityRuntime::IsStageContext(env, AsyncContext->argv[ARGS_ZERO], isStageMode); napi_status status = AbilityRuntime::IsStageContext(env, AsyncContext->argv[ARGS_ZERO], isStageMode);
if (status != napi_ok || !isStageMode) { if (status != napi_ok || !isStageMode) {
HILOG_ERROR("modal picker: is not StageMode context"); HILOG_ERROR("[picker]: is not StageMode context");
return nullptr; return nullptr;
} }
auto context = AbilityRuntime::GetStageModeContext(env, AsyncContext->argv[ARGS_ZERO]); auto context = AbilityRuntime::GetStageModeContext(env, AsyncContext->argv[ARGS_ZERO]);
if (context == nullptr) { if (context == nullptr) {
HILOG_ERROR("modal picker: Failed to get native stage context instance"); HILOG_ERROR("[picker]: Failed to get native stage context instance");
return nullptr; return nullptr;
} }
auto abilityContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::AbilityContext>(context); auto abilityContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::AbilityContext>(context);
if (abilityContext == nullptr) { if (abilityContext == nullptr) {
auto uiExtensionContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::UIExtensionContext>(context); auto uiExtensionContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::UIExtensionContext>(context);
if (uiExtensionContext == nullptr) { if (uiExtensionContext == nullptr) {
HILOG_ERROR("modal picker: Fail to convert to abilityContext or uiExtensionContext"); HILOG_ERROR("[picker]: Fail to convert to abilityContext or uiExtensionContext");
return nullptr; return nullptr;
} }
return uiExtensionContext->GetUIContent(); return uiExtensionContext->GetUIContent();
@ -179,16 +255,23 @@ Ace::UIContent *GetUIContent(napi_env env, napi_callback_info info,
} }
static napi_value StartPickerExtension(napi_env env, napi_callback_info info, static napi_value StartPickerExtension(napi_env env, napi_callback_info info,
unique_ptr<PickerAsyncContext> &AsyncContext) unique_ptr<PickerAsyncContext> &asyncContext)
{ {
HILOG_INFO("modal picker: StartPickerExtension begin."); HILOG_INFO("[picker]: StartPickerExtension begin.");
Ace::UIContent *uiContent = GetUIContent(env, info, AsyncContext); Ace::UIContent *uiContent;
if (asyncContext->argc == 3 && window_) {
HILOG_INFO("[picker] Will get uiContent by window.");
uiContent = window_->GetUIContent();
} else {
uiContent= GetUIContent(env, info, asyncContext);
}
if (uiContent == nullptr) { if (uiContent == nullptr) {
HILOG_ERROR("modal picker: get uiContent failed"); HILOG_ERROR("[picker]: get uiContent failed");
return nullptr; return nullptr;
} }
AAFwk::Want request; AAFwk::Want request;
AppExecFwk::UnwrapWant(env, AsyncContext->argv[ARGS_ONE], request); AppExecFwk::UnwrapWant(env, asyncContext->argv[ARGS_ONE], request);
std::string targetType = request.GetStringParam("extType"); std::string targetType = request.GetStringParam("extType");
std::string pickerType; std::string pickerType;
@ -196,8 +279,8 @@ static napi_value StartPickerExtension(napi_env env, napi_callback_info info,
pickerType = request.GetStringParam("pickerType"); pickerType = request.GetStringParam("pickerType");
} }
request.SetParam(ABILITY_WANT_PARAMS_UIEXTENSIONTARGETTYPE, targetType); request.SetParam(ABILITY_WANT_PARAMS_UIEXTENSIONTARGETTYPE, targetType);
AsyncContext->pickerCallBack = make_shared<PickerCallBack>(); asyncContext->pickerCallBack = make_shared<PickerCallBack>();
auto callback = std::make_shared<ModalUICallback>(uiContent, AsyncContext->pickerCallBack); auto callback = std::make_shared<ModalUICallback>(uiContent, asyncContext->pickerCallBack);
Ace::ModalUIExtensionCallbacks extensionCallback = { Ace::ModalUIExtensionCallbacks extensionCallback = {
.onRelease = std::bind(&ModalUICallback::OnRelease, callback, std::placeholders::_1), .onRelease = std::bind(&ModalUICallback::OnRelease, callback, std::placeholders::_1),
.onResult = std::bind(&ModalUICallback::OnResultForModal, callback, std::placeholders::_1, .onResult = std::bind(&ModalUICallback::OnResultForModal, callback, std::placeholders::_1,
@ -208,11 +291,11 @@ static napi_value StartPickerExtension(napi_env env, napi_callback_info info,
.onDestroy = std::bind(&ModalUICallback::OnDestroy, callback), .onDestroy = std::bind(&ModalUICallback::OnDestroy, callback),
}; };
Ace::ModalUIExtensionConfig config; Ace::ModalUIExtensionConfig config;
HILOG_INFO("modal picker: will CreateModalUIExtension by extType: %{public}s, pickerType: %{public}s", HILOG_INFO("[picker]: will CreateModalUIExtension by extType: %{public}s, pickerType: %{public}s",
targetType.c_str(), pickerType.c_str()); targetType.c_str(), pickerType.c_str());
int sessionId = uiContent->CreateModalUIExtension(request, extensionCallback, config); int sessionId = uiContent->CreateModalUIExtension(request, extensionCallback, config);
if (sessionId == 0) { if (sessionId == 0) {
HILOG_ERROR("modal picker: create modalUIExtension failed"); HILOG_ERROR("[picker]: create modalUIExtension failed");
return nullptr; return nullptr;
} }
callback->SetSessionId(sessionId); callback->SetSessionId(sessionId);
@ -225,22 +308,29 @@ template <class AsyncContext>
static napi_status AsyncContextSetStaticObjectInfo(napi_env env, napi_callback_info info, static napi_status AsyncContextSetStaticObjectInfo(napi_env env, napi_callback_info info,
AsyncContext &asyncContext, const size_t minArgs, const size_t maxArgs) AsyncContext &asyncContext, const size_t minArgs, const size_t maxArgs)
{ {
HILOG_INFO("modal picker: AsyncContextSetStaticObjectInfo begin."); HILOG_INFO("[picker]: AsyncContextSetStaticObjectInfo begin.");
napi_value thisVar = nullptr; napi_value thisVar = nullptr;
asyncContext->argc = maxArgs; asyncContext->argc = maxArgs;
napi_status ret = napi_get_cb_info(env, info, &asyncContext->argc, &(asyncContext->argv[ARGS_ZERO]), napi_status ret = napi_get_cb_info(env, info, &asyncContext->argc, &(asyncContext->argv[ARGS_ZERO]),
&thisVar, nullptr); &thisVar, nullptr);
if (ret != napi_ok) { if (ret != napi_ok) {
HILOG_ERROR("modal picker: Failed to get cb info"); HILOG_ERROR("[picker]: Failed to get cb info");
return ret; return ret;
} }
if (asyncContext->argc == ARGS_THREE) {
int res = GetCustomShowingWindow(env, asyncContext, info, window_);
if (res != ERR_OK) {
HILOG_ERROR("[picker] Failed to get cb window_ info.");
return napi_invalid_arg;
}
}
if (!((asyncContext->argc >= minArgs) && (asyncContext->argc <= maxArgs))) { if (!((asyncContext->argc >= minArgs) && (asyncContext->argc <= maxArgs))) {
HILOG_ERROR("modal picker: Number of args is invalid"); HILOG_ERROR("[picker]: Number of args is invalid");
return napi_invalid_arg; return napi_invalid_arg;
} }
if (minArgs > 0) { if (minArgs > 0) {
if (asyncContext->argv[ARGS_ZERO] == nullptr) { if (asyncContext->argv[ARGS_ZERO] == nullptr) {
HILOG_ERROR("modal picker: Argument list is empty"); HILOG_ERROR("[picker]: Argument list is empty");
return napi_invalid_arg; return napi_invalid_arg;
} }
} }
@ -250,12 +340,13 @@ static napi_status AsyncContextSetStaticObjectInfo(napi_env env, napi_callback_i
static napi_value ParseArgsStartModalPicker(napi_env env, napi_callback_info info, static napi_value ParseArgsStartModalPicker(napi_env env, napi_callback_info info,
unique_ptr<PickerAsyncContext> &context) unique_ptr<PickerAsyncContext> &context)
{ {
HILOG_INFO("modal picker: ParseArgsStartModalPicker begin."); HILOG_INFO("[picker]: ParseArgsStartModalPicker begin.");
constexpr size_t minArgs = ARGS_TWO; constexpr size_t minArgs = ARGS_TWO;
constexpr size_t maxArgs = ARGS_THREE; constexpr size_t maxArgs = ARGS_THREE;
napi_status status = AsyncContextSetStaticObjectInfo(env, info, context, minArgs, maxArgs); napi_status status = AsyncContextSetStaticObjectInfo(env, info, context, minArgs, maxArgs);
if (status != napi_ok) { if (status != napi_ok) {
HILOG_ERROR("modal picker: AsyncContextSetStaticObjectInfo faild"); HILOG_ERROR("[picker]: AsyncContextSetStaticObjectInfo faild");
return nullptr;
} }
napi_value result = nullptr; napi_value result = nullptr;
napi_value ret = StartPickerExtension(env, info, context); napi_value ret = StartPickerExtension(env, info, context);
@ -268,7 +359,7 @@ static napi_value ParseArgsStartModalPicker(napi_env env, napi_callback_info inf
napi_value PickerNExporter::StartModalPicker(napi_env env, napi_callback_info info) napi_value PickerNExporter::StartModalPicker(napi_env env, napi_callback_info info)
{ {
HILOG_INFO("modal picker: StartModalPicker begin."); HILOG_INFO("[picker]: StartModalPicker begin.");
unique_ptr<PickerAsyncContext> asyncContext = make_unique<PickerAsyncContext>(); unique_ptr<PickerAsyncContext> asyncContext = make_unique<PickerAsyncContext>();
napi_value ret = ParseArgsStartModalPicker(env, info, asyncContext); napi_value ret = ParseArgsStartModalPicker(env, info, asyncContext);
if (ret == nullptr) { if (ret == nullptr) {