1 /*
2  * Copyright (c) 2022  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 "bundle_state_init.h"
17 
18 #include "bundle_state_condition.h"
19 #include "bundle_state_query.h"
20 
21 namespace OHOS {
22 namespace DeviceUsageStats {
23 EXTERN_C_START
24 
25 static const uint8_t ARG_FIRST = 1;
26 
27 napi_ref typeConstructor_ = nullptr;
28 
29 /*
30  * Module export function
31  */
BundleStateInit(napi_env env,napi_value exports)32 static napi_value BundleStateInit(napi_env env, napi_value exports)
33 {
34     /*
35      * Propertise define
36      */
37     napi_property_descriptor desc[] = {
38         DECLARE_NAPI_FUNCTION("isIdleState", IsIdleState),
39         DECLARE_NAPI_FUNCTION("queryAppUsagePriorityGroup", QueryAppUsagePriorityGroup),
40         DECLARE_NAPI_FUNCTION("queryCurrentBundleActiveStates", QueryCurrentBundleActiveStates),
41         DECLARE_NAPI_FUNCTION("queryBundleActiveStates", QueryBundleActiveStates),
42         DECLARE_NAPI_FUNCTION("queryBundleStateInfoByInterval", QueryBundleStateInfoByInterval),
43         DECLARE_NAPI_FUNCTION("queryBundleStateInfos", QueryBundleStateInfos),
44     };
45 
46     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
47 
48     InitIntervalType(env, exports);
49     return exports;
50 }
51 
InitIntervalType(napi_env env,napi_value exports)52 napi_value InitIntervalType(napi_env env, napi_value exports)
53 {
54     napi_value by_optimized;
55     napi_value by_daily;
56     napi_value by_weekly;
57     napi_value by_monthly;
58     napi_value by_annually;
59     int32_t refCount = 1;
60 
61     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_OPTIMIZED),
62         &by_optimized);
63     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_DAILY),
64         &by_daily);
65     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_WEEKLY),
66         &by_weekly);
67     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_MONTHLY),
68         &by_monthly);
69     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_ANNUALLY),
70         &by_annually);
71 
72     napi_property_descriptor desc[] = {
73         DECLARE_NAPI_STATIC_PROPERTY("BY_OPTIMIZED", by_optimized),
74         DECLARE_NAPI_STATIC_PROPERTY("BY_DAILY", by_daily),
75         DECLARE_NAPI_STATIC_PROPERTY("BY_WEEKLY", by_weekly),
76         DECLARE_NAPI_STATIC_PROPERTY("BY_MONTHLY", by_monthly),
77         DECLARE_NAPI_STATIC_PROPERTY("BY_ANNUALLY", by_annually),
78     };
79 
80     napi_value result = nullptr;
81     napi_define_class(env, "IntervalType", NAPI_AUTO_LENGTH, EnumTypeConstructor,
82         nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
83     napi_create_reference(env, result, refCount, &typeConstructor_);
84     napi_set_named_property(env, exports, "IntervalType", result);
85     return exports;
86 }
87 
EnumTypeConstructor(napi_env env,napi_callback_info info)88 napi_value EnumTypeConstructor(napi_env env, napi_callback_info info)
89 {
90     size_t argc = 0;
91     napi_value args[ARG_FIRST] = {0};
92     napi_value res = nullptr;
93     void *data = nullptr;
94 
95     napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data);
96     if (status != napi_ok) {
97         return nullptr;
98     }
99 
100     return res;
101 }
102 
103 /*
104  * Module register function
105  */
RegisterModule(void)106 __attribute__((constructor)) void RegisterModule(void)
107 {
108     napi_module_register(&_module);
109 }
110 EXTERN_C_END
111 }  // namespace DeviceUsageStats
112 }  // namespace OHOS
113 
114