1 /*
2  * Copyright (c) 2021 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 #include <string>
16 
17 #include "napi/native_api.h"
18 #include "napi/native_node_api.h"
19 
20 #include "frameworks/core/components/common/layout/grid_system_manager.h"
21 
22 namespace OHOS::Ace::Napi {
23 
JSGridGetSystemLayoutInfo(napi_env env,napi_callback_info info)24 static napi_value JSGridGetSystemLayoutInfo(napi_env env, napi_callback_info info)
25 {
26     std::string gridInfo = OHOS::Ace::GridSystemManager::GetInstance().GetCurrentGridInfo().ToString();
27     if (gridInfo.empty()) {
28         LOGE("gridInfo is empty");
29         return nullptr;
30     }
31     napi_value globalValue;
32     napi_get_global(env, &globalValue);
33     napi_value jsonValue;
34     napi_get_named_property(env, globalValue, "JSON", &jsonValue);
35     napi_value parseValue;
36     napi_get_named_property(env, jsonValue, "parse", &parseValue);
37     napi_value gridInfoNApi;
38     napi_create_string_utf8(env, gridInfo.c_str(), NAPI_AUTO_LENGTH, &gridInfoNApi);
39     napi_value funcArgv[1] = { gridInfoNApi };
40     napi_value result;
41     napi_call_function(env, jsonValue, parseValue, 1, funcArgv, &result);
42     napi_valuetype valueType = napi_undefined;
43     napi_typeof(env, result, &valueType);
44     if (valueType != napi_object) {
45         LOGE("parse result fail");
46         return nullptr;
47     }
48     return result;
49 }
50 
GridExport(napi_env env,napi_value exports)51 static napi_value GridExport(napi_env env, napi_value exports)
52 {
53     napi_property_descriptor gridDesc[] = {
54         DECLARE_NAPI_FUNCTION("getSystemLayoutInfo", JSGridGetSystemLayoutInfo),
55     };
56     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(gridDesc) / sizeof(gridDesc[0]), gridDesc));
57     return exports;
58 }
59 
60 static napi_module gridModule = {
61     .nm_version = 1,
62     .nm_flags = 0,
63     .nm_filename = nullptr,
64     .nm_register_func = GridExport,
65     .nm_modname = "grid",
66     .nm_priv = ((void*)0),
67     .reserved = { 0 },
68 };
69 
GridRegister()70 extern "C" __attribute__((constructor)) void GridRegister()
71 {
72     napi_module_register(&gridModule);
73 }
74 
75 } // namespace OHOS::Ace::Napi
76