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 #ifndef FOUNDATION_RESOURCESCHEDULE_DEVICE_USAGE_STATISTICS_BUNDLE_STATE_DATA_H
17 #define FOUNDATION_RESOURCESCHEDULE_DEVICE_USAGE_STATISTICS_BUNDLE_STATE_DATA_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 #include "bundle_active_event.h"
26 #include "bundle_active_event_stats.h"
27 #include "bundle_active_package_stats.h"
28 #include "app_group_observer_napi.h"
29 
30 namespace OHOS {
31 namespace DeviceUsageStats {
32 #define ARGS_ONE 1
33 #define ARGS_TWO 2
34 #define ARGS_THREE 3
35 #define ARGS_FOUR 4
36 
37 #define PARAM_FIRST 0
38 #define PARAM_SECOND 1
39 #define PARAM_THIRD 2
40 #define PARAM_FOURTH 3
41 
42 #define BUNDLE_STATE_OK 0
43 #define INTERVAL_TYPE_DEFAULT 0
44 #define INTERVAL_NUMBER_MIN 0
45 #define INTERVAL_NUMBER_MAX 4
46 #define TIME_NUMBER_MIN 0
47 #define GROUP_NUMBER_MIN 5
48 #define GROUP_NUMBER_MAX 50
49 
50 struct AsyncWorkData {
51     explicit AsyncWorkData(napi_env napiEnv);
52     virtual ~AsyncWorkData();
53     napi_env env = nullptr;
54     napi_async_work asyncWork = nullptr;
55     napi_deferred deferred = nullptr;
56     napi_ref callback = nullptr;
57     bool isCallback = false;
58     int32_t errorCode = 0;
59 };
60 
61 struct CallbackReceiveDataWorker {
62     napi_env env = nullptr;
63     napi_ref ref = nullptr;
64     int32_t oldGroup = 0;
65     int32_t newGroup = 0;
66     int32_t userId = -1;
67     uint32_t changeReason = 0;
68     std::string bundleName;
69 };
70 
71 struct AsyncCallbackInfoIsIdleState : public AsyncWorkData {
AsyncCallbackInfoIsIdleStateAsyncCallbackInfoIsIdleState72     explicit AsyncCallbackInfoIsIdleState(napi_env env) : AsyncWorkData(env) {}
73     std::string bundleName = "";
74     bool state = false;
75 };
76 
77 struct AsyncQueryAppGroupCallbackInfo : public AsyncWorkData {
AsyncQueryAppGroupCallbackInfoAsyncQueryAppGroupCallbackInfo78     explicit AsyncQueryAppGroupCallbackInfo(napi_env env) : AsyncWorkData(env) {}
79     std::string bundleName = "";
80     int32_t priorityGroup = -1;
81 };
82 
83 struct AsyncCallbackInfoStates : public AsyncWorkData {
AsyncCallbackInfoStatesAsyncCallbackInfoStates84     explicit AsyncCallbackInfoStates(napi_env env) : AsyncWorkData(env) {}
85     int64_t beginTime = -1;
86     int64_t endTime = -1;
87     std::vector<BundleActiveEvent> BundleActiveState;
88 };
89 
90 struct AsyncCallbackInfoAppUsageByInterval : public AsyncWorkData {
AsyncCallbackInfoAppUsageByIntervalAsyncCallbackInfoAppUsageByInterval91     explicit AsyncCallbackInfoAppUsageByInterval(napi_env env) : AsyncWorkData(env) {}
92     int32_t intervalType = -1;
93     int64_t beginTime = -1;
94     int64_t endTime = -1;
95     std::vector<BundleActivePackageStats> packageStats;
96 };
97 
98 struct AsyncCallbackInfoEventStats : public AsyncWorkData {
AsyncCallbackInfoEventStatsAsyncCallbackInfoEventStats99     explicit AsyncCallbackInfoEventStats(napi_env env) : AsyncWorkData(env) {}
100     int64_t beginTime = -1;
101     int64_t endTime = -1;
102     std::vector<BundleActiveEventStats> eventStats;
103 };
104 
105 struct AsyncCallbackInfoAppUsage : public AsyncWorkData {
AsyncCallbackInfoAppUsageAsyncCallbackInfoAppUsage106     explicit AsyncCallbackInfoAppUsage(napi_env env) : AsyncWorkData(env) {}
107     int64_t beginTime = -1;
108     int64_t endTime = -1;
109     std::shared_ptr<std::map<std::string, BundleActivePackageStats>> packageStats;
110 };
111 struct AsyncCallbackInfoSetAppGroup : public AsyncWorkData {
AsyncCallbackInfoSetAppGroupAsyncCallbackInfoSetAppGroup112     explicit AsyncCallbackInfoSetAppGroup(napi_env env) : AsyncWorkData(env) {}
113     int32_t newGroup = -1;;
114     std::string bundleName = "";
115     bool state = true;
116 };
117 
118 struct AsyncCallbackInfoModuleRecord : public AsyncWorkData {
AsyncCallbackInfoModuleRecordAsyncCallbackInfoModuleRecord119     explicit AsyncCallbackInfoModuleRecord(napi_env env) : AsyncWorkData(env) {}
120     int32_t maxNum = -1;
121     std::vector<BundleActiveModuleRecord> moduleRecords;
122 };
123 
124 struct AsyncUnRegisterCallbackInfo : public AsyncWorkData {
AsyncUnRegisterCallbackInfoAsyncUnRegisterCallbackInfo125     explicit AsyncUnRegisterCallbackInfo(napi_env env) : AsyncWorkData(env) {}
126     sptr<AppGroupObserver> observer = nullptr;
127     bool state = true;
128 };
129 
130 struct AsyncRegisterCallbackInfo : public AsyncWorkData {
AsyncRegisterCallbackInfoAsyncRegisterCallbackInfo131     explicit AsyncRegisterCallbackInfo(napi_env env) : AsyncWorkData(env) {}
132     sptr<AppGroupObserver> observer = nullptr;
133     bool state = true;
134 };
135 
136 struct IsIdleStateParamsInfo {
137     std::string bundleName = "";
138     napi_ref callback = nullptr;
139     int32_t errorCode = 0;
140 };
141 
142 struct QueryAppGroupParamsInfo {
143     std::string bundleName;
144     napi_ref callback = nullptr;
145     int32_t errorCode = 0;
146 };
147 
148 struct StatesParamsInfo {
149     int64_t beginTime = -1;
150     int64_t endTime = -1;
151     napi_ref callback = nullptr;
152     int32_t errorCode = 0;
153 };
154 
155 struct AppUsageParamsByIntervalInfo {
156     int32_t intervalType = -1;
157     int64_t beginTime = -1;
158     int64_t endTime = -1;
159     napi_ref callback = nullptr;
160     int32_t errorCode = 0;
161 };
162 
163 struct QueryBundleStatsParamsInfo {
164     int64_t beginTime = -1;
165     int64_t endTime = -1;
166     napi_ref callback = nullptr;
167     int32_t errorCode = 0;
168 };
169 
170 struct ModuleRecordParamsInfo {
171     int32_t maxNum = -1;
172     napi_ref callback = nullptr;
173     int32_t errorCode = 0;
174 };
175 
176 struct ParamsBundleGroupInfo {
177     std::string bundleName = "";
178     int32_t newGroup = -1;
179     napi_ref callback = nullptr;
180     int32_t errorCode = 0;
181 };
182 
183 struct RegisterCallbackInfo {
184     napi_ref callback = nullptr;
185     int32_t errorCode = 0;
186 };
187 
188 struct UnRegisterCallbackInfo {
189     napi_ref callback = nullptr;
190     int32_t errorCode = 0;
191 };
192 
193 struct EventStatesParamsInfo {
194     int64_t beginTime = -1;
195     int64_t endTime = -1;
196     napi_ref callback = nullptr;
197     int32_t errorCode = 0;
198 };
199 }  // namespace DeviceUsageStats
200 }  // namespace OHOS
201 #endif  // FOUNDATION_RESOURCESCHEDULE_DEVICE_USAGE_STATISTICS_BUNDLE_STATE_DATA_H
202 
203