1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "dataobs_mgr_proxy.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "errors.h"
19eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
20eace7efcSopenharmony_ci#include "dataobs_mgr_errors.h"
21eace7efcSopenharmony_ci#include "common_utils.h"
22eace7efcSopenharmony_ci
23eace7efcSopenharmony_cinamespace OHOS {
24eace7efcSopenharmony_cinamespace AAFwk {
25eace7efcSopenharmony_cibool DataObsManagerProxy::WriteInterfaceToken(MessageParcel &data)
26eace7efcSopenharmony_ci{
27eace7efcSopenharmony_ci    if (!data.WriteInterfaceToken(DataObsManagerProxy::GetDescriptor())) {
28eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "write token error");
29eace7efcSopenharmony_ci        return false;
30eace7efcSopenharmony_ci    }
31eace7efcSopenharmony_ci    return true;
32eace7efcSopenharmony_ci}
33eace7efcSopenharmony_ci
34eace7efcSopenharmony_cibool DataObsManagerProxy::WriteParam(MessageParcel &data, const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
35eace7efcSopenharmony_ci{
36eace7efcSopenharmony_ci    if (!data.WriteString(uri.ToString())) {
37eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "write uri error");
38eace7efcSopenharmony_ci        return false;
39eace7efcSopenharmony_ci    }
40eace7efcSopenharmony_ci
41eace7efcSopenharmony_ci    if (dataObserver == nullptr) {
42eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "null dataObserver");
43eace7efcSopenharmony_ci        return false;
44eace7efcSopenharmony_ci    }
45eace7efcSopenharmony_ci
46eace7efcSopenharmony_ci    if (!data.WriteRemoteObject(dataObserver->AsObject())) {
47eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "write dataObserver error");
48eace7efcSopenharmony_ci        return false;
49eace7efcSopenharmony_ci    }
50eace7efcSopenharmony_ci    return true;
51eace7efcSopenharmony_ci}
52eace7efcSopenharmony_ci
53eace7efcSopenharmony_ciint32_t DataObsManagerProxy::RegisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
54eace7efcSopenharmony_ci{
55eace7efcSopenharmony_ci    MessageParcel data;
56eace7efcSopenharmony_ci    MessageParcel reply;
57eace7efcSopenharmony_ci    MessageOption option;
58eace7efcSopenharmony_ci
59eace7efcSopenharmony_ci    if (!WriteInterfaceToken(data)) {
60eace7efcSopenharmony_ci        return IPC_PARCEL_ERROR;
61eace7efcSopenharmony_ci    }
62eace7efcSopenharmony_ci
63eace7efcSopenharmony_ci    if (!WriteParam(data, uri, dataObserver)) {
64eace7efcSopenharmony_ci        return INVALID_PARAM;
65eace7efcSopenharmony_ci    }
66eace7efcSopenharmony_ci
67eace7efcSopenharmony_ci    auto error = SendTransactCmd(IDataObsMgr::REGISTER_OBSERVER, data, reply, option);
68eace7efcSopenharmony_ci    if (error != NO_ERROR) {
69eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "sendRequest error:%{public}d, uri:%{public}s", error,
70eace7efcSopenharmony_ci            CommonUtils::Anonymous(uri.ToString()).c_str());
71eace7efcSopenharmony_ci        return error;
72eace7efcSopenharmony_ci    }
73eace7efcSopenharmony_ci
74eace7efcSopenharmony_ci    int32_t res = IPC_ERROR;
75eace7efcSopenharmony_ci    return reply.ReadInt32(res) ? res : IPC_ERROR;
76eace7efcSopenharmony_ci}
77eace7efcSopenharmony_ci
78eace7efcSopenharmony_ciint32_t DataObsManagerProxy::UnregisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
79eace7efcSopenharmony_ci{
80eace7efcSopenharmony_ci    MessageParcel data;
81eace7efcSopenharmony_ci    MessageParcel reply;
82eace7efcSopenharmony_ci    MessageOption option;
83eace7efcSopenharmony_ci
84eace7efcSopenharmony_ci    if (!WriteInterfaceToken(data)) {
85eace7efcSopenharmony_ci        return IPC_PARCEL_ERROR;
86eace7efcSopenharmony_ci    }
87eace7efcSopenharmony_ci
88eace7efcSopenharmony_ci    if (!WriteParam(data, uri, dataObserver)) {
89eace7efcSopenharmony_ci        return INVALID_PARAM;
90eace7efcSopenharmony_ci    }
91eace7efcSopenharmony_ci
92eace7efcSopenharmony_ci    auto error = SendTransactCmd(IDataObsMgr::UNREGISTER_OBSERVER, data, reply, option);
93eace7efcSopenharmony_ci    if (error != NO_ERROR) {
94eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "sendRequest error:%{public}d, uri:%{public}s", error,
95eace7efcSopenharmony_ci            CommonUtils::Anonymous(uri.ToString()).c_str());
96eace7efcSopenharmony_ci        return error;
97eace7efcSopenharmony_ci    }
98eace7efcSopenharmony_ci    int32_t res = IPC_ERROR;
99eace7efcSopenharmony_ci    return reply.ReadInt32(res) ? res : IPC_ERROR;
100eace7efcSopenharmony_ci}
101eace7efcSopenharmony_ci
102eace7efcSopenharmony_ciint32_t DataObsManagerProxy::NotifyChange(const Uri &uri)
103eace7efcSopenharmony_ci{
104eace7efcSopenharmony_ci    MessageParcel data;
105eace7efcSopenharmony_ci    MessageParcel reply;
106eace7efcSopenharmony_ci    MessageOption option;
107eace7efcSopenharmony_ci
108eace7efcSopenharmony_ci    if (!WriteInterfaceToken(data)) {
109eace7efcSopenharmony_ci        return IPC_PARCEL_ERROR;
110eace7efcSopenharmony_ci    }
111eace7efcSopenharmony_ci    if (!data.WriteString(uri.ToString())) {
112eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "write uri error, uri:%{public}s",
113eace7efcSopenharmony_ci            CommonUtils::Anonymous(uri.ToString()).c_str());
114eace7efcSopenharmony_ci        return INVALID_PARAM;
115eace7efcSopenharmony_ci    }
116eace7efcSopenharmony_ci    auto error = SendTransactCmd(IDataObsMgr::NOTIFY_CHANGE, data, reply, option);
117eace7efcSopenharmony_ci    if (error != NO_ERROR) {
118eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "sendRequest error:%{public}d, uri:%{public}s", error,
119eace7efcSopenharmony_ci            CommonUtils::Anonymous(uri.ToString()).c_str());
120eace7efcSopenharmony_ci        return IPC_ERROR;
121eace7efcSopenharmony_ci    }
122eace7efcSopenharmony_ci
123eace7efcSopenharmony_ci    int32_t res = IPC_ERROR;
124eace7efcSopenharmony_ci    return reply.ReadInt32(res) ? res : IPC_ERROR;
125eace7efcSopenharmony_ci}
126eace7efcSopenharmony_ci
127eace7efcSopenharmony_ciStatus DataObsManagerProxy::RegisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver,
128eace7efcSopenharmony_ci    bool isDescendants)
129eace7efcSopenharmony_ci{
130eace7efcSopenharmony_ci    MessageParcel data;
131eace7efcSopenharmony_ci    MessageParcel reply;
132eace7efcSopenharmony_ci    MessageOption option;
133eace7efcSopenharmony_ci
134eace7efcSopenharmony_ci    if (!WriteInterfaceToken(data)) {
135eace7efcSopenharmony_ci        return IPC_PARCEL_ERROR;
136eace7efcSopenharmony_ci    }
137eace7efcSopenharmony_ci
138eace7efcSopenharmony_ci    if (!WriteParam(data, uri, dataObserver)) {
139eace7efcSopenharmony_ci        return INVALID_PARAM;
140eace7efcSopenharmony_ci    }
141eace7efcSopenharmony_ci
142eace7efcSopenharmony_ci    if (!data.WriteBool(isDescendants)) {
143eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "isDescendants error, uri:%{public}s,isDescendants:%{public}d",
144eace7efcSopenharmony_ci            CommonUtils::Anonymous(uri.ToString()).c_str(), isDescendants);
145eace7efcSopenharmony_ci        return INVALID_PARAM;
146eace7efcSopenharmony_ci    }
147eace7efcSopenharmony_ci
148eace7efcSopenharmony_ci    auto error = SendTransactCmd(IDataObsMgr::REGISTER_OBSERVER_EXT, data, reply, option);
149eace7efcSopenharmony_ci    if (error != NO_ERROR) {
150eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR,
151eace7efcSopenharmony_ci            "sendRequest error: %{public}d, uri:%{public}s, isDescendants:%{public}d", error,
152eace7efcSopenharmony_ci            CommonUtils::Anonymous(uri.ToString()).c_str(), isDescendants);
153eace7efcSopenharmony_ci        return IPC_ERROR;
154eace7efcSopenharmony_ci    }
155eace7efcSopenharmony_ci    int32_t res = IPC_ERROR;
156eace7efcSopenharmony_ci    return reply.ReadInt32(res) ? static_cast<Status>(res) : IPC_ERROR;
157eace7efcSopenharmony_ci}
158eace7efcSopenharmony_ci
159eace7efcSopenharmony_ciStatus DataObsManagerProxy::UnregisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
160eace7efcSopenharmony_ci{
161eace7efcSopenharmony_ci    MessageParcel data;
162eace7efcSopenharmony_ci    MessageParcel reply;
163eace7efcSopenharmony_ci    MessageOption option;
164eace7efcSopenharmony_ci
165eace7efcSopenharmony_ci    if (!WriteInterfaceToken(data)) {
166eace7efcSopenharmony_ci        return IPC_PARCEL_ERROR;
167eace7efcSopenharmony_ci    }
168eace7efcSopenharmony_ci
169eace7efcSopenharmony_ci    if (!WriteParam(data, uri, dataObserver)) {
170eace7efcSopenharmony_ci        return INVALID_PARAM;
171eace7efcSopenharmony_ci    }
172eace7efcSopenharmony_ci
173eace7efcSopenharmony_ci    auto error = SendTransactCmd(IDataObsMgr::UNREGISTER_OBSERVER_EXT, data, reply, option);
174eace7efcSopenharmony_ci    if (error != NO_ERROR) {
175eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "sendRequest error:%{public}d, uri:%{public}s", error,
176eace7efcSopenharmony_ci            CommonUtils::Anonymous(uri.ToString()).c_str());
177eace7efcSopenharmony_ci        return IPC_ERROR;
178eace7efcSopenharmony_ci    }
179eace7efcSopenharmony_ci    int32_t res = IPC_ERROR;
180eace7efcSopenharmony_ci    return reply.ReadInt32(res) ? static_cast<Status>(res) : IPC_ERROR;
181eace7efcSopenharmony_ci}
182eace7efcSopenharmony_ci
183eace7efcSopenharmony_ciStatus DataObsManagerProxy::UnregisterObserverExt(sptr<IDataAbilityObserver> dataObserver)
184eace7efcSopenharmony_ci{
185eace7efcSopenharmony_ci    MessageParcel data;
186eace7efcSopenharmony_ci    MessageParcel reply;
187eace7efcSopenharmony_ci    MessageOption option;
188eace7efcSopenharmony_ci
189eace7efcSopenharmony_ci    if (!WriteInterfaceToken(data)) {
190eace7efcSopenharmony_ci        return IPC_PARCEL_ERROR;
191eace7efcSopenharmony_ci    }
192eace7efcSopenharmony_ci
193eace7efcSopenharmony_ci    if (dataObserver == nullptr) {
194eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "null dataObserver");
195eace7efcSopenharmony_ci        return INVALID_PARAM;
196eace7efcSopenharmony_ci    }
197eace7efcSopenharmony_ci
198eace7efcSopenharmony_ci    if (!data.WriteRemoteObject(dataObserver->AsObject())) {
199eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "write dataObserver error");
200eace7efcSopenharmony_ci        return INVALID_PARAM;
201eace7efcSopenharmony_ci    }
202eace7efcSopenharmony_ci
203eace7efcSopenharmony_ci    auto error = SendTransactCmd(IDataObsMgr::UNREGISTER_OBSERVER_ALL_EXT, data, reply, option);
204eace7efcSopenharmony_ci    if (error != NO_ERROR) {
205eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "sendRequest error:%{public}d", error);
206eace7efcSopenharmony_ci        return IPC_ERROR;
207eace7efcSopenharmony_ci    }
208eace7efcSopenharmony_ci    int32_t res = IPC_ERROR;
209eace7efcSopenharmony_ci    return reply.ReadInt32(res) ? static_cast<Status>(res) : IPC_ERROR;
210eace7efcSopenharmony_ci}
211eace7efcSopenharmony_ci
212eace7efcSopenharmony_ciStatus DataObsManagerProxy::NotifyChangeExt(const ChangeInfo &changeInfo)
213eace7efcSopenharmony_ci{
214eace7efcSopenharmony_ci    MessageParcel data;
215eace7efcSopenharmony_ci    MessageParcel reply;
216eace7efcSopenharmony_ci    MessageOption option;
217eace7efcSopenharmony_ci
218eace7efcSopenharmony_ci    if (!WriteInterfaceToken(data)) {
219eace7efcSopenharmony_ci        return IPC_PARCEL_ERROR;
220eace7efcSopenharmony_ci    }
221eace7efcSopenharmony_ci
222eace7efcSopenharmony_ci    if (!ChangeInfo::Marshalling(changeInfo, data)) {
223eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR,
224eace7efcSopenharmony_ci            "changeInfo marshalling error, changeType:%{public}ud, num:%{public}zu,"
225eace7efcSopenharmony_ci            "null data:%{public}d, size:%{public}ud",
226eace7efcSopenharmony_ci            changeInfo.changeType_, changeInfo.uris_.size(), changeInfo.data_ == nullptr, changeInfo.size_);
227eace7efcSopenharmony_ci        return INVALID_PARAM;
228eace7efcSopenharmony_ci    }
229eace7efcSopenharmony_ci
230eace7efcSopenharmony_ci    auto error = SendTransactCmd(IDataObsMgr::NOTIFY_CHANGE_EXT, data, reply, option);
231eace7efcSopenharmony_ci    if (error != NO_ERROR) {
232eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR,
233eace7efcSopenharmony_ci            "sendRequest error: %{public}d, changeType:%{public}ud, num:%{public}zu,"
234eace7efcSopenharmony_ci            "null data:%{public}d, size:%{public}ud",
235eace7efcSopenharmony_ci            error, changeInfo.changeType_, changeInfo.uris_.size(), changeInfo.data_ == nullptr, changeInfo.size_);
236eace7efcSopenharmony_ci        return IPC_ERROR;
237eace7efcSopenharmony_ci    }
238eace7efcSopenharmony_ci    int32_t res = IPC_ERROR;
239eace7efcSopenharmony_ci    return reply.ReadInt32(res) ? static_cast<Status>(res) : IPC_ERROR;
240eace7efcSopenharmony_ci}
241eace7efcSopenharmony_ci
242eace7efcSopenharmony_ciint32_t DataObsManagerProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
243eace7efcSopenharmony_ci    MessageParcel &reply, MessageOption &option)
244eace7efcSopenharmony_ci{
245eace7efcSopenharmony_ci    sptr<IRemoteObject> remote = Remote();
246eace7efcSopenharmony_ci    if (remote == nullptr) {
247eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "null remote");
248eace7efcSopenharmony_ci        return ERR_NULL_OBJECT;
249eace7efcSopenharmony_ci    }
250eace7efcSopenharmony_ci
251eace7efcSopenharmony_ci    int32_t ret = remote->SendRequest(code, data, reply, option);
252eace7efcSopenharmony_ci    if (ret != NO_ERROR) {
253eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "sendRequest errorCode:%{public}d, ret:%{public}d", code, ret);
254eace7efcSopenharmony_ci        return ret;
255eace7efcSopenharmony_ci    }
256eace7efcSopenharmony_ci    return NO_ERROR;
257eace7efcSopenharmony_ci}
258eace7efcSopenharmony_ci}  // namespace AAFwk
259eace7efcSopenharmony_ci}  // namespace OHOS
260