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 #ifndef OHOS_APPEXECFWK_LIBZIP_ZLIB_CALLBACK_H
16 #define OHOS_APPEXECFWK_LIBZIP_ZLIB_CALLBACK_H
17 
18 #include <uv.h>
19 
20 #include "appexecfwk_errors.h"
21 #include "base_cb_info.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "napi/native_common.h"
25 #include "nocopyable.h"
26 #include "zip_utils.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace LIBZIP {
31 class ZlibCallbackInfo {
32 public:
33     ZlibCallbackInfo() = default;
34     ZlibCallbackInfo(napi_env env, napi_ref callback, napi_deferred deferred, bool isCallback);
35     virtual ~ZlibCallbackInfo();
36     void OnZipUnZipFinish(ErrCode result);
37     bool GetIsCallback() const;
38     void SetIsCallback(bool isCallback);
39     void SetCallback(napi_ref callback);
40     void SetDeferred(napi_deferred deferred);
41     void SetDeliverErrCode(bool isDeliverErrCode);
42     void SetValid(bool valid);
43 private:
44     int32_t ExcuteWork(uv_loop_s* loop, uv_work_t* work);
45 private:
46     napi_env env_ = nullptr;
47     napi_ref callback_ = nullptr;
48     napi_deferred deferred_ = nullptr;
49     bool isCallBack_ = false;
50     bool deliverErrcode_ = false;
51     bool valid_ = true;
52     std::mutex validMutex_;
53     DISALLOW_COPY_AND_MOVE(ZlibCallbackInfo);
54 };
55 
56 struct AsyncCallbackInfo {
57     napi_env env;
58     napi_ref callback;
59     napi_deferred deferred;
60     bool isCallBack = false;
61     ErrCode callbackResult;
62     bool deliverErrcode = false;
63     ZlibCallbackInfo *data = nullptr;
64 };
65 
66 struct OriginalSizeCallbackInfo : public BaseCallbackInfo {
OriginalSizeCallbackInfoOriginalSizeCallbackInfo67     explicit OriginalSizeCallbackInfo(napi_env napiEnv) : BaseCallbackInfo(napiEnv) {}
68     std::string srcFile = "";
69     int64_t originalSize = 0;
70 };
71 }  // namespace LIBZIP
72 }  // namespace AppExecFwk
73 }  // namespace OHOS
74 #endif  // OHOS_APPEXECFWK_LIBZIP_ZLIB_CALLBACK_H