• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "ObjectServiceProxy"
17 #include "object_service_proxy.h"
18 
19 #include <logger.h>
20 #include "itypes_util.h"
21 #include "log_print.h"
22 #include "objectstore_errors.h"
23 
24 namespace OHOS::DistributedObject {
25 using namespace ObjectStore;
26 
27 using ObjectCode = ObjectStoreService::ObjectServiceInterfaceCode;
28 
ObjectServiceProxy(const sptr<IRemoteObject> & impl)29 ObjectServiceProxy::ObjectServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IObjectService>(impl)
30 {
31     ZLOGI("init service proxy.");
32 }
33 
ObjectStoreSave(const std::string & bundleName,const std::string & sessionId,const std::string & deviceId,const std::map<std::string,std::vector<uint8_t>> & objectData,sptr<IRemoteObject> callback)34 int32_t ObjectServiceProxy::ObjectStoreSave(const std::string &bundleName, const std::string &sessionId,
35     const std::string &deviceId, const std::map<std::string, std::vector<uint8_t>> &objectData,
36     sptr<IRemoteObject> callback)
37 {
38     MessageParcel data;
39     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
40         ZLOGE("write descriptor failed");
41         return ERR_IPC;
42     }
43     if (!ITypesUtil::Marshal(data, bundleName, sessionId, deviceId, objectData, callback)) {
44         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
45         return ERR_IPC;
46     }
47     MessageParcel reply;
48     MessageOption mo { MessageOption::TF_SYNC };
49     sptr<IRemoteObject> remoteObject = Remote();
50     if (remoteObject == nullptr) {
51         LOG_ERROR("ObjectStoreSave remoteObject is nullptr.");
52         return ERR_IPC;
53     }
54     int32_t error = remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_SAVE), data, reply, mo);
55     if (error != 0) {
56         ZLOGE("SendRequest returned %d", error);
57         return ERR_IPC;
58     }
59     return reply.ReadInt32();
60 }
61 
ObjectStoreRevokeSave(const std::string & bundleName,const std::string & sessionId,sptr<IRemoteObject> callback)62 int32_t ObjectServiceProxy::ObjectStoreRevokeSave(
63     const std::string &bundleName, const std::string &sessionId, sptr<IRemoteObject> callback)
64 {
65     MessageParcel data;
66     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
67         ZLOGE("write descriptor failed");
68         return ERR_IPC;
69     }
70 
71     if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback)) {
72         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
73         return ERR_IPC;
74     }
75 
76     MessageParcel reply;
77     MessageOption mo { MessageOption::TF_SYNC };
78     sptr<IRemoteObject> remoteObject = Remote();
79     if (remoteObject == nullptr) {
80         LOG_ERROR("ObjectStoreRevokeSave remoteObject is nullptr.");
81         return ERR_IPC;
82     }
83     int32_t error =
84         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_REVOKE_SAVE), data, reply, mo);
85     if (error != 0) {
86         ZLOGE("SendRequest returned %d", error);
87         return ERR_IPC;
88     }
89     return reply.ReadInt32();
90 }
91 
ObjectStoreRetrieve(const std::string & bundleName,const std::string & sessionId,sptr<IRemoteObject> callback)92 int32_t ObjectServiceProxy::ObjectStoreRetrieve(
93     const std::string &bundleName, const std::string &sessionId, sptr<IRemoteObject> callback)
94 {
95     MessageParcel data;
96     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
97         ZLOGE("write descriptor failed");
98         return ERR_IPC;
99     }
100 
101     if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback)) {
102         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
103         return ERR_IPC;
104     }
105 
106     MessageParcel reply;
107     MessageOption mo { MessageOption::TF_SYNC };
108     sptr<IRemoteObject> remoteObject = Remote();
109     if (remoteObject == nullptr) {
110         LOG_ERROR("ObjectStoreRetrieve remoteObject is nullptr.");
111         return ERR_IPC;
112     }
113     int32_t error =
114         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_RETRIEVE), data, reply, mo);
115     if (error != 0) {
116         ZLOGE("SendRequest returned %d", error);
117         return ERR_IPC;
118     }
119     return reply.ReadInt32();
120 }
121 
RegisterDataObserver(const std::string & bundleName,const std::string & sessionId,sptr<IRemoteObject> callback)122 int32_t ObjectServiceProxy::RegisterDataObserver(const std::string &bundleName,
123                                                  const std::string &sessionId, sptr<IRemoteObject> callback)
124 {
125     MessageParcel data;
126     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
127         ZLOGE("write descriptor failed");
128         return ERR_IPC;
129     }
130 
131     if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback)) {
132         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
133         return ERR_IPC;
134     }
135 
136     MessageParcel reply;
137     MessageOption mo { MessageOption::TF_SYNC };
138     sptr<IRemoteObject> remoteObject = Remote();
139     if (remoteObject == nullptr) {
140         LOG_ERROR("RegisterDataObserver remoteObject is nullptr.");
141         return ERR_IPC;
142     }
143     int32_t error =
144         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_REGISTER_OBSERVER), data, reply, mo);
145     if (error != 0) {
146         ZLOGE("SendRequest returned %d", error);
147         return ERR_IPC;
148     }
149     return reply.ReadInt32();
150 }
151 
UnregisterDataChangeObserver(const std::string & bundleName,const std::string & sessionId)152 int32_t ObjectServiceProxy::UnregisterDataChangeObserver(const std::string &bundleName, const std::string &sessionId)
153 {
154     MessageParcel data;
155     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
156         ZLOGE("write descriptor failed");
157         return ERR_IPC;
158     }
159 
160     if (!ITypesUtil::Marshal(data, bundleName, sessionId)) {
161         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
162         return ERR_IPC;
163     }
164 
165     MessageParcel reply;
166     MessageOption mo { MessageOption::TF_SYNC };
167     sptr<IRemoteObject> remoteObject = Remote();
168     if (remoteObject == nullptr) {
169         LOG_ERROR("UnregisterDataChangeObserver remoteObject is nullptr.");
170         return ERR_IPC;
171     }
172     int32_t error =
173         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_UNREGISTER_OBSERVER), data, reply, mo);
174     if (error != 0) {
175         ZLOGE("SendRequest returned %d", error);
176         return ERR_IPC;
177     }
178     return reply.ReadInt32();
179 }
180 } // namespace OHOS::DistributedObject
181