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 "itypes_util.h"
23 #include "log_print.h"
24 #include "utils/anonymous.h"
25
26 namespace OHOS::DistributedObject {
27 using namespace DistributedKv;
ObjectStoreSaveOnRemote(MessageParcel & data,MessageParcel & reply)28 int32_t ObjectServiceStub::ObjectStoreSaveOnRemote(MessageParcel &data, MessageParcel &reply)
29 {
30 std::string sessionId;
31 std::string bundleName;
32 std::string deviceId;
33 std::map<std::string, std::vector<uint8_t>> objectData;
34 sptr<IRemoteObject> obj;
35 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, deviceId, objectData, obj)) {
36 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s deviceId:%{public}s objectData size:%{public}zu",
37 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(),
38 DistributedData::Anonymous::Change(deviceId).c_str(), objectData.size());
39 return IPC_STUB_INVALID_DATA_ERR;
40 }
41 if (obj == nullptr) {
42 ZLOGW("callback null");
43 return -1;
44 }
45 int32_t status = ObjectStoreSave(bundleName, sessionId, deviceId, objectData, obj);
46 if (!ITypesUtil::Marshal(reply, status)) {
47 ZLOGE("Marshal status:0x%{public}x", status);
48 return IPC_STUB_WRITE_PARCEL_ERR;
49 }
50 return 0;
51 }
52
ObjectStoreRevokeSaveOnRemote(MessageParcel & data,MessageParcel & reply)53 int32_t ObjectServiceStub::ObjectStoreRevokeSaveOnRemote(MessageParcel &data, MessageParcel &reply)
54 {
55 std::string sessionId;
56 std::string bundleName;
57 sptr<IRemoteObject> obj;
58 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) {
59 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
60 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
61 return IPC_STUB_INVALID_DATA_ERR;
62 }
63 if (obj == nullptr) {
64 ZLOGW("callback null");
65 return -1;
66 }
67 int32_t status = ObjectStoreRevokeSave(bundleName, sessionId, obj);
68 if (!ITypesUtil::Marshal(reply, status)) {
69 ZLOGE("Marshal status:0x%{public}x", status);
70 return IPC_STUB_WRITE_PARCEL_ERR;
71 }
72 return 0;
73 }
74
ObjectStoreRetrieveOnRemote(MessageParcel & data,MessageParcel & reply)75 int32_t ObjectServiceStub::ObjectStoreRetrieveOnRemote(MessageParcel &data, MessageParcel &reply)
76 {
77 std::string sessionId;
78 std::string bundleName;
79 sptr<IRemoteObject> obj;
80 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) {
81 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
82 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
83 return IPC_STUB_INVALID_DATA_ERR;
84 }
85 if (obj == nullptr) {
86 ZLOGW("callback null");
87 return -1;
88 }
89 int32_t status = ObjectStoreRetrieve(bundleName, sessionId, obj);
90 if (!ITypesUtil::Marshal(reply, status)) {
91 ZLOGE("Marshal status:0x%{public}x", status);
92 return IPC_STUB_WRITE_PARCEL_ERR;
93 }
94 return 0;
95 }
96
OnSubscribeRequest(MessageParcel & data,MessageParcel & reply)97 int32_t ObjectServiceStub::OnSubscribeRequest(MessageParcel &data, MessageParcel &reply)
98 {
99 std::string sessionId;
100 std::string bundleName;
101 sptr<IRemoteObject> obj;
102 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) {
103 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
104 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
105 return IPC_STUB_INVALID_DATA_ERR;
106 }
107 if (obj == nullptr) {
108 ZLOGW("callback null");
109 return -1;
110 }
111 int32_t status = RegisterDataObserver(bundleName, sessionId, obj);
112 if (!ITypesUtil::Marshal(reply, status)) {
113 ZLOGE("Marshal status:0x%{public}x", status);
114 return IPC_STUB_WRITE_PARCEL_ERR;
115 }
116 return 0;
117 }
118
OnUnsubscribeRequest(MessageParcel & data,MessageParcel & reply)119 int32_t ObjectServiceStub::OnUnsubscribeRequest(MessageParcel &data, MessageParcel &reply)
120 {
121 std::string sessionId;
122 std::string bundleName;
123 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) {
124 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
125 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
126 return IPC_STUB_INVALID_DATA_ERR;
127 }
128 int32_t status = UnregisterDataChangeObserver(bundleName, sessionId);
129 if (!ITypesUtil::Marshal(reply, status)) {
130 ZLOGE("Marshal status:0x%{public}x", status);
131 return IPC_STUB_WRITE_PARCEL_ERR;
132 }
133 return 0;
134 }
135
CheckInterfaceToken(MessageParcel & data)136 bool ObjectServiceStub::CheckInterfaceToken(MessageParcel& data)
137 {
138 auto localDescriptor = IObjectService::GetDescriptor();
139 auto remoteDescriptor = data.ReadInterfaceToken();
140 if (remoteDescriptor != localDescriptor) {
141 ZLOGE("interface token is not equal");
142 return false;
143 }
144 return true;
145 }
146
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)147 int ObjectServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply)
148 {
149 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
150 if (!CheckInterfaceToken(data)) {
151 return -1;
152 }
153 if (code >= 0 && code < static_cast<uint32_t>(ObjectCode::OBJECTSTORE_SERVICE_CMD_MAX)) {
154 return (this->*HANDLERS[code])(data, reply);
155 }
156 return -1;
157 }
158 } // namespace OHOS::DistributedRdb
159