1 /*
2  * Copyright (c) 2023 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_TRANS_LISTENER_H
17 #define FILEMANAGEMENT_FILE_API_TRANS_LISTENER_H
18 
19 #include <condition_variable>
20 
21 #include "copy.h"
22 #include "distributed_file_daemon_manager.h"
23 #include "file_trans_listener_stub.h"
24 #include "file_uri.h"
25 #include "hmdfs_info.h"
26 
27 constexpr int NONE = 0;
28 constexpr int SUCCESS = 1;
29 constexpr int FAILED = 2;
30 namespace OHOS {
31 namespace FileManagement {
32 namespace ModuleFileIO {
33 struct CopyEvent {
34     int copyResult = NONE;
35     int32_t errorCode = 0;
36 };
37 class TransListener : public Storage::DistributedFile::FileTransListenerStub {
38 public:
39     int32_t OnFileReceive(uint64_t totalBytes, uint64_t processedBytes) override;
40     int32_t OnFinished(const std::string &sessionName) override;
41     int32_t OnFailed(const std::string &sessionName, int32_t errorCode) override;
42     static NError CopyFileFromSoftBus(const std::string &srcUri,
43                                       const std::string &destUri,
44                                       std::shared_ptr<FileInfos> fileInfos,
45                                       std::shared_ptr<JsCallbackObject> callback);
46 private:
47     static std::string GetNetworkIdFromUri(const std::string &uri);
48     static void CallbackComplete(uv_work_t *work, int stat);
49     static void RmDir(const std::string &path);
50     static std::string CreateDfsCopyPath();
51     static std::string GetFileName(const std::string &path);
52     static int32_t CopyToSandBox(const std::string &srcUri, const std::string &disSandboxPath,
53         const std::string &sandboxPath, const std::string &currentId);
54     static int32_t PrepareCopySession(const std::string &srcUri,
55                                       const std::string &destUri,
56                                       TransListener* transListener,
57                                       Storage::DistributedFile::HmdfsInfo &info,
58                                       std::string &disSandboxPath);
59     static NError HandleCopyFailure(CopyEvent &copyEvent, const Storage::DistributedFile::HmdfsInfo &info,
60         const std::string &disSandboxPath, const std::string &currentId);
61     static int WaitForCopyResult(TransListener* transListener);
62     static std::atomic<uint32_t> getSequenceId_;
63     std::mutex cvMutex_;
64     std::condition_variable cv_;
65     CopyEvent copyEvent_;
66     std::mutex callbackMutex_;
67     std::shared_ptr<JsCallbackObject> callback_;
68 };
69 } // namespace ModuleFileIO
70 } // namespace FileManagement
71 } // namespace OHOS
72 
73 #endif // FILEMANAGEMENT_FILE_API_TRANS_LISTENER_H