1 /*
2  * Copyright (c) 2022 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 #define LOG_TAG "RdbNotifierProxy"
16 #include "rdb_notifier_proxy.h"
17 #include "itypes_util.h"
18 #include "log_print.h"
19 #include "utils/anonymous.h"
20 namespace OHOS::DistributedRdb {
21 using NotifierIFCode = RelationalStore::IRdbNotifierInterfaceCode;
22 
RdbNotifierProxy(const sptr<IRemoteObject> & object)23 RdbNotifierProxy::RdbNotifierProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<RdbNotifierProxyBroker>(object)
24 {
25 }
26 
~RdbNotifierProxy()27 RdbNotifierProxy::~RdbNotifierProxy() noexcept
28 {
29 }
30 
OnComplete(uint32_t seqNum,Details && result)31 int32_t RdbNotifierProxy::OnComplete(uint32_t seqNum, Details &&result)
32 {
33     MessageParcel data;
34     if (!data.WriteInterfaceToken(GetDescriptor())) {
35         ZLOGE("write descriptor failed");
36         return RDB_ERROR;
37     }
38     if (!ITypesUtil::Marshal(data, seqNum, result)) {
39         return RDB_ERROR;
40     }
41 
42     MessageParcel reply;
43     MessageOption option(MessageOption::TF_ASYNC);
44     if (Remote()->SendRequest(
45         static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_SYNC_COMPLETE), data, reply, option) != 0) {
46         ZLOGE("seqNum:%{public}u, send request failed", seqNum);
47         return RDB_ERROR;
48     }
49     return RDB_OK;
50 }
51 
OnChange(const Origin & origin,const PrimaryFields & primaries,ChangeInfo && changeInfo)52 int32_t RdbNotifierProxy::OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo)
53 {
54     MessageParcel data;
55     if (!data.WriteInterfaceToken(GetDescriptor())) {
56         ZLOGE("write descriptor failed");
57         return RDB_ERROR;
58     }
59     if (!ITypesUtil::Marshal(data, origin, primaries, changeInfo)) {
60         ZLOGE("write store name or devices failed");
61         return RDB_ERROR;
62     }
63 
64     MessageParcel reply;
65     MessageOption option(MessageOption::TF_ASYNC);
66     if (Remote()->SendRequest(
67         static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_DATA_CHANGE), data, reply, option) != 0) {
68         ZLOGE("storeName:%{public}s, send request failed", DistributedData::Anonymous::Change(origin.store).c_str());
69         return RDB_ERROR;
70     }
71     return RDB_OK;
72 }
73 
OnComplete(const std::string & storeName,Details && result)74 int32_t RdbNotifierProxy::OnComplete(const std::string& storeName, Details&& result)
75 {
76     MessageParcel data;
77     if (!data.WriteInterfaceToken(GetDescriptor())) {
78         ZLOGE("write descriptor failed");
79         return RDB_ERROR;
80     }
81     if (!ITypesUtil::Marshal(data, storeName, result)) {
82         return RDB_ERROR;
83     }
84 
85     MessageParcel reply;
86     MessageOption option(MessageOption::TF_ASYNC);
87     if (Remote()->SendRequest(
88         static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_AUTO_SYNC_COMPLETE), data, reply, option) != 0) {
89         ZLOGE("storeName:%{public}s, send request failed", DistributedData::Anonymous::Change(storeName).c_str());
90         return RDB_ERROR;
91     }
92     return RDB_OK;
93 }
94 } // namespace OHOS::DistributedRdb
95