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_SERVICES_DBINDER_DBINDER_STUB_H
17 #define OHOS_IPC_SERVICES_DBINDER_DBINDER_STUB_H
18 
19 #include <string>
20 #include <parcel.h>
21 #include "ipc_object_stub.h"
22 
23 namespace OHOS {
24 #ifdef BINDER_IPC_32BIT
25 typedef unsigned int binder_uintptr_t;
26 #else
27 typedef unsigned long long binder_uintptr_t;
28 #endif
29 
30 class DBinderServiceStub : public IPCObjectStub {
31 public:
32     explicit DBinderServiceStub(const std::string &serviceName, const std::string &deviceID,
33         binder_uintptr_t binderObject);
34     ~DBinderServiceStub();
35 
36     /**
37      * @brief Serialize a specified DBinderServiceStub object.
38      * @param parcel Indicates the object storing the data.
39      * @param object Indicates the serialized object.
40      * @return Returns <b>true</b> if serialized successfully; returns <b>false</b> otherwise.
41      * @since 12
42      */
43     static bool Marshalling(Parcel &parcel, const sptr<IRemoteObject> &object);
44 
45     /**
46      * @brief Serialize self.
47      * @param parcel Indicates the object storing the data.
48      * @return Returns <b>true</b> if serialized successfully; returns <b>false</b> otherwise.
49      * @since 12
50      */
51     bool Marshalling(Parcel &parcel) const override;
52 
53     /**
54      * @brief Gets the process protocol.
55      * @param code Indicates the message code of the request.
56      * @param data Indicates the object storing the data to be sent.
57      * @param reply Indicates the object receiving the response data.
58      * @param option Indicates a synchronous (default) or asynchronous request.
59      * @return Returns {@code 0} if valid notifications; returns an error code if the operation fails.
60      * @since 9
61      */
62     int32_t ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
63 
64     /**
65      * @brief Response processing of the request.
66      * @param code Indicates the service request code sent from the peer end.
67      * @param data Indicates the  object sent from the peer end.
68      * @param reply Indicates the response message object sent from the remote service.
69      * @param options Indicates whether the operation is synchronous or asynchronous.
70      * @return Returns {@code 0} if the operation succeeds; returns an error code if the operation fails.
71      * @since 9
72      */
73     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
74 
75     /**
76      * @brief Get and save the dbinder object data.
77      * @param pid Indicates the sender pid.
78      * @param uid Indicates the sender uid.
79      * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code
80      * defined in {@link ipc_types.h} otherwise.
81      * @since 12
82      */
83     int GetAndSaveDBinderData(pid_t pid, uid_t uid) override;
84 
85     /**
86      * @brief Obtains the service name.
87      * @return Returns the service name.
88      * @since 9
89      */
90     const std::string &GetServiceName();
91 
92     /**
93      * @brief Obtain the device ID.
94      * @return Returns the device ID.
95      * @since 9
96      */
97     const std::string &GetDeviceID();
98 
99     /**
100      * @brief Obtain the binder object.
101      * @return Returns the binder object.
102      * @since 9
103      */
104     binder_uintptr_t GetBinderObject() const;
105 
106 private:
107     int32_t ProcessDeathRecipient(MessageParcel &data);
108     int32_t AddDbinderDeathRecipient(MessageParcel &data);
109     int32_t RemoveDbinderDeathRecipient(MessageParcel &data);
110     bool CheckSessionObjectValidity();
111     int SaveDBinderData(const std::string &localBusName);
112 
113     const std::string serviceName_;
114     const std::string deviceID_;
115     binder_uintptr_t binderObject_;
116     std::unique_ptr<uint8_t[]> dbinderData_ {nullptr};
117 };
118 } // namespace OHOS
119 #endif // OHOS_IPC_SERVICES_DBINDER_DBINDER_STUB_H
120