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