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