• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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 "message_parcel.h"
17 
18 #include <sys/mman.h>
19 #include <unistd.h>
20 
21 #include "iremote_object.h"
22 
23 namespace {
24 static std::u16string g_interfaceTokenName {};
25 } // namespace
26 
27 namespace OHOS {
MessageParcel()28 MessageParcel::MessageParcel()
29     : Parcel(), writeRawDataFd_(-1), readRawDataFd_(-1),
30     kernelMappedWrite_(nullptr), kernelMappedRead_(nullptr),
31     rawData_(nullptr), rawDataSize_(0)
32 {
33 }
34 
MessageParcel(Allocator * allocator)35 MessageParcel::MessageParcel(Allocator* allocator)
36     : Parcel(allocator), writeRawDataFd_(-1), readRawDataFd_(-1),
37     kernelMappedWrite_(nullptr), kernelMappedRead_(nullptr),
38     rawData_(nullptr), rawDataSize_(0)
39 {
40 }
41 
~MessageParcel()42 MessageParcel::~MessageParcel()
43 {
44     if (kernelMappedWrite_ != nullptr) {
45         ::munmap(kernelMappedWrite_, rawDataSize_);
46         kernelMappedWrite_ = nullptr;
47     }
48     if (kernelMappedRead_ != nullptr) {
49         ::munmap(kernelMappedRead_, rawDataSize_);
50         kernelMappedRead_ = nullptr;
51     }
52 
53     if (readRawDataFd_ > 0) {
54         ::close(readRawDataFd_);
55         readRawDataFd_ = -1;
56     }
57     if (writeRawDataFd_ > 0) {
58         ::close(writeRawDataFd_);
59         writeRawDataFd_ = -1;
60     }
61 
62     ClearFileDescriptor();
63 
64     rawData_ = nullptr;
65     rawDataSize_ = 0;
66 }
67 
68 #ifndef CONFIG_IPC_SINGLE
WriteDBinderProxy(const sptr<IRemoteObject> & object,uint32_t handle,uint64_t stubIndex)69 bool MessageParcel::WriteDBinderProxy(const sptr<IRemoteObject>& object, uint32_t handle, uint64_t stubIndex)
70 {
71     (void)object;
72     (void)handle;
73     (void)stubIndex;
74     return false;
75 }
76 #endif
77 
WriteRemoteObject(const sptr<IRemoteObject> & object)78 bool MessageParcel::WriteRemoteObject(const sptr<IRemoteObject>& object)
79 {
80     (void)object;
81     return false;
82 }
83 
ReadRemoteObject()84 sptr<IRemoteObject> MessageParcel::ReadRemoteObject()
85 {
86     return nullptr;
87 }
88 
WriteFileDescriptor(int fd)89 bool MessageParcel::WriteFileDescriptor(int fd)
90 {
91     (void)fd;
92     return false;
93 }
94 
ReadFileDescriptor()95 int MessageParcel::ReadFileDescriptor()
96 {
97     return -1;
98 }
99 
ClearFileDescriptor()100 void MessageParcel::ClearFileDescriptor() {}
101 
ContainFileDescriptors() const102 bool MessageParcel::ContainFileDescriptors() const
103 {
104     return false;
105 }
106 
WriteInterfaceToken(std::u16string name)107 bool MessageParcel::WriteInterfaceToken(std::u16string name)
108 {
109 #ifdef MOCK_WRITE_INTERFACE_TOKEN_RETURN_TRUE
110     g_interfaceTokenName = name;
111     return true;
112 #else
113     (void)name;
114     return false;
115 #endif
116 }
117 
ReadInterfaceToken()118 std::u16string MessageParcel::ReadInterfaceToken()
119 {
120 #ifdef MOCK_WRITE_INTERFACE_TOKEN_RETURN_TRUE
121     return g_interfaceTokenName;
122 #else
123     return ReadString16();
124 #endif
125 }
126 
WriteRawData(const void * data,size_t size)127 bool MessageParcel::WriteRawData(const void* data, size_t size)
128 {
129     (void)data;
130     (void)size;
131     return false;
132 }
133 
RestoreRawData(std::shared_ptr<char> rawData,size_t size)134 bool MessageParcel::RestoreRawData(std::shared_ptr<char> rawData, size_t size)
135 {
136     (void)rawData;
137     (void)size;
138     return false;
139 }
140 
ReadRawData(size_t size)141 const void* MessageParcel::ReadRawData(size_t size)
142 {
143     (void)size;
144     return nullptr;
145 }
146 
GetRawData() const147 const void* MessageParcel::GetRawData() const
148 {
149     return nullptr;
150 }
151 
GetRawDataSize() const152 size_t MessageParcel::GetRawDataSize() const
153 {
154     return rawDataSize_;
155 }
156 
GetRawDataCapacity() const157 size_t MessageParcel::GetRawDataCapacity() const
158 {
159     return MAX_RAWDATA_SIZE;
160 }
161 
WriteNoException()162 void MessageParcel::WriteNoException()
163 {
164     WriteInt32(0);
165 }
166 
ReadException()167 int32_t MessageParcel::ReadException()
168 {
169     return ReadInt32();
170 }
171 
WriteAshmem(sptr<Ashmem> ashmem)172 bool MessageParcel::WriteAshmem(sptr<Ashmem> ashmem)
173 {
174     (void)ashmem;
175     return false;
176 }
177 
ReadAshmem()178 sptr<Ashmem> MessageParcel::ReadAshmem()
179 {
180     return nullptr;
181 }
182 
Append(MessageParcel & data)183 bool MessageParcel::Append(MessageParcel& data)
184 {
185     (void)data;
186     return false;
187 }
188 } // namespace OHOS
189