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_RDB_MANAGER_IMPL_H
17 #define DISTRIBUTED_RDB_RDB_MANAGER_IMPL_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 
23 #include "refbase.h"
24 #include "iremote_object.h"
25 #include "iremote_proxy.h"
26 #include "concurrent_map.h"
27 #include "rdb_types.h"
28 #include "irdb_service.h"
29 
30 namespace OHOS::DistributedRdb {
31 class RdbService;
32 class RdbServiceProxy;
33 class RdbStoreDataServiceProxy;
34 class RdbManagerImpl {
35 public:
36     static constexpr int RETRY_INTERVAL = 1;
37     static constexpr int WAIT_TIME = 2;
38 
39     static RdbManagerImpl &GetInstance();
40 
41     std::pair<int32_t, std::shared_ptr<RdbService>> GetRdbService(const RdbSyncerParam &param);
42 
43     void OnRemoteDied();
44 
45     class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
46     public:
ServiceDeathRecipient(RdbManagerImpl * owner)47         explicit ServiceDeathRecipient(RdbManagerImpl* owner) : owner_(owner) {}
OnRemoteDied(const wptr<IRemoteObject> & object)48         void OnRemoteDied(const wptr<IRemoteObject> &object) override
49         {
50             if (owner_ != nullptr) {
51                 owner_->OnRemoteDied();
52             }
53         }
54     private:
55         RdbManagerImpl* owner_;
56     };
57 
58 private:
59     RdbManagerImpl();
60 
61     ~RdbManagerImpl();
62 
63     void ResetServiceHandle();
64 
65     static std::shared_ptr<RdbStoreDataServiceProxy> GetDistributedDataManager(const std::string &bundleName);
66 
67     std::mutex mutex_;
68     std::shared_ptr<RdbStoreDataServiceProxy> distributedDataMgr_;
69     std::shared_ptr<RdbService> rdbService_;
70     RdbSyncerParam param_;
71 };
72 
73 class RdbStoreDataServiceProxy : public IRemoteProxy<DistributedRdb::IKvStoreDataService> {
74 public:
75     explicit RdbStoreDataServiceProxy(const sptr<IRemoteObject> &impl);
76     ~RdbStoreDataServiceProxy() = default;
77     sptr<IRemoteObject> GetFeatureInterface(const std::string &name) override;
78     int32_t RegisterDeathObserver(const std::string &bundleName, sptr<IRemoteObject> observer) override;
79 };
80 } // namespace OHOS::DistributedRdb
81 #endif
82