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