• 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 #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, int32_t subUser, std::shared_ptr<Observer> observer,
32         const Convertor &cvt);
33     ~ObserverBridge();
34     Status RegisterRemoteObserver(uint32_t realType);
35     Status UnregisterRemoteObserver(uint32_t realType);
36     void OnChange(const DBChangedData &data) override;
37     void OnServiceDeath();
38 
39 private:
40     class ObserverClient : public KvStoreObserverClient {
41     public:
42         ObserverClient(std::shared_ptr<Observer> observer, const Convertor &cvt);
43         void OnChange(const ChangeNotification &data) override __attribute__((no_sanitize("cfi")));
44         void OnChange(const DataOrigin &origin, Keys &&keys) override __attribute__((no_sanitize("cfi")));
45 
46     private:
47         friend class ObserverBridge;
48         const Convertor &convert_;
49         uint32_t realType_;
50     };
51 
52     template<class T>
53     static std::vector<Entry> ConvertDB(const T &dbEntries, std::string &deviceId, const Convertor &convert);
54     AppId appId_;
55     StoreId storeId_;
56     int32_t subUser_;
57     std::shared_ptr<DistributedKv::KvStoreObserver> observer_;
58     sptr<ObserverClient> remote_;
59     const Convertor &convert_;
60     std::mutex mutex_;
61 };
62 } // namespace OHOS::DistributedKv
63 #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_OBSERVER_BRIDGE_H
64