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 #include "dataobs_mgr_proxy.h"
17
18 #include "errors.h"
19 #include "hilog_wrapper.h"
20 #include "dataobs_mgr_errors.h"
21 #include "common_utils.h"
22
23 namespace OHOS {
24 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)25 bool DataObsManagerProxy::WriteInterfaceToken(MessageParcel &data)
26 {
27 if (!data.WriteInterfaceToken(DataObsManagerProxy::GetDescriptor())) {
28 HILOG_ERROR("failed, write interface token error");
29 return false;
30 }
31 return true;
32 }
33
WriteParam(MessageParcel & data,const Uri & uri,sptr<IDataAbilityObserver> dataObserver)34 bool DataObsManagerProxy::WriteParam(MessageParcel &data, const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
35 {
36 if (!data.WriteString(uri.ToString())) {
37 HILOG_ERROR("failed, write uri error");
38 return false;
39 }
40
41 if (dataObserver == nullptr) {
42 HILOG_ERROR("failed, dataObserver is nullptr");
43 return false;
44 }
45
46 if (!data.WriteRemoteObject(dataObserver->AsObject())) {
47 HILOG_ERROR("failed, write dataObserver error");
48 return false;
49 }
50 return true;
51 }
52
RegisterObserver(const Uri & uri,sptr<IDataAbilityObserver> dataObserver)53 int32_t DataObsManagerProxy::RegisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
54 {
55 MessageParcel data;
56 MessageParcel reply;
57 MessageOption option;
58
59 if (!WriteInterfaceToken(data)) {
60 return IPC_PARCEL_ERROR;
61 }
62
63 if (!WriteParam(data, uri, dataObserver)) {
64 return INVALID_PARAM;
65 }
66
67 auto error = SendTransactCmd(IDataObsMgr::REGISTER_OBSERVER, data, reply, option);
68 if (error != NO_ERROR) {
69 HILOG_ERROR("failed, SendRequest error: %{public}d, uri: %{public}s", error,
70 CommonUtils::Anonymous(uri.ToString()).c_str());
71 return error;
72 }
73
74 int32_t res = IPC_ERROR;
75 return reply.ReadInt32(res) ? res : IPC_ERROR;
76 }
77
UnregisterObserver(const Uri & uri,sptr<IDataAbilityObserver> dataObserver)78 int32_t DataObsManagerProxy::UnregisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
79 {
80 MessageParcel data;
81 MessageParcel reply;
82 MessageOption option;
83
84 if (!WriteInterfaceToken(data)) {
85 return IPC_PARCEL_ERROR;
86 }
87
88 if (!WriteParam(data, uri, dataObserver)) {
89 return INVALID_PARAM;
90 }
91
92 auto error = SendTransactCmd(IDataObsMgr::UNREGISTER_OBSERVER, data, reply, option);
93 if (error != NO_ERROR) {
94 HILOG_ERROR("failed, SendRequest error: %{public}d, uri: %{public}s", error,
95 CommonUtils::Anonymous(uri.ToString()).c_str());
96 return error;
97 }
98 int32_t res = IPC_ERROR;
99 return reply.ReadInt32(res) ? res : IPC_ERROR;
100 }
101
NotifyChange(const Uri & uri)102 int32_t DataObsManagerProxy::NotifyChange(const Uri &uri)
103 {
104 MessageParcel data;
105 MessageParcel reply;
106 MessageOption option;
107
108 if (!WriteInterfaceToken(data)) {
109 return IPC_PARCEL_ERROR;
110 }
111 if (!data.WriteString(uri.ToString())) {
112 HILOG_ERROR("failed, write uri error, uri: %{public}s", CommonUtils::Anonymous(uri.ToString()).c_str());
113 return INVALID_PARAM;
114 }
115 auto error = SendTransactCmd(IDataObsMgr::NOTIFY_CHANGE, data, reply, option);
116 if (error != NO_ERROR) {
117 HILOG_ERROR("failed, SendRequest error: %{public}d, uri: %{public}s", error,
118 CommonUtils::Anonymous(uri.ToString()).c_str());
119 return IPC_ERROR;
120 }
121
122 int32_t res = IPC_ERROR;
123 return reply.ReadInt32(res) ? res : IPC_ERROR;
124 }
125
RegisterObserverExt(const Uri & uri,sptr<IDataAbilityObserver> dataObserver,bool isDescendants)126 Status DataObsManagerProxy::RegisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver,
127 bool isDescendants)
128 {
129 MessageParcel data;
130 MessageParcel reply;
131 MessageOption option;
132
133 if (!WriteInterfaceToken(data)) {
134 return IPC_PARCEL_ERROR;
135 }
136
137 if (!WriteParam(data, uri, dataObserver)) {
138 return INVALID_PARAM;
139 }
140
141 if (!data.WriteBool(isDescendants)) {
142 HILOG_ERROR("failed, write isDescendants error, uri: %{public}s, isDescendants: %{public}d",
143 CommonUtils::Anonymous(uri.ToString()).c_str(), isDescendants);
144 return INVALID_PARAM;
145 }
146
147 auto error = SendTransactCmd(IDataObsMgr::REGISTER_OBSERVER_EXT, data, reply, option);
148 if (error != NO_ERROR) {
149 HILOG_ERROR("failed, SendRequest error: %{public}d, uri: %{public}s, isDescendants: %{public}d", error,
150 CommonUtils::Anonymous(uri.ToString()).c_str(), isDescendants);
151 return IPC_ERROR;
152 }
153 int32_t res = IPC_ERROR;
154 return reply.ReadInt32(res) ? static_cast<Status>(res) : IPC_ERROR;
155 }
156
UnregisterObserverExt(const Uri & uri,sptr<IDataAbilityObserver> dataObserver)157 Status DataObsManagerProxy::UnregisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
158 {
159 MessageParcel data;
160 MessageParcel reply;
161 MessageOption option;
162
163 if (!WriteInterfaceToken(data)) {
164 return IPC_PARCEL_ERROR;
165 }
166
167 if (!WriteParam(data, uri, dataObserver)) {
168 return INVALID_PARAM;
169 }
170
171 auto error = SendTransactCmd(IDataObsMgr::UNREGISTER_OBSERVER_EXT, data, reply, option);
172 if (error != NO_ERROR) {
173 HILOG_ERROR("failed, SendRequest error: %{public}d, uri: %{public}s", error,
174 CommonUtils::Anonymous(uri.ToString()).c_str());
175 return IPC_ERROR;
176 }
177 int32_t res = IPC_ERROR;
178 return reply.ReadInt32(res) ? static_cast<Status>(res) : IPC_ERROR;
179 }
180
UnregisterObserverExt(sptr<IDataAbilityObserver> dataObserver)181 Status DataObsManagerProxy::UnregisterObserverExt(sptr<IDataAbilityObserver> dataObserver)
182 {
183 MessageParcel data;
184 MessageParcel reply;
185 MessageOption option;
186
187 if (!WriteInterfaceToken(data)) {
188 return IPC_PARCEL_ERROR;
189 }
190
191 if (dataObserver == nullptr) {
192 HILOG_ERROR("failed, dataObserver is nullptr");
193 return INVALID_PARAM;
194 }
195
196 if (!data.WriteRemoteObject(dataObserver->AsObject())) {
197 HILOG_ERROR("failed, write dataObserver error");
198 return INVALID_PARAM;
199 }
200
201 auto error = SendTransactCmd(IDataObsMgr::UNREGISTER_OBSERVER_ALL_EXT, data, reply, option);
202 if (error != NO_ERROR) {
203 HILOG_ERROR("failed, SendRequest error: %{public}d", error);
204 return IPC_ERROR;
205 }
206 int32_t res = IPC_ERROR;
207 return reply.ReadInt32(res) ? static_cast<Status>(res) : IPC_ERROR;
208 }
209
NotifyChangeExt(const ChangeInfo & changeInfo)210 Status DataObsManagerProxy::NotifyChangeExt(const ChangeInfo &changeInfo)
211 {
212 MessageParcel data;
213 MessageParcel reply;
214 MessageOption option;
215
216 if (!WriteInterfaceToken(data)) {
217 return IPC_PARCEL_ERROR;
218 }
219
220 if (!ChangeInfo::Marshalling(changeInfo, data)) {
221 HILOG_ERROR("failed, changeInfo marshalling error, changeType:%{public}ud, num of uris:%{public}zu, data is "
222 "nullptr:%{public}d, size:%{public}ud",
223 changeInfo.changeType_, changeInfo.uris_.size(), changeInfo.data_ == nullptr, changeInfo.size_);
224 return INVALID_PARAM;
225 }
226
227 auto error = SendTransactCmd(IDataObsMgr::NOTIFY_CHANGE_EXT, data, reply, option);
228 if (error != NO_ERROR) {
229 HILOG_ERROR("failed, SendRequest error: %{public}d, changeType:%{public}ud, num of uris:%{public}zu, data is "
230 "nullptr:%{public}d, size:%{public}ud",
231 error, changeInfo.changeType_, changeInfo.uris_.size(), changeInfo.data_ == nullptr, changeInfo.size_);
232 return IPC_ERROR;
233 }
234 int32_t res = IPC_ERROR;
235 return reply.ReadInt32(res) ? static_cast<Status>(res) : IPC_ERROR;
236 }
237
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)238 int32_t DataObsManagerProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
239 MessageParcel &reply, MessageOption &option)
240 {
241 sptr<IRemoteObject> remote = Remote();
242 if (remote == nullptr) {
243 HILOG_ERROR("remote object is nullptr.");
244 return ERR_NULL_OBJECT;
245 }
246
247 int32_t ret = remote->SendRequest(code, data, reply, option);
248 if (ret != NO_ERROR) {
249 HILOG_ERROR("SendRequest failed. code is %{public}d, ret is %{public}d.", code, ret);
250 return ret;
251 }
252 return NO_ERROR;
253 }
254 } // namespace AAFwk
255 } // namespace OHOS
256