1 /*
2 * Copyright (c) 2021 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 "KvStoreObserverProxy"
17
18 #include "ikvstore_observer.h"
19
20 #include <cinttypes>
21 #include <ipc_skeleton.h>
22 #include "kv_types_util.h"
23 #include "itypes_util.h"
24 #include "log_print.h"
25 #include "message_parcel.h"
26 namespace OHOS {
27 namespace DistributedKv {
28 using namespace std::chrono;
29
30 enum {
31 ONCHANGE,
32 };
33
KvStoreObserverProxy(const sptr<IRemoteObject> & impl)34 KvStoreObserverProxy::KvStoreObserverProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IKvStoreObserver>(impl)
35 {
36 }
37
GetBufferSize(const std::vector<Entry> & entries)38 int64_t GetBufferSize(const std::vector<Entry> &entries)
39 {
40 int64_t bufferSize = 0;
41 for (const auto &item : entries) {
42 bufferSize += item.key.RawSize() + item.value.RawSize();
43 }
44 return bufferSize;
45 }
46
OnChange(const ChangeNotification & changeNotification)47 void KvStoreObserverProxy::OnChange(const ChangeNotification &changeNotification)
48 {
49 MessageParcel data;
50 MessageParcel reply;
51 if (!data.WriteInterfaceToken(KvStoreObserverProxy::GetDescriptor())) {
52 ZLOGE("write descriptor failed");
53 return;
54 }
55 int64_t insertSize = ITypesUtil::GetTotalSize(changeNotification.GetInsertEntries());
56 int64_t updateSize = ITypesUtil::GetTotalSize(changeNotification.GetUpdateEntries());
57 int64_t deleteSize = ITypesUtil::GetTotalSize(changeNotification.GetDeleteEntries());
58 int64_t totalSize = insertSize + updateSize + deleteSize + sizeof(uint32_t);
59 if (insertSize < 0 || updateSize < 0 || deleteSize < 0 || !data.WriteInt32(totalSize)) {
60 ZLOGE("Write ChangeNotification buffer size to parcel failed.");
61 return;
62 }
63 ZLOGD("I(%" PRId64 ") U(%" PRId64 ") D(%" PRId64 ") T(%" PRId64 ")", insertSize, updateSize, deleteSize, totalSize);
64 if (totalSize < SWITCH_RAW_DATA_SIZE) {
65 if (!ITypesUtil::Marshal(data, changeNotification)) {
66 ZLOGW("Write ChangeNotification to parcel failed.");
67 return;
68 }
69 } else {
70 if (!ITypesUtil::Marshal(data, changeNotification.GetDeviceId(), uint32_t(changeNotification.IsClear())) ||
71 !ITypesUtil::MarshalToBuffer(changeNotification.GetInsertEntries(), insertSize, data) ||
72 !ITypesUtil::MarshalToBuffer(changeNotification.GetUpdateEntries(), updateSize, data) ||
73 !ITypesUtil::MarshalToBuffer(changeNotification.GetDeleteEntries(), deleteSize, data)) {
74 ZLOGE("WriteChangeList to Parcel by buffer failed");
75 return;
76 }
77 }
78
79 MessageOption mo{ MessageOption::TF_WAIT_TIME };
80 int error = Remote()->SendRequest(ONCHANGE, data, reply, mo);
81 if (error != 0) {
82 ZLOGE("SendRequest failed, error %d", error);
83 }
84 }
85
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)86 int32_t KvStoreObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
87 MessageOption &option)
88 {
89 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
90 std::u16string descriptor = KvStoreObserverStub::GetDescriptor();
91 std::u16string remoteDescriptor = data.ReadInterfaceToken();
92 if (descriptor != remoteDescriptor) {
93 ZLOGE("local descriptor is not equal to remote");
94 return -1;
95 }
96 switch (code) {
97 case ONCHANGE: {
98 const int errorResult = -1;
99 int totalSize = data.ReadInt32();
100 if (totalSize < SWITCH_RAW_DATA_SIZE) {
101 ChangeNotification notification({}, {}, {}, "", false);
102 if (!ITypesUtil::Unmarshal(data, notification)) {
103 ZLOGE("changeNotification is nullptr");
104 return errorResult;
105 }
106 OnChange(notification);
107 } else {
108 std::string deviceId;
109 uint32_t clear = 0;
110 std::vector<Entry> inserts;
111 std::vector<Entry> updates;
112 std::vector<Entry> deletes;
113 if (!ITypesUtil::Unmarshal(data, deviceId, clear) ||
114 !ITypesUtil::UnmarshalFromBuffer(data, inserts) ||
115 !ITypesUtil::UnmarshalFromBuffer(data, updates) ||
116 !ITypesUtil::UnmarshalFromBuffer(data, deletes)) {
117 ZLOGE("WriteChangeList to Parcel by buffer failed");
118 return errorResult;
119 }
120 ChangeNotification change(std::move(inserts), std::move(updates), std::move(deletes), deviceId,
121 clear != 0);
122 OnChange(change);
123 }
124 return 0;
125 }
126 default:
127 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
128 }
129 }
130 } // namespace DistributedKv
131 } // namespace OHOS
132