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 using namespace std;
31
InstantiateChecksum(napi_env env)32 static NapiValue InstantiateChecksum(napi_env env)
33 {
34 napi_value objChecksum = NapiClass::InstantiateClass(env, ChecksumNExporter::className_, {});
35 if (!objChecksum) {
36 NapiBusinessError(EFAULT, true).ThrowErr(env, "INNER BUG. Cannot instantiate stream");
37 return NapiValue();
38 }
39
40 return { env, objChecksum };
41 }
42
Sync(napi_env env,napi_callback_info info)43 napi_value CreateChecksum::Sync(napi_env env, napi_callback_info info)
44 {
45 return InstantiateChecksum(env).val_;
46 }
47
Async(napi_env env,napi_callback_info info)48 napi_value CreateChecksum::Async(napi_env env, napi_callback_info info)
49 {
50 NapiFuncArg funcArg(env, info);
51 if (!funcArg.InitArgs(ArgumentCount::ZERO, ArgumentCount::ONE)) {
52 APP_LOGE("Number of arguments unmatched");
53 NapiBusinessError(EINVAL, true).ThrowErr(env);
54 return nullptr;
55 }
56
57 auto cbExec = [](napi_env env) -> NapiBusinessError {
58 return NapiBusinessError(ERRNO_NOERR);
59 };
60
61 auto cbCompl = [](napi_env env, NapiBusinessError err) -> NapiValue {
62 if (err) {
63 return { env, err.GetNapiErr(env) };
64 }
65 return InstantiateChecksum(env);
66 };
67
68 NapiValue thisVar(env, funcArg.GetThisVar());
69 if (funcArg.GetArgc() == ArgumentCount::ZERO) {
70 return NapiAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_CREATE_CHECKSUM_NAME, cbExec, cbCompl).val_;
71 } else {
72 NapiValue cb(env, funcArg[ArgumentPosition::FIRST]);
73 return NapiAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_CREATE_CHECKSUM_NAME, cbExec, cbCompl).val_;
74 }
75 }
76 } // namespace LIBZIP
77 } // namespace AppExecFwk
78 } // namespace OHOS