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