123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "interfaces/napi/kits/utils/napi_utils.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "bridge/common/utils/engine_helper.h"
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_cinamespace OHOS::Ace::Napi {
2123b3eb3cSopenharmony_cistatic NG::FrameNode* ParseFrameNode(napi_env env, napi_callback_info info)
2223b3eb3cSopenharmony_ci{
2323b3eb3cSopenharmony_ci    size_t argc = 1;
2423b3eb3cSopenharmony_ci    napi_value argv[2] = { nullptr };
2523b3eb3cSopenharmony_ci    napi_value thisVar = nullptr;
2623b3eb3cSopenharmony_ci    void* data = nullptr;
2723b3eb3cSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
2823b3eb3cSopenharmony_ci    NAPI_ASSERT(env, argc >= 1, "wrong number of argument\n");
2923b3eb3cSopenharmony_ci
3023b3eb3cSopenharmony_ci    napi_value nodePtr = nullptr;
3123b3eb3cSopenharmony_ci    NAPI_CALL(env, napi_get_named_property(env, argv[0], "nodePtr_", &nodePtr));
3223b3eb3cSopenharmony_ci    napi_valuetype valueType = napi_undefined;
3323b3eb3cSopenharmony_ci    napi_typeof(env, nodePtr, &valueType);
3423b3eb3cSopenharmony_ci    NG::UINode* uiNode = nullptr;
3523b3eb3cSopenharmony_ci    if (valueType == napi_external) {
3623b3eb3cSopenharmony_ci        NAPI_CALL(env, napi_get_value_external(env, nodePtr, (void**)&uiNode));
3723b3eb3cSopenharmony_ci    }
3823b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(uiNode, nullptr);
3923b3eb3cSopenharmony_ci    return reinterpret_cast<NG::FrameNode*>(uiNode);
4023b3eb3cSopenharmony_ci}
4123b3eb3cSopenharmony_ci
4223b3eb3cSopenharmony_cistatic napi_value JSAddFrameNode(napi_env env, napi_callback_info info)
4323b3eb3cSopenharmony_ci{
4423b3eb3cSopenharmony_ci    NG::FrameNode* frameNode = ParseFrameNode(env, info);
4523b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(frameNode, nullptr);
4623b3eb3cSopenharmony_ci    size_t argc = 2;
4723b3eb3cSopenharmony_ci    napi_value argv[2] = { nullptr };
4823b3eb3cSopenharmony_ci    napi_value thisVar = nullptr;
4923b3eb3cSopenharmony_ci    void* data = nullptr;
5023b3eb3cSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
5123b3eb3cSopenharmony_ci    auto delegate = EngineHelper::GetCurrentDelegateSafely();
5223b3eb3cSopenharmony_ci    if (!delegate) {
5323b3eb3cSopenharmony_ci        NapiThrow(env, "UI execution context not found.", ERROR_CODE_INTERNAL_ERROR);
5423b3eb3cSopenharmony_ci        return nullptr;
5523b3eb3cSopenharmony_ci    }
5623b3eb3cSopenharmony_ci    if (argc == 1) {
5723b3eb3cSopenharmony_ci        delegate->AddFrameNodeToOverlay(AceType::Claim(frameNode));
5823b3eb3cSopenharmony_ci    } else {
5923b3eb3cSopenharmony_ci        napi_valuetype valueType = napi_undefined;
6023b3eb3cSopenharmony_ci        napi_typeof(env, argv[1], &valueType);
6123b3eb3cSopenharmony_ci        if (valueType == napi_number) {
6223b3eb3cSopenharmony_ci            int32_t index = 0;
6323b3eb3cSopenharmony_ci            napi_get_value_int32(env, argv[1], &index);
6423b3eb3cSopenharmony_ci            delegate->AddFrameNodeToOverlay(AceType::Claim(frameNode), index);
6523b3eb3cSopenharmony_ci        } else {
6623b3eb3cSopenharmony_ci            NapiThrow(env, "the second paramter is not number.", ERROR_CODE_PARAM_INVALID);
6723b3eb3cSopenharmony_ci            return nullptr;
6823b3eb3cSopenharmony_ci        }
6923b3eb3cSopenharmony_ci    }
7023b3eb3cSopenharmony_ci    return nullptr;
7123b3eb3cSopenharmony_ci}
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_cistatic napi_value JSRemoveFrameNode(napi_env env, napi_callback_info info)
7423b3eb3cSopenharmony_ci{
7523b3eb3cSopenharmony_ci    NG::FrameNode* frameNode = ParseFrameNode(env, info);
7623b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(frameNode, nullptr);
7723b3eb3cSopenharmony_ci    auto delegate = EngineHelper::GetCurrentDelegateSafely();
7823b3eb3cSopenharmony_ci    if (!delegate) {
7923b3eb3cSopenharmony_ci        NapiThrow(env, "UI execution context not found.", ERROR_CODE_INTERNAL_ERROR);
8023b3eb3cSopenharmony_ci        return nullptr;
8123b3eb3cSopenharmony_ci    }
8223b3eb3cSopenharmony_ci    delegate->RemoveFrameNodeOnOverlay(AceType::Claim(frameNode));
8323b3eb3cSopenharmony_ci    return nullptr;
8423b3eb3cSopenharmony_ci}
8523b3eb3cSopenharmony_ci
8623b3eb3cSopenharmony_cistatic napi_value JSShowNode(napi_env env, napi_callback_info info)
8723b3eb3cSopenharmony_ci{
8823b3eb3cSopenharmony_ci    NG::FrameNode* frameNode = ParseFrameNode(env, info);
8923b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(frameNode, nullptr);
9023b3eb3cSopenharmony_ci    auto delegate = EngineHelper::GetCurrentDelegateSafely();
9123b3eb3cSopenharmony_ci    if (!delegate) {
9223b3eb3cSopenharmony_ci        NapiThrow(env, "UI execution context not found.", ERROR_CODE_INTERNAL_ERROR);
9323b3eb3cSopenharmony_ci        return nullptr;
9423b3eb3cSopenharmony_ci    }
9523b3eb3cSopenharmony_ci    delegate->ShowNodeOnOverlay(AceType::Claim(frameNode));
9623b3eb3cSopenharmony_ci    return nullptr;
9723b3eb3cSopenharmony_ci}
9823b3eb3cSopenharmony_ci
9923b3eb3cSopenharmony_cistatic napi_value JSHideNode(napi_env env, napi_callback_info info)
10023b3eb3cSopenharmony_ci{
10123b3eb3cSopenharmony_ci    NG::FrameNode* frameNode = ParseFrameNode(env, info);
10223b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(frameNode, nullptr);
10323b3eb3cSopenharmony_ci    auto delegate = EngineHelper::GetCurrentDelegateSafely();
10423b3eb3cSopenharmony_ci    if (!delegate) {
10523b3eb3cSopenharmony_ci        NapiThrow(env, "UI execution context not found.", ERROR_CODE_INTERNAL_ERROR);
10623b3eb3cSopenharmony_ci        return nullptr;
10723b3eb3cSopenharmony_ci    }
10823b3eb3cSopenharmony_ci    delegate->HideNodeOnOverlay(AceType::Claim(frameNode));
10923b3eb3cSopenharmony_ci    return nullptr;
11023b3eb3cSopenharmony_ci}
11123b3eb3cSopenharmony_ci
11223b3eb3cSopenharmony_cistatic napi_value JSShowAllFrameNodes(napi_env env, napi_callback_info info)
11323b3eb3cSopenharmony_ci{
11423b3eb3cSopenharmony_ci    auto delegate = EngineHelper::GetCurrentDelegateSafely();
11523b3eb3cSopenharmony_ci    if (!delegate) {
11623b3eb3cSopenharmony_ci        NapiThrow(env, "UI execution context not found.", ERROR_CODE_INTERNAL_ERROR);
11723b3eb3cSopenharmony_ci        return nullptr;
11823b3eb3cSopenharmony_ci    }
11923b3eb3cSopenharmony_ci    delegate->ShowAllNodesOnOverlay();
12023b3eb3cSopenharmony_ci    return nullptr;
12123b3eb3cSopenharmony_ci}
12223b3eb3cSopenharmony_ci
12323b3eb3cSopenharmony_cistatic napi_value JSHideAllFrameNodes(napi_env env, napi_callback_info info)
12423b3eb3cSopenharmony_ci{
12523b3eb3cSopenharmony_ci    auto delegate = EngineHelper::GetCurrentDelegateSafely();
12623b3eb3cSopenharmony_ci    if (!delegate) {
12723b3eb3cSopenharmony_ci        NapiThrow(env, "UI execution context not found.", ERROR_CODE_INTERNAL_ERROR);
12823b3eb3cSopenharmony_ci        return nullptr;
12923b3eb3cSopenharmony_ci    }
13023b3eb3cSopenharmony_ci    delegate->HideAllNodesOnOverlay();
13123b3eb3cSopenharmony_ci    return nullptr;
13223b3eb3cSopenharmony_ci}
13323b3eb3cSopenharmony_ci
13423b3eb3cSopenharmony_cistatic napi_value OverlayManagerExport(napi_env env, napi_value exports)
13523b3eb3cSopenharmony_ci{
13623b3eb3cSopenharmony_ci    napi_property_descriptor overlayManagerDesc[] = {
13723b3eb3cSopenharmony_ci        DECLARE_NAPI_FUNCTION("addFrameNode", JSAddFrameNode),
13823b3eb3cSopenharmony_ci        DECLARE_NAPI_FUNCTION("removeFrameNode", JSRemoveFrameNode),
13923b3eb3cSopenharmony_ci        DECLARE_NAPI_FUNCTION("showNode", JSShowNode),
14023b3eb3cSopenharmony_ci        DECLARE_NAPI_FUNCTION("hideNode", JSHideNode),
14123b3eb3cSopenharmony_ci        DECLARE_NAPI_FUNCTION("showAllFrameNodes", JSShowAllFrameNodes),
14223b3eb3cSopenharmony_ci        DECLARE_NAPI_FUNCTION("hideAllFrameNodes", JSHideAllFrameNodes)
14323b3eb3cSopenharmony_ci    };
14423b3eb3cSopenharmony_ci    NAPI_CALL(env, napi_define_properties(
14523b3eb3cSopenharmony_ci        env, exports, sizeof(overlayManagerDesc) / sizeof(overlayManagerDesc[0]), overlayManagerDesc));
14623b3eb3cSopenharmony_ci    return exports;
14723b3eb3cSopenharmony_ci}
14823b3eb3cSopenharmony_ci
14923b3eb3cSopenharmony_cistatic napi_module overlayManagerModule = {
15023b3eb3cSopenharmony_ci    .nm_version = 1,
15123b3eb3cSopenharmony_ci    .nm_flags = 0,
15223b3eb3cSopenharmony_ci    .nm_filename = nullptr,
15323b3eb3cSopenharmony_ci    .nm_register_func = OverlayManagerExport,
15423b3eb3cSopenharmony_ci    .nm_modname = "overlay",
15523b3eb3cSopenharmony_ci    .nm_priv = ((void*)0),
15623b3eb3cSopenharmony_ci    .reserved = { 0 },
15723b3eb3cSopenharmony_ci};
15823b3eb3cSopenharmony_ci
15923b3eb3cSopenharmony_ciextern "C" __attribute__((constructor)) void OverlayRegister()
16023b3eb3cSopenharmony_ci{
16123b3eb3cSopenharmony_ci    napi_module_register(&overlayManagerModule);
16223b3eb3cSopenharmony_ci}
16323b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Napi
164