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 InitGroupType(env, exports);
50 return exports;
51 }
52
InitIntervalType(napi_env env,napi_value exports)53 napi_value InitIntervalType(napi_env env, napi_value exports)
54 {
55 napi_value by_optimized;
56 napi_value by_daily;
57 napi_value by_weekly;
58 napi_value by_monthly;
59 napi_value by_annually;
60 int32_t refCount = 1;
61
62 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_OPTIMIZED),
63 &by_optimized);
64 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_DAILY),
65 &by_daily);
66 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_WEEKLY),
67 &by_weekly);
68 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_MONTHLY),
69 &by_monthly);
70 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_ANNUALLY),
71 &by_annually);
72
73 napi_property_descriptor desc[] = {
74 DECLARE_NAPI_STATIC_PROPERTY("BY_OPTIMIZED", by_optimized),
75 DECLARE_NAPI_STATIC_PROPERTY("BY_DAILY", by_daily),
76 DECLARE_NAPI_STATIC_PROPERTY("BY_WEEKLY", by_weekly),
77 DECLARE_NAPI_STATIC_PROPERTY("BY_MONTHLY", by_monthly),
78 DECLARE_NAPI_STATIC_PROPERTY("BY_ANNUALLY", by_annually),
79 };
80
81 napi_value result = nullptr;
82 napi_define_class(env, "IntervalType", NAPI_AUTO_LENGTH, EnumTypeConstructor,
83 nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
84 napi_create_reference(env, result, refCount, &typeConstructor_);
85 napi_set_named_property(env, exports, "IntervalType", result);
86 return exports;
87 }
88
InitGroupType(napi_env env,napi_value exports)89 napi_value InitGroupType(napi_env env, napi_value exports)
90 {
91 napi_value active_group_alive;
92 napi_value active_group_daily;
93 napi_value active_group_fixed;
94 napi_value active_group_rare;
95 napi_value active_group_limit;
96 napi_value active_group_never;
97 int32_t refCount = 1;
98
99 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::ACTIVE_GROUP_ALIVE),
100 &active_group_alive);
101 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::ACTIVE_GROUP_DAILY),
102 &active_group_daily);
103 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::ACTIVE_GROUP_FIXED),
104 &active_group_fixed);
105 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::ACTIVE_GROUP_RARE),
106 &active_group_rare);
107 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::ACTIVE_GROUP_LIMIT),
108 &active_group_limit);
109 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::ACTIVE_GROUP_NEVER),
110 &active_group_never);
111
112 napi_property_descriptor desc[] = {
113 DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_ALIVE", active_group_alive),
114 DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_DAILY", active_group_daily),
115 DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_FIXED", active_group_fixed),
116 DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_RARE", active_group_rare),
117 DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_LIMIT", active_group_limit),
118 DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_NEVER", active_group_never),
119 };
120
121 napi_value result = nullptr;
122 napi_define_class(env, "GroupType", NAPI_AUTO_LENGTH, EnumTypeConstructor,
123 nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
124 napi_create_reference(env, result, refCount, &typeConstructor_);
125 napi_set_named_property(env, exports, "GroupType", result);
126 return exports;
127 }
128
EnumTypeConstructor(napi_env env,napi_callback_info info)129 napi_value EnumTypeConstructor(napi_env env, napi_callback_info info)
130 {
131 size_t argc = 0;
132 napi_value args[ARG_FIRST] = {0};
133 napi_value res = nullptr;
134 void *data = nullptr;
135
136 napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data);
137 if (status != napi_ok) {
138 return nullptr;
139 }
140
141 return res;
142 }
143
144 /*
145 * Module register function
146 */
RegisterModule(void)147 __attribute__((constructor)) void RegisterModule(void)
148 {
149 napi_module_register(&_module);
150 }
151 EXTERN_C_END
152 } // namespace DeviceUsageStats
153 } // namespace OHOS
154
155