• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "KVDBObserverProxy"
17 
18 #include "kvdb_observer_proxy.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     CLOUD_ONCHANGE,
32     ONCHANGE,
33 };
34 
KVDBObserverProxy(const sptr<IRemoteObject> & impl)35 KVDBObserverProxy::KVDBObserverProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IKvStoreObserver>(impl)
36 {
37 }
38 
OnChange(const ChangeNotification & changeNotification)39 void KVDBObserverProxy::OnChange(const ChangeNotification &changeNotification)
40 {
41     MessageParcel data;
42     MessageParcel reply;
43     if (!data.WriteInterfaceToken(KVDBObserverProxy::GetDescriptor())) {
44         ZLOGE("Write descriptor failed");
45         return;
46     }
47     int64_t insertSize = ITypesUtil::GetTotalSize(changeNotification.GetInsertEntries());
48     int64_t updateSize = ITypesUtil::GetTotalSize(changeNotification.GetUpdateEntries());
49     int64_t deleteSize = ITypesUtil::GetTotalSize(changeNotification.GetDeleteEntries());
50     int64_t totalSize = insertSize + updateSize + deleteSize + sizeof(uint32_t);
51     if (insertSize < 0 || updateSize < 0 || deleteSize < 0 || !data.WriteInt32(totalSize)) {
52         ZLOGE("Write ChangeNotification buffer size to parcel failed.");
53         return;
54     }
55     ZLOGD("I(%" PRId64 ") U(%" PRId64 ") D(%" PRId64 ") T(%" PRId64 ")", insertSize, updateSize, deleteSize, totalSize);
56     if (totalSize < SWITCH_RAW_DATA_SIZE) {
57         if (!ITypesUtil::Marshal(data, changeNotification)) {
58             ZLOGW("Write ChangeNotification to parcel failed.");
59             return;
60         }
61     } else {
62         if (!ITypesUtil::Marshal(data, changeNotification.GetDeviceId(), uint32_t(changeNotification.IsClear())) ||
63             !ITypesUtil::MarshalToBuffer(changeNotification.GetInsertEntries(), insertSize, data) ||
64             !ITypesUtil::MarshalToBuffer(changeNotification.GetUpdateEntries(), updateSize, data) ||
65             !ITypesUtil::MarshalToBuffer(changeNotification.GetDeleteEntries(), deleteSize, data)) {
66             ZLOGE("WriteChangeList to Parcel by buffer failed");
67             return;
68         }
69     }
70 
71     MessageOption mo{ MessageOption::TF_WAIT_TIME };
72     int error = Remote()->SendRequest(ONCHANGE, data, reply, mo);
73     if (error != 0) {
74         ZLOGE("SendRequest failed, error %d", error);
75     }
76 }
77 
OnChange(const DataOrigin & origin,Keys && keys)78 void KVDBObserverProxy::OnChange(const DataOrigin &origin, Keys &&keys)
79 {
80     MessageParcel data;
81     MessageParcel reply;
82     if (!data.WriteInterfaceToken(KVDBObserverProxy::GetDescriptor())) {
83         ZLOGE("Write descriptor failed");
84         return;
85     }
86     if (!ITypesUtil::Marshal(data, origin.store, keys[OP_INSERT], keys[OP_UPDATE], keys[OP_DELETE])) {
87         ZLOGE("WriteChangeInfo to Parcel failed.");
88         return;
89     }
90 
91     MessageOption mo{ MessageOption::TF_WAIT_TIME };
92     int error = Remote()->SendRequest(CLOUD_ONCHANGE, data, reply, mo);
93     if (error != 0) {
94         ZLOGE("SendRequest failed, error %d", error);
95     }
96 }
97 } // namespace DistributedKv
98 } // namespace OHOS