• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "KvStoreServiceDeathNotifier"
17 
18 #include "kvstore_service_death_notifier.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "kvstore_client_death_observer.h"
23 #include "log_print.h"
24 #include "refbase.h"
25 #include "system_ability_definition.h"
26 
27 namespace OHOS {
28 namespace DistributedKv {
29 AppId KvStoreServiceDeathNotifier::appId_;
30 std::mutex KvStoreServiceDeathNotifier::mutex_;
31 std::mutex KvStoreServiceDeathNotifier::watchMutex_;
32 sptr<IKvStoreDataService> KvStoreServiceDeathNotifier::kvDataServiceProxy_;
33 sptr<KvStoreServiceDeathNotifier::ServiceDeathRecipient> KvStoreServiceDeathNotifier::deathRecipientPtr_;
34 sptr<IRemoteObject> KvStoreServiceDeathNotifier::clientDeathObserverPtr_;
35 std::set<std::shared_ptr<KvStoreDeathRecipient>> KvStoreServiceDeathNotifier::serviceDeathWatchers_;
36 
SetAppId(const AppId & appId)37 void KvStoreServiceDeathNotifier::SetAppId(const AppId &appId)
38 {
39     std::lock_guard<decltype(mutex_)> lg(mutex_);
40     appId_ = appId;
41 }
42 
GetAppId()43 AppId KvStoreServiceDeathNotifier::GetAppId()
44 {
45     std::lock_guard<decltype(mutex_)> lg(mutex_);
46     return appId_;
47 }
48 
GetDistributedKvDataService()49 sptr<IKvStoreDataService> KvStoreServiceDeathNotifier::GetDistributedKvDataService()
50 {
51     ZLOGD("begin.");
52     std::lock_guard<std::mutex> lg(watchMutex_);
53     if (kvDataServiceProxy_ != nullptr) {
54         return kvDataServiceProxy_;
55     }
56 
57     ZLOGI("create remote proxy.");
58     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59     if (samgr == nullptr) {
60         ZLOGE("get samgr fail.");
61         return nullptr;
62     }
63 
64     auto remote = samgr->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
65     kvDataServiceProxy_ = iface_cast<IKvStoreDataService>(remote);
66     if (kvDataServiceProxy_ == nullptr) {
67         ZLOGE("initialize proxy failed.");
68         return nullptr;
69     }
70 
71     if (deathRecipientPtr_ == nullptr) {
72         deathRecipientPtr_ = new (std::nothrow) ServiceDeathRecipient();
73         if (deathRecipientPtr_ == nullptr) {
74             ZLOGW("new KvStoreDeathRecipient failed");
75             return nullptr;
76         }
77     }
78     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipientPtr_))) {
79         ZLOGE("failed to add death recipient.");
80     }
81 
82     RegisterClientDeathObserver();
83 
84     return kvDataServiceProxy_;
85 }
86 
RegisterClientDeathObserver()87 void KvStoreServiceDeathNotifier::RegisterClientDeathObserver()
88 {
89     if (kvDataServiceProxy_ == nullptr) {
90         return;
91     }
92     if (clientDeathObserverPtr_ == nullptr) {
93         clientDeathObserverPtr_ = new (std::nothrow) KvStoreClientDeathObserver();
94     }
95     if (clientDeathObserverPtr_ == nullptr) {
96         ZLOGW("new KvStoreClientDeathObserver failed");
97         return;
98     }
99     kvDataServiceProxy_->RegisterClientDeathObserver(GetAppId(), clientDeathObserverPtr_);
100 }
101 
AddServiceDeathWatcher(std::shared_ptr<KvStoreDeathRecipient> watcher)102 void KvStoreServiceDeathNotifier::AddServiceDeathWatcher(std::shared_ptr<KvStoreDeathRecipient> watcher)
103 {
104     std::lock_guard<std::mutex> lg(watchMutex_);
105     auto ret = serviceDeathWatchers_.insert(std::move(watcher));
106     if (ret.second) {
107         ZLOGI("success set size: %zu", serviceDeathWatchers_.size());
108     } else {
109         ZLOGE("failed set size: %zu", serviceDeathWatchers_.size());
110     }
111 }
112 
RemoveServiceDeathWatcher(std::shared_ptr<KvStoreDeathRecipient> watcher)113 void KvStoreServiceDeathNotifier::RemoveServiceDeathWatcher(std::shared_ptr<KvStoreDeathRecipient> watcher)
114 {
115     std::lock_guard<std::mutex> lg(watchMutex_);
116     auto it = serviceDeathWatchers_.find(std::move(watcher));
117     if (it != serviceDeathWatchers_.end()) {
118         serviceDeathWatchers_.erase(it);
119         ZLOGI("find & erase set size: %zu", serviceDeathWatchers_.size());
120     } else {
121         ZLOGE("no found set size: %zu", serviceDeathWatchers_.size());
122     }
123 }
124 
OnRemoteDied(const wptr<IRemoteObject> & remote)125 void KvStoreServiceDeathNotifier::ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
126 {
127     ZLOGW("DistributedDataMgrService died.");
128     // Need to do this with the lock held
129     std::lock_guard<std::mutex> lg(watchMutex_);
130     kvDataServiceProxy_ = nullptr;
131     ZLOGI("watcher set size: %zu", serviceDeathWatchers_.size());
132     for (const auto &watcher : serviceDeathWatchers_) {
133         if (watcher == nullptr) {
134             ZLOGI("watcher is nullptr");
135             continue;
136         }
137 
138         std::thread th = std::thread([watcher]() {
139             watcher->OnRemoteDied();
140         });
141         th.detach();
142     }
143 }
144 
ServiceDeathRecipient()145 KvStoreServiceDeathNotifier::ServiceDeathRecipient::ServiceDeathRecipient()
146 {
147     ZLOGI("constructor.");
148 }
149 
~ServiceDeathRecipient()150 KvStoreServiceDeathNotifier::ServiceDeathRecipient::~ServiceDeathRecipient()
151 {
152     ZLOGI("destructor.");
153 }
154 }  // namespace DistributedKv
155 }  // namespace OHOS
156