1/*
2 * Copyright (c) 2023 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 "init.h"
17
18#include "allow_type.h"
19#include "common.h"
20#include "standby_napi_module.h"
21
22namespace OHOS {
23namespace DevStandbyMgr {
24EXTERN_C_START
25
26napi_value DeviceStandbyFuncInit(napi_env env, napi_value exports)
27{
28    napi_property_descriptor desc[] = {
29        DECLARE_NAPI_FUNCTION("isDeviceInStandby", IsDeviceInStandby),
30        DECLARE_NAPI_FUNCTION("getExemptedApps", GetExemptionListApps),
31        DECLARE_NAPI_FUNCTION("requestExemptionResource", ApplyAllowResource),
32        DECLARE_NAPI_FUNCTION("releaseExemptionResource", UnapplyAllowResource),
33    };
34    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
35
36    return exports;
37}
38
39void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName)
40{
41    napi_value prop = nullptr;
42    if (napi_create_int32(env, objName, &prop) == napi_ok) {
43        napi_set_named_property(env, dstObj, propName, prop);
44    }
45}
46
47napi_value DeviceStandbyTypeInit(napi_env env, napi_value exports)
48{
49    napi_value obj = nullptr;
50    napi_create_object(env, &obj);
51
52    SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(AllowType::NETWORK), "NETWORK");
53    SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(AllowType::RUNNING_LOCK), "RUNNING_LOCK");
54    SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(AllowType::TIMER), "TIMER");
55    SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(AllowType::WORK_SCHEDULER), "WORK_SCHEDULER");
56    SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(AllowType::AUTO_SYNC), "AUTO_SYNC");
57    SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(AllowType::PUSH), "PUSH");
58    SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(AllowType::FREEZE), "FREEZE");
59
60    napi_property_descriptor exportFuncs[] = {
61        DECLARE_NAPI_PROPERTY("ResourceType", obj),
62    };
63
64    napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
65    return exports;
66}
67
68/*
69 * Module export function
70 */
71static napi_value DeviceStandbyInit(napi_env env, napi_value exports)
72{
73    /*
74     * Properties define
75     */
76    DeviceStandbyFuncInit(env, exports);
77    DeviceStandbyTypeInit(env, exports);
78    return exports;
79}
80
81/*
82 * Module register function
83 */
84__attribute__((constructor)) void RegisterModule(void)
85{
86    napi_module_register(&g_module);
87}
88EXTERN_C_END
89}  // namespace DevStandbyMgr
90}  // namespace OHOS
91