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 DATASHARESERVICE_DATA_SHARE_MANAGER_IMPL_H
17 #define DATASHARESERVICE_DATA_SHARE_MANAGER_IMPL_H
18 
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 
26 #include "call_reporter.h"
27 #include "concurrent_map.h"
28 #include "data_share_service_proxy.h"
29 #include "datashare_errno.h"
30 #include "idata_share_client_death_observer.h"
31 #include "iremote_object.h"
32 #include "refbase.h"
33 #include "system_ability_status_change_stub.h"
34 
35 namespace OHOS {
36 class ExecutorPool;
37 namespace DataShare {
38 class DataShareKvServiceProxy;
39 class GeneralControllerServiceImpl;
40 class DataShareManagerImpl {
41 public:
42     static DataShareManagerImpl* GetInstance();
43 
44     static std::shared_ptr<DataShareServiceProxy> GetServiceProxy();
45 
46     void OnRemoteDied();
47 
48     void SetDeathCallback(std::function<void(std::shared_ptr<DataShareServiceProxy>)> deathCallback);
49 
50     void SetBundleName(const std::string &bundleName);
51 
52     class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
53     public:
ServiceDeathRecipient(DataShareManagerImpl * owner)54         explicit ServiceDeathRecipient(DataShareManagerImpl *owner) : owner_(owner)
55         {
56         }
OnRemoteDied(const wptr<IRemoteObject> & object)57         void OnRemoteDied(const wptr<IRemoteObject> &object) override
58         {
59             if (owner_ != nullptr) {
60                 owner_->OnRemoteDied();
61             }
62         }
63 
64     private:
65         DataShareManagerImpl *owner_;
66     };
67 
68     class DataShareClientStatusChangeStub : public SystemAbilityStatusChangeStub {
69     public:
DataShareClientStatusChangeStub(DataShareManagerImpl * owner)70         explicit DataShareClientStatusChangeStub(DataShareManagerImpl *owner) : owner_(owner)
71         {
72         }
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)73         void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override
74         {
75             if (owner_ != nullptr) {
76                 owner_->OnAddSystemAbility(systemAbilityId, deviceId);
77             }
78         }
79 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)80         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override
81         {
82         }
83     private:
84         DataShareManagerImpl *owner_;
85     };
86 
87     void SetRegisterCallback(GeneralControllerServiceImpl* ptr, std::function<void()> registerCallback);
88 
89     void RemoveRegisterCallback(GeneralControllerServiceImpl* ptr);
90 
91     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
92 
93     void SetCallCount(const std::string &funcName, const std::string &uri);
94 
95 private:
96     DataShareManagerImpl();
97 
98     virtual ~DataShareManagerImpl();
99 
100     std::shared_ptr<DataShareServiceProxy> GetProxy();
101 
102     void LinkToDeath(const sptr<IRemoteObject> remote);
103 
104     sptr<DataShareServiceProxy> GetDataShareServiceProxy();
105 
106     void ResetServiceHandle();
107 
108     void RegisterClientDeathObserver();
109 
110     static sptr<DataShareKvServiceProxy> GetDistributedDataManager();
111     static std::mutex pmutex_;
112     static DataShareManagerImpl* manager_;
113     std::mutex mutex_;
114     sptr<DataShareKvServiceProxy> dataMgrService_;
115     std::shared_ptr<DataShareServiceProxy> dataShareService_;
116     std::string bundleName_;
117     std::function<void(std::shared_ptr<DataShareServiceProxy>)> deathCallback_ = {};
118     sptr<IRemoteObject> clientDeathObserverPtr_;
119     ConcurrentMap<GeneralControllerServiceImpl*, std::function<void()>> observers_;
120     DataShareCallReporter dataShareCallReporter_;
121 };
122 }
123 } // namespace OHOS::DataShare
124 #endif
125