1 /*
2  * Copyright (c) 2023-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 #ifndef INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_GENERAL_CALLBACKS_H
16 #define INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_GENERAL_CALLBACKS_H
17 
18 #include <memory>
19 #include <node_api.h>
20 #include <string>
21 
22 #include "filemgmt_libn.h"
23 
24 namespace OHOS::FileManagement::Backup {
25 using InputArgsParser = std::function<bool(napi_env, std::vector<napi_value> &)>;
26 
27 class BackupRestoreCallback {
28 public:
29     BackupRestoreCallback(napi_env env, LibN::NVal thisPtr, LibN::NVal cb);
30     ~BackupRestoreCallback();
31     void CallJsMethod(InputArgsParser argParser);
32     explicit operator bool() const;
33 private:
34     napi_env env_;
35     LibN::NAsyncContextCallback *ctx_ = nullptr;
36 };
37 
38 struct WorkArgs {
39     std::mutex callbackMutex;
40     std::condition_variable callbackCondition;
41     std::atomic<bool> isReady {false};
42     BackupRestoreCallback *ptr = nullptr;
43     InputArgsParser argParser;
44 };
45 
46 class GeneralCallbacks {
47 public:
GeneralCallbacks(const napi_env & env,const LibN::NVal & thisPtr,const LibN::NVal & jsCallbacks)48     GeneralCallbacks(const napi_env &env, const LibN::NVal &thisPtr, const LibN::NVal &jsCallbacks)
49         : onFileReady(env, thisPtr, jsCallbacks.GetProp("onFileReady")),
50           onBundleBegin(env, thisPtr, jsCallbacks.GetProp("onBundleBegin")),
51           onBundleEnd(env, thisPtr, jsCallbacks.GetProp("onBundleEnd")),
52           onAllBundlesEnd(env, thisPtr, jsCallbacks.GetProp("onAllBundlesEnd")),
53           onBackupServiceDied(env, thisPtr, jsCallbacks.GetProp("onBackupServiceDied")),
54           onResultReport(env, thisPtr, jsCallbacks.GetProp("onResultReport")),
55           onProcess(env, thisPtr, jsCallbacks.GetProp("onProcess")) {};
56 
57 public:
58     LibN::NAsyncWorkCallback onFileReady;
59     LibN::NAsyncWorkCallback onBundleBegin;
60     LibN::NAsyncWorkCallback onBundleEnd;
61     LibN::NAsyncWorkCallback onAllBundlesEnd;
62     BackupRestoreCallback onBackupServiceDied;
63     BackupRestoreCallback onResultReport;
64     BackupRestoreCallback onProcess;
65 };
66 } // namespace OHOS::FileManagement::Backup
67 #endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_GENERAL_CALLBACKS_H