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_data.h"
20 #include "bundle_state_common.h"
21
22 namespace OHOS {
23 namespace DeviceUsageStats {
NapiGetNull(napi_env env)24 napi_value BundleStateCommon::NapiGetNull(napi_env env)
25 {
26 napi_value result = nullptr;
27 napi_get_null(env, &result);
28 return result;
29 }
30
GetCallbackPromiseResult(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)31 void BundleStateCommon::GetCallbackPromiseResult(const napi_env &env,
32 const CallbackPromiseInfo &info, const napi_value &result)
33 {
34 if (info.isCallback) {
35 SetCallbackInfo(env, info.callback, info.errorCode, result);
36 } else {
37 SetPromiseInfo(env, info.deferred, result, info.errorCode);
38 }
39 }
40
SetCallbackInfo(const napi_env & env,const napi_ref & callbackIn,const int & errorCode,const napi_value & result)41 void BundleStateCommon::SetCallbackInfo(
42 const napi_env &env, const napi_ref &callbackIn, const int &errorCode, const napi_value &result)
43 {
44 napi_value undefined = nullptr;
45 napi_get_undefined(env, &undefined);
46
47 napi_value callback = nullptr;
48 napi_value resultout = nullptr;
49 napi_get_reference_value(env, callbackIn, &callback);
50 napi_value results[ARGS_TWO] = {nullptr};
51 results[PARAM_FIRST] = GetErrorValue(env, errorCode);
52 results[PARAM_SECOND] = result;
53 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &results[PARAM_FIRST],
54 &resultout));
55 }
56
GetBundleActiveEventForResult(napi_env env,const std::vector<BundleActiveEvent> & bundleActiveStates,napi_value result)57 void BundleStateCommon::GetBundleActiveEventForResult(
58 napi_env env, const std::vector<BundleActiveEvent> &bundleActiveStates, napi_value result)
59 {
60 int32_t index = 0;
61 for (const auto &item : bundleActiveStates) {
62 napi_value bundleActiveState = nullptr;
63 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &bundleActiveState));
64
65 napi_value bundleName = nullptr;
66 NAPI_CALL_RETURN_VOID(
67 env, napi_create_string_utf8(env, item.bundleName_.c_str(), NAPI_AUTO_LENGTH, &bundleName));
68 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, bundleActiveState, "bundleName", bundleName));
69
70 napi_value stateType = nullptr;
71 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, item.eventId_, &stateType));
72 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, bundleActiveState, "stateType", stateType));
73
74 napi_value stateOccurredTime = nullptr;
75 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.timeStamp_, &stateOccurredTime));
76 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, bundleActiveState, "stateOccurredTime",
77 stateOccurredTime));
78
79 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, result, index, bundleActiveState));
80 index++;
81 }
82 }
83
GetBundleStateInfoByIntervalForResult(napi_env env,const std::vector<BundleActivePackageStats> & packageStats,napi_value result)84 void BundleStateCommon::GetBundleStateInfoByIntervalForResult(
85 napi_env env, const std::vector<BundleActivePackageStats> &packageStats, napi_value result)
86 {
87 int32_t index = 0;
88 for (const auto &item : packageStats) {
89 napi_value packageObject = nullptr;
90 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &packageObject));
91
92 napi_value bundleName = nullptr;
93 NAPI_CALL_RETURN_VOID(
94 env, napi_create_string_utf8(env, item.bundleName_.c_str(), NAPI_AUTO_LENGTH, &bundleName));
95 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "bundleName", bundleName));
96
97 napi_value abilityPrevAccessTime = nullptr;
98 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.lastTimeUsed_, &abilityPrevAccessTime));
99 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "abilityPrevAccessTime",
100 abilityPrevAccessTime));
101
102 napi_value abilityInFgTotalTime = nullptr;
103 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.totalInFrontTime_, &abilityInFgTotalTime));
104 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "abilityInFgTotalTime",
105 abilityInFgTotalTime));
106
107 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, result, index, packageObject));
108 index++;
109 }
110 }
111
GetBundleStateInfoForResult(napi_env env,const std::shared_ptr<std::map<std::string,BundleActivePackageStats>> & packageStats,napi_value result)112 void BundleStateCommon::GetBundleStateInfoForResult(napi_env env,
113 const std::shared_ptr<std::map<std::string, BundleActivePackageStats>> &packageStats, napi_value result)
114 {
115 if (packageStats == nullptr) {
116 BUNDLE_ACTIVE_LOGE("PackageStats is invalid");
117 return;
118 }
119 for (const auto &item : *packageStats) {
120 napi_value packageObject = nullptr;
121 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &packageObject));
122 napi_value bundleName = nullptr;
123 NAPI_CALL_RETURN_VOID(
124 env, napi_create_string_utf8(env, item.second.bundleName_.c_str(), NAPI_AUTO_LENGTH, &bundleName));
125 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "bundleName", bundleName));
126
127 napi_value abilityPrevAccessTime = nullptr;
128 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.second.lastTimeUsed_, &abilityPrevAccessTime));
129 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "abilityPrevAccessTime",
130 abilityPrevAccessTime));
131
132 napi_value abilityInFgTotalTime = nullptr;
133 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, item.second.totalInFrontTime_, &abilityInFgTotalTime));
134 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, packageObject, "abilityInFgTotalTime",
135 abilityInFgTotalTime));
136
137 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, item.first.c_str(), packageObject));
138 }
139 }
140
SetPromiseInfo(const napi_env & env,const napi_deferred & deferred,const napi_value & result,const int & errorCode)141 void BundleStateCommon::SetPromiseInfo(const napi_env &env, const napi_deferred &deferred,
142 const napi_value &result, const int &errorCode)
143 {
144 if (errorCode == ERR_OK) {
145 napi_resolve_deferred(env, deferred, result);
146 } else {
147 napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode));
148 }
149 }
150
GetErrorValue(napi_env env,int errCode)151 napi_value BundleStateCommon::GetErrorValue(napi_env env, int errCode)
152 {
153 if (errCode == ERR_OK) {
154 return NapiGetNull(env);
155 }
156 napi_value result = nullptr;
157 napi_value eCode = nullptr;
158 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
159 NAPI_CALL(env, napi_create_object(env, &result));
160 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
161 return result;
162 }
163
JSParaError(const napi_env & env,const napi_ref & callback,const int & errorCode)164 napi_value BundleStateCommon::JSParaError(const napi_env &env, const napi_ref &callback, const int &errorCode)
165 {
166 if (callback) {
167 napi_value result = nullptr;
168 napi_create_array(env, &result);
169 SetCallbackInfo(env, callback, errorCode, result);
170 return result;
171 } else {
172 napi_value promise = nullptr;
173 napi_deferred deferred = nullptr;
174 napi_create_promise(env, &deferred, &promise);
175 napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode));
176 return promise;
177 }
178 }
179
GetTypeStringValue(napi_env env,napi_value param,const std::string & result)180 std::string BundleStateCommon::GetTypeStringValue(napi_env env, napi_value param, const std::string &result)
181 {
182 size_t size = 0;
183 if (napi_get_value_string_utf8(env, param, nullptr, 0, &size) != BUNDLE_STATE_OK) {
184 return result;
185 }
186
187 std::string value("");
188 if (size == 0) {
189 return result;
190 }
191
192 char *buf = new (std::nothrow) char[size + 1];
193 if (buf == nullptr) {
194 return value;
195 }
196
197 if (memset_s(buf, size + 1, 0, size + 1) != EOK) {
198 delete[] buf;
199 buf = nullptr;
200 return value;
201 }
202
203 bool rev = napi_get_value_string_utf8(env, param, buf, size + 1, &size) == BUNDLE_STATE_OK;
204 if (rev) {
205 value = buf;
206 } else {
207 value = result;
208 }
209
210 delete[] buf;
211 buf = nullptr;
212 return value;
213 }
214
GetInt64NumberValue(const napi_env & env,const napi_value & value,int64_t & result)215 napi_value BundleStateCommon::GetInt64NumberValue(const napi_env &env, const napi_value &value, int64_t &result)
216 {
217 napi_valuetype valuetype = napi_undefined;
218
219 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
220 if (valuetype != napi_number) {
221 BUNDLE_ACTIVE_LOGE("Wrong argument type, number expected.");
222 return nullptr;
223 }
224 napi_get_value_int64(env, value, &result);
225 return BundleStateCommon::NapiGetNull(env);
226 }
227
GetInt32NumberValue(const napi_env & env,const napi_value & value,int32_t & result)228 napi_value BundleStateCommon::GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result)
229 {
230 napi_valuetype valuetype = napi_undefined;
231 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
232 if (valuetype != napi_number) {
233 BUNDLE_ACTIVE_LOGE("Wrong argument type. Number expected.");
234 return nullptr;
235 }
236 napi_get_value_int32(env, value, &result);
237 return BundleStateCommon::NapiGetNull(env);
238 }
239
SettingCallbackPromiseInfo(const napi_env & env,const napi_ref & callback,CallbackPromiseInfo & info,napi_value & promise)240 void BundleStateCommon::SettingCallbackPromiseInfo(
241 const napi_env &env, const napi_ref &callback, CallbackPromiseInfo &info, napi_value &promise)
242 {
243 if (callback) {
244 info.callback = callback;
245 info.isCallback = true;
246 } else {
247 napi_deferred deferred = nullptr;
248 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
249 info.deferred = deferred;
250 info.isCallback = false;
251 }
252 }
253
GetPackageStats(int64_t & beginTime,int64_t & endTime,int32_t & errCode)254 std::shared_ptr<std::map<std::string, BundleActivePackageStats>> BundleStateCommon::GetPackageStats(
255 int64_t &beginTime, int64_t &endTime, int32_t &errCode)
256 {
257 std::vector<BundleActivePackageStats> packageStats =
258 BundleActiveClient::GetInstance().QueryPackageStats(INTERVAL_TYPE_DEFAULT, beginTime, endTime, errCode);
259 std::shared_ptr<std::map<std::string, BundleActivePackageStats>> mergedPackageStats =
260 std::make_shared<std::map<std::string, BundleActivePackageStats>>();
261 if (packageStats.empty()) {
262 return nullptr;
263 }
264 for (auto packageStat : packageStats) {
265 std::map<std::string, BundleActivePackageStats>::iterator iter =
266 mergedPackageStats->find(packageStat.bundleName_);
267 if (iter != mergedPackageStats->end()) {
268 MergePackageStats(iter->second, packageStat);
269 } else {
270 mergedPackageStats->
271 insert(std::pair<std::string, BundleActivePackageStats>(packageStat.bundleName_, packageStat));
272 }
273 }
274 return mergedPackageStats;
275 }
276
MergePackageStats(BundleActivePackageStats & left,const BundleActivePackageStats & right)277 void BundleStateCommon::MergePackageStats(BundleActivePackageStats &left, const BundleActivePackageStats &right)
278 {
279 if (left.bundleName_ != right.bundleName_) {
280 BUNDLE_ACTIVE_LOGE("Merge package stats failed, existing packageName : %{public}s,"
281 " new packageName : %{public}s,", left.bundleName_.c_str(), right.bundleName_.c_str());
282 return;
283 }
284 left.lastTimeUsed_ = std::max(left.lastTimeUsed_, right.lastTimeUsed_);
285 left.lastContiniousTaskUsed_ = std::max(left.lastContiniousTaskUsed_, right.lastContiniousTaskUsed_);
286 left.totalInFrontTime_ += right.totalInFrontTime_;
287 left.totalContiniousTaskUsedTime_ += right.totalContiniousTaskUsedTime_;
288 left.bundleStartedCount_ += right.bundleStartedCount_;
289 }
290 } // namespace DeviceUsageStats
291 } // namespace OHOS