1 /* 2 * Copyright (c) 2021 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 KVSTORE_SERVICE_DEATH_NOTIFIER_H 17 #define KVSTORE_SERVICE_DEATH_NOTIFIER_H 18 19 #include <memory> 20 #include <set> 21 #include <thread> 22 #include "ikvstore_data_service.h" 23 #include "iremote_object.h" 24 #include "kvstore_death_recipient.h" 25 #include "refbase.h" 26 27 namespace OHOS { 28 namespace DistributedKv { 29 class KvStoreServiceDeathNotifier final { 30 public: 31 // get DistributedKvDataService proxy object. 32 static sptr<IKvStoreDataService> GetDistributedKvDataService(); 33 // temporarily used, should get in service side from binder. 34 static void SetAppId(const AppId &appId); 35 static AppId GetAppId(); 36 // add watcher for server die msg. 37 static void AddServiceDeathWatcher(std::shared_ptr<KvStoreDeathRecipient> watcher); 38 // remove watcher for server die msg. 39 static void RemoveServiceDeathWatcher(std::shared_ptr<KvStoreDeathRecipient> watcher); 40 41 private: 42 KvStoreServiceDeathNotifier() = default; 43 ~KvStoreServiceDeathNotifier() = default; 44 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 45 public: 46 ServiceDeathRecipient(); 47 48 virtual ~ServiceDeathRecipient(); 49 50 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 51 }; 52 static KvStoreServiceDeathNotifier& GetInstance(); 53 54 static std::mutex instanceMutex_; 55 static KvStoreServiceDeathNotifier *instance_; 56 57 // add watcher for server die msg. 58 void RegisterClientDeathObserver(); 59 AppId appId_; 60 // lock for kvDataServiceProxy_ and serviceDeathWatchers_. 61 std::mutex watchMutex_; 62 std::mutex mutex_; 63 sptr<IKvStoreDataService> kvDataServiceProxy_; 64 sptr<ServiceDeathRecipient> deathRecipientPtr_; 65 sptr<IRemoteObject> clientDeathObserverPtr_; 66 // set of watchers for server die msg. 67 std::set<std::shared_ptr<KvStoreDeathRecipient>> serviceDeathWatchers_; 68 }; 69 } // namespace DistributedKv 70 } // namespace OHOS 71 #endif // KVSTORE_SERVICE_DEATH_NOTIFIER_H 72