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_IREMOTE_OBJECT_H
17 #define OHOS_IPC_IREMOTE_OBJECT_H
18
19 #include <codecvt>
20 #include <locale>
21 #include <string>
22 #include "ipc_types.h"
23 #include "message_parcel.h"
24 #include "message_option.h"
25
26 namespace OHOS {
27 class IRemoteBroker;
to_utf16(const std::string & str)28 inline std::u16string to_utf16(const std::string &str)
29 {
30 return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(str);
31 }
32
33 class IRemoteObject : public virtual Parcelable, public virtual RefBase {
34 public:
35 /**
36 * @brief Enumerates communication protocols.
37 * @since 9
38 */
39 enum {
40 IF_PROT_DEFAULT, /* Invoker family. */
41 IF_PROT_BINDER = IF_PROT_DEFAULT,
42 IF_PROT_DATABUS,
43 IF_PROT_ERROR,
44 };
45 enum {
46 DATABUS_TYPE,
47 };
48 class DeathRecipient : public virtual RefBase {
49 public:
50 /**
51 * @brief Methods that enumerate death notifications.
52 * @since 9
53 */
54 enum {
55 ADD_DEATH_RECIPIENT,
56 REMOVE_DEATH_RECIPIENT,
57 NOTICE_DEATH_RECIPIENT,
58 TEST_SERVICE_DEATH_RECIPIENT,
59 TEST_DEVICE_DEATH_RECIPIENT,
60 };
61
62 /**
63 * @brief Called to perform subsequent operations when a death notification of the remote object is received.
64 * @param object Indicates the IRemoteObject pointer object.
65 * @return void
66 * @since 9
67 */
68 virtual void OnRemoteDied(const wptr<IRemoteObject> &object) = 0;
69 };
70
71 /**
72 * @brief Obtains the object reference count.
73 * @return Returns the resulting object reference count.
74 * @since 9
75 */
76 virtual int32_t GetObjectRefCount() = 0;
77
78 /**
79 * @brief Sends message to the peer process in synchronous or asynchronous mode.
80 * @param code Indicates the message code of the request.
81 * @param data Indicates the object storing the data to be sent.
82 * @param reply Indicates the object receiving the response data.
83 * @param option Indicates a synchronous (default) or asynchronous request.
84 * @return Returns the result of the send request.
85 * @since 9
86 */
87 virtual int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) = 0;
88
89 /**
90 * @brief Determine whether it is a proxy object.
91 * @return Returns <b>true</b> if as a proxy object; returns <b>false</b> otherwise.
92 * @since 9
93 */
94 virtual bool IsProxyObject() const;
95
96 /**
97 * @brief Check the object is dead.
98 * @return Returns <b>true</b> if the object has dead; returns <b>false</b> otherwise.
99 * @since 9
100 */
101 virtual bool IsObjectDead() const;
102
103 /**
104 * @brief Obtains the interface descriptor.
105 * @return Returns the resulting interface descriptor.
106 * @since 9
107 */
108 virtual std::u16string GetInterfaceDescriptor();
109
110 /**
111 * @brief Check the legitimacy of the object.
112 * @return Returns <b>true</b> if the identity is legal; returns <b>false</b> otherwise.
113 * @since 9
114 */
115 virtual bool CheckObjectLegality() const;
116
117 /**
118 * @brief Add a callback used to receive notifications of the death of a remote object.
119 * @return Returns <b>true</b> if the identity is legal; returns <b>false</b> otherwise.
120 * @since 9
121 */
122 virtual bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) = 0;
123
124 /**
125 * @brief Remove a callback used to receive notifications of the death of a remote object.
126 * @return Returns <b>true</b> if the identity is legal; returns <b>false</b> otherwise.
127 * @since 9
128 */
129 virtual bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) = 0;
130
131 /**
132 * @brief Marshal this object.
133 * @param parcel Indicates a marshaling parcel type object.
134 * @return Returns <b>true</b> if the marshalling is successful; returns <b>false</b> otherwise.
135 * @since 9
136 */
137 virtual bool Marshalling(Parcel &parcel) const override;
138
139 /**
140 * @brief Unmarshal this object.
141 * @return Returns the IRemoteObject pointer object.
142 * @since 9
143 */
144 static sptr<IRemoteObject> Unmarshalling(Parcel &parcel);
145
146 /**
147 * @brief Marshal this object.
148 * @param parcel Indicates a marshaling parcel type object.
149 * @param object Indicates an IRemoteObject pointer type object.
150 * @return Returns <b>true</b> if the marshalling is successful; returns <b>false</b> otherwise.
151 * @since 9
152 */
153 static bool Marshalling(Parcel &parcel, const sptr<IRemoteObject> &object);
154
155 /**
156 * @brief Obtains the interface.
157 * @return Returns an IRemoteBroker pointer object.
158 * @since 9
159 */
160 virtual sptr<IRemoteBroker> AsInterface();
161
162 /**
163 * @brief Dump the contents.
164 * @param fd Indicates the file descriptor.
165 * @param args Indicates a vector containing u16string.
166 * @return Returns {@link ERR_NONE} if the dump is successful; returns an error code
167 * defined in {@link ipc_types.h} otherwise.
168 * @since 9
169 */
170 virtual int Dump(int fd, const std::vector<std::u16string> &args) = 0;
171
172 const std::u16string descriptor_;
173
174 /**
175 * @brief Obtains the object descriptor.
176 * @return Returns the object descriptor.
177 * @since 9
178 */
179 std::u16string GetObjectDescriptor() const;
180
181 protected:
182 explicit IRemoteObject(std::u16string descriptor = nullptr);
183 virtual ~IRemoteObject() = default;
184 };
185 } // namespace OHOS
186 #endif // OHOS_IPC_IREMOTE_OBJECT_H
187