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_COMMON_H
17 #define FOUNDATION_RESOURCESCHEDULE_DEVICE_USAGE_STATISTICS_BUNDLE_STATE_COMMON_H
18 
19 #include "bundle_active_client.h"
20 #include "bundle_state_data.h"
21 #include "bundle_state_query.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "bundle_state_inner_errors.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28 const std::int32_t STR_MAX_SIZE = 64;
29 class BundleStateCommon {
30 public:
31     static napi_value NapiGetboolean(napi_env env, const bool &isValue);
32 
33     static napi_value NapiGetNull(napi_env env);
34 
35     static napi_value NapiGetUndefined(napi_env env);
36 
37     static napi_value HandleParamErr(const napi_env &env, int32_t errCode, const std::string& operation);
38 
39     static napi_value GetErrorValue(napi_env env, int32_t errCode);
40 
41     static std::string GetSaErrCodeMsg(int32_t errCode, int32_t reflectCode);
42 
43     static int32_t GetReflectErrCode(int32_t errCode);
44 
45     static void SettingAsyncWorkData(
46         const napi_env &env, const napi_ref &callback, AsyncWorkData &workData, napi_value &promise);
47 
48     static void GetCallbackPromiseResult(const napi_env &env, const AsyncWorkData &workData, const napi_value &result);
49 
50     static void SetCallbackInfo(
51         const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, const napi_value &result);
52 
53     static void GetBundleActiveEventForResult(
54         napi_env env, const std::vector<BundleActiveEvent> &BundleActiveState, napi_value result, bool isNewVersion);
55 
56     static void GetBundleStateInfoByIntervalForResult(
57         napi_env env, const std::vector<BundleActivePackageStats> &packageStats, napi_value result);
58 
59     static void GetBundleActiveEventStatsForResult(napi_env env,
60         const std::vector<BundleActiveEventStats> &eventStats, napi_value result);
61 
62     static void GetBundleActiveNotificationNumberForResult(napi_env env,
63         const std::vector<BundleActiveEventStats> &eventStats, napi_value result);
64 
65     static void GetBundleStateInfoForResult(napi_env env,
66         const std::shared_ptr<std::map<std::string, BundleActivePackageStats>> &packageStats, napi_value result);
67 
68     static void GetModuleRecordForResult(napi_env env,
69         const std::vector<BundleActiveModuleRecord> &moduleRecords, napi_value result);
70 
71     static void GetModuleRecordBasicForResult(napi_env env,
72         const BundleActiveModuleRecord &moduleRecords, napi_value moduleObject);
73 
74     static void SetPromiseInfo(const napi_env &env, const napi_deferred &deferred,
75         const napi_value &result, const int32_t &errorCode);
76 
77     static napi_value JSParaError(const napi_env &env, const napi_ref &callback, const int32_t &errorCode);
78 
79     static std::string GetTypeStringValue(napi_env env, napi_value param, const std::string &result);
80 
81     static napi_value GetInt64NumberValue(const napi_env &env, const napi_value &value, int64_t &result);
82 
83     static napi_value GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result);
84 
85     static std::shared_ptr<std::map<std::string, BundleActivePackageStats>> QueryBundleStatsInfos(
86 	    int64_t &beginTime, int64_t &endTime, int32_t &errCode);
87 
88     static void MergePackageStats(BundleActivePackageStats &left, const BundleActivePackageStats &right);
89 
90     static std::unique_ptr<AsyncCallbackInfoEventStats> HandleEventStatsInfo(const napi_env &env,
91         AsyncCallbackInfoEventStats *asyncCallbackInfo, EventStatesParamsInfo &params);
92 
93     template <typename PARAMT, typename ASYNCT>
94     static void AsyncInit(napi_env env, PARAMT &params, ASYNCT* &asyncCallbackInfo);
95 };
96 
97 template <typename PARAMT, typename ASYNCT>
AsyncInit(napi_env env,PARAMT & params,ASYNCT * & asyncCallbackInfo)98 void BundleStateCommon::AsyncInit(napi_env env, PARAMT &params, ASYNCT* &asyncCallbackInfo)
99 {
100     if (params.errorCode != ERR_OK) {
101         return;
102     }
103     asyncCallbackInfo = new (std::nothrow) ASYNCT(env);
104     if (!asyncCallbackInfo) {
105         params.errorCode = ERR_ASYNC_CALLBACK_NULLPTR;
106         BundleStateCommon::HandleParamErr(env, ERR_ASYNC_CALLBACK_NULLPTR, "");
107         return;
108     }
109     if (memset_s(asyncCallbackInfo, sizeof(*asyncCallbackInfo), 0, sizeof(*asyncCallbackInfo))
110         != EOK) {
111         params.errorCode = ERR_ASYNC_CALLBACK_INIT_FAILED;
112         BundleStateCommon::HandleParamErr(env, ERR_ASYNC_CALLBACK_INIT_FAILED, "");
113         delete asyncCallbackInfo;
114         asyncCallbackInfo = nullptr;
115     }
116 }
117 }  // namespace DeviceUsageStats
118 }  // namespace OHOS
119 #endif  // FOUNDATION_RESOURCESCHEDULE_DEVICE_USAGE_STATISTICS_BUNDLE_STATE_COMMON_H
120 
121