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 "RdbNotifierStub"
16 #include "rdb_notifier_stub.h"
17 
18 #include <ipc_skeleton.h>
19 
20 #include "itypes_util.h"
21 #include "logger.h"
22 
23 namespace OHOS::DistributedRdb {
24 using namespace OHOS::Rdb;
25 
RdbNotifierStub(const SyncCompleteHandler & completeNotifier,const AutoSyncCompleteHandler & autoSyncCompleteHandler,const DataChangeHandler & changeNotifier)26 RdbNotifierStub::RdbNotifierStub(const SyncCompleteHandler &completeNotifier,
27     const AutoSyncCompleteHandler &autoSyncCompleteHandler, const DataChangeHandler &changeNotifier)
28     : IRemoteStub<RdbNotifierStubBroker>(), completeNotifier_(completeNotifier),
29       autoSyncCompleteHandler_(autoSyncCompleteHandler), changeNotifier_(changeNotifier)
30 {
31 }
32 
~RdbNotifierStub()33 RdbNotifierStub::~RdbNotifierStub() noexcept
34 {
35 }
36 
CheckInterfaceToken(MessageParcel & data)37 bool RdbNotifierStub::CheckInterfaceToken(MessageParcel& data)
38 {
39     auto localDescriptor = GetDescriptor();
40     auto remoteDescriptor = data.ReadInterfaceToken();
41     if (remoteDescriptor != localDescriptor) {
42         LOG_ERROR("interface token is not equal.");
43         return false;
44     }
45     return true;
46 }
47 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int RdbNotifierStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
49                                      MessageOption &option)
50 {
51     LOG_DEBUG("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
52     if (!CheckInterfaceToken(data)) {
53         return RDB_ERROR;
54     }
55 
56     if (code >= 0 && code < static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_MAX)) {
57         return (this->*HANDLES[code])(data, reply);
58     }
59 
60     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61 }
62 
OnCompleteInner(MessageParcel & data,MessageParcel & reply)63 int32_t RdbNotifierStub::OnCompleteInner(MessageParcel &data, MessageParcel &reply)
64 {
65     uint32_t seqNum = 0;
66     Details result;
67     if (!ITypesUtil::Unmarshal(data, seqNum, result)) {
68         LOG_ERROR("read sync result failed.");
69         return RDB_ERROR;
70     }
71     return OnComplete(seqNum, std::move(result));
72 }
73 
OnComplete(uint32_t seqNum,Details && result)74 int32_t RdbNotifierStub::OnComplete(uint32_t seqNum, Details &&result)
75 {
76     if (completeNotifier_) {
77         completeNotifier_(seqNum, std::move(result));
78     }
79     return RDB_OK;
80 }
81 
OnComplete(const std::string & storeName,Details && result)82 int32_t RdbNotifierStub::OnComplete(const std::string &storeName, Details &&result)
83 {
84     if (autoSyncCompleteHandler_) {
85         autoSyncCompleteHandler_(storeName, std::move(result));
86     }
87     return RDB_OK;
88 }
89 
OnChange(const Origin & origin,const PrimaryFields & primaries,ChangeInfo && changeInfo)90 int32_t RdbNotifierStub::OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo)
91 {
92     if (changeNotifier_ != nullptr) {
93         changeNotifier_(origin, primaries, std::move(changeInfo));
94     }
95     return RDB_OK;
96 }
97 
OnChangeInner(MessageParcel & data,MessageParcel & reply)98 int32_t RdbNotifierStub::OnChangeInner(MessageParcel &data, MessageParcel &reply)
99 {
100     Origin origin;
101     PrimaryFields primaries;
102     ChangeInfo changeInfo;
103     if (!ITypesUtil::Unmarshal(data, origin, primaries, changeInfo)) {
104         LOG_ERROR("read sync result failed.");
105         return RDB_ERROR;
106     }
107     return OnChange(origin, primaries, std::move(changeInfo));
108 }
109 
OnAutoSyncCompleteInner(MessageParcel & data,MessageParcel & reply)110 int32_t RdbNotifierStub::OnAutoSyncCompleteInner(MessageParcel &data, MessageParcel &reply)
111 {
112     std::string storeName;
113     Details result;
114     if (!ITypesUtil::Unmarshal(data, storeName, result)) {
115         LOG_ERROR("read sync result failed.");
116         return RDB_ERROR;
117     }
118     return OnComplete(storeName, std::move(result));
119 }
120 } // namespace OHOS::DistributedRdb
121