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 <map> 20 #include <memory> 21 #include <mutex> 22 23 #include "data_share_service_proxy.h" 24 #include "data_share_errno.h" 25 #include "idata_share_client_death_observer.h" 26 #include "iremote_object.h" 27 #include "refbase.h" 28 29 namespace OHOS { 30 class ExecutorPool; 31 namespace DataShare { 32 class DataShareKvServiceProxy; 33 class DataShareManagerImpl { 34 public: 35 static DataShareManagerImpl* GetInstance(); 36 37 static std::shared_ptr<DataShareServiceProxy> GetServiceProxy(); 38 39 void OnRemoteDied(); 40 41 void SetDeathCallback(std::function<void(std::shared_ptr<DataShareServiceProxy>)> deathCallback); 42 43 void SetBundleName(const std::string &bundleName); 44 45 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 46 public: ServiceDeathRecipient(DataShareManagerImpl * owner)47 explicit ServiceDeathRecipient(DataShareManagerImpl *owner) : owner_(owner) 48 { 49 } OnRemoteDied(const wptr<IRemoteObject> & object)50 void OnRemoteDied(const wptr<IRemoteObject> &object) override 51 { 52 if (owner_ != nullptr) { 53 owner_->OnRemoteDied(); 54 } 55 } 56 57 private: 58 DataShareManagerImpl *owner_; 59 }; 60 61 private: 62 DataShareManagerImpl(); 63 64 virtual ~DataShareManagerImpl(); 65 66 std::shared_ptr<DataShareServiceProxy> GetProxy(); 67 68 void LinkToDeath(const sptr<IRemoteObject> remote); 69 70 sptr<DataShareServiceProxy> GetDataShareServiceProxy(); 71 72 void ResetServiceHandle(); 73 74 void RegisterClientDeathObserver(); 75 76 static sptr<DataShareKvServiceProxy> GetDistributedDataManager(); 77 static std::mutex pmutex_; 78 static DataShareManagerImpl* manager_; 79 std::mutex mutex_; 80 sptr<DataShareKvServiceProxy> dataMgrService_; 81 std::shared_ptr<DataShareServiceProxy> dataShareService_; 82 std::string bundleName_; 83 static constexpr int WAIT_TIME = 2; 84 static constexpr int MAX_THREADS = 2; 85 static constexpr int MIN_THREADS = 0; 86 std::shared_ptr<ExecutorPool> pool_; 87 std::function<void(std::shared_ptr<DataShareServiceProxy>)> deathCallback_ = {}; 88 sptr<IRemoteObject> clientDeathObserverPtr_; 89 }; 90 } 91 } // namespace OHOS::DataShare 92 #endif 93