• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NOTIFIER_H
17 #define DISTRIBUTED_RDB_NOTIFIER_H
18 
19 #include <iremote_broker.h>
20 #include <iremote_proxy.h>
21 #include <iremote_stub.h>
22 
23 #include "rdb_types.h"
24 #include "visibility.h"
25 
26 namespace OHOS::DistributedRdb {
27 class API_EXPORT IRdbNotifier : public IRemoteBroker {
28 public:
29     enum {
30         RDB_NOTIFIER_CMD_SYNC_COMPLETE,
31         RDB_NOTIFIER_CMD_DATA_CHANGE,
32         RDB_NOTIFIER_CMD_MAX
33     };
34 
35     virtual int32_t OnComplete(uint32_t seqNum, const SyncResult& result) = 0;
36 
37     virtual int32_t OnChange(const std::string& storeName, const std::vector<std::string>& devices) = 0;
38 
39     DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbNotifier");
40 };
41 
42 class API_EXPORT RdbNotifierProxy : public IRemoteProxy<IRdbNotifier> {
43 public:
44     explicit RdbNotifierProxy(const sptr<IRemoteObject>& object);
45     virtual ~RdbNotifierProxy() noexcept;
46 
47     int32_t OnComplete(uint32_t seqNum, const SyncResult& result) override;
48 
49     int32_t OnChange(const std::string& storeName, const std::vector<std::string>& devices) override;
50 
51 private:
52     static inline BrokerDelegator<RdbNotifierProxy> delegator_;
53 };
54 
55 using RdbSyncCompleteNotifier = std::function<void(uint32_t, const SyncResult&)>;
56 using RdbDataChangeNotifier = std::function<void(const std::string&, const std::vector<std::string>&)>;
57 
58 class API_EXPORT RdbNotifierStub : public IRemoteStub<IRdbNotifier> {
59 public:
60     RdbNotifierStub(const RdbSyncCompleteNotifier&, const RdbDataChangeNotifier&);
61     virtual ~RdbNotifierStub() noexcept;
62 
63     int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
64 
65     int32_t OnCompleteInner(MessageParcel& data, MessageParcel& reply);
66     int32_t OnComplete(uint32_t seqNum, const SyncResult& result) override;
67 
68     int32_t OnChangeInner(MessageParcel&data, MessageParcel& reply);
69     int32_t OnChange(const std::string& storeName, const std::vector<std::string>& devices) override;
70 
71 private:
72     bool CheckInterfaceToken(MessageParcel& data);
73 
74     using RequestHandle = int32_t (RdbNotifierStub::*)(MessageParcel&, MessageParcel&);
75     static constexpr RequestHandle HANDLES[RDB_NOTIFIER_CMD_MAX] = {
76         [RDB_NOTIFIER_CMD_SYNC_COMPLETE] = &RdbNotifierStub::OnCompleteInner,
77         [RDB_NOTIFIER_CMD_DATA_CHANGE] = &RdbNotifierStub::OnChangeInner,
78     };
79 
80     RdbSyncCompleteNotifier completeNotifier_;
81     RdbDataChangeNotifier changeNotifier_;
82 };
83 } // namespace OHOS::DistributedRdb
84 #endif
85