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