1 /*
2 * Copyright (C) 2021 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 "dbinder_session_object.h"
17
18 #include "ipc_process_skeleton.h"
19 #include "ISessionService.h"
20
21 namespace OHOS {
DBinderSessionObject(std::shared_ptr<Session> session,const std::string & serviceName,const std::string & serverDeviceId)22 DBinderSessionObject::DBinderSessionObject(std::shared_ptr<Session> session, const std::string &serviceName,
23 const std::string &serverDeviceId)
24 : session_(session), serviceName_(serviceName), serverDeviceId_(serverDeviceId)
25 {}
26
~DBinderSessionObject()27 DBinderSessionObject::~DBinderSessionObject()
28 {
29 if (session_ != nullptr) {
30 std::shared_ptr<ISessionService> manager = ISessionService::GetInstance();
31 if (manager != nullptr) {
32 (void)manager->CloseSession(session_);
33 }
34 }
35
36 session_ = nullptr;
37 buff_ = nullptr;
38 }
39
SetBusSession(std::shared_ptr<Session> session)40 void DBinderSessionObject::SetBusSession(std::shared_ptr<Session> session)
41 {
42 session_ = session;
43 }
44
GetBusSession() const45 std::shared_ptr<Session> DBinderSessionObject::GetBusSession() const
46 {
47 return session_;
48 }
49
GetSessionBuff()50 std::shared_ptr<BufferObject> DBinderSessionObject::GetSessionBuff()
51 {
52 if (buff_ == nullptr) {
53 std::lock_guard<std::mutex> lockGuard(buffMutex_);
54 if (buff_ == nullptr) {
55 std::shared_ptr<BufferObject> temp = std::make_shared<BufferObject>();
56 buff_ = temp;
57 }
58 }
59
60 return buff_;
61 }
62
SetServiceName(const std::string & serviceName)63 void DBinderSessionObject::SetServiceName(const std::string &serviceName)
64 {
65 serviceName_ = serviceName;
66 }
67
GetServiceName() const68 std::string DBinderSessionObject::GetServiceName() const
69 {
70 return serviceName_;
71 }
72
SetDeviceId(const std::string & serverDeviceId)73 void DBinderSessionObject::SetDeviceId(const std::string &serverDeviceId)
74 {
75 serverDeviceId_ = serverDeviceId;
76 }
77
GetDeviceId() const78 std::string DBinderSessionObject::GetDeviceId() const
79 {
80 return serverDeviceId_;
81 }
82
GetFlatSessionLen()83 uint32_t DBinderSessionObject::GetFlatSessionLen()
84 {
85 return sizeof(struct FlatDBinderSession);
86 }
87
GetSessionHandle() const88 uint32_t DBinderSessionObject::GetSessionHandle() const
89 {
90 if (session_ != nullptr) {
91 return IPCProcessSkeleton::ConvertChannelID2Int(session_->GetChannelId());
92 }
93 return 0;
94 }
95 } // namespace OHOS
96