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 #ifndef FILEMANAGEMENT_FILE_API_CONNECTDFS_H
17 #define FILEMANAGEMENT_FILE_API_CONNECTDFS_H
18 
19 #include <chrono>
20 #include <set>
21 #include <sys/inotify.h>
22 #include <thread>
23 
24 #include "bundle_mgr_client_impl.h"
25 #include "common_func.h"
26 #include "dfs_listener/file_dfs_listener_stub.h"
27 #include "distributed_file_daemon_manager.h"
28 #include "filemgmt_libn.h"
29 #include "n_async/n_ref.h"
30 
31 namespace OHOS {
32 namespace FileManagement {
33 namespace ModuleFileIO {
34 using namespace std;
35 using namespace OHOS::FileManagement::LibN;
36 using namespace OHOS::AppExecFwk;
37 
38 napi_value WrapInt32(napi_env &env, int32_t num, const std::string &paramName);
39 napi_value WrapString(napi_env &env, const std::string &param, const std::string &paramName);
40 
41 struct CallbackInfo {
42     napi_env env;
43     napi_ref callback;
44     napi_deferred deferred;
45 };
46 
47 struct CBBase {
48     CallbackInfo cbInfo;
49     napi_async_work asyncWork = nullptr;
50     napi_deferred deferred = nullptr;
51 };
52 
53 struct DfsConnectCB {
54     napi_env env;
55     napi_ref callback = nullptr;
56 };
57 
58 class NAPIDfsListener : public FileDfsListenerStub {
59 public:
60     void OnStatus(const std::string &networkId, int32_t status) override;
61     void SetConnectDfsEnv(const napi_env &env);
62     void SetConnectDfsCBRef(const napi_ref &ref);
63     void SetConnectDfsPromiseRef(const napi_deferred &promiseDeferred);
64 
65 private:
66     napi_env env_ = nullptr;
67     napi_ref onStatusRef_ = nullptr;
68     napi_deferred promiseDeferred_ = nullptr;
69 };
70 
71 struct ConnectDfsCB {
72     CBBase cbBase;
73     std::string networkId;
74     sptr<NAPIDfsListener> jsCallbackObject;
75     DfsConnectCB dfsConnectCB;
76     int result = 0;
77     int status = 0;
78     napi_ref callbackRef;
79 };
80 
81 ConnectDfsCB *CheckAndGetParameters(uv_work_t *work, napi_handle_scope *scope);
82 
83 class ConnectDfs final {
84 public:
85     static napi_value Async(napi_env env, napi_callback_info info);
86 
87 private:
88     static tuple<bool, std::string> ParseJsOperand(napi_env env, NVal paramFromJsArg);
89     static tuple<bool, NVal> GetListenerFromOptionArg(napi_env env, const NFuncArg &funcArg);
90     static int ParseJsParam(napi_env env, NFuncArg &funcArg, ConnectDfsCB *connectDfsCB);
91     static ConnectDfsCB *CreateConnectDfsCBCBInfo(napi_env &env);
92 };
93 
94 } // namespace ModuleFileIO
95 } // namespace FileManagement
96 } // namespace OHOS
97 
98 #endif // FILEMANAGEMENT_FILE_API_CONNECTDFS_H