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 #include "network/softbus/softbus_handler.h"
17
18 #include "dfs_error.h"
19 #include "network/softbus/softbus_file_receive_listener.h"
20 #include "network/softbus/softbus_file_send_listener.h"
21 #include "network/softbus/softbus_session_listener.h"
22 #include "utils_log.h"
23 #include <utility>
24
25 #include "session.h"
26 #include "utils_directory.h"
27
28 namespace OHOS {
29 namespace Storage {
30 namespace DistributedFile {
31 constexpr uint32_t MAX_SIZE = 128;
32 using namespace OHOS::FileManagement;
SoftBusHandler()33 SoftBusHandler::SoftBusHandler()
34 {
35 sessionListener_.OnSessionOpened = DistributedFile::SoftBusSessionListener::OnSessionOpened;
36 sessionListener_.OnSessionClosed = DistributedFile::SoftBusSessionListener::OnSessionClosed;
37 sessionListener_.OnBytesReceived = nullptr;
38 sessionListener_.OnMessageReceived = nullptr;
39 sessionListener_.OnQosEvent = nullptr;
40
41 fileReceiveListener_.OnReceiveFileStarted = DistributedFile::SoftBusFileReceiveListener::OnReceiveFileStarted;
42 fileReceiveListener_.OnReceiveFileProcess = DistributedFile::SoftBusFileReceiveListener::OnReceiveFileProcess;
43 fileReceiveListener_.OnReceiveFileFinished = DistributedFile::SoftBusFileReceiveListener::OnReceiveFileFinished;
44 fileReceiveListener_.OnFileTransError = DistributedFile::SoftBusFileReceiveListener::OnFileTransError;
45
46 fileSendListener_.OnSendFileProcess = DistributedFile::SoftBusFileSendListener::OnSendFileProcess;
47 fileSendListener_.OnSendFileFinished = DistributedFile::SoftBusFileSendListener::OnSendFileFinished;
48 fileSendListener_.OnFileTransError = DistributedFile::SoftBusFileSendListener::OnFileTransError;
49 }
50
51 SoftBusHandler::~SoftBusHandler() = default;
52
GetInstance()53 SoftBusHandler &SoftBusHandler::GetInstance()
54 {
55 LOGD("SoftBusHandle::GetInstance");
56 static SoftBusHandler handle;
57 return handle;
58 }
59
CreateSessionServer(const std::string & packageName,const std::string & sessionName)60 int32_t SoftBusHandler::CreateSessionServer(const std::string &packageName, const std::string &sessionName)
61 {
62 return ::CreateSessionServer(packageName.c_str(), sessionName.c_str(), &sessionListener_);
63 }
64
SetFileReceiveListener(const std::string & packageName,const std::string & sessionName,const std::string & dstPath)65 int32_t SoftBusHandler::SetFileReceiveListener(const std::string &packageName,
66 const std::string &sessionName,
67 const std::string &dstPath)
68 {
69 return ::SetFileReceiveListener(packageName.c_str(), sessionName.c_str(), &fileReceiveListener_, dstPath.c_str());
70 }
71
SetFileSendListener(const std::string & packageName,const std::string & sessionName)72 int32_t SoftBusHandler::SetFileSendListener(const std::string &packageName, const std::string &sessionName)
73 {
74 return ::SetFileSendListener(packageName.c_str(), sessionName.c_str(), &fileSendListener_);
75 }
76
ChangeOwnerIfNeeded(int sessionId)77 void SoftBusHandler::ChangeOwnerIfNeeded(int sessionId)
78 {
79 char sessionName[MAX_SIZE] = {};
80 auto ret = ::GetMySessionName(sessionId, sessionName, MAX_SIZE);
81 if (ret != E_OK) {
82 LOGE("GetMySessionName failed, ret = %{public}d", ret);
83 return;
84 }
85 SoftBusSessionPool::SessionInfo sessionInfo {};
86 ret = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName, sessionInfo);
87 if (!ret) {
88 LOGE("GetSessionInfo failed");
89 return;
90 }
91 if (DistributedFile::Utils::ChangeOwnerRecursive(sessionInfo.dstPath, sessionInfo.uid, sessionInfo.uid) != 0) {
92 LOGE("ChangeOwnerRecursive failed");
93 }
94 }
95
CloseSession(int sessionId)96 void SoftBusHandler::CloseSession(int sessionId)
97 {
98 char sessionName[MAX_SIZE] = {};
99 auto ret = ::GetMySessionName(sessionId, sessionName, MAX_SIZE);
100 if (ret != E_OK) {
101 LOGE("GetMySessionName failed, ret = %{public}d", ret);
102 return;
103 }
104 ::CloseSession(sessionId);
105 RemoveSessionServer(SERVICE_NAME.c_str(), sessionName);
106 SoftBusSessionPool::GetInstance().DeleteSessionInfo(sessionName);
107 }
108 } // namespace DistributedFile
109 } // namespace Storage
110 } // namespace OHOS