1 /*
2  * Copyright (C) 2021-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 #ifndef OHOS_IPC_IPC_OBJECT_STUB_H
17 #define OHOS_IPC_IPC_OBJECT_STUB_H
18 
19 #include <list>
20 #include "ipc_object_proxy.h"
21 #include "iremote_object.h"
22 
23 namespace OHOS {
24 struct RefCountNode {
25     int remotePid;
26     std::string deviceId;
27 };
28 
29 class IPCObjectStub : public IRemoteObject {
30 public:
31     /**
32      * @brief Enumerates object types.
33      * @since 9
34      */
35     enum {
36         OBJECT_TYPE_NATIVE,
37         OBJECT_TYPE_JAVA,
38         OBJECT_TYPE_JAVASCRIPT,
39     };
40 
41     explicit IPCObjectStub(std::u16string descriptor = std::u16string(), bool serialInvokeFlag = false);
42     ~IPCObjectStub();
43 
44     /**
45      * @brief Determine whether it is a Proxy Object.
46      * @return Returns <b>true</b> if it is a Proxy Object; returns <b>false</b> otherwise.
47      * @since 9
48      */
IsProxyObject()49     bool IsProxyObject() const override
50     {
51         return false;
52     };
53 
54     /**
55      * @brief Determine the reference count of the object.
56      * @return Returns the reference pointer count.
57      * @since 9
58      */
59     int32_t GetObjectRefCount() override;
60 
61     /**
62      * @brief Dump the contents.
63      * @param fd Indicates the file descriptor.
64      * @param args Indicates a vector containing u16string.
65      * @return Returns {@link ERR_NONE} if the dump is successful; returns an error code
66      * defined in {@link ipc_types.h} otherwise.
67      * @since 9
68      */
69     int Dump(int fd, const std::vector<std::u16string> &args) override;
70 
71     /**
72      * @brief Sets an entry for receiving requests.
73      * @param code Indicates the service request code sent from the peer end.
74      * @param data Indicates the object sent from the peer end.
75      * @param reply Indicates the response message object sent from the remote service.
76      * @param option Indicates whether the operation is synchronous or asynchronous.
77      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
78      * defined in {@link ipc_types.h} otherwise.
79      * @since 9
80      */
81     virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
82 
83     /**
84      * @brief Sends a request to the peer object.
85      * @param code Indicates the message code of the request.
86      * @param data Indicates the object storing the data to be sent.
87      * @param reply Indicates the object receiving the response data.
88      * @param option Indicates a synchronous (default) or asynchronous request.
89      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
90      * defined in {@link ipc_types.h}otherwise.
91      * @since 9
92      */
93     int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
94 
95     /**
96      * @brief The callback function that is strongly referenced for the first time.
97      * @param objectId Indicates the object Id.
98      * @return void
99      * @since 9
100      */
101     void OnFirstStrongRef(const void *objectId) override;
102 
103     /**
104      * @brief The callback function that is strongly referenced for the last time.
105      * @param objectId Indicates the object Id.
106      * @return void
107      * @since 9
108      */
109     void OnLastStrongRef(const void *objectId) override;
110 
111     /**
112      * @brief Register for callbacks to receive death notifications.
113      * @param recipient Indicates the DeathRecipient pointer callback to register.
114      * @return Returns <b>true</b> if register succeeds; returns <b>false</b> otherwise.
115      * @since 9
116      */
117     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override;
118 
119     /**
120      * @brief Unregister for callbacks to receive death notifications.
121      * @param recipient Indicates the DeathRecipient pointer callback to register.
122      * @return Returns <b>true</b> if unregister succeeds; returns <b>false</b> otherwise.
123      * @since 9
124      */
125     bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override;
126 
127     /**
128      * @brief Obtains the PID of the object.
129      * @return Returns the PID of the object.
130      * @since 9
131      */
132     int GetCallingPid();
133 
134     /**
135      * @brief Obtains the UID of the object.
136      * @return Returns the UID of the object.
137      * @since 9
138      */
139     int GetCallingUid();
140 
141     /**
142      * @brief Obtains calling token ID of caller.
143      * @return Returns the TokenId of caller.
144      * @since 9
145      */
146     uint32_t GetCallingTokenID();
147 
148     /**
149      * @brief Obtains full calling token ID of caller.
150      * @return Returns the full TokenId of caller.
151      * @since 9
152      */
153     uint64_t GetCallingFullTokenID();
154 
155     /**
156      * @brief Obtains the first token ID.
157      * @return Returns the first TokenId.
158      * @since 9
159      */
160     uint32_t GetFirstTokenID();
161 
162     /**
163      * @brief Obtains the first full token ID.
164      * @return Returns the first full TokenId.
165      * @since 9
166      */
167     uint64_t GetFirstFullTokenID();
168 
169     /**
170      * @brief Take a remote dump.
171      * @param code Indicates the service request code sent from the peer end.
172      * @param data Indicates the object sent from the peer end.
173      * @param reply Indicates the response message object sent from the remote service.
174      * @param option Indicates whether the operation is synchronous or asynchronous.
175      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
176      * defined in {@link ipc_types.h} otherwise.
177      * @since 9
178      */
179     virtual int OnRemoteDump(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
180 
181     /**
182      * @brief Obtains the process protocol.
183      * @param code Indicates the service request code sent from the peer end.
184      * @param data Indicates the object sent from the peer end.
185      * @param reply Indicates the response message object sent from the remote service.
186      * @param option Indicates whether the operation is synchronous or asynchronous.
187      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
188      * defined in {@link ipc_types.h} otherwise.
189      * @since 9
190      */
191     virtual int32_t ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
192 
193     /**
194      * @brief Obtains the object type.
195      * @return Returns an enumeration value that represents the type of object.
196      * @since 9
197      */
198     virtual int GetObjectType() const;
199 
200     /**
201      * @brief Obtains the last request time.
202      * @return Returns the last request time.
203      * @since 11
204      */
205     uint64_t GetLastRequestTime();
206 
207     /**
208      * @brief Obtain the flag for sid.
209      * @return Return the value of flag.
210      * @since 12
211      */
212     bool GetRequestSidFlag() const;
213 
214     /**
215      * @brief Set the value of the sid flag.
216      * @return void.
217      * @since 12
218      */
219     void SetRequestSidFlag(bool flag);
220 
221     /**
222      * @brief Get and save the dbinder object data.
223      * @param pid Indicates the sender pid.
224      * @param uid Indicates the sender uid.
225      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
226      * defined in {@link ipc_types.h} otherwise.
227      * @since 12
228      */
229     virtual int GetAndSaveDBinderData(pid_t pid, uid_t uid);
230 
231 #ifndef CONFIG_IPC_SINGLE
232     /**
233      * @brief Invoker the calling thread.
234      * @param code Indicates the message code.
235      * @param data Indicates the object sent to the peer process.
236      * @param reply Indicates the object returned by the peer process.
237      * @param option Indicates the synchronous or asynchronous mode to send messages.
238      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
239      * defined in {@link ipc_types.h} otherwise.
240      * @since 9
241      */
242     int32_t InvokerThread(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
243 
244     /**
245      * @brief Notification service death.
246      * @param data Indicates the object sent to the peer process.
247      * @param reply Indicates the object returned by the peer process.
248      * @param option Indicates the synchronous or asynchronous mode to send messages.
249      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
250      * defined in {@link ipc_types.h} otherwise.
251      * @since 9
252      */
253     int32_t NoticeServiceDie(MessageParcel &data, MessageParcel &reply, MessageOption &option);
254 
255     /**
256      * @brief Invoke the data bus thread.
257      * @param data Indicates the object sent to the peer process.
258      * @param reply Indicates the object returned by the peer process.
259      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
260      * defined in {@link ipc_types.h} otherwise.
261      * @since 9
262      */
263     int32_t InvokerDataBusThread(MessageParcel &data, MessageParcel &reply);
264 
265     /**
266      * @brief Add authentication information.
267      * @param data Indicates the object sent to the peer process.
268      * @param reply Indicates the object returned by the peer process.
269      * @param code Indicates the message code of this request.
270      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
271      * defined in {@link ipc_types.h} otherwise.
272      * @since 9
273      */
274     int32_t AddAuthInfo(MessageParcel &data, MessageParcel &reply, uint32_t code);
275 #endif
276 
277 private:
278 #ifndef CONFIG_IPC_SINGLE
279     int GetPidUid(MessageParcel &data, MessageParcel &reply);
280     std::string GetSessionName();
281     int32_t GetSessionNameForPidUid(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
282     int32_t GetGrantedSessionName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
283     std::string CreateSessionName(int uid, int pid);
284     int RemoveSessionName(MessageParcel &data);
285     bool IsSamgrCall();
286     int DBinderInvokeListenThread(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
287     int DBinderIncRefsTransaction(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
288     int DBinderDecRefsTransaction(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
289     int DBinderAddCommAuth(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
290     int DBinderGetSessionName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
291     int DBinderGetGrantedSessionName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
292     int DBinderGetSessionNameForPidUid(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
293     int DBinderGetPidUid(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
294     int DBinderRemoveSessionName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
295 #endif
296 
297     bool IsDeviceIdIllegal(const std::string &deviceID);
298     int DBinderPingTransaction(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
299     int DBinderSearchDescriptor(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
300     int DBinderSearchRefCount(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
301     int DBinderDumpTransaction(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
302     int SendRequestInner(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
303 
304     std::recursive_mutex serialRecursiveMutex_;
305     bool serialInvokeFlag_;
306     uint64_t lastRequestTime_;
307     std::atomic<bool> requestSidFlag_ = false;
308 };
309 } // namespace OHOS
310 #endif // OHOS_IPC_IPC_OBJECT_STUB_H
311