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_stub.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "string_ex.h"
19eace7efcSopenharmony_ci
20eace7efcSopenharmony_ci#include "data_ability_observer_proxy.h"
21eace7efcSopenharmony_ci#include "dataobs_mgr_errors.h"
22eace7efcSopenharmony_ci#include "ipc_skeleton.h"
23eace7efcSopenharmony_ci#include "common_utils.h"
24eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
25eace7efcSopenharmony_ci
26eace7efcSopenharmony_cinamespace OHOS {
27eace7efcSopenharmony_cinamespace AAFwk {
28eace7efcSopenharmony_ciusing Uri = OHOS::Uri;
29eace7efcSopenharmony_ci
30eace7efcSopenharmony_ciconst DataObsManagerStub::RequestFuncType DataObsManagerStub::HANDLES[TRANS_BUTT] = {
31eace7efcSopenharmony_ci    &DataObsManagerStub::RegisterObserverInner,
32eace7efcSopenharmony_ci    &DataObsManagerStub::UnregisterObserverInner,
33eace7efcSopenharmony_ci    &DataObsManagerStub::NotifyChangeInner,
34eace7efcSopenharmony_ci    &DataObsManagerStub::RegisterObserverExtInner,
35eace7efcSopenharmony_ci    &DataObsManagerStub::UnregisterObserverExtInner,
36eace7efcSopenharmony_ci    &DataObsManagerStub::UnregisterObserverExtALLInner,
37eace7efcSopenharmony_ci    &DataObsManagerStub::NotifyChangeExtInner
38eace7efcSopenharmony_ci};
39eace7efcSopenharmony_ci
40eace7efcSopenharmony_ciDataObsManagerStub::DataObsManagerStub() {}
41eace7efcSopenharmony_ci
42eace7efcSopenharmony_ciDataObsManagerStub::~DataObsManagerStub() {}
43eace7efcSopenharmony_ci
44eace7efcSopenharmony_ciint DataObsManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
45eace7efcSopenharmony_ci{
46eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::DBOBSMGR, "code: %{public}d, flags: %{public}d, callingPid:%{public}d", code, option.GetFlags(),
47eace7efcSopenharmony_ci        IPCSkeleton::GetCallingPid());
48eace7efcSopenharmony_ci    std::u16string descriptor = DataObsManagerStub::GetDescriptor();
49eace7efcSopenharmony_ci    std::u16string remoteDescriptor = data.ReadInterfaceToken();
50eace7efcSopenharmony_ci    if (descriptor != remoteDescriptor) {
51eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR,
52eace7efcSopenharmony_ci            "local descriptor≠remote, descriptor:%{public}s, remoteDescriptor:%{public}s",
53eace7efcSopenharmony_ci            CommonUtils::Anonymous(Str16ToStr8(descriptor)).c_str(),
54eace7efcSopenharmony_ci            CommonUtils::Anonymous(Str16ToStr8(remoteDescriptor)).c_str());
55eace7efcSopenharmony_ci        return ERR_INVALID_STATE;
56eace7efcSopenharmony_ci    }
57eace7efcSopenharmony_ci
58eace7efcSopenharmony_ci    if (code < TRANS_HEAD || code >= TRANS_BUTT || HANDLES[code] == nullptr) {
59eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
60eace7efcSopenharmony_ci        return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61eace7efcSopenharmony_ci    }
62eace7efcSopenharmony_ci    return (this->*HANDLES[code])(data, reply);
63eace7efcSopenharmony_ci}
64eace7efcSopenharmony_ci
65eace7efcSopenharmony_ciint DataObsManagerStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
66eace7efcSopenharmony_ci{
67eace7efcSopenharmony_ci    Uri uri(data.ReadString());
68eace7efcSopenharmony_ci    if (uri.ToString().empty()) {
69eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
70eace7efcSopenharmony_ci        return IPC_STUB_INVALID_DATA_ERR;
71eace7efcSopenharmony_ci    }
72eace7efcSopenharmony_ci
73eace7efcSopenharmony_ci    auto remote = data.ReadRemoteObject();
74eace7efcSopenharmony_ci    auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
75eace7efcSopenharmony_ci    int32_t result = RegisterObserver(uri, observer);
76eace7efcSopenharmony_ci    reply.WriteInt32(result);
77eace7efcSopenharmony_ci    return NO_ERROR;
78eace7efcSopenharmony_ci}
79eace7efcSopenharmony_ci
80eace7efcSopenharmony_ciint DataObsManagerStub::UnregisterObserverInner(MessageParcel &data, MessageParcel &reply)
81eace7efcSopenharmony_ci{
82eace7efcSopenharmony_ci    Uri uri(data.ReadString());
83eace7efcSopenharmony_ci    if (uri.ToString().empty()) {
84eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
85eace7efcSopenharmony_ci        return IPC_STUB_INVALID_DATA_ERR;
86eace7efcSopenharmony_ci    }
87eace7efcSopenharmony_ci
88eace7efcSopenharmony_ci    auto remote = data.ReadRemoteObject();
89eace7efcSopenharmony_ci    auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
90eace7efcSopenharmony_ci    int32_t result = UnregisterObserver(uri, observer);
91eace7efcSopenharmony_ci    reply.WriteInt32(result);
92eace7efcSopenharmony_ci    return NO_ERROR;
93eace7efcSopenharmony_ci}
94eace7efcSopenharmony_ci
95eace7efcSopenharmony_ciint DataObsManagerStub::NotifyChangeInner(MessageParcel &data, MessageParcel &reply)
96eace7efcSopenharmony_ci{
97eace7efcSopenharmony_ci    Uri uri(data.ReadString());
98eace7efcSopenharmony_ci    if (uri.ToString().empty()) {
99eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
100eace7efcSopenharmony_ci        return IPC_STUB_INVALID_DATA_ERR;
101eace7efcSopenharmony_ci    }
102eace7efcSopenharmony_ci
103eace7efcSopenharmony_ci    int32_t result = NotifyChange(uri);
104eace7efcSopenharmony_ci    reply.WriteInt32(result);
105eace7efcSopenharmony_ci    return NO_ERROR;
106eace7efcSopenharmony_ci}
107eace7efcSopenharmony_ci
108eace7efcSopenharmony_ciint32_t DataObsManagerStub::RegisterObserverExtInner(MessageParcel &data, MessageParcel &reply)
109eace7efcSopenharmony_ci{
110eace7efcSopenharmony_ci    Uri uri(data.ReadString());
111eace7efcSopenharmony_ci    if (uri.ToString().empty()) {
112eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
113eace7efcSopenharmony_ci        return IPC_STUB_INVALID_DATA_ERR;
114eace7efcSopenharmony_ci    }
115eace7efcSopenharmony_ci    auto remote = data.ReadRemoteObject();
116eace7efcSopenharmony_ci    auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
117eace7efcSopenharmony_ci    bool isDescendants = data.ReadBool();
118eace7efcSopenharmony_ci    reply.WriteInt32(RegisterObserverExt(uri, observer, isDescendants));
119eace7efcSopenharmony_ci    return SUCCESS;
120eace7efcSopenharmony_ci}
121eace7efcSopenharmony_ci
122eace7efcSopenharmony_ciint32_t DataObsManagerStub::UnregisterObserverExtInner(MessageParcel &data, MessageParcel &reply)
123eace7efcSopenharmony_ci{
124eace7efcSopenharmony_ci    Uri uri(data.ReadString());
125eace7efcSopenharmony_ci    if (uri.ToString().empty()) {
126eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
127eace7efcSopenharmony_ci        return IPC_STUB_INVALID_DATA_ERR;
128eace7efcSopenharmony_ci    }
129eace7efcSopenharmony_ci    auto remote = data.ReadRemoteObject();
130eace7efcSopenharmony_ci    auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
131eace7efcSopenharmony_ci
132eace7efcSopenharmony_ci    reply.WriteInt32(UnregisterObserverExt(uri, observer));
133eace7efcSopenharmony_ci    return SUCCESS;
134eace7efcSopenharmony_ci}
135eace7efcSopenharmony_ci
136eace7efcSopenharmony_ciint32_t DataObsManagerStub::UnregisterObserverExtALLInner(MessageParcel &data, MessageParcel &reply)
137eace7efcSopenharmony_ci{
138eace7efcSopenharmony_ci    auto remote = data.ReadRemoteObject();
139eace7efcSopenharmony_ci    auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
140eace7efcSopenharmony_ci    reply.WriteInt32(UnregisterObserverExt(observer));
141eace7efcSopenharmony_ci    return SUCCESS;
142eace7efcSopenharmony_ci}
143eace7efcSopenharmony_ci
144eace7efcSopenharmony_ciint32_t DataObsManagerStub::NotifyChangeExtInner(MessageParcel &data, MessageParcel &reply)
145eace7efcSopenharmony_ci{
146eace7efcSopenharmony_ci    ChangeInfo changeInfo;
147eace7efcSopenharmony_ci    if (!ChangeInfo::Unmarshalling(changeInfo, data)) {
148eace7efcSopenharmony_ci        return IPC_STUB_INVALID_DATA_ERR;
149eace7efcSopenharmony_ci    }
150eace7efcSopenharmony_ci
151eace7efcSopenharmony_ci    reply.WriteInt32(NotifyChangeExt(changeInfo));
152eace7efcSopenharmony_ci    return SUCCESS;
153eace7efcSopenharmony_ci}
154eace7efcSopenharmony_ci}  // namespace AAFwk
155eace7efcSopenharmony_ci}  // namespace OHOS
156