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 #include "connection_observer_client_impl.h" 17 18 #include "connection_observer_errors.h" 19 #include "connection_observer_stub_impl.h" 20 #include "hilog_wrapper.h" 21 #include "iservice_registry.h" 22 #include "system_ability_definition.h" 23 24 namespace OHOS { 25 namespace AbilityRuntime { RegisterObserver(const std::shared_ptr<ConnectionObserver> & observer)26int32_t ConnectionObserverClientImpl::RegisterObserver(const std::shared_ptr<ConnectionObserver> &observer) 27 { 28 if (!observer) { 29 HILOG_ERROR("ConnectionObserverClientImpl::RegisterObserver invalid observer."); 30 return ERR_INVALID_OBSERVER; 31 } 32 33 auto proxy = GetServiceProxy(); 34 35 std::lock_guard<std::recursive_mutex> guard(observerLock_); 36 if (!RegisterObserverToServiceLocked(proxy)) { 37 HILOG_ERROR("register to service failed."); 38 return ERR_REGISTER_FAILED; 39 } 40 41 return AddObserversLocked(observer); 42 } 43 UnregisterObserver(const std::shared_ptr<ConnectionObserver> & observer)44int32_t ConnectionObserverClientImpl::UnregisterObserver(const std::shared_ptr<ConnectionObserver> &observer) 45 { 46 if (!observer) { 47 HILOG_ERROR("unregister, observer is invalid."); 48 return ERR_INVALID_OBSERVER; 49 } 50 51 auto proxy = GetServiceProxy(); 52 53 std::lock_guard<std::recursive_mutex> guard(observerLock_); 54 auto ret = RemoveObserversLocked(observer); 55 if (userObservers_.empty()) { 56 UnregisterFromServiceLocked(proxy); 57 } 58 59 return ret; 60 } 61 GetDlpConnectionInfos(std::vector<DlpConnectionInfo> & infos)62int32_t ConnectionObserverClientImpl::GetDlpConnectionInfos(std::vector<DlpConnectionInfo> &infos) 63 { 64 auto proxy = GetServiceProxy(); 65 if (!proxy) { 66 HILOG_ERROR("unregister, observer is invalid."); 67 return ERR_NO_PROXY; 68 } 69 70 return proxy->GetDlpConnectionInfos(infos); 71 } 72 HandleExtensionConnected(const ConnectionData & data)73void ConnectionObserverClientImpl::HandleExtensionConnected(const ConnectionData &data) 74 { 75 auto observers = GetObservers(); 76 for (auto it = observers.begin(); it != observers.end(); ++it) { 77 auto observer = *it; 78 if (observer) { 79 observer->OnExtensionConnected(data); 80 } 81 } 82 } 83 HandleExtensionDisconnected(const ConnectionData & data)84void ConnectionObserverClientImpl::HandleExtensionDisconnected(const ConnectionData &data) 85 { 86 auto observers = GetObservers(); 87 for (auto it = observers.begin(); it != observers.end(); ++it) { 88 auto observer = *it; 89 if (observer) { 90 observer->OnExtensionDisconnected(data); 91 } 92 } 93 } 94 HandleDlpAbilityOpened(const DlpStateData & data)95void ConnectionObserverClientImpl::HandleDlpAbilityOpened(const DlpStateData &data) 96 { 97 auto observers = GetObservers(); 98 for (auto it = observers.begin(); it != observers.end(); ++it) { 99 auto observer = *it; 100 if (observer) { 101 observer->OnDlpAbilityOpened(data); 102 } 103 } 104 } 105 HandleDlpAbilityClosed(const DlpStateData & data)106void ConnectionObserverClientImpl::HandleDlpAbilityClosed(const DlpStateData &data) 107 { 108 auto observers = GetObservers(); 109 for (auto it = observers.begin(); it != observers.end(); ++it) { 110 auto observer = *it; 111 if (observer) { 112 observer->OnDlpAbilityClosed(data); 113 } 114 } 115 } 116 RegisterObserverToServiceLocked(const std::shared_ptr<ServiceProxyAdapter> & proxy)117bool ConnectionObserverClientImpl::RegisterObserverToServiceLocked(const std::shared_ptr<ServiceProxyAdapter> &proxy) 118 { 119 if (isRegistered_) { 120 return true; 121 } 122 123 if (!proxy) { 124 HILOG_ERROR("fail to get service."); 125 return false; 126 } 127 128 if (!observer_) { 129 observer_ = sptr<IConnectionObserver>(new (std::nothrow) ConnectionObserverStubImpl(shared_from_this())); 130 } 131 132 if (proxy->RegisterObserver(observer_) != ERR_OK) { 133 HILOG_ERROR("register connection observer failed."); 134 return false; 135 } 136 isRegistered_ = true; 137 return true; 138 } 139 UnregisterFromServiceLocked(const std::shared_ptr<ServiceProxyAdapter> & proxy)140void ConnectionObserverClientImpl::UnregisterFromServiceLocked(const std::shared_ptr<ServiceProxyAdapter> &proxy) 141 { 142 if (!isRegistered_ || !observer_) { 143 return; 144 } 145 146 if (!proxy) { 147 return; 148 } 149 150 if (proxy->UnregisterObserver(observer_) != ERR_OK) { 151 HILOG_ERROR("unregister connection observer failed."); 152 return; 153 } 154 isRegistered_ = false; 155 } 156 AddObserversLocked(const std::shared_ptr<ConnectionObserver> & observer)157int32_t ConnectionObserverClientImpl::AddObserversLocked(const std::shared_ptr<ConnectionObserver> &observer) 158 { 159 if (userObservers_.find(observer) != userObservers_.end()) { 160 HILOG_ERROR("observer was already registered."); 161 return ERR_OBSERVER_ALREADY_REGISTERED; 162 } 163 userObservers_.emplace(observer); 164 return ERR_OK; 165 } 166 RemoveObserversLocked(const std::shared_ptr<ConnectionObserver> & observer)167int32_t ConnectionObserverClientImpl::RemoveObserversLocked(const std::shared_ptr<ConnectionObserver> &observer) 168 { 169 if (userObservers_.find(observer) == userObservers_.end()) { 170 HILOG_ERROR("unregister no such observer."); 171 return ERR_OBSERVER_NOT_REGISTERED; 172 } 173 userObservers_.erase(observer); 174 return ERR_OK; 175 } 176 GetServiceProxy()177std::shared_ptr<ServiceProxyAdapter> ConnectionObserverClientImpl::GetServiceProxy() 178 { 179 std::lock_guard<std::recursive_mutex> guard(proxyLock_); 180 if (!serviceAdapter_) { 181 ConnectLocked(); 182 } 183 return serviceAdapter_; 184 } 185 ConnectLocked()186void ConnectionObserverClientImpl::ConnectLocked() 187 { 188 if (serviceAdapter_ != nullptr) { 189 return; 190 } 191 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 192 if (systemManager == nullptr) { 193 HILOG_ERROR("Fail to get system ability registry."); 194 return; 195 } 196 sptr<IRemoteObject> remoteObj = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); 197 if (remoteObj == nullptr) { 198 HILOG_ERROR("Fail to connect ability manager service."); 199 return; 200 } 201 202 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>( 203 new (std::nothrow) ServiceDeathRecipient(shared_from_this())); 204 if (deathRecipient_ == nullptr) { 205 HILOG_ERROR("Failed to create AbilityMgrDeathRecipient!"); 206 return; 207 } 208 if ((remoteObj->IsProxyObject()) && (!remoteObj->AddDeathRecipient(deathRecipient_))) { 209 HILOG_ERROR("Add death recipient to AbilityManagerService failed."); 210 return; 211 } 212 213 serviceAdapter_ = std::make_shared<ServiceProxyAdapter>(remoteObj); 214 HILOG_INFO("Connect ability manager service success."); 215 } 216 HandleRemoteDied(const wptr<IRemoteObject> & remote)217void ConnectionObserverClientImpl::HandleRemoteDied(const wptr<IRemoteObject> &remote) 218 { 219 if (!ResetProxy(remote)) { 220 return; 221 } 222 NotifyServiceDiedToObservers(); 223 } 224 ResetProxy(const wptr<IRemoteObject> & remote)225bool ConnectionObserverClientImpl::ResetProxy(const wptr<IRemoteObject> &remote) 226 { 227 std::lock_guard<std::recursive_mutex> guard(proxyLock_); 228 if (serviceAdapter_ == nullptr) { 229 return false; 230 } 231 232 auto proxyObject = serviceAdapter_->GetProxyObject(); 233 if ((proxyObject != nullptr) && (proxyObject == remote.promote())) { 234 proxyObject->RemoveDeathRecipient(deathRecipient_); 235 serviceAdapter_ = nullptr; 236 return true; 237 } 238 239 return false; 240 } 241 ResetStatus()242void ConnectionObserverClientImpl::ResetStatus() 243 { 244 std::lock_guard<std::recursive_mutex> guard(observerLock_); 245 isRegistered_ = false; 246 userObservers_.clear(); 247 } 248 NotifyServiceDiedToObservers()249void ConnectionObserverClientImpl::NotifyServiceDiedToObservers() 250 { 251 auto observers = GetObservers(); 252 ResetStatus(); 253 for (auto it = observers.begin(); it != observers.end(); ++it) { 254 auto observer = *it; 255 if (observer) { 256 observer->OnServiceDied(); 257 } 258 } 259 } 260 GetObservers()261std::unordered_set<std::shared_ptr<ConnectionObserver>> ConnectionObserverClientImpl::GetObservers() 262 { 263 std::lock_guard<std::recursive_mutex> guard(observerLock_); 264 return userObservers_; 265 } 266 OnRemoteDied(const wptr<IRemoteObject> & remote)267void ConnectionObserverClientImpl::ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote) 268 { 269 HILOG_INFO("ServiceDeathRecipient handle remote abilityms died."); 270 auto owner = owner_.lock(); 271 if (!owner) { 272 HILOG_ERROR("ServiceDeathRecipient handle remote abilityms died."); 273 return; 274 } 275 owner->HandleRemoteDied(remote); 276 } 277 } // namespace AbilityRuntime 278 } // namespace OHOS 279