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 
16 #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STREAM_STREAM_N_EXPORTER_H
17 #define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STREAM_STREAM_N_EXPORTER_H
18 
19 #include "filemgmt_libn.h"
20 
21 #include <mutex>
22 #include "stream_entity.h"
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 using namespace OHOS::FileManagement::LibN;
27 class StreamNExporter final : public NExporter {
28 public:
29     static std::mutex mutex;
30 
31     inline static const std::string className_ = "FsStream";
32 
33     bool Export() override;
34     std::string GetClassName() override;
35 
36     static napi_value Constructor(napi_env env, napi_callback_info cbInfo);
37 
38     static napi_value WriteSync(napi_env env, napi_callback_info cbInfo);
39     static napi_value ReadSync(napi_env env, napi_callback_info cbInfo);
40     static napi_value CloseSync(napi_env env, napi_callback_info cbInfo);
41     static napi_value FlushSync(napi_env env, napi_callback_info cbInfo);
42 
43     static napi_value Write(napi_env env, napi_callback_info cbInfo);
44     static napi_value Read(napi_env env, napi_callback_info cbInfo);
45     static napi_value Close(napi_env env, napi_callback_info cbInfo);
46     static napi_value Seek(napi_env env, napi_callback_info cbInfo);
47     static napi_value Flush(napi_env env, napi_callback_info cbInfo);
48 
49     static std::shared_ptr<FILE> GetFilePtr(StreamEntity *streamEntity);
50     static StreamEntity *GetEntityOf(napi_env env, NFuncArg &funcArg);
51 
52     StreamNExporter(napi_env env, napi_value exports);
53     ~StreamNExporter() override;
54 };
55 
56 struct AsyncReadArg {
57     size_t lenRead = 0;
58     NRef refReadBuf;
59 
AsyncReadArgAsyncReadArg60     explicit AsyncReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {}
61     ~AsyncReadArg() = default;
62 };
63 
64 struct AsyncWriteArg {
65     NRef refWriteArrayBuf;
66     std::unique_ptr<char[]> guardWriteStr = nullptr;
67     size_t actLen = 0;
68 
AsyncWriteArgAsyncWriteArg69     explicit AsyncWriteArg(NVal refWriteArrayBuf) : refWriteArrayBuf(refWriteArrayBuf) {}
AsyncWriteArgAsyncWriteArg70     explicit AsyncWriteArg(std::unique_ptr<char[]> &&guardWriteStr) : guardWriteStr(std::move(guardWriteStr)) {}
71     ~AsyncWriteArg() = default;
72 };
73 
74 const std::string PROCEDURE_STREAM_WRITE_NAME = "FileIOStreamWrite";
75 const std::string PROCEDURE_STREAM_READ_NAME = "FileIOStreamRead";
76 const std::string PROCEDURE_STREAM_CLOSE_NAME = "FileIOStreamClose";
77 const std::string PROCEDURE_STREAM_FLUSH_NAME = "FileIOStreamFlush";
78 
79 } // namespace ModuleFileIO
80 } // namespace FileManagement
81 } // namespace OHOS
82 #endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STREAM_STREAM_N_EXPORTER_H