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