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 #include "ikvstore_data_service.h"
17 #include "datashare_itypes_utils.h"
18 #include "datashare_log.h"
19
20 using namespace OHOS::DistributedShare::DataShare;
21
22 namespace OHOS {
23 namespace DataShare {
DataShareKvServiceProxy(const sptr<IRemoteObject> & impl)24 DataShareKvServiceProxy::DataShareKvServiceProxy(const sptr<IRemoteObject> &impl)
25 : IRemoteProxy<IKvStoreDataService>(impl)
26 {
27 LOG_DEBUG("Init data service proxy.");
28 }
29
GetFeatureInterface(const std::string & name)30 sptr<IRemoteObject> DataShareKvServiceProxy::GetFeatureInterface(const std::string &name)
31 {
32 MessageParcel data;
33 if (!data.WriteInterfaceToken(DataShareKvServiceProxy::GetDescriptor())) {
34 LOG_ERROR("Write descriptor failed");
35 return nullptr;
36 }
37 if (!data.WriteString(name)) {
38 LOG_ERROR("Write name failed");
39 return nullptr;
40 }
41
42 MessageParcel reply;
43 MessageOption mo { MessageOption::TF_SYNC };
44 int32_t error = Remote()->SendRequest(
45 static_cast<uint32_t>(IKvStoreDataInterfaceCode::GET_FEATURE_INTERFACE), data, reply, mo);
46 if (error != 0) {
47 LOG_ERROR("SendRequest returned %{public}d", error);
48 return nullptr;
49 }
50 auto remoteObject = reply.ReadRemoteObject();
51 if (remoteObject == nullptr) {
52 LOG_ERROR("Remote object is nullptr!");
53 return nullptr;
54 }
55 return remoteObject;
56 }
57
RegisterClientDeathObserver(const std::string & appId,sptr<IRemoteObject> observer)58 uint32_t DataShareKvServiceProxy::RegisterClientDeathObserver(const std::string &appId, sptr<IRemoteObject> observer)
59 {
60 if (observer == nullptr) {
61 LOG_ERROR("observer is nullptr");
62 return -1;
63 }
64
65 MessageParcel data;
66 MessageParcel reply;
67 if (!data.WriteInterfaceToken(DataShareKvServiceProxy::GetDescriptor())) {
68 LOG_ERROR("write descriptor failed");
69 return -1;
70 }
71 if (!ITypesUtil::Marshal(data, appId, observer)) {
72 LOG_ERROR("remote observer fail");
73 return -1;
74 }
75 MessageOption mo { MessageOption::TF_SYNC };
76 int32_t error = Remote()->SendRequest(
77 static_cast<uint32_t>(IKvStoreDataInterfaceCode::REGISTERCLIENTDEATHOBSERVER), data, reply, mo);
78 if (error != 0) {
79 LOG_WARN("failed during IPC. errCode %{public}d", error);
80 return -1;
81 }
82 return static_cast<uint32_t>(reply.ReadInt32());
83 }
84 }
85 }
86