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 
16 #ifndef DISTRIBUTED_RDB_RDB_NOTIFIER_STUB_H
17 #define DISTRIBUTED_RDB_RDB_NOTIFIER_STUB_H
18 #include "rdb_notifier.h"
19 #include <iremote_broker.h>
20 #include <iremote_stub.h>
21 namespace OHOS::DistributedRdb {
22 using NotifierIFCode = RelationalStore::IRdbNotifierInterfaceCode;
23 
24 class RdbNotifierStubBroker : public IRdbNotifier, public IRemoteBroker {
25 public:
26     DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbNotifier");
27 };
28 
29 class RdbNotifierStub : public IRemoteStub<RdbNotifierStubBroker> {
30 public:
31     using SyncCompleteHandler = std::function<void(uint32_t, Details &&)>;
32     using AutoSyncCompleteHandler = std::function<void(std::string, Details &&)>;
33     using DataChangeHandler = std::function<void(const Origin &origin, const PrimaryFields &primaries,
34         ChangeInfo &&changeInfo)>;
35     RdbNotifierStub(const SyncCompleteHandler&, const AutoSyncCompleteHandler&, const DataChangeHandler&);
36     virtual ~RdbNotifierStub() noexcept;
37 
38     int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
39     int32_t OnComplete(uint32_t seqNum, Details &&result) override;
40     int32_t OnComplete(const std::string& storeName, Details &&result) override;
41     int32_t OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) override;
42 
43 private:
44     int32_t OnCompleteInner(MessageParcel& data, MessageParcel& reply);
45     int32_t OnAutoSyncCompleteInner(MessageParcel& data, MessageParcel& reply);
46     int32_t OnChangeInner(MessageParcel& data, MessageParcel& reply);
47     bool CheckInterfaceToken(MessageParcel& data);
48 
49     using RequestHandle = int32_t (RdbNotifierStub::*)(MessageParcel&, MessageParcel&);
50     static constexpr RequestHandle HANDLES[static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_MAX)] = {
51         [static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_SYNC_COMPLETE)] = &RdbNotifierStub::OnCompleteInner,
52         [static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_AUTO_SYNC_COMPLETE)] =
53             &RdbNotifierStub::OnAutoSyncCompleteInner,
54         [static_cast<uint32_t>(NotifierIFCode::RDB_NOTIFIER_CMD_DATA_CHANGE)] = &RdbNotifierStub::OnChangeInner,
55     };
56 
57     SyncCompleteHandler completeNotifier_;
58     AutoSyncCompleteHandler autoSyncCompleteHandler_;
59     DataChangeHandler changeNotifier_;
60 };
61 } // namespace OHOS::DistributedRdb
62 #endif // DISTRIBUTED_RDB_RDB_NOTIFIER_STUB_H
63