1 /*
2 * Copyright (c) 2021 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 "storage_statistics_n_exporter.h"
17
18 #include <singleton.h>
19 #include <tuple>
20
21 #include "n_async/n_async_work_callback.h"
22 #include "n_async/n_async_work_promise.h"
23 #include "n_class.h"
24 #include "n_func_arg.h"
25 #include "n_val.h"
26 #include "n_error.h"
27 #include "storage_manager_connect.h"
28 #include "storage_service_log.h"
29 #include "storage_service_errno.h"
30 #include "storage_statistics_napi.h"
31
32 using namespace OHOS::FileManagement::LibN;
33
34 namespace OHOS {
35 namespace StorageManager {
GetTotalSizeOfVolume(napi_env env,napi_callback_info info)36 napi_value GetTotalSizeOfVolume(napi_env env, napi_callback_info info)
37 {
38 if (!IsSystemApp()) {
39 NError(E_PERMISSION_SYS).ThrowErr(env);
40 return nullptr;
41 }
42 NFuncArg funcArg(env, info);
43 if (!funcArg.InitArgs((int)NARG_CNT::ONE, (int)NARG_CNT::TWO)) {
44 NError(E_PARAMS).ThrowErr(env);
45 return nullptr;
46 }
47
48 bool succ = false;
49 std::unique_ptr<char []> uuid;
50 tie(succ, uuid, std::ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String();
51 if (!succ) {
52 NError(E_PARAMS).ThrowErr(env);
53 return nullptr;
54 }
55
56 auto resultSize = std::make_shared<int64_t>();
57 std::string uuidString(uuid.get());
58 auto cbExec = [uuidString, resultSize]() -> NError {
59 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetTotalSizeOfVolume(uuidString,
60 *resultSize);
61 if (errNum != E_OK) {
62 return NError(Convert2JsErrNum(errNum));
63 }
64 return NError(ERRNO_NOERR);
65 };
66
67 auto cbComplete = [resultSize](napi_env env, NError err) -> NVal {
68 if (err) {
69 return { env, err.GetNapiErr(env) };
70 }
71 return NVal::CreateInt64(env, *resultSize);
72 };
73
74 std::string procedureName = "GetTotalSizeOfVolume";
75 NVal thisVar(env, funcArg.GetThisVar());
76 if (funcArg.GetArgc() == (uint)NARG_CNT::ONE) {
77 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
78 } else {
79 NVal cb(env, funcArg[(int)NARG_POS::SECOND]);
80 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
81 }
82 }
83
84
GetFreeSizeOfVolume(napi_env env,napi_callback_info info)85 napi_value GetFreeSizeOfVolume(napi_env env, napi_callback_info info)
86 {
87 if (!IsSystemApp()) {
88 NError(E_PERMISSION_SYS).ThrowErr(env);
89 return nullptr;
90 }
91 NFuncArg funcArg(env, info);
92 if (!funcArg.InitArgs((int)NARG_CNT::ONE, (int)NARG_CNT::TWO)) {
93 NError(E_PARAMS).ThrowErr(env);
94 return nullptr;
95 }
96
97 bool succ = false;
98 std::unique_ptr<char []> uuid;
99 tie(succ, uuid, std::ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String();
100 if (!succ) {
101 NError(E_PARAMS).ThrowErr(env);
102 return nullptr;
103 }
104
105 auto resultSize = std::make_shared<int64_t>();
106 std::string uuidString(uuid.get());
107 auto cbExec = [uuidString, resultSize]() -> NError {
108 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetFreeSizeOfVolume(uuidString,
109 *resultSize);
110 if (errNum != E_OK) {
111 return NError(Convert2JsErrNum(errNum));
112 }
113 return NError(ERRNO_NOERR);
114 };
115 auto cbComplete = [resultSize](napi_env env, NError err) -> NVal {
116 if (err) {
117 return { env, err.GetNapiErr(env) };
118 }
119 return { NVal::CreateInt64(env, *resultSize) };
120 };
121
122 std::string procedureName = "getFreeSizeOfVolume";
123 NVal thisVar(env, funcArg.GetThisVar());
124 if (funcArg.GetArgc() == (uint)NARG_CNT::ONE) {
125 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
126 } else {
127 NVal cb(env, funcArg[(int)NARG_POS::SECOND]);
128 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
129 }
130 }
131
GetBundleStats(napi_env env,napi_callback_info info)132 napi_value GetBundleStats(napi_env env, napi_callback_info info)
133 {
134 if (!IsSystemApp()) {
135 NError(E_PERMISSION_SYS).ThrowErr(env);
136 return nullptr;
137 }
138 NFuncArg funcArg(env, info);
139 if (!funcArg.InitArgs((int)NARG_CNT::ONE, (int)NARG_CNT::TWO)) {
140 NError(E_PARAMS).ThrowErr(env);
141 return nullptr;
142 }
143
144 bool succ = false;
145 std::unique_ptr<char []> name;
146 tie(succ, name, std::ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String();
147 if (!succ) {
148 NError(E_PARAMS).ThrowErr(env);
149 return nullptr;
150 }
151 auto bundleStats = std::make_shared<BundleStats>();
152 std::string nameString(name.get());
153 auto cbExec = [nameString, bundleStats]() -> NError {
154 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetBundleStats(nameString,
155 *bundleStats);
156 if (errNum != E_OK) {
157 return NError(Convert2JsErrNum(errNum));
158 }
159 return NError(ERRNO_NOERR);
160 };
161 auto cbComplete = [bundleStats](napi_env env, NError err) -> NVal {
162 if (err) {
163 return { env, err.GetNapiErr(env) };
164 }
165 NVal bundleObject = NVal::CreateObject(env);
166 bundleObject.AddProp("appSize", NVal::CreateInt64(env, (bundleStats->appSize_)).val_);
167 bundleObject.AddProp("cacheSize", NVal::CreateInt64(env,
168 (bundleStats->cacheSize_)).val_);
169 bundleObject.AddProp("dataSize", NVal::CreateInt64(env, (bundleStats->dataSize_)).val_);
170 return bundleObject;
171 };
172 std::string procedureName = "GetBundleStats";
173 NVal thisVar(env, funcArg.GetThisVar());
174 if (funcArg.GetArgc() == (uint)NARG_CNT::ONE) {
175 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
176 } else {
177 NVal cb(env, funcArg[(int)NARG_POS::SECOND]);
178 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
179 }
180 }
181
GetCurrentBundleStats(napi_env env,napi_callback_info info)182 napi_value GetCurrentBundleStats(napi_env env, napi_callback_info info)
183 {
184 NFuncArg funcArg(env, info);
185 if (!funcArg.InitArgs((int)NARG_CNT::ZERO, (int)NARG_CNT::ONE)) {
186 NError(E_PARAMS).ThrowErr(env);
187 return nullptr;
188 }
189 auto bundleStats = std::make_shared<BundleStats>();
190 auto cbExec = [bundleStats]() -> NError {
191 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetCurrentBundleStats(*bundleStats);
192 if (errNum != E_OK) {
193 return NError(Convert2JsErrNum(errNum));
194 }
195 return NError(ERRNO_NOERR);
196 };
197 auto cbComplete = [bundleStats](napi_env env, NError err) -> NVal {
198 if (err) {
199 return { env, err.GetNapiErr(env) };
200 }
201 NVal bundleObject = NVal::CreateObject(env);
202 bundleObject.AddProp("appSize", NVal::CreateInt64(env, (bundleStats->appSize_)).val_);
203 bundleObject.AddProp("cacheSize", NVal::CreateInt64(env,
204 (bundleStats->cacheSize_)).val_);
205 bundleObject.AddProp("dataSize", NVal::CreateInt64(env, (bundleStats->dataSize_)).val_);
206 return bundleObject;
207 };
208 std::string procedureName = "GetCurrentBundleStats";
209 NVal thisVar(env, funcArg.GetThisVar());
210 if (funcArg.GetArgc() == (uint)NARG_CNT::ZERO) {
211 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
212 } else {
213 NVal cb(env, funcArg[(int)NARG_POS::FIRST]);
214 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
215 }
216 }
217
GetSystemSize(napi_env env,napi_callback_info info)218 napi_value GetSystemSize(napi_env env, napi_callback_info info)
219 {
220 if (!IsSystemApp()) {
221 NError(E_PERMISSION_SYS).ThrowErr(env);
222 return nullptr;
223 }
224 NFuncArg funcArg(env, info);
225 if (!funcArg.InitArgs((int)NARG_CNT::ZERO, (int)NARG_CNT::ONE)) {
226 NError(E_PARAMS).ThrowErr(env);
227 return nullptr;
228 }
229
230 auto resultSize = std::make_shared<int64_t>();
231 auto cbExec = [resultSize]() -> NError {
232 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetSystemSize(*resultSize);
233 if (errNum != E_OK) {
234 return NError(Convert2JsErrNum(errNum));
235 }
236 return NError(ERRNO_NOERR);
237 };
238 auto cbComplete = [resultSize](napi_env env, NError err) -> NVal {
239 if (err) {
240 return { env, err.GetNapiErr(env) };
241 }
242 return { NVal::CreateInt64(env, *resultSize) };
243 };
244
245 std::string procedureName = "GetSystemSize";
246 NVal thisVar(env, funcArg.GetThisVar());
247 if (funcArg.GetArgc() == (uint)NARG_CNT::ZERO) {
248 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
249 } else {
250 NVal cb(env, funcArg[(int)NARG_POS::FIRST]);
251 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
252 }
253 }
254
GetUserStorageStats(napi_env env,napi_callback_info info)255 napi_value GetUserStorageStats(napi_env env, napi_callback_info info)
256 {
257 if (!IsSystemApp()) {
258 NError(E_PERMISSION_SYS).ThrowErr(env);
259 return nullptr;
260 }
261 NFuncArg funcArg(env, info);
262 if (!funcArg.InitArgs((int)NARG_CNT::ZERO, (int)NARG_CNT::TWO)) {
263 NError(E_PARAMS).ThrowErr(env);
264 return nullptr;
265 }
266 bool fac = false;
267 int64_t userId = -1;
268 if (funcArg.GetArgc() >= 1) {
269 NVal ui(env, NVal(env, funcArg[(int)NARG_POS::FIRST]).val_);
270 if (ui.TypeIs(napi_number)) {
271 bool succ = false;
272 std::tie(succ, userId) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToInt64();
273 if (!succ) {
274 NError(E_PARAMS).ThrowErr(env);
275 return nullptr;
276 }
277 fac = true;
278 }
279 }
280
281 auto storageStats = std::make_shared<StorageStats>();
282 auto cbExec = [fac, userId, storageStats]() -> NError {
283 int32_t errNum;
284 if (!fac) {
285 errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetUserStorageStats(*storageStats);
286 } else {
287 errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetUserStorageStats(userId, *storageStats);
288 }
289 if (errNum != E_OK) {
290 return NError(Convert2JsErrNum(errNum));
291 }
292 return NError(ERRNO_NOERR);
293 };
294 auto cbComplete = [storageStats](napi_env env, NError err) -> NVal {
295 if (err) {
296 return { env, err.GetNapiErr(env) };
297 }
298 NVal totalObject = NVal::CreateObject(env);
299 totalObject.AddProp("total", NVal::CreateInt64(env, (storageStats->total_)).val_);
300 totalObject.AddProp("audio", NVal::CreateInt64(env, (storageStats->audio_)).val_);
301 totalObject.AddProp("video", NVal::CreateInt64(env, (storageStats->video_)).val_);
302 totalObject.AddProp("image", NVal::CreateInt64(env, (storageStats->image_)).val_);
303 totalObject.AddProp("file", NVal::CreateInt64(env, (storageStats->file_)).val_);
304 totalObject.AddProp("app", NVal::CreateInt64(env, (storageStats->app_)).val_);
305 return totalObject;
306 };
307 std::string procedureName = "GetUserStorageStats";
308 NVal thisVar(env, funcArg.GetThisVar());
309 if (funcArg.GetArgc() == (uint)NARG_CNT::ZERO || (funcArg.GetArgc() == (uint)NARG_CNT::ONE && fac)) {
310 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
311 } else {
312 if (!fac) {
313 NVal cb(env, funcArg[(int)NARG_POS::FIRST]);
314 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
315 }
316 NVal cb(env, funcArg[(int)NARG_POS::SECOND]);
317 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
318 }
319 }
320
GetTotalSize(napi_env env,napi_callback_info info)321 napi_value GetTotalSize(napi_env env, napi_callback_info info)
322 {
323 if (!IsSystemApp()) {
324 NError(E_PERMISSION_SYS).ThrowErr(env);
325 return nullptr;
326 }
327 NFuncArg funcArg(env, info);
328 if (!funcArg.InitArgs((int)NARG_CNT::ZERO, (int)NARG_CNT::ONE)) {
329 NError(E_PARAMS).ThrowErr(env);
330 return nullptr;
331 }
332
333 auto resultSize = std::make_shared<int64_t>();
334 auto cbExec = [resultSize]() -> NError {
335 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetTotalSize(*resultSize);
336 if (errNum != E_OK) {
337 return NError(Convert2JsErrNum(errNum));
338 }
339 return NError(ERRNO_NOERR);
340 };
341 auto cbComplete = [resultSize](napi_env env, NError err) -> NVal {
342 if (err) {
343 return { env, err.GetNapiErr(env) };
344 }
345 return { NVal::CreateInt64(env, *resultSize) };
346 };
347
348 std::string procedureName = "GetTotalSize";
349 NVal thisVar(env, funcArg.GetThisVar());
350 if (funcArg.GetArgc() == (uint)NARG_CNT::ZERO) {
351 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
352 } else {
353 NVal cb(env, funcArg[(int)NARG_POS::FIRST]);
354 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
355 }
356 }
357
GetFreeSize(napi_env env,napi_callback_info info)358 napi_value GetFreeSize(napi_env env, napi_callback_info info)
359 {
360 if (!IsSystemApp()) {
361 NError(E_PERMISSION_SYS).ThrowErr(env);
362 return nullptr;
363 }
364 NFuncArg funcArg(env, info);
365 if (!funcArg.InitArgs((int)NARG_CNT::ZERO, (int)NARG_CNT::ONE)) {
366 NError(E_PARAMS).ThrowErr(env);
367 return nullptr;
368 }
369
370 auto resultSize = std::make_shared<int64_t>();
371 auto cbExec = [resultSize]() -> NError {
372 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetFreeSize(*resultSize);
373 if (errNum != E_OK) {
374 return NError(Convert2JsErrNum(errNum));
375 }
376 return NError(ERRNO_NOERR);
377 };
378 auto cbComplete = [resultSize](napi_env env, NError err) -> NVal {
379 if (err) {
380 return { env, err.GetNapiErr(env) };
381 }
382 return { NVal::CreateInt64(env, *resultSize) };
383 };
384
385 std::string procedureName = "GetFreeSize";
386 NVal thisVar(env, funcArg.GetThisVar());
387 if (funcArg.GetArgc() == (uint)NARG_CNT::ZERO) {
388 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
389 } else {
390 NVal cb(env, funcArg[(int)NARG_POS::FIRST]);
391 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
392 }
393 }
394
GetTotalSizeSync(napi_env env,napi_callback_info info)395 napi_value GetTotalSizeSync(napi_env env, napi_callback_info info)
396 {
397 if (!IsSystemApp()) {
398 NError(E_PERMISSION_SYS).ThrowErr(env);
399 return nullptr;
400 }
401 NFuncArg funcArg(env, info);
402 if (!funcArg.InitArgs((int)NARG_CNT::ZERO)) {
403 NError(E_PARAMS).ThrowErr(env);
404 return nullptr;
405 }
406
407 auto resultSize = std::make_shared<int64_t>();
408
409 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetTotalSize(*resultSize);
410 if (errNum != E_OK) {
411 NError(Convert2JsErrNum(errNum)).ThrowErr(env);
412 return nullptr;
413 }
414
415 return NVal::CreateInt64(env, *resultSize).val_;
416 }
417
GetFreeSizeSync(napi_env env,napi_callback_info info)418 napi_value GetFreeSizeSync(napi_env env, napi_callback_info info)
419 {
420 if (!IsSystemApp()) {
421 NError(E_PERMISSION_SYS).ThrowErr(env);
422 return nullptr;
423 }
424 NFuncArg funcArg(env, info);
425 if (!funcArg.InitArgs((int)NARG_CNT::ZERO)) {
426 NError(E_PARAMS).ThrowErr(env);
427 return nullptr;
428 }
429
430 auto resultSize = std::make_shared<int64_t>();
431
432 int32_t errNum = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetFreeSize(*resultSize);
433 if (errNum != E_OK) {
434 NError(Convert2JsErrNum(errNum)).ThrowErr(env);
435 return nullptr;
436 }
437 return NVal::CreateInt64(env, *resultSize).val_;
438 }
439 } // namespace StorageManager
440 } // namespace OHOS
441