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 #include "distributed_file_daemon_manager_impl.h"
17
18 #include "asset/asset_adapter_sa_client.h"
19 #include "copy/file_copy_manager.h"
20 #include "copy/file_size_utils.h"
21 #include "dfs_error.h"
22 #include "distributed_file_daemon_proxy.h"
23 #include "utils_log.h"
24
25 #undef LOG_DOMAIN
26 #undef LOG_TAG
27 #define LOG_DOMAIN 0xD001600
28 #define LOG_TAG "distributedfile_daemon"
29
30 namespace OHOS {
31 namespace Storage {
32 namespace DistributedFile {
33 using namespace OHOS::Storage;
GetInstance()34 DistributedFileDaemonManagerImpl &DistributedFileDaemonManagerImpl::GetInstance()
35 {
36 static DistributedFileDaemonManagerImpl instance;
37 return instance;
38 };
39
OpenP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)40 int32_t DistributedFileDaemonManagerImpl::OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
41 {
42 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
43 if (!distributedFileDaemonProxy) {
44 LOGE("proxy is null");
45
46 return OHOS::FileManagement::E_SA_LOAD_FAILED;
47 }
48 return distributedFileDaemonProxy->OpenP2PConnection(deviceInfo);
49 }
50
CloseP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)51 int32_t DistributedFileDaemonManagerImpl::CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
52 {
53 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
54 if (!distributedFileDaemonProxy) {
55 LOGE("proxy is null");
56
57 return OHOS::FileManagement::E_SA_LOAD_FAILED;
58 }
59 return distributedFileDaemonProxy->CloseP2PConnection(deviceInfo);
60 }
61
OpenP2PConnectionEx(const std::string & networkId,sptr<IFileDfsListener> remoteReverseObj)62 int32_t DistributedFileDaemonManagerImpl::OpenP2PConnectionEx(const std::string &networkId,
63 sptr<IFileDfsListener> remoteReverseObj)
64 {
65 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
66 if (!distributedFileDaemonProxy) {
67 LOGE("proxy is null.");
68 return OHOS::FileManagement::E_SA_LOAD_FAILED;
69 }
70 return distributedFileDaemonProxy->OpenP2PConnectionEx(networkId, remoteReverseObj);
71 }
72
CloseP2PConnectionEx(const std::string & networkId)73 int32_t DistributedFileDaemonManagerImpl::CloseP2PConnectionEx(const std::string &networkId)
74 {
75 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
76 if (!distributedFileDaemonProxy) {
77 LOGE("proxy is null.");
78 return OHOS::FileManagement::E_SA_LOAD_FAILED;
79 }
80 return distributedFileDaemonProxy->CloseP2PConnectionEx(networkId);
81 }
82
PrepareSession(const std::string & srcUri,const std::string & dstUri,const std::string & srcDeviceId,const sptr<IRemoteObject> & listener,HmdfsInfo & info)83 int32_t DistributedFileDaemonManagerImpl::PrepareSession(const std::string &srcUri,
84 const std::string &dstUri,
85 const std::string &srcDeviceId,
86 const sptr<IRemoteObject> &listener,
87 HmdfsInfo &info)
88 {
89 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
90 if (distributedFileDaemonProxy == nullptr) {
91 LOGE("proxy is null");
92 return OHOS::FileManagement::E_SA_LOAD_FAILED;
93 }
94 return distributedFileDaemonProxy->PrepareSession(srcUri, dstUri, srcDeviceId, listener, info);
95 }
96
RequestSendFile(const std::string & srcUri,const std::string & dstPath,const std::string & remoteDeviceId,const std::string & sessionName)97 int32_t DistributedFileDaemonManagerImpl::RequestSendFile(const std::string &srcUri,
98 const std::string &dstPath,
99 const std::string &remoteDeviceId,
100 const std::string &sessionName)
101 {
102 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
103 if (distributedFileDaemonProxy == nullptr) {
104 LOGE("proxy is null");
105 return OHOS::FileManagement::E_SA_LOAD_FAILED;
106 }
107 return distributedFileDaemonProxy->RequestSendFile(srcUri, dstPath, remoteDeviceId, sessionName);
108 }
109
GetRemoteCopyInfo(const std::string & srcUri,bool & isFile,bool & isDir)110 int32_t DistributedFileDaemonManagerImpl::GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir)
111 {
112 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
113 if (distributedFileDaemonProxy == nullptr) {
114 LOGE("proxy is null");
115 return OHOS::FileManagement::E_SA_LOAD_FAILED;
116 }
117 return distributedFileDaemonProxy->GetRemoteCopyInfo(srcUri, isFile, isDir);
118 }
119
CancelCopyTask(const std::string & sessionName)120 int32_t DistributedFileDaemonManagerImpl::CancelCopyTask(const std::string &sessionName)
121 {
122 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
123 if (distributedFileDaemonProxy == nullptr) {
124 LOGE("proxy is null");
125 return OHOS::FileManagement::E_SA_LOAD_FAILED;
126 }
127 return distributedFileDaemonProxy->CancelCopyTask(sessionName);
128 }
129
PushAsset(int32_t userId,const sptr<AssetObj> & assetObj,const sptr<IAssetSendCallback> & sendCallback)130 int32_t DistributedFileDaemonManagerImpl::PushAsset(int32_t userId,
131 const sptr<AssetObj> &assetObj,
132 const sptr<IAssetSendCallback> &sendCallback)
133 {
134 LOGI("DistributedFileDaemonManagerImpl PushAsset enter.");
135 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
136 if (distributedFileDaemonProxy == nullptr) {
137 LOGE("proxy is null");
138 return OHOS::FileManagement::E_SA_LOAD_FAILED;
139 }
140 return distributedFileDaemonProxy->PushAsset(userId, assetObj, sendCallback);
141 }
142
RegisterAssetCallback(const sptr<IAssetRecvCallback> & recvCallback)143 int32_t DistributedFileDaemonManagerImpl::RegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback)
144 {
145 LOGI("DistributedFileDaemonManagerImpl registerAssetCallback enter.");
146
147 if (recvCallback == nullptr) {
148 LOGE("Register IAssetRecvCallback is null.");
149 return OHOS::FileManagement::E_NULLPTR;
150 }
151 int32_t ret = AssetAdapterSaClient::GetInstance().AddListener(recvCallback);
152 if (ret != OHOS::FileManagement::E_OK) {
153 LOGE("AssetAdapterSaClient addListener fail.");
154 return ret;
155 }
156 return OHOS::FileManagement::E_OK;
157 }
158
UnRegisterAssetCallback(const sptr<IAssetRecvCallback> & recvCallback)159 int32_t DistributedFileDaemonManagerImpl::UnRegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback)
160 {
161 LOGI("DistributedFileDaemonManagerImpl unRegisterAssetCallback enter.");
162 if (recvCallback == nullptr) {
163 LOGE("UnRegister IAssetRecvCallback is null.");
164 return OHOS::FileManagement::E_NULLPTR;
165 }
166 int32_t ret = AssetAdapterSaClient::GetInstance().RemoveListener(recvCallback);
167 if (ret != OHOS::FileManagement::E_OK) {
168 LOGE("AssetAdapterSaClient removeListener fail.");
169 return ret;
170 }
171 return OHOS::FileManagement::E_OK;
172 }
173
GetSize(const std::string & uri,bool isSrcUri,uint64_t & size)174 int32_t DistributedFileDaemonManagerImpl::GetSize(const std::string &uri, bool isSrcUri, uint64_t &size)
175 {
176 return FileSizeUtils::GetSize(uri, isSrcUri, size);
177 }
178
IsDirectory(const std::string & uri,bool isSrcUri,bool & isDirectory)179 int32_t DistributedFileDaemonManagerImpl::IsDirectory(const std::string &uri, bool isSrcUri, bool &isDirectory)
180 {
181 return FileSizeUtils::IsDirectory(uri, isSrcUri, isDirectory);
182 }
183
Copy(const std::string & srcUri,const std::string & destUri,ProcessCallback processCallback)184 int32_t DistributedFileDaemonManagerImpl::Copy(const std::string &srcUri,
185 const std::string &destUri, ProcessCallback processCallback)
186 {
187 return FileCopyManager::GetInstance()->Copy(srcUri, destUri, processCallback);
188 }
189
Cancel(const std::string & srcUri,const std::string & destUri)190 int32_t DistributedFileDaemonManagerImpl::Cancel(const std::string &srcUri, const std::string &destUri)
191 {
192 return FileCopyManager::GetInstance()->Cancel(srcUri, destUri);
193 }
194
Cancel()195 int32_t DistributedFileDaemonManagerImpl::Cancel()
196 {
197 return FileCopyManager::GetInstance()->Cancel();
198 }
199 } // namespace DistributedFile
200 } // namespace Storage
201 } // namespace OHOS
202