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, MessageOption &option)
49 {
50 LOG_DEBUG("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
51 if (!CheckInterfaceToken(data)) {
52 return RDB_ERROR;
53 }
54
55 if (code < static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_MAX)) {
56 return (this->*HANDLES[code])(data, reply);
57 }
58
59 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61
OnCompleteInner(MessageParcel & data,MessageParcel & reply)62 int32_t RdbNotifierStub::OnCompleteInner(MessageParcel &data, MessageParcel &reply)
63 {
64 uint32_t seqNum = 0;
65 Details result;
66 if (!ITypesUtil::Unmarshal(data, seqNum, result)) {
67 LOG_ERROR("read sync result failed.");
68 return RDB_ERROR;
69 }
70 return OnComplete(seqNum, std::move(result));
71 }
72
OnComplete(uint32_t seqNum,Details && result)73 int32_t RdbNotifierStub::OnComplete(uint32_t seqNum, Details &&result)
74 {
75 if (completeNotifier_) {
76 completeNotifier_(seqNum, std::move(result));
77 }
78 return RDB_OK;
79 }
80
OnComplete(const std::string & storeName,Details && result)81 int32_t RdbNotifierStub::OnComplete(const std::string &storeName, Details &&result)
82 {
83 if (autoSyncCompleteHandler_) {
84 autoSyncCompleteHandler_(storeName, std::move(result));
85 }
86 return RDB_OK;
87 }
88
OnChange(const Origin & origin,const PrimaryFields & primaries,ChangeInfo && changeInfo)89 int32_t RdbNotifierStub::OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo)
90 {
91 if (changeNotifier_ != nullptr) {
92 changeNotifier_(origin, primaries, std::move(changeInfo));
93 }
94 return RDB_OK;
95 }
96
OnChangeInner(MessageParcel & data,MessageParcel & reply)97 int32_t RdbNotifierStub::OnChangeInner(MessageParcel &data, MessageParcel &reply)
98 {
99 Origin origin;
100 PrimaryFields primaries;
101 ChangeInfo changeInfo;
102 if (!ITypesUtil::Unmarshal(data, origin, primaries, changeInfo)) {
103 LOG_ERROR("read sync result failed.");
104 return RDB_ERROR;
105 }
106 return OnChange(origin, primaries, std::move(changeInfo));
107 }
108
OnAutoSyncCompleteInner(MessageParcel & data,MessageParcel & reply)109 int32_t RdbNotifierStub::OnAutoSyncCompleteInner(MessageParcel &data, MessageParcel &reply)
110 {
111 std::string storeName;
112 Details result;
113 if (!ITypesUtil::Unmarshal(data, storeName, result)) {
114 LOG_ERROR("read sync result failed.");
115 return RDB_ERROR;
116 }
117 return OnComplete(storeName, std::move(result));
118 }
119 } // namespace OHOS::DistributedRdb
120