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