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 "securec.h"
17
18 #include "bundle_active_log.h"
19 #include "bundle_state_common.h"
20
21 namespace OHOS {
22 namespace DeviceUsageStats {
23 const int ERR_MULTIPLE = 100;
AsyncWorkData(napi_env napiEnv)24 AsyncWorkData::AsyncWorkData(napi_env napiEnv)
25 {
26 env = napiEnv;
27 }
28
~AsyncWorkData()29 AsyncWorkData::~AsyncWorkData()
30 {
31 if (callback) {
32 BUNDLE_ACTIVE_LOGI("delete callback");
33 napi_delete_reference(env, callback);
34 callback = nullptr;
35 }
36 if (asyncWork) {
37 BUNDLE_ACTIVE_LOGI("delete asyncwork");
38 napi_delete_async_work(env, asyncWork);
39 asyncWork = nullptr;
40 }
41 }
42
HandleParamErr(const napi_env & env,int32_t errCode,const std::string & operation)43 napi_value BundleStateCommon::HandleParamErr(const napi_env &env, int32_t errCode, const std::string& operation)
44 {
45 if (errCode == ERR_OK) {
46 return nullptr;
47 }
48 BUNDLE_ACTIVE_LOGE("HandleParamErr %{public}d", errCode);
49 auto iter = paramErrCodeMsgMap.find(errCode);
50 if (iter != paramErrCodeMsgMap.end()) {
51 std::string errMessage = "BussinessError 401: Parameter error. ";
52 errMessage.append(operation);
53 errMessage.append(iter->second);
54 napi_throw_error(env, std::to_string(ERR_PARAM_ERROR).c_str(), errMessage.c_str());
55 }
56 return nullptr;
57 }
58
GetSaErrCodeMsg(int32_t errCode,int32_t reflectCode)59 std::string BundleStateCommon::GetSaErrCodeMsg(int32_t errCode, int32_t reflectCode)
60 {
61 BUNDLE_ACTIVE_LOGE("GetSaErrCodeMsg %{public}d", errCode);
62 auto iter = saErrCodeMsgMap.find(errCode);
63 std::string errMessage;
64 if (iter != saErrCodeMsgMap.end()) {
65 errMessage.append("BussinessError ");
66 errMessage.append(std::to_string(reflectCode)).append(":").append(iter->second);
67 }
68 return errMessage;
69 }
70
NapiGetNull(napi_env env)71 napi_value BundleStateCommon::NapiGetNull(napi_env env)
72 {
73 napi_value result = nullptr;
74 napi_get_null(env, &result);
75 return result;
76 }
77
GetCallbackPromiseResult(const napi_env & env,const AsyncWorkData & workData,const napi_value & result)78 void BundleStateCommon::GetCallbackPromiseResult(const napi_env &env,
79 const AsyncWorkData &workData, const napi_value &result)
80 {
81 if (workData.isCallback) {
82 SetCallbackInfo(env, workData.callback, workData.errorCode, result);
83 } else {
84 SetPromiseInfo(env, workData.deferred, result, workData.errorCode);
85 }
86 }
87
SetCallbackInfo(const napi_env & env,const napi_ref & callbackIn,const int32_t & errorCode,const napi_value & result)88 void BundleStateCommon::SetCallbackInfo(
89 const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, const napi_value &result)
90 {
91 napi_value undefined = nullptr;
92 napi_get_undefined(env, &undefined);
93
94 napi_value callback = nullptr;
95 napi_value resultout = nullptr;
96 napi_get_reference_value(env, callbackIn, &callback);
97 napi_value results[ARGS_TWO] = {nullptr};
98 results[PARAM_FIRST] = GetErrorValue(env, errorCode);
99 results[PARAM_SECOND] = result;
100 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &results[PARAM_FIRST],
101 &resultout));
102 }
103
GetBundleActiveEventForResult(napi_env env,const std::vector<BundleActiveEvent> & bundleActiveStates,napi_value result,bool isNewVersion)104 void BundleStateCommon::GetBundleActiveEventForResult(
105 napi_env env, const std::vector<BundleActiveEvent> &bundleActiveStates, napi_value result, bool isNewVersion)
106 {
107 int32_t index = 0;
108 for (const auto &item : bundleActiveStates) {
109 napi_value bundleActiveState = nullptr;
110 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &bundleActiveState));
111
112 napi_value bundleName = nullptr;
113 NAPI_CALL_RETURN_VOID(
114 env, napi_create_string_utf8(env, item.bundleName_.c_str(), NAPI_AUTO_LENGTH, &bundleName));
115 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, bundleActiveState, "bundleName", bundleName));
116
117 napi_value eventId = nullptr;
118 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, item.eventId_, &eventId));
119
120 napi_value eventOccurredTime = nullptr;
121 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.timeStamp_, &eventOccurredTime));
122
123 if (isNewVersion) {
124 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, bundleActiveState, "eventId", eventId));
125 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, bundleActiveState, "eventOccurredTime",
126 eventOccurredTime));
127 } else {
128 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, bundleActiveState, "stateType", eventId));
129 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, bundleActiveState, "stateOccurredTime",
130 eventOccurredTime));
131 }
132
133 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, result, index, bundleActiveState));
134 index++;
135 }
136 }
137
GetBundleStateInfoByIntervalForResult(napi_env env,const std::vector<BundleActivePackageStats> & packageStats,napi_value result)138 void BundleStateCommon::GetBundleStateInfoByIntervalForResult(
139 napi_env env, const std::vector<BundleActivePackageStats> &packageStats, napi_value result)
140 {
141 int32_t index = 0;
142 for (const auto &item : packageStats) {
143 napi_value packageObject = nullptr;
144 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &packageObject));
145
146 napi_value bundleName = nullptr;
147 NAPI_CALL_RETURN_VOID(
148 env, napi_create_string_utf8(env, item.bundleName_.c_str(), NAPI_AUTO_LENGTH, &bundleName));
149 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "bundleName", bundleName));
150
151 napi_value abilityPrevAccessTime = nullptr;
152 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.lastTimeUsed_, &abilityPrevAccessTime));
153 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "abilityPrevAccessTime",
154 abilityPrevAccessTime));
155
156 napi_value abilityInFgTotalTime = nullptr;
157 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.totalInFrontTime_, &abilityInFgTotalTime));
158 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "abilityInFgTotalTime",
159 abilityInFgTotalTime));
160
161 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, result, index, packageObject));
162 index++;
163 }
164 }
165
GetBundleActiveEventStatsForResult(napi_env env,const std::vector<BundleActiveEventStats> & eventStats,napi_value result)166 void BundleStateCommon::GetBundleActiveEventStatsForResult(napi_env env,
167 const std::vector<BundleActiveEventStats> &eventStats, napi_value result)
168 {
169 int32_t index = 0;
170 for (const auto &item : eventStats) {
171 napi_value eventStatsObject = nullptr;
172 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &eventStatsObject));
173
174 napi_value name = nullptr;
175 NAPI_CALL_RETURN_VOID(
176 env, napi_create_string_utf8(env, item.name_.c_str(), NAPI_AUTO_LENGTH, &name));
177 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, eventStatsObject, "name", name));
178
179 napi_value eventId = nullptr;
180 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, item.eventId_, &eventId));
181 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, eventStatsObject, "eventId", eventId));
182
183 napi_value count = nullptr;
184 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, item.count_, &count));
185 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, eventStatsObject, "count", count));
186
187 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, result, index, eventStatsObject));
188 index++;
189 }
190 }
191
GetBundleActiveNotificationNumberForResult(napi_env env,const std::vector<BundleActiveEventStats> & eventStats,napi_value result)192 void BundleStateCommon::GetBundleActiveNotificationNumberForResult(napi_env env,
193 const std::vector<BundleActiveEventStats> &eventStats, napi_value result)
194 {
195 int32_t index = 0;
196 for (const auto &item : eventStats) {
197 napi_value eventStatsObject = nullptr;
198 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &eventStatsObject));
199
200 napi_value name = nullptr;
201 NAPI_CALL_RETURN_VOID(
202 env, napi_create_string_utf8(env, item.name_.c_str(), NAPI_AUTO_LENGTH, &name));
203 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, eventStatsObject, "name", name));
204
205 napi_value eventId = nullptr;
206 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, item.eventId_, &eventId));
207 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, eventStatsObject, "eventId", eventId));
208
209 napi_value count = nullptr;
210 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, item.count_, &count));
211 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, eventStatsObject, "count", count));
212
213 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, result, index, eventStatsObject));
214 index++;
215 }
216 }
217
GetBundleStateInfoForResult(napi_env env,const std::shared_ptr<std::map<std::string,BundleActivePackageStats>> & packageStats,napi_value result)218 void BundleStateCommon::GetBundleStateInfoForResult(napi_env env,
219 const std::shared_ptr<std::map<std::string, BundleActivePackageStats>> &packageStats, napi_value result)
220 {
221 if (packageStats == nullptr) {
222 BUNDLE_ACTIVE_LOGE("PackageStats is invalid");
223 return;
224 }
225 for (const auto &item : *packageStats) {
226 napi_value packageObject = nullptr;
227 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &packageObject));
228 napi_value bundleName = nullptr;
229 NAPI_CALL_RETURN_VOID(
230 env, napi_create_string_utf8(env, item.second.bundleName_.c_str(), NAPI_AUTO_LENGTH, &bundleName));
231 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "bundleName", bundleName));
232
233 napi_value abilityPrevAccessTime = nullptr;
234 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.second.lastTimeUsed_, &abilityPrevAccessTime));
235 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "abilityPrevAccessTime",
236 abilityPrevAccessTime));
237
238 napi_value abilityInFgTotalTime = nullptr;
239 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.second.totalInFrontTime_, &abilityInFgTotalTime));
240 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "abilityInFgTotalTime",
241 abilityInFgTotalTime));
242
243 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, item.first.c_str(), packageObject));
244 }
245 }
246
GetModuleRecordBasicForResult(napi_env env,const BundleActiveModuleRecord & moduleRecords,napi_value moduleObject)247 void BundleStateCommon::GetModuleRecordBasicForResult(napi_env env,
248 const BundleActiveModuleRecord &moduleRecords, napi_value moduleObject)
249 {
250 napi_value bundleName = nullptr;
251 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, moduleRecords.bundleName_.c_str(),
252 NAPI_AUTO_LENGTH, &bundleName));
253 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "bundleName", bundleName));
254 napi_value appLabelId = nullptr;
255 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, moduleRecords.appLabelId_, &appLabelId));
256 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "appLabelId", appLabelId));
257 napi_value moduleName = nullptr;
258 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, moduleRecords.moduleName_.c_str(), NAPI_AUTO_LENGTH,
259 &moduleName));
260 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "moduleName", moduleName));
261 napi_value labelId = nullptr;
262 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, moduleRecords.labelId_, &labelId));
263 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "labelId", labelId));
264 napi_value descriptionId = nullptr;
265 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, moduleRecords.descriptionId_, &descriptionId));
266 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "descriptionId", descriptionId));
267 napi_value abilityName = nullptr;
268 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, moduleRecords.abilityName_.c_str(), NAPI_AUTO_LENGTH,
269 &abilityName));
270 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "abilityName", abilityName));
271 napi_value abilityLableId = nullptr;
272 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, moduleRecords.abilityLableId_, &abilityLableId));
273 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "abilityLableId", abilityLableId));
274 napi_value abilityDescriptionId = nullptr;
275 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, moduleRecords.abilityDescriptionId_,
276 &abilityDescriptionId));
277 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "abilityDescriptionId",
278 abilityDescriptionId));
279 napi_value abilityIconId = nullptr;
280 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, moduleRecords.abilityIconId_, &abilityIconId));
281 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "abilityIconId", abilityIconId));
282 napi_value launchedCount = nullptr;
283 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, moduleRecords.launchedCount_, &launchedCount));
284 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "launchedCount", launchedCount));
285 napi_value lastModuleUsedTime = nullptr;
286 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, moduleRecords.lastModuleUsedTime_, &lastModuleUsedTime));
287 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject,
288 "lastModuleUsedTime", lastModuleUsedTime));
289 }
290
GetModuleRecordForResult(napi_env env,const std::vector<BundleActiveModuleRecord> & moduleRecords,napi_value result)291 void BundleStateCommon::GetModuleRecordForResult(napi_env env,
292 const std::vector<BundleActiveModuleRecord> &moduleRecords, napi_value result)
293 {
294 int32_t index = 0;
295 for (const auto &item : moduleRecords) {
296 napi_value moduleObject = nullptr;
297 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &moduleObject));
298 GetModuleRecordBasicForResult(env, item, moduleObject);
299
300 napi_value formRecords = nullptr;
301 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &formRecords));
302 int32_t formIdx = 0;
303 for (const auto& oneFormRecord : item.formRecords_) {
304 napi_value formObject = nullptr;
305 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &formObject));
306 napi_value formName = nullptr;
307 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, oneFormRecord.formName_.c_str(), NAPI_AUTO_LENGTH,
308 &formName));
309 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, formObject, "formName", formName));
310
311 napi_value formDimension = nullptr;
312 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, oneFormRecord.formDimension_, &formDimension));
313 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, formObject, "formDimension", formDimension));
314
315 napi_value formId = nullptr;
316 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, oneFormRecord.formId_, &formId));
317 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, formObject, "formId", formId));
318
319 napi_value formLastUsedTime = nullptr;
320 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, oneFormRecord.formLastUsedTime_, &formLastUsedTime));
321 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, formObject, "formLastUsedTime", formLastUsedTime));
322
323 napi_value count = nullptr;
324 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, oneFormRecord.count_, &count));
325 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, formObject, "count", count));
326 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, formRecords, formIdx, formObject));
327 formIdx++;
328 }
329 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, moduleObject, "formRecords", formRecords));
330 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, result, index, moduleObject));
331 index++;
332 }
333 }
334
SetPromiseInfo(const napi_env & env,const napi_deferred & deferred,const napi_value & result,const int32_t & errorCode)335 void BundleStateCommon::SetPromiseInfo(const napi_env &env, const napi_deferred &deferred,
336 const napi_value &result, const int32_t &errorCode)
337 {
338 switch (errorCode) {
339 case ERR_OK:
340 napi_resolve_deferred(env, deferred, result);
341 break;
342 default:
343 napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode));
344 break;
345 }
346 }
347
GetReflectErrCode(int32_t errCode)348 int32_t BundleStateCommon::GetReflectErrCode(int32_t errCode)
349 {
350 if (errCode < ERR_GET_SYSTEM_ABILITY_MANAGER_FAILED) {
351 return errCode;
352 }
353 return errCode / ERR_MULTIPLE;
354 }
355
GetErrorValue(napi_env env,int32_t errCode)356 napi_value BundleStateCommon::GetErrorValue(napi_env env, int32_t errCode)
357 {
358 if (errCode == ERR_OK) {
359 return NapiGetNull(env);
360 }
361 napi_value result = nullptr;
362 napi_value eCode = nullptr;
363 napi_value eMsg = nullptr;
364 int32_t reflectCode = GetReflectErrCode(errCode);
365 std::string errMsg = GetSaErrCodeMsg(errCode, reflectCode);
366 NAPI_CALL(env, napi_create_int32(env, reflectCode, &eCode));
367 NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), errMsg.length(), &eMsg));
368 NAPI_CALL(env, napi_create_object(env, &result));
369 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
370 NAPI_CALL(env, napi_set_named_property(env, result, "message", eMsg));
371 return result;
372 }
373
JSParaError(const napi_env & env,const napi_ref & callback,const int32_t & errorCode)374 napi_value BundleStateCommon::JSParaError(const napi_env &env, const napi_ref &callback, const int32_t &errorCode)
375 {
376 if (callback) {
377 napi_value result = nullptr;
378 napi_create_array(env, &result);
379 SetCallbackInfo(env, callback, errorCode, result);
380 return result;
381 } else {
382 napi_value promise = nullptr;
383 napi_deferred deferred = nullptr;
384 napi_create_promise(env, &deferred, &promise);
385 napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode));
386 return promise;
387 }
388 }
389
GetTypeStringValue(napi_env env,napi_value param,const std::string & result)390 std::string BundleStateCommon::GetTypeStringValue(napi_env env, napi_value param, const std::string &result)
391 {
392 size_t size = 0;
393 if (napi_get_value_string_utf8(env, param, nullptr, 0, &size) != BUNDLE_STATE_OK) {
394 return result;
395 }
396
397 std::string value("");
398 if (size == 0) {
399 return result;
400 }
401
402 char *buf = new (std::nothrow) char[size + 1];
403 if (buf == nullptr) {
404 return value;
405 }
406
407 if (memset_s(buf, size + 1, 0, size + 1) != EOK) {
408 delete[] buf;
409 buf = nullptr;
410 return value;
411 }
412
413 bool rev = napi_get_value_string_utf8(env, param, buf, size + 1, &size) == BUNDLE_STATE_OK;
414 if (rev) {
415 value = buf;
416 } else {
417 value = result;
418 }
419
420 delete[] buf;
421 buf = nullptr;
422 return value;
423 }
424
GetInt64NumberValue(const napi_env & env,const napi_value & value,int64_t & result)425 napi_value BundleStateCommon::GetInt64NumberValue(const napi_env &env, const napi_value &value, int64_t &result)
426 {
427 napi_valuetype valuetype = napi_undefined;
428
429 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
430 if (valuetype != napi_number) {
431 BUNDLE_ACTIVE_LOGE("Wrong argument type, number expected.");
432 return nullptr;
433 }
434 napi_get_value_int64(env, value, &result);
435 return BundleStateCommon::NapiGetNull(env);
436 }
437
GetInt32NumberValue(const napi_env & env,const napi_value & value,int32_t & result)438 napi_value BundleStateCommon::GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result)
439 {
440 napi_valuetype valuetype = napi_undefined;
441 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
442 if (valuetype != napi_number) {
443 BUNDLE_ACTIVE_LOGE("Wrong argument type. Number expected.");
444 return nullptr;
445 }
446 napi_get_value_int32(env, value, &result);
447 return BundleStateCommon::NapiGetNull(env);
448 }
449
SettingAsyncWorkData(const napi_env & env,const napi_ref & callback,AsyncWorkData & workData,napi_value & promise)450 void BundleStateCommon::SettingAsyncWorkData(
451 const napi_env &env, const napi_ref &callback, AsyncWorkData &workData, napi_value &promise)
452 {
453 if (callback) {
454 workData.callback = callback;
455 workData.isCallback = true;
456 } else {
457 napi_deferred deferred = nullptr;
458 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
459 workData.deferred = deferred;
460 workData.isCallback = false;
461 }
462 }
463
QueryBundleStatsInfos(int64_t & beginTime,int64_t & endTime,int32_t & errCode)464 std::shared_ptr<std::map<std::string, BundleActivePackageStats>> BundleStateCommon::QueryBundleStatsInfos(
465 int64_t &beginTime, int64_t &endTime, int32_t &errCode)
466 {
467 std::vector<BundleActivePackageStats> packageStats;
468 errCode = BundleActiveClient::GetInstance().QueryBundleStatsInfoByInterval(packageStats, INTERVAL_TYPE_DEFAULT,
469 beginTime, endTime);
470 std::shared_ptr<std::map<std::string, BundleActivePackageStats>> mergedPackageStats =
471 std::make_shared<std::map<std::string, BundleActivePackageStats>>();
472 if (packageStats.empty()) {
473 return nullptr;
474 }
475 for (auto packageStat : packageStats) {
476 std::map<std::string, BundleActivePackageStats>::iterator iter =
477 mergedPackageStats->find(packageStat.bundleName_);
478 if (iter != mergedPackageStats->end()) {
479 MergePackageStats(iter->second, packageStat);
480 } else {
481 mergedPackageStats->
482 insert(std::pair<std::string, BundleActivePackageStats>(packageStat.bundleName_, packageStat));
483 }
484 }
485 return mergedPackageStats;
486 }
487
MergePackageStats(BundleActivePackageStats & left,const BundleActivePackageStats & right)488 void BundleStateCommon::MergePackageStats(BundleActivePackageStats &left, const BundleActivePackageStats &right)
489 {
490 if (left.bundleName_ != right.bundleName_) {
491 BUNDLE_ACTIVE_LOGE("Merge package stats failed, existing packageName : %{public}s,"
492 " new packageName : %{public}s,", left.bundleName_.c_str(), right.bundleName_.c_str());
493 return;
494 }
495 left.lastTimeUsed_ = std::max(left.lastTimeUsed_, right.lastTimeUsed_);
496 left.lastContiniousTaskUsed_ = std::max(left.lastContiniousTaskUsed_, right.lastContiniousTaskUsed_);
497 left.totalInFrontTime_ += right.totalInFrontTime_;
498 left.totalContiniousTaskUsedTime_ += right.totalContiniousTaskUsedTime_;
499 left.bundleStartedCount_ += right.bundleStartedCount_;
500 }
501
HandleEventStatsInfo(const napi_env & env,AsyncCallbackInfoEventStats * asyncCallbackInfo,EventStatesParamsInfo & params)502 std::unique_ptr<AsyncCallbackInfoEventStats> BundleStateCommon::HandleEventStatsInfo(const napi_env &env,
503 AsyncCallbackInfoEventStats *asyncCallbackInfo, EventStatesParamsInfo ¶ms)
504 {
505 if (!asyncCallbackInfo) {
506 params.errorCode = ERR_ASYNC_CALLBACK_NULLPTR;
507 BundleStateCommon::HandleParamErr(env, ERR_ASYNC_CALLBACK_NULLPTR, "");
508 return nullptr;
509 }
510 if (memset_s(asyncCallbackInfo, sizeof(*asyncCallbackInfo), 0, sizeof(*asyncCallbackInfo)) != EOK) {
511 params.errorCode = ERR_ASYNC_CALLBACK_INIT_FAILED;
512 BundleStateCommon::HandleParamErr(env, ERR_ASYNC_CALLBACK_INIT_FAILED, "");
513 delete asyncCallbackInfo;
514 asyncCallbackInfo = nullptr;
515 return nullptr;
516 }
517 std::unique_ptr<AsyncCallbackInfoEventStats> callbackPtr {asyncCallbackInfo};
518 callbackPtr->beginTime = params.beginTime;
519 callbackPtr->endTime = params.endTime;
520 BUNDLE_ACTIVE_LOGI("CallbackPtr->beginTime: %{public}lld, callbackPtr->endTime: %{public}lld",
521 (long long)callbackPtr->beginTime, (long long)callbackPtr->endTime);
522 return callbackPtr;
523 }
524 } // namespace DeviceUsageStats
525 } // namespace OHOS
526
527