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_manager.h" 24 #include "data_share_service_proxy.h" 25 #include "data_share_types.h" 26 #include "idata_share_service.h" 27 #include "iremote_object.h" 28 #include "refbase.h" 29 30 namespace OHOS::DataShare { 31 class DataShareKvServiceProxy; 32 class DataShareManagerImpl { 33 public: 34 static DataShareManagerImpl &GetInstance(); 35 36 std::shared_ptr<IDataShareService> GetDataShareService(); 37 38 void OnRemoteDied(); 39 40 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 41 public: ServiceDeathRecipient(DataShareManagerImpl * owner)42 explicit ServiceDeathRecipient(DataShareManagerImpl *owner) : owner_(owner) {} OnRemoteDied(const wptr<IRemoteObject> & object)43 void OnRemoteDied(const wptr<IRemoteObject> &object) override 44 { 45 if (owner_ != nullptr) { 46 owner_->OnRemoteDied(); 47 } 48 } 49 50 private: 51 DataShareManagerImpl *owner_; 52 }; 53 54 private: 55 DataShareManagerImpl(); 56 57 ~DataShareManagerImpl(); 58 59 sptr<DataShareServiceProxy> GetDataShareServiceProxy(); 60 61 void ResetServiceHandle(); 62 63 static std::shared_ptr<DataShareKvServiceProxy> GetDistributedDataManager(); 64 65 std::mutex mutex_; 66 std::shared_ptr<DataShareKvServiceProxy> dataMgrService_; 67 std::shared_ptr<DataShareServiceProxy> dataShareService_; 68 std::string bundleName_; 69 static constexpr int GET_SA_RETRY_TIMES = 3; 70 static constexpr int RETRY_INTERVAL = 1; 71 static constexpr int WAIT_TIME = 2; 72 }; 73 74 class DataShareKvServiceProxy : public IRemoteProxy<DataShare::IKvStoreDataService> { 75 public: 76 explicit DataShareKvServiceProxy(const sptr<IRemoteObject> &impl); 77 ~DataShareKvServiceProxy() = default; 78 sptr<IRemoteObject> GetFeatureInterface(const std::string &name) override; 79 80 private: 81 static inline BrokerDelegator<DataShareKvServiceProxy> delegator_; 82 }; 83 } // namespace OHOS::DataShare 84 #endif 85