1 /*
2  * Copyright (c) 2022 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 #define LOG_TAG "ObjectServiceStub"
17 
18 #include "object_service_stub.h"
19 
20 #include <ipc_skeleton.h>
21 
22 #include "common_types.h"
23 #include "object_types_utils.h"
24 #include "log_print.h"
25 #include "utils/anonymous.h"
26 
27 namespace OHOS::DistributedObject {
28 using namespace DistributedKv;
ObjectStoreSaveOnRemote(MessageParcel & data,MessageParcel & reply)29 int32_t ObjectServiceStub::ObjectStoreSaveOnRemote(MessageParcel &data, MessageParcel &reply)
30 {
31     std::string sessionId;
32     std::string bundleName;
33     std::string deviceId;
34     std::map<std::string, std::vector<uint8_t>> objectData;
35     sptr<IRemoteObject> obj;
36     if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, deviceId, objectData, obj) || obj == nullptr) {
37         ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s deviceId:%{public}s objectData size:%{public}zu, "
38               "callback is nullptr:%{public}d",
39             DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(),
40             DistributedData::Anonymous::Change(deviceId).c_str(), objectData.size(), obj == nullptr);
41         return IPC_STUB_INVALID_DATA_ERR;
42     }
43     int32_t status = ObjectStoreSave(bundleName, sessionId, deviceId, objectData, obj);
44     if (!ITypesUtil::Marshal(reply, status)) {
45         ZLOGE("Marshal status:0x%{public}x", status);
46         return IPC_STUB_WRITE_PARCEL_ERR;
47     }
48     return 0;
49 }
50 
OnAssetChangedOnRemote(MessageParcel & data,MessageParcel & reply)51 int32_t ObjectServiceStub::OnAssetChangedOnRemote(MessageParcel &data, MessageParcel &reply)
52 {
53     std::string sessionId;
54     std::string bundleName;
55     std::string deviceId;
56     if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, deviceId)) {
57         ZLOGE("Unmarshal fail sessionId:%{public}s bundleName:%{public}s deviceId:%{public}s",
58             DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(),
59             DistributedData::Anonymous::Change(deviceId).c_str());
60         return IPC_STUB_INVALID_DATA_ERR;
61     }
62     ObjectStore::Asset assetValue;
63     if (!ITypesUtil::Unmarshalling(assetValue, data)) {
64         ZLOGE("Unmarshal assetValue fail, asset name: %{public}s", assetValue.name.c_str());
65         return IPC_STUB_INVALID_DATA_ERR;
66     }
67     int32_t status = OnAssetChanged(bundleName, sessionId, deviceId, assetValue);
68     if (!ITypesUtil::Marshal(reply, status)) {
69         ZLOGE("Marshal status:0x%{public}x", status);
70         return IPC_STUB_WRITE_PARCEL_ERR;
71     }
72     return status;
73 }
74 
ObjectStoreBindAssetOnRemote(MessageParcel & data,MessageParcel & reply)75 int32_t ObjectServiceStub::ObjectStoreBindAssetOnRemote(MessageParcel &data, MessageParcel &reply)
76 {
77     std::string sessionId;
78     std::string bundleName;
79     ObjectStore::Asset asset;
80     ObjectStore::AssetBindInfo bindInfo;
81     if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, asset, bindInfo)) {
82         ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s assetName:%{public}s bindStore:%{public}s",
83               DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(),
84               asset.name.c_str(), bindInfo.storeName.c_str());
85         return IPC_STUB_INVALID_DATA_ERR;
86     }
87 
88     int32_t status = BindAssetStore(bundleName, sessionId, asset, bindInfo);
89     if (!ITypesUtil::Marshal(reply, status)) {
90         ZLOGE("Marshal status:0x%{public}x", status);
91         return IPC_STUB_WRITE_PARCEL_ERR;
92     }
93     return 0;
94 }
95 
ObjectStoreRevokeSaveOnRemote(MessageParcel & data,MessageParcel & reply)96 int32_t ObjectServiceStub::ObjectStoreRevokeSaveOnRemote(MessageParcel &data, MessageParcel &reply)
97 {
98     std::string sessionId;
99     std::string bundleName;
100     sptr<IRemoteObject> obj;
101     if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj) || obj == nullptr) {
102         ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s, callback is nullptr: %{public}d",
103             DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(), obj == nullptr);
104         return IPC_STUB_INVALID_DATA_ERR;
105     }
106     int32_t status = ObjectStoreRevokeSave(bundleName, sessionId, obj);
107     if (!ITypesUtil::Marshal(reply, status)) {
108         ZLOGE("Marshal status:0x%{public}x", status);
109         return IPC_STUB_WRITE_PARCEL_ERR;
110     }
111     return 0;
112 }
113 
ObjectStoreRetrieveOnRemote(MessageParcel & data,MessageParcel & reply)114 int32_t ObjectServiceStub::ObjectStoreRetrieveOnRemote(MessageParcel &data, MessageParcel &reply)
115 {
116     std::string sessionId;
117     std::string bundleName;
118     sptr<IRemoteObject> obj;
119     if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj) || obj == nullptr) {
120         ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s, callback is nullptr: %{public}d",
121             DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(), obj == nullptr);
122         return IPC_STUB_INVALID_DATA_ERR;
123     }
124     int32_t status = ObjectStoreRetrieve(bundleName, sessionId, obj);
125     if (!ITypesUtil::Marshal(reply, status)) {
126         ZLOGE("Marshal status:0x%{public}x", status);
127         return IPC_STUB_WRITE_PARCEL_ERR;
128     }
129     return 0;
130 }
131 
OnSubscribeRequest(MessageParcel & data,MessageParcel & reply)132 int32_t ObjectServiceStub::OnSubscribeRequest(MessageParcel &data, MessageParcel &reply)
133 {
134     std::string sessionId;
135     std::string bundleName;
136     sptr<IRemoteObject> obj;
137     if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj) || obj == nullptr) {
138         ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s, callback is nullptr: %{public}d",
139             DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(), obj == nullptr);
140         return IPC_STUB_INVALID_DATA_ERR;
141     }
142     int32_t status = RegisterDataObserver(bundleName, sessionId, obj);
143     if (!ITypesUtil::Marshal(reply, status)) {
144         ZLOGE("Marshal status:0x%{public}x", status);
145         return IPC_STUB_WRITE_PARCEL_ERR;
146     }
147     return 0;
148 }
149 
OnUnsubscribeRequest(MessageParcel & data,MessageParcel & reply)150 int32_t ObjectServiceStub::OnUnsubscribeRequest(MessageParcel &data, MessageParcel &reply)
151 {
152     std::string sessionId;
153     std::string bundleName;
154     if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) {
155         ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
156             DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
157         return IPC_STUB_INVALID_DATA_ERR;
158     }
159     int32_t status = UnregisterDataChangeObserver(bundleName, sessionId);
160     if (!ITypesUtil::Marshal(reply, status)) {
161         ZLOGE("Marshal status:0x%{public}x", status);
162         return IPC_STUB_WRITE_PARCEL_ERR;
163     }
164     return 0;
165 }
166 
OnDeleteSnapshot(MessageParcel & data,MessageParcel & reply)167 int32_t ObjectServiceStub::OnDeleteSnapshot(MessageParcel &data, MessageParcel &reply)
168 {
169     std::string sessionId;
170     std::string bundleName;
171     if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) {
172         ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
173               DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
174         return IPC_STUB_INVALID_DATA_ERR;
175     }
176     int32_t status = DeleteSnapshot(bundleName, sessionId);
177     if (!ITypesUtil::Marshal(reply, status)) {
178         ZLOGE("Marshal status:0x%{public}x", status);
179         return IPC_STUB_WRITE_PARCEL_ERR;
180     }
181     return 0;
182 }
183 
OnIsContinue(MessageParcel & data,MessageParcel & reply)184 int32_t ObjectServiceStub::OnIsContinue(MessageParcel &data, MessageParcel &reply)
185 {
186     bool isContinue = false;
187     int32_t status = IsContinue(isContinue);
188     if (!ITypesUtil::Marshal(reply, isContinue)) {
189         ZLOGE("Marshal isContinue failed, isContinue: %{public}d, status: %{public}d", isContinue, status);
190         return -1;
191     }
192     return 0;
193 }
194 
CheckInterfaceToken(MessageParcel & data)195 bool ObjectServiceStub::CheckInterfaceToken(MessageParcel& data)
196 {
197     auto localDescriptor = IObjectService::GetDescriptor();
198     auto remoteDescriptor = data.ReadInterfaceToken();
199     if (remoteDescriptor != localDescriptor) {
200         ZLOGE("interface token is not equal");
201         return false;
202     }
203     return true;
204 }
205 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)206 int ObjectServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply)
207 {
208     ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
209     if (!CheckInterfaceToken(data)) {
210         return -1;
211     }
212     if (code >= 0 && code < static_cast<uint32_t>(ObjectCode::OBJECTSTORE_SERVICE_CMD_MAX)) {
213         return (this->*HANDLERS[code])(data, reply);
214     }
215     return -1;
216 }
217 } // namespace OHOS::DistributedRdb
218