• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void CleanRef();
34 private:
35     napi_env env_;
36     LibN::NAsyncContextCallback *ctx_ = nullptr;
37 };
38 
39 struct WorkArgs {
40     std::mutex callbackMutex;
41     std::condition_variable callbackCondition;
42     std::atomic<bool> isReady {false};
43     BackupRestoreCallback *ptr = nullptr;
44     InputArgsParser argParser;
45 };
46 
47 class GeneralCallbacks {
48 public:
GeneralCallbacks(const napi_env & env,const LibN::NVal & thisPtr,const LibN::NVal & jsCallbacks)49     GeneralCallbacks(const napi_env &env, const LibN::NVal &thisPtr, const LibN::NVal &jsCallbacks)
50         : onFileReady(env, thisPtr, jsCallbacks.GetProp("onFileReady")),
51           onBundleBegin(env, thisPtr, jsCallbacks.GetProp("onBundleBegin")),
52           onBundleEnd(env, thisPtr, jsCallbacks.GetProp("onBundleEnd")),
53           onAllBundlesEnd(env, thisPtr, jsCallbacks.GetProp("onAllBundlesEnd")),
54           onBackupServiceDied(env, thisPtr, jsCallbacks.GetProp("onBackupServiceDied")),
55           onResultReport(env, thisPtr, jsCallbacks.GetProp("onResultReport")),
56           onProcess(env, thisPtr, jsCallbacks.GetProp("onProcess")),
57           onBackupSizeReport(env, thisPtr, jsCallbacks.GetProp("onBackupSizeReport")) {};
58 public:
59     void RemoveCallbackRef();
60 public:
61     LibN::NAsyncWorkCallback onFileReady;
62     LibN::NAsyncWorkCallback onBundleBegin;
63     LibN::NAsyncWorkCallback onBundleEnd;
64     LibN::NAsyncWorkCallback onAllBundlesEnd;
65     BackupRestoreCallback onBackupServiceDied;
66     BackupRestoreCallback onResultReport;
67     BackupRestoreCallback onProcess;
68     BackupRestoreCallback onBackupSizeReport;
69 };
70 } // namespace OHOS::FileManagement::Backup
71 #endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_GENERAL_CALLBACKS_H