1 /*
2 * Copyright (c) 2023 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 "create_checksum.h"
17
18 #include <memory>
19 #include <tuple>
20
21 #include "class_checksum/checksum_entity.h"
22 #include "class_checksum/checksum_n_exporter.h"
23 #include "common/common_func.h"
24 #include "common/file_utils.h"
25 #include "app_log_wrapper.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace LIBZIP {
30
InstantiateChecksum(napi_env env)31 static NapiValue InstantiateChecksum(napi_env env)
32 {
33 napi_value objChecksum = NapiClass::InstantiateClass(env, ChecksumNExporter::className_, {});
34 if (!objChecksum) {
35 NapiBusinessError(EFAULT, true).ThrowErr(env, "INNER BUG. Cannot instantiate stream");
36 return NapiValue();
37 }
38
39 return { env, objChecksum };
40 }
41
Sync(napi_env env,napi_callback_info info)42 napi_value CreateChecksum::Sync(napi_env env, napi_callback_info info)
43 {
44 return InstantiateChecksum(env).val_;
45 }
46
Async(napi_env env,napi_callback_info info)47 napi_value CreateChecksum::Async(napi_env env, napi_callback_info info)
48 {
49 NapiFuncArg funcArg(env, info);
50 if (!funcArg.InitArgs(ArgumentCount::ZERO, ArgumentCount::ONE)) {
51 APP_LOGE("Number of arguments unmatched");
52 NapiBusinessError(EINVAL, true).ThrowErr(env);
53 return nullptr;
54 }
55
56 auto cbExec = [](napi_env env) -> NapiBusinessError {
57 return NapiBusinessError(ERRNO_NOERR);
58 };
59
60 auto cbCompl = [](napi_env env, NapiBusinessError err) -> NapiValue {
61 if (err) {
62 return { env, err.GetNapiErr(env) };
63 }
64 return InstantiateChecksum(env);
65 };
66
67 NapiValue thisVar(env, funcArg.GetThisVar());
68 if (funcArg.GetArgc() == ArgumentCount::ZERO) {
69 return NapiAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_CREATE_CHECKSUM_NAME, cbExec, cbCompl).val_;
70 } else {
71 NapiValue cb(env, funcArg[ArgumentPosition::FIRST]);
72 return NapiAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_CREATE_CHECKSUM_NAME, cbExec, cbCompl).val_;
73 }
74 }
75 } // namespace LIBZIP
76 } // namespace AppExecFwk
77 } // namespace OHOS