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_SERVICE_CLIENT_H
16 #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_SERVICE_CLIENT_H
17 #include <functional>
18 #include "concurrent_map.h"
19 #include "iremote_broker.h"
20 #include "iremote_proxy.h"
21 #include "kvdb_service.h"
22 #include "kvdb_notifier_client.h"
23 #include "task_executor.h"
24 namespace OHOS::DistributedKv {
25 class KVDBServiceProxy : public KVDBService, public IRemoteBroker {
26 public:
27     using KVDBService::KVDBService;
28 };
29 
30 class KVDBServiceClient : public IRemoteProxy<KVDBServiceProxy> {
31 public:
32     static std::shared_ptr<KVDBServiceClient> GetInstance();
33     Status GetStoreIds(const AppId &appId, std::vector<StoreId> &storeIds) override;
34     Status BeforeCreate(const AppId &appId, const StoreId &storeId, const Options &options) override;
35     Status AfterCreate(const AppId &appId, const StoreId &storeId, const Options &options,
36         const std::vector<uint8_t> &password) override;
37     Status Delete(const AppId &appId, const StoreId &storeId) override;
38     Status Close(const AppId &appId, const StoreId &storeId) override;
39     Status Sync(const AppId &appId, const StoreId &storeId, SyncInfo &syncInfo) override;
40     Status RegServiceNotifier(const AppId &appId, sptr<IKVDBNotifier> notifier) override;
41     Status UnregServiceNotifier(const AppId &appIdd) override;
42     Status SetSyncParam(const AppId &appId, const StoreId &storeId, const KvSyncParam &syncParam) override;
43     Status GetSyncParam(const AppId &appId, const StoreId &storeId, KvSyncParam &syncParam) override;
44     Status EnableCapability(const AppId &appId, const StoreId &storeId) override;
45     Status DisableCapability(const AppId &appId, const StoreId &storeId) override;
46     Status SetCapability(const AppId &appId, const StoreId &storeId, const std::vector<std::string> &local,
47         const std::vector<std::string> &remote) override;
48     Status AddSubscribeInfo(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override;
49     Status RmvSubscribeInfo(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override;
50     Status Subscribe(const AppId &appId, const StoreId &storeId, sptr<IKvStoreObserver> observer) override;
51     Status Unsubscribe(const AppId &appId, const StoreId &storeId, sptr<IKvStoreObserver> observer) override;
52     Status GetBackupPassword(const AppId &appId, const StoreId &storeId, std::vector<uint8_t> &password,
53         int32_t passwordType) override;
54     Status CloudSync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override;
55     Status NotifyDataChange(const AppId &appId, const StoreId &storeId, uint64_t delay) override;
56     Status PutSwitch(const AppId &appId, const SwitchData &data) override;
57     Status GetSwitch(const AppId &appId, const std::string &networkId, SwitchData &data) override;
58     Status SubscribeSwitchData(const AppId &appId) override;
59     Status UnsubscribeSwitchData(const AppId &appId) override;
60     Status SetConfig(const AppId &appId, const StoreId &storeId, const StoreConfig &storeConfig) override;
61     Status RemoveDeviceData(const AppId &appId, const StoreId &storeId, const std::string &device) override;
62 
63     sptr<KVDBNotifierClient> GetServiceAgent(const AppId &appId);
64 
65 protected:
66     explicit KVDBServiceClient(const sptr<IRemoteObject> &object);
67     virtual ~KVDBServiceClient() = default;
68 
69 private:
70     class ServiceDeath : public KvStoreDeathRecipient {
71     public:
72         ServiceDeath() = default;
73         virtual ~ServiceDeath() = default;
74         void OnRemoteDied() override;
75     };
76     static std::mutex mutex_;
77     static std::shared_ptr<KVDBServiceClient> instance_;
78     static std::atomic_bool isWatched_;
79     sptr<IRemoteObject> remote_;
80     std::mutex agentMtx_;
81     sptr<KVDBNotifierClient> serviceAgent_;
82 };
83 } // namespace OHOS::DistributedKv
84 #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_SERVICE_CLIENT_H