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 #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_OBSERVER_BRIDGE_H
16 #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_OBSERVER_BRIDGE_H
17 #include "convertor.h"
18 #include "kv_store_nb_delegate.h"
19 #include "kv_store_observer.h"
20 #include "kvstore_observer.h"
21 #include "kvstore_observer_client.h"
22 namespace OHOS::DistributedKv {
23 class IKvStoreObserver;
24 class ObserverBridge : public DistributedDB::KvStoreObserver {
25 public:
26     using Observer = DistributedKv::KvStoreObserver;
27     using DBEntry = DistributedDB::Entry;
28     using DBKey = DistributedDB::Key;
29     using DBChangedData = DistributedDB::KvStoreChangedData;
30 
31     ObserverBridge(AppId appId, StoreId storeId, std::shared_ptr<Observer> observer, const Convertor &cvt);
32     ~ObserverBridge();
33     Status RegisterRemoteObserver(uint32_t realType);
34     Status UnregisterRemoteObserver(uint32_t realType);
35     void OnChange(const DBChangedData &data) override;
36     void OnServiceDeath();
37 
38 private:
39     class ObserverClient : public KvStoreObserverClient {
40     public:
41         ObserverClient(std::shared_ptr<Observer> observer, const Convertor &cvt);
42         void OnChange(const ChangeNotification &data) override __attribute__((no_sanitize("cfi")));
43         void OnChange(const DataOrigin &origin, Keys &&keys) override __attribute__((no_sanitize("cfi")));
44 
45     private:
46         friend class ObserverBridge;
47         const Convertor &convert_;
48         uint32_t realType_;
49     };
50 
51     template<class T>
52     static std::vector<Entry> ConvertDB(const T &dbEntries, std::string &deviceId, const Convertor &convert);
53     AppId appId_;
54     StoreId storeId_;
55     std::shared_ptr<DistributedKv::KvStoreObserver> observer_;
56     sptr<ObserverClient> remote_;
57     const Convertor &convert_;
58     std::mutex mutex_;
59 };
60 } // namespace OHOS::DistributedKv
61 #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_OBSERVER_BRIDGE_H
62