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 "DataMgrServiceProxy"
17
18 #include "datamgr_service_proxy.h"
19 #include <ipc_skeleton.h>
20 #include "itypes_util.h"
21 #include "message_parcel.h"
22 #include "types.h"
23 #include "log_print.h"
24
25 namespace OHOS {
26 namespace DistributedKv {
DataMgrServiceProxy(const sptr<IRemoteObject> & impl)27 DataMgrServiceProxy::DataMgrServiceProxy(const sptr<IRemoteObject> &impl)
28 : IRemoteProxy<IKvStoreDataService>(impl)
29 {
30 ZLOGI("init data service proxy.");
31 }
32
GetFeatureInterface(const std::string & name)33 sptr<IRemoteObject> DataMgrServiceProxy::GetFeatureInterface(const std::string &name)
34 {
35 ZLOGI("%s", name.c_str());
36 MessageParcel data;
37 if (!data.WriteInterfaceToken(DataMgrServiceProxy::GetDescriptor())) {
38 ZLOGE("write descriptor failed");
39 return nullptr;
40 }
41
42 if (!ITypesUtil::Marshal(data, name)) {
43 ZLOGE("write name failed, name is %{public}s", name.c_str());
44 return nullptr;
45 }
46
47 MessageParcel reply;
48 MessageOption mo { MessageOption::TF_SYNC };
49 int32_t error = Remote()->SendRequest(
50 static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::GET_FEATURE_INTERFACE), data, reply, mo);
51 if (error != 0) {
52 ZLOGE("SendRequest returned %{public}d", error);
53 return nullptr;
54 }
55
56 sptr<IRemoteObject> remoteObject;
57 if (!ITypesUtil::Unmarshal(reply, remoteObject)) {
58 ZLOGE("remote object is nullptr");
59 return nullptr;
60 }
61 return remoteObject;
62 }
63
RegisterClientDeathObserver(const AppId & appId,sptr<IRemoteObject> observer)64 Status DataMgrServiceProxy::RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer)
65 {
66 MessageParcel data;
67 MessageParcel reply;
68 if (!data.WriteInterfaceToken(DataMgrServiceProxy::GetDescriptor())) {
69 ZLOGE("write descriptor failed");
70 return Status::IPC_ERROR;
71 }
72 if (!data.WriteString(appId.appId)) {
73 ZLOGW("failed to write string.");
74 return Status::IPC_ERROR;
75 }
76 if (observer != nullptr) {
77 if (!data.WriteRemoteObject(observer)) {
78 ZLOGW("failed to write parcel.");
79 return Status::IPC_ERROR;
80 }
81 } else {
82 ZLOGE("observer is null");
83 return Status::INVALID_ARGUMENT;
84 }
85
86 MessageOption mo { MessageOption::TF_SYNC };
87 int32_t error = Remote()->SendRequest(
88 static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::REGISTERCLIENTDEATHOBSERVER), data, reply, mo);
89 if (error != 0) {
90 ZLOGW("failed during IPC. errCode %d", error);
91 return Status::IPC_ERROR;
92 }
93 return static_cast<Status>(reply.ReadInt32());
94 }
95
ClearAppStorage(const std::string & bundleName,int32_t userId,int32_t appIndex,int32_t tokenId)96 int32_t DataMgrServiceProxy::ClearAppStorage(const std::string &bundleName, int32_t userId, int32_t appIndex,
97 int32_t tokenId)
98 {
99 MessageParcel data;
100 MessageParcel reply;
101 if (!data.WriteInterfaceToken(DataMgrServiceProxy::GetDescriptor())) {
102 ZLOGE("write descriptor failed");
103 return Status::IPC_ERROR;
104 }
105 if (!ITypesUtil::Marshal(data, bundleName, userId, appIndex, tokenId)) {
106 ZLOGW("failed to write bundleName:%{public}s, user:%{public}d, appIndex:%{public}d, tokenID:%{public}d",
107 bundleName.c_str(), userId, appIndex, tokenId);
108 return Status::IPC_ERROR;
109 }
110
111 MessageOption mo { MessageOption::TF_SYNC };
112 int32_t error = Remote()->SendRequest(
113 static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::CLEAR_APP_STORAGE), data, reply, mo);
114 if (error != 0) {
115 ZLOGW("failed during IPC. errCode %d", error);
116 return Status::IPC_ERROR;
117 }
118 return static_cast<Status>(reply.ReadInt32());
119 }
120 } // namespace DistributedKv
121 } // namespace OHOS
122