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