1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17 #include "interfaces/napi/kits/utils/napi_utils.h"
18
19 #include "core/common/ace_engine.h"
20 #include "frameworks/core/common/window_free_container.h"
21 #include "frameworks/bridge/common/utils/engine_helper.h"
22
23 namespace OHOS::Ace::Napi {
JSCreateContainerWithoutWindow(napi_env env,napi_callback_info info)24 static napi_value JSCreateContainerWithoutWindow(napi_env env, napi_callback_info info)
25 {
26 napi_value result = nullptr;
27
28 napi_value thisVal = nullptr;
29 std::size_t argc = 1;
30 napi_value argv = nullptr;
31 void *context = nullptr;
32
33 napi_get_cb_info(env, info, &argc, &argv, &thisVal, nullptr);
34 if (argc != 1) {
35 LOGE("Wrong arg count.");
36 NapiThrow(env, "The number of parameters is incorrect.", ERROR_CODE_PARAM_INVALID);
37 return result;
38 }
39
40 #ifndef PREVIEW
41 // There is no napi context in previewer, no need to check ..
42 if (napi_unwrap(env, argv, &context) != napi_ok || !context) {
43 NapiThrow(env, "Invalid parameter type of context.", ERROR_CODE_PARAM_INVALID);
44 return result;
45 }
46 #endif
47
48 auto container = OHOS::Ace::Platform::WindowFreeContainer::CreateWindowFreeContainer(env, context);
49 if (!container) {
50 NapiThrow(env, "Create container without window failed.", ERROR_CODE_INTERNAL_ERROR);
51 return result;
52 }
53
54 auto frontend = container->GetFrontend();
55 if (!frontend) {
56 return result;
57 }
58 result = frontend->GetContextValue();
59
60 return result;
61 }
62
JSDestroyContainerWithoutWindow(napi_env,napi_callback_info info)63 static napi_value JSDestroyContainerWithoutWindow(napi_env, napi_callback_info info)
64 {
65 napi_value result = nullptr;
66 OHOS::Ace::Platform::WindowFreeContainer::DestroyWindowFreeContainer();
67
68 return result;
69 }
70
containerUtilsExport(napi_env env,napi_value exports)71 static napi_value containerUtilsExport(napi_env env, napi_value exports)
72 {
73 napi_property_descriptor containerUtilsDesc[] = {
74 DECLARE_NAPI_FUNCTION("createContainerWithoutWindow", JSCreateContainerWithoutWindow),
75 DECLARE_NAPI_FUNCTION("destroyContainerWithoutWindow", JSDestroyContainerWithoutWindow),
76 };
77 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(containerUtilsDesc) / sizeof(containerUtilsDesc[0]),
78 containerUtilsDesc));
79
80 return exports;
81 }
82
83 static napi_module containerUtilsModule = {
84 .nm_version = 1,
85 .nm_flags = 0,
86 .nm_filename = nullptr,
87 .nm_register_func = containerUtilsExport,
88 .nm_modname = "arkui.containerUtils",
89 .nm_priv = ((void*)0),
90 .reserved = { 0 },
91 };
92
ContainerUtilsRegister()93 extern "C" __attribute__((constructor)) void ContainerUtilsRegister()
94 {
95 napi_module_register(&containerUtilsModule);
96 }
97
98 } // namespace OHOS::Ace::Napi
99