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 virtual ~DataShareManagerImpl(); 38 39 std::shared_ptr<DataShareServiceProxy> GetServiceProxy(); 40 41 void OnRemoteDied(); 42 43 void SetDeathCallback(std::function<void(std::shared_ptr<DataShareServiceProxy>)> deathCallback); 44 45 void SetBundleName(const std::string &bundleName); 46 47 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 48 public: ServiceDeathRecipient(DataShareManagerImpl * owner)49 explicit ServiceDeathRecipient(DataShareManagerImpl *owner) : owner_(owner) 50 { 51 } OnRemoteDied(const wptr<IRemoteObject> & object)52 void OnRemoteDied(const wptr<IRemoteObject> &object) override 53 { 54 if (owner_ != nullptr) { 55 owner_->OnRemoteDied(); 56 } 57 } 58 59 private: 60 DataShareManagerImpl *owner_; 61 }; 62 63 private: 64 DataShareManagerImpl(); 65 66 void LinkToDeath(const sptr<IRemoteObject> remote); 67 68 sptr<DataShareServiceProxy> GetDataShareServiceProxy(); 69 70 void ResetServiceHandle(); 71 72 void RegisterClientDeathObserver(); 73 74 static std::shared_ptr<DataShareKvServiceProxy> GetDistributedDataManager(); 75 76 std::mutex mutex_; 77 std::shared_ptr<DataShareKvServiceProxy> dataMgrService_; 78 std::shared_ptr<DataShareServiceProxy> dataShareService_; 79 std::string bundleName_; 80 static constexpr int GET_SA_RETRY_TIMES = 3; 81 static constexpr int RETRY_INTERVAL = 1; 82 static constexpr int WAIT_TIME = 2; 83 static constexpr int MAX_THREADS = 2; 84 static constexpr int MIN_THREADS = 0; 85 std::shared_ptr<ExecutorPool> pool_; 86 std::function<void(std::shared_ptr<DataShareServiceProxy>)> deathCallback_ = {}; 87 sptr<IRemoteObject> clientDeathObserverPtr_; 88 }; 89 } 90 } // namespace OHOS::DataShare 91 #endif 92