1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "connection_state_manager.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include "connection_observer_errors.h" 19eace7efcSopenharmony_ci#include "global_constant.h" 20eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h" 21eace7efcSopenharmony_ci#include "iservice_registry.h" 22eace7efcSopenharmony_ci#include "system_ability_definition.h" 23eace7efcSopenharmony_ci 24eace7efcSopenharmony_cinamespace OHOS { 25eace7efcSopenharmony_cinamespace AAFwk { 26eace7efcSopenharmony_cinamespace { 27eace7efcSopenharmony_cistatic const int MAX_RETRY = 10; 28eace7efcSopenharmony_cistatic const int DELAY_TIME = 1000; 29eace7efcSopenharmony_ciOHOS::sptr<OHOS::AppExecFwk::IAppMgr> GetAppMgr() 30eace7efcSopenharmony_ci{ 31eace7efcSopenharmony_ci OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager = 32eace7efcSopenharmony_ci OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 33eace7efcSopenharmony_ci if (!systemAbilityManager) { 34eace7efcSopenharmony_ci return nullptr; 35eace7efcSopenharmony_ci } 36eace7efcSopenharmony_ci OHOS::sptr<OHOS::IRemoteObject> object = systemAbilityManager->GetSystemAbility(OHOS::APP_MGR_SERVICE_ID); 37eace7efcSopenharmony_ci return OHOS::iface_cast<OHOS::AppExecFwk::IAppMgr>(object); 38eace7efcSopenharmony_ci} 39eace7efcSopenharmony_ci} 40eace7efcSopenharmony_ciusing namespace OHOS::AbilityRuntime; 41eace7efcSopenharmony_ci 42eace7efcSopenharmony_ciConnectionStateManager::ConnectionStateManager() {} 43eace7efcSopenharmony_ci 44eace7efcSopenharmony_ciConnectionStateManager::~ConnectionStateManager() {} 45eace7efcSopenharmony_ci 46eace7efcSopenharmony_cistd::string ConnectionStateManager::GetProcessNameByPid(int32_t pid) 47eace7efcSopenharmony_ci{ 48eace7efcSopenharmony_ci return std::to_string(pid); 49eace7efcSopenharmony_ci} 50eace7efcSopenharmony_ci 51eace7efcSopenharmony_civoid ConnectionStateManager::Init(const std::shared_ptr<TaskHandlerWrap> &handler) 52eace7efcSopenharmony_ci{ 53eace7efcSopenharmony_ci if (!observerController_) { 54eace7efcSopenharmony_ci observerController_ = std::make_shared<ConnectionObserverController>(); 55eace7efcSopenharmony_ci } 56eace7efcSopenharmony_ci handler_ = handler; 57eace7efcSopenharmony_ci if (!handler) { 58eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "invalid eventhandler"); 59eace7efcSopenharmony_ci InitAppStateObserver(); 60eace7efcSopenharmony_ci return; 61eace7efcSopenharmony_ci } 62eace7efcSopenharmony_ci auto initConnectionStateManagerTask = [weak = weak_from_this()]() { 63eace7efcSopenharmony_ci auto self = weak.lock(); 64eace7efcSopenharmony_ci if (!self) { 65eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "invalid self pointer"); 66eace7efcSopenharmony_ci return; 67eace7efcSopenharmony_ci } 68eace7efcSopenharmony_ci self->InitAppStateObserver(); 69eace7efcSopenharmony_ci }; 70eace7efcSopenharmony_ci handler->SubmitTask(initConnectionStateManagerTask, "InitConnectionStateManager"); 71eace7efcSopenharmony_ci} 72eace7efcSopenharmony_ci 73eace7efcSopenharmony_ciint ConnectionStateManager::RegisterObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer) 74eace7efcSopenharmony_ci{ 75eace7efcSopenharmony_ci if (!observerController_) { 76eace7efcSopenharmony_ci return ERR_SERVICE_NOT_INIT; 77eace7efcSopenharmony_ci } 78eace7efcSopenharmony_ci 79eace7efcSopenharmony_ci return observerController_->AddObserver(observer); 80eace7efcSopenharmony_ci} 81eace7efcSopenharmony_ci 82eace7efcSopenharmony_ciint ConnectionStateManager::UnregisterObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer) 83eace7efcSopenharmony_ci{ 84eace7efcSopenharmony_ci if (!observerController_) { 85eace7efcSopenharmony_ci return ERR_SERVICE_NOT_INIT; 86eace7efcSopenharmony_ci } 87eace7efcSopenharmony_ci observerController_->RemoveObserver(observer); 88eace7efcSopenharmony_ci 89eace7efcSopenharmony_ci return 0; 90eace7efcSopenharmony_ci} 91eace7efcSopenharmony_ci 92eace7efcSopenharmony_civoid ConnectionStateManager::AddConnection(std::shared_ptr<ConnectionRecord> connectionRecord) 93eace7efcSopenharmony_ci{ 94eace7efcSopenharmony_ci std::shared_ptr<ConnectionObserverController> controller = observerController_; 95eace7efcSopenharmony_ci if (!controller) { 96eace7efcSopenharmony_ci return; 97eace7efcSopenharmony_ci } 98eace7efcSopenharmony_ci 99eace7efcSopenharmony_ci if (!connectionRecord) { 100eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "invalid connection record"); 101eace7efcSopenharmony_ci return; 102eace7efcSopenharmony_ci } 103eace7efcSopenharmony_ci 104eace7efcSopenharmony_ci ConnectionData connectionData; 105eace7efcSopenharmony_ci if (!AddConnectionInner(connectionRecord, connectionData)) { 106eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::CONNECTION, "no need notify observers"); 107eace7efcSopenharmony_ci return; 108eace7efcSopenharmony_ci } 109eace7efcSopenharmony_ci controller->NotifyExtensionConnected(connectionData); 110eace7efcSopenharmony_ci} 111eace7efcSopenharmony_ci 112eace7efcSopenharmony_civoid ConnectionStateManager::RemoveConnection(std::shared_ptr<ConnectionRecord> connectionRecord, 113eace7efcSopenharmony_ci bool isCallerDied) 114eace7efcSopenharmony_ci{ 115eace7efcSopenharmony_ci std::shared_ptr<ConnectionObserverController> controller = observerController_; 116eace7efcSopenharmony_ci if (!controller) { 117eace7efcSopenharmony_ci return; 118eace7efcSopenharmony_ci } 119eace7efcSopenharmony_ci 120eace7efcSopenharmony_ci if (!connectionRecord) { 121eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "invalid connection record"); 122eace7efcSopenharmony_ci return; 123eace7efcSopenharmony_ci } 124eace7efcSopenharmony_ci 125eace7efcSopenharmony_ci // if caller died, notify at once. 126eace7efcSopenharmony_ci if (isCallerDied) { 127eace7efcSopenharmony_ci HandleCallerDied(connectionRecord->GetCallerPid()); 128eace7efcSopenharmony_ci return; 129eace7efcSopenharmony_ci } 130eace7efcSopenharmony_ci 131eace7efcSopenharmony_ci ConnectionData connectionData; 132eace7efcSopenharmony_ci if (!RemoveConnectionInner(connectionRecord, connectionData)) { 133eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::CONNECTION, "no need notify observers"); 134eace7efcSopenharmony_ci return; 135eace7efcSopenharmony_ci } 136eace7efcSopenharmony_ci controller->NotifyExtensionDisconnected(connectionData); 137eace7efcSopenharmony_ci} 138eace7efcSopenharmony_ci 139eace7efcSopenharmony_civoid ConnectionStateManager::AddDataAbilityConnection(const DataAbilityCaller &caller, 140eace7efcSopenharmony_ci const std::shared_ptr<DataAbilityRecord> &record) 141eace7efcSopenharmony_ci{ 142eace7efcSopenharmony_ci if (!CheckDataAbilityConnectionParams(caller, record)) { 143eace7efcSopenharmony_ci return; 144eace7efcSopenharmony_ci } 145eace7efcSopenharmony_ci 146eace7efcSopenharmony_ci ConnectionData connectionData; 147eace7efcSopenharmony_ci if (!AddDataAbilityConnectionInner(caller, record, connectionData)) { 148eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "no need notify observers"); 149eace7efcSopenharmony_ci return; 150eace7efcSopenharmony_ci } 151eace7efcSopenharmony_ci observerController_->NotifyExtensionConnected(connectionData); 152eace7efcSopenharmony_ci} 153eace7efcSopenharmony_ci 154eace7efcSopenharmony_civoid ConnectionStateManager::RemoveDataAbilityConnection(const DataAbilityCaller &caller, 155eace7efcSopenharmony_ci const std::shared_ptr<DataAbilityRecord> &record) 156eace7efcSopenharmony_ci{ 157eace7efcSopenharmony_ci if (!CheckDataAbilityConnectionParams(caller, record)) { 158eace7efcSopenharmony_ci return; 159eace7efcSopenharmony_ci } 160eace7efcSopenharmony_ci 161eace7efcSopenharmony_ci ConnectionData connectionData; 162eace7efcSopenharmony_ci if (!RemoveDataAbilityConnectionInner(caller, record, connectionData)) { 163eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "no need notify observers"); 164eace7efcSopenharmony_ci return; 165eace7efcSopenharmony_ci } 166eace7efcSopenharmony_ci observerController_->NotifyExtensionDisconnected(connectionData); 167eace7efcSopenharmony_ci} 168eace7efcSopenharmony_ci 169eace7efcSopenharmony_cibool ConnectionStateManager::CheckDataAbilityConnectionParams(const DataAbilityCaller &caller, 170eace7efcSopenharmony_ci const std::shared_ptr<DataAbilityRecord> &record) const 171eace7efcSopenharmony_ci{ 172eace7efcSopenharmony_ci if (!observerController_) { 173eace7efcSopenharmony_ci return false; 174eace7efcSopenharmony_ci } 175eace7efcSopenharmony_ci 176eace7efcSopenharmony_ci if (!record) { 177eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "invalid data ability record"); 178eace7efcSopenharmony_ci return false; 179eace7efcSopenharmony_ci } 180eace7efcSopenharmony_ci 181eace7efcSopenharmony_ci if (caller.callerPid == 0) { 182eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "invalid callerPid"); 183eace7efcSopenharmony_ci return false; 184eace7efcSopenharmony_ci } 185eace7efcSopenharmony_ci 186eace7efcSopenharmony_ci return true; 187eace7efcSopenharmony_ci} 188eace7efcSopenharmony_ci 189eace7efcSopenharmony_civoid ConnectionStateManager::HandleDataAbilityDied(const std::shared_ptr<DataAbilityRecord> &record) 190eace7efcSopenharmony_ci{ 191eace7efcSopenharmony_ci if (!record) { 192eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "invalid data ability"); 193eace7efcSopenharmony_ci return; 194eace7efcSopenharmony_ci } 195eace7efcSopenharmony_ci 196eace7efcSopenharmony_ci auto token = record->GetToken(); 197eace7efcSopenharmony_ci if (!token) { 198eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "invalid token"); 199eace7efcSopenharmony_ci return; 200eace7efcSopenharmony_ci } 201eace7efcSopenharmony_ci 202eace7efcSopenharmony_ci std::vector<AbilityRuntime::ConnectionData> allData; 203eace7efcSopenharmony_ci HandleDataAbilityDiedInner(token, allData); 204eace7efcSopenharmony_ci if (allData.empty()) { 205eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "empty allData"); 206eace7efcSopenharmony_ci return; 207eace7efcSopenharmony_ci } 208eace7efcSopenharmony_ci 209eace7efcSopenharmony_ci std::shared_ptr<ConnectionObserverController> controller = observerController_; 210eace7efcSopenharmony_ci if (!controller) { 211eace7efcSopenharmony_ci return; 212eace7efcSopenharmony_ci } 213eace7efcSopenharmony_ci 214eace7efcSopenharmony_ci for (auto& item : allData) { 215eace7efcSopenharmony_ci controller->NotifyExtensionDisconnected(item); 216eace7efcSopenharmony_ci } 217eace7efcSopenharmony_ci} 218eace7efcSopenharmony_ci 219eace7efcSopenharmony_civoid ConnectionStateManager::HandleDataAbilityCallerDied(int32_t callerPid) 220eace7efcSopenharmony_ci{ 221eace7efcSopenharmony_ci if (callerPid <= 0) { 222eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "invalid callerPid"); 223eace7efcSopenharmony_ci return; 224eace7efcSopenharmony_ci } 225eace7efcSopenharmony_ci 226eace7efcSopenharmony_ci HandleCallerDied(callerPid); 227eace7efcSopenharmony_ci} 228eace7efcSopenharmony_ci 229eace7efcSopenharmony_ci#ifdef WITH_DLP 230eace7efcSopenharmony_civoid ConnectionStateManager::AddDlpManager(const std::shared_ptr<AbilityRecord> &dlpManger) 231eace7efcSopenharmony_ci{ 232eace7efcSopenharmony_ci if (!dlpManger) { 233eace7efcSopenharmony_ci return; 234eace7efcSopenharmony_ci } 235eace7efcSopenharmony_ci 236eace7efcSopenharmony_ci auto userId = dlpManger->GetOwnerMissionUserId(); 237eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(dlpLock_); 238eace7efcSopenharmony_ci auto it = dlpItems_.find(userId); 239eace7efcSopenharmony_ci if (it == dlpItems_.end()) { 240eace7efcSopenharmony_ci dlpItems_[userId] = std::make_shared<DlpStateItem>(dlpManger->GetUid(), dlpManger->GetPid()); 241eace7efcSopenharmony_ci } 242eace7efcSopenharmony_ci} 243eace7efcSopenharmony_ci 244eace7efcSopenharmony_civoid ConnectionStateManager::RemoveDlpManager(const std::shared_ptr<AbilityRecord> &dlpManger) 245eace7efcSopenharmony_ci{ 246eace7efcSopenharmony_ci if (!dlpManger) { 247eace7efcSopenharmony_ci return; 248eace7efcSopenharmony_ci } 249eace7efcSopenharmony_ci 250eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(dlpLock_); 251eace7efcSopenharmony_ci dlpItems_.erase(dlpManger->GetOwnerMissionUserId()); 252eace7efcSopenharmony_ci} 253eace7efcSopenharmony_ci 254eace7efcSopenharmony_civoid ConnectionStateManager::AddDlpAbility(const std::shared_ptr<AbilityRecord> &dlpAbility) 255eace7efcSopenharmony_ci{ 256eace7efcSopenharmony_ci std::shared_ptr<ConnectionObserverController> controller = observerController_; 257eace7efcSopenharmony_ci if (!controller) { 258eace7efcSopenharmony_ci return; 259eace7efcSopenharmony_ci } 260eace7efcSopenharmony_ci 261eace7efcSopenharmony_ci DlpStateData dlpData; 262eace7efcSopenharmony_ci if (!HandleDlpAbilityInner(dlpAbility, true, dlpData)) { 263eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::CONNECTION, "no need report dlp opened conn state"); 264eace7efcSopenharmony_ci return; 265eace7efcSopenharmony_ci } 266eace7efcSopenharmony_ci controller->NotifyDlpAbilityOpened(dlpData); 267eace7efcSopenharmony_ci} 268eace7efcSopenharmony_ci 269eace7efcSopenharmony_civoid ConnectionStateManager::RemoveDlpAbility(const std::shared_ptr<AbilityRecord> &dlpAbility) 270eace7efcSopenharmony_ci{ 271eace7efcSopenharmony_ci std::shared_ptr<ConnectionObserverController> controller = observerController_; 272eace7efcSopenharmony_ci if (!controller) { 273eace7efcSopenharmony_ci return; 274eace7efcSopenharmony_ci } 275eace7efcSopenharmony_ci 276eace7efcSopenharmony_ci DlpStateData dlpData; 277eace7efcSopenharmony_ci if (!HandleDlpAbilityInner(dlpAbility, false, dlpData)) { 278eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::CONNECTION, "no need report dlp closed conn state"); 279eace7efcSopenharmony_ci return; 280eace7efcSopenharmony_ci } 281eace7efcSopenharmony_ci controller->NotifyDlpAbilityClosed(dlpData); 282eace7efcSopenharmony_ci} 283eace7efcSopenharmony_ci#endif // WITH_DLP 284eace7efcSopenharmony_ci 285eace7efcSopenharmony_civoid ConnectionStateManager::HandleAppDied(int32_t pid) 286eace7efcSopenharmony_ci{ 287eace7efcSopenharmony_ci HandleCallerDied(pid); 288eace7efcSopenharmony_ci} 289eace7efcSopenharmony_ci 290eace7efcSopenharmony_ci#ifdef WITH_DLP 291eace7efcSopenharmony_civoid ConnectionStateManager::GetDlpConnectionInfos(std::vector<AbilityRuntime::DlpConnectionInfo> &infos) 292eace7efcSopenharmony_ci{ 293eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(dlpLock_); 294eace7efcSopenharmony_ci for (auto it = dlpItems_.begin(); it != dlpItems_.end(); it++) { 295eace7efcSopenharmony_ci auto item = it->second; 296eace7efcSopenharmony_ci if (!item) { 297eace7efcSopenharmony_ci continue; 298eace7efcSopenharmony_ci } 299eace7efcSopenharmony_ci 300eace7efcSopenharmony_ci AbilityRuntime::DlpConnectionInfo info; 301eace7efcSopenharmony_ci info.dlpUid = item->GetDlpUid(); 302eace7efcSopenharmony_ci info.openedAbilityCount = item->GetOpenedAbilitySize(); 303eace7efcSopenharmony_ci infos.emplace_back(info); 304eace7efcSopenharmony_ci } 305eace7efcSopenharmony_ci} 306eace7efcSopenharmony_ci#endif // WITH_DLP 307eace7efcSopenharmony_ci 308eace7efcSopenharmony_civoid ConnectionStateManager::GetConnectionData(std::vector<AbilityRuntime::ConnectionData> &connectionData) 309eace7efcSopenharmony_ci{ 310eace7efcSopenharmony_ci std::lock_guard guard(stateLock_); 311eace7efcSopenharmony_ci for (const auto &stateItem : connectionStates_) { 312eace7efcSopenharmony_ci if (!stateItem.second) { 313eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "Unexpected null"); 314eace7efcSopenharmony_ci continue; 315eace7efcSopenharmony_ci } 316eace7efcSopenharmony_ci 317eace7efcSopenharmony_ci std::vector<AbilityRuntime::ConnectionData> allConnectionData; 318eace7efcSopenharmony_ci stateItem.second->GenerateAllConnectionData(allConnectionData); 319eace7efcSopenharmony_ci connectionData.insert(connectionData.end(), allConnectionData.begin(), allConnectionData.end()); 320eace7efcSopenharmony_ci } 321eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::CONNECTION, "GetConnectionData: %{public}zu", connectionData.size()); 322eace7efcSopenharmony_ci} 323eace7efcSopenharmony_ci 324eace7efcSopenharmony_cibool ConnectionStateManager::AddConnectionInner(std::shared_ptr<ConnectionRecord> connectionRecord, 325eace7efcSopenharmony_ci AbilityRuntime::ConnectionData &data) 326eace7efcSopenharmony_ci{ 327eace7efcSopenharmony_ci std::shared_ptr<ConnectionStateItem> targetItem = nullptr; 328eace7efcSopenharmony_ci auto callerPid = connectionRecord->GetCallerPid(); 329eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(stateLock_); 330eace7efcSopenharmony_ci auto it = connectionStates_.find(callerPid); 331eace7efcSopenharmony_ci if (it == connectionStates_.end()) { 332eace7efcSopenharmony_ci targetItem = ConnectionStateItem::CreateConnectionStateItem(connectionRecord); 333eace7efcSopenharmony_ci if (targetItem) { 334eace7efcSopenharmony_ci connectionStates_[callerPid] = targetItem; 335eace7efcSopenharmony_ci } 336eace7efcSopenharmony_ci } else { 337eace7efcSopenharmony_ci targetItem = it->second; 338eace7efcSopenharmony_ci } 339eace7efcSopenharmony_ci 340eace7efcSopenharmony_ci if (!targetItem) { 341eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "find targetItem failed"); 342eace7efcSopenharmony_ci return false; 343eace7efcSopenharmony_ci } 344eace7efcSopenharmony_ci 345eace7efcSopenharmony_ci return targetItem->AddConnection(connectionRecord, data); 346eace7efcSopenharmony_ci} 347eace7efcSopenharmony_ci 348eace7efcSopenharmony_cibool ConnectionStateManager::RemoveConnectionInner(std::shared_ptr<ConnectionRecord> connectionRecord, 349eace7efcSopenharmony_ci AbilityRuntime::ConnectionData &data) 350eace7efcSopenharmony_ci{ 351eace7efcSopenharmony_ci auto callerPid = connectionRecord->GetCallerPid(); 352eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(stateLock_); 353eace7efcSopenharmony_ci auto it = connectionStates_.find(callerPid); 354eace7efcSopenharmony_ci if (it == connectionStates_.end()) { 355eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "find target failed, callerPid:%{public}d", callerPid); 356eace7efcSopenharmony_ci return false; 357eace7efcSopenharmony_ci } 358eace7efcSopenharmony_ci 359eace7efcSopenharmony_ci auto targetItem = it->second; 360eace7efcSopenharmony_ci if (!targetItem) { 361eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "find targetItem failed"); 362eace7efcSopenharmony_ci return false; 363eace7efcSopenharmony_ci } 364eace7efcSopenharmony_ci 365eace7efcSopenharmony_ci bool result = targetItem->RemoveConnection(connectionRecord, data); 366eace7efcSopenharmony_ci if (result && targetItem->IsEmpty()) { 367eace7efcSopenharmony_ci connectionStates_.erase(it); 368eace7efcSopenharmony_ci } 369eace7efcSopenharmony_ci return result; 370eace7efcSopenharmony_ci} 371eace7efcSopenharmony_ci 372eace7efcSopenharmony_civoid ConnectionStateManager::HandleCallerDied(int32_t callerPid) 373eace7efcSopenharmony_ci{ 374eace7efcSopenharmony_ci auto connectionStateItem = RemoveDiedCaller(callerPid); 375eace7efcSopenharmony_ci if (!connectionStateItem) { 376eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::CONNECTION, "no connectionStateItem"); 377eace7efcSopenharmony_ci return; 378eace7efcSopenharmony_ci } 379eace7efcSopenharmony_ci 380eace7efcSopenharmony_ci std::vector<AbilityRuntime::ConnectionData> allConnectionData; 381eace7efcSopenharmony_ci connectionStateItem->GenerateAllConnectionData(allConnectionData); 382eace7efcSopenharmony_ci if (allConnectionData.empty()) { 383eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "empty allConnectionData"); 384eace7efcSopenharmony_ci return; 385eace7efcSopenharmony_ci } 386eace7efcSopenharmony_ci 387eace7efcSopenharmony_ci std::shared_ptr<ConnectionObserverController> controller = observerController_; 388eace7efcSopenharmony_ci if (!controller) { 389eace7efcSopenharmony_ci return; 390eace7efcSopenharmony_ci } 391eace7efcSopenharmony_ci 392eace7efcSopenharmony_ci for (auto& connectionData : allConnectionData) { 393eace7efcSopenharmony_ci controller->NotifyExtensionDisconnected(connectionData); 394eace7efcSopenharmony_ci } 395eace7efcSopenharmony_ci} 396eace7efcSopenharmony_ci 397eace7efcSopenharmony_cistd::shared_ptr<ConnectionStateItem> ConnectionStateManager::RemoveDiedCaller(int32_t callerPid) 398eace7efcSopenharmony_ci{ 399eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(stateLock_); 400eace7efcSopenharmony_ci auto it = connectionStates_.find(callerPid); 401eace7efcSopenharmony_ci if (it == connectionStates_.end()) { 402eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "callerPid:%{public}d", callerPid); 403eace7efcSopenharmony_ci return nullptr; 404eace7efcSopenharmony_ci } 405eace7efcSopenharmony_ci auto stateItem = it->second; 406eace7efcSopenharmony_ci (void)connectionStates_.erase(it); 407eace7efcSopenharmony_ci 408eace7efcSopenharmony_ci return stateItem; 409eace7efcSopenharmony_ci} 410eace7efcSopenharmony_ci 411eace7efcSopenharmony_cibool ConnectionStateManager::AddDataAbilityConnectionInner(const DataAbilityCaller &caller, 412eace7efcSopenharmony_ci const std::shared_ptr<DataAbilityRecord> &record, ConnectionData &data) 413eace7efcSopenharmony_ci{ 414eace7efcSopenharmony_ci std::shared_ptr<ConnectionStateItem> targetItem = nullptr; 415eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(stateLock_); 416eace7efcSopenharmony_ci auto it = connectionStates_.find(caller.callerPid); 417eace7efcSopenharmony_ci if (it == connectionStates_.end()) { 418eace7efcSopenharmony_ci targetItem = ConnectionStateItem::CreateConnectionStateItem(caller); 419eace7efcSopenharmony_ci if (targetItem) { 420eace7efcSopenharmony_ci connectionStates_[caller.callerPid] = targetItem; 421eace7efcSopenharmony_ci } 422eace7efcSopenharmony_ci } else { 423eace7efcSopenharmony_ci targetItem = it->second; 424eace7efcSopenharmony_ci } 425eace7efcSopenharmony_ci 426eace7efcSopenharmony_ci if (!targetItem) { 427eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "find targetItem failed"); 428eace7efcSopenharmony_ci return false; 429eace7efcSopenharmony_ci } 430eace7efcSopenharmony_ci 431eace7efcSopenharmony_ci return targetItem->AddDataAbilityConnection(caller, record, data); 432eace7efcSopenharmony_ci} 433eace7efcSopenharmony_ci 434eace7efcSopenharmony_cibool ConnectionStateManager::RemoveDataAbilityConnectionInner(const DataAbilityCaller &caller, 435eace7efcSopenharmony_ci const std::shared_ptr<DataAbilityRecord> &record, AbilityRuntime::ConnectionData &data) 436eace7efcSopenharmony_ci{ 437eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(stateLock_); 438eace7efcSopenharmony_ci auto it = connectionStates_.find(caller.callerPid); 439eace7efcSopenharmony_ci if (it == connectionStates_.end()) { 440eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "find target item failed, callerPid:%{public}d", caller.callerPid); 441eace7efcSopenharmony_ci return false; 442eace7efcSopenharmony_ci } 443eace7efcSopenharmony_ci 444eace7efcSopenharmony_ci auto targetItem = it->second; 445eace7efcSopenharmony_ci if (!targetItem) { 446eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "find targetItem failed"); 447eace7efcSopenharmony_ci return false; 448eace7efcSopenharmony_ci } 449eace7efcSopenharmony_ci 450eace7efcSopenharmony_ci bool result = targetItem->RemoveDataAbilityConnection(caller, record, data); 451eace7efcSopenharmony_ci if (result && targetItem->IsEmpty()) { 452eace7efcSopenharmony_ci connectionStates_.erase(it); 453eace7efcSopenharmony_ci } 454eace7efcSopenharmony_ci return result; 455eace7efcSopenharmony_ci} 456eace7efcSopenharmony_ci 457eace7efcSopenharmony_civoid ConnectionStateManager::HandleDataAbilityDiedInner(const sptr<IRemoteObject> &abilityToken, 458eace7efcSopenharmony_ci std::vector<AbilityRuntime::ConnectionData> &allData) 459eace7efcSopenharmony_ci{ 460eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(stateLock_); 461eace7efcSopenharmony_ci for (auto it = connectionStates_.begin(); it != connectionStates_.end();) { 462eace7efcSopenharmony_ci auto item = it->second; 463eace7efcSopenharmony_ci if (!item) { 464eace7efcSopenharmony_ci connectionStates_.erase(it++); 465eace7efcSopenharmony_ci continue; 466eace7efcSopenharmony_ci } 467eace7efcSopenharmony_ci 468eace7efcSopenharmony_ci AbilityRuntime::ConnectionData data; 469eace7efcSopenharmony_ci if (item->HandleDataAbilityDied(abilityToken, data)) { 470eace7efcSopenharmony_ci allData.emplace_back(data); 471eace7efcSopenharmony_ci } 472eace7efcSopenharmony_ci 473eace7efcSopenharmony_ci if (item->IsEmpty()) { 474eace7efcSopenharmony_ci connectionStates_.erase(it++); 475eace7efcSopenharmony_ci } else { 476eace7efcSopenharmony_ci it++; 477eace7efcSopenharmony_ci } 478eace7efcSopenharmony_ci } 479eace7efcSopenharmony_ci} 480eace7efcSopenharmony_ci 481eace7efcSopenharmony_ci#ifdef WITH_DLP 482eace7efcSopenharmony_cibool ConnectionStateManager::HandleDlpAbilityInner(const std::shared_ptr<AbilityRecord> &dlpAbility, 483eace7efcSopenharmony_ci bool isAdd, AbilityRuntime::DlpStateData &dlpData) 484eace7efcSopenharmony_ci{ 485eace7efcSopenharmony_ci if (!dlpAbility) { 486eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::CONNECTION, "invalid dlp ability"); 487eace7efcSopenharmony_ci return false; 488eace7efcSopenharmony_ci } 489eace7efcSopenharmony_ci 490eace7efcSopenharmony_ci if (dlpAbility->GetAppIndex() <= AbilityRuntime::GlobalConstant::MAX_APP_CLONE_INDEX) { 491eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::CONNECTION, " not dlp ability, do not report connection stat"); 492eace7efcSopenharmony_ci return false; 493eace7efcSopenharmony_ci } 494eace7efcSopenharmony_ci 495eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(dlpLock_); 496eace7efcSopenharmony_ci auto it = dlpItems_.find(dlpAbility->GetOwnerMissionUserId()); 497eace7efcSopenharmony_ci if (it == dlpItems_.end()) { 498eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "invalid state"); 499eace7efcSopenharmony_ci return false; 500eace7efcSopenharmony_ci } 501eace7efcSopenharmony_ci 502eace7efcSopenharmony_ci auto dlpItem = it->second; 503eace7efcSopenharmony_ci if (!dlpItem) { 504eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "invalid dlpItem"); 505eace7efcSopenharmony_ci return false; 506eace7efcSopenharmony_ci } 507eace7efcSopenharmony_ci 508eace7efcSopenharmony_ci if (isAdd) { 509eace7efcSopenharmony_ci return dlpItem->AddDlpConnectionState(dlpAbility, dlpData); 510eace7efcSopenharmony_ci } 511eace7efcSopenharmony_ci 512eace7efcSopenharmony_ci return dlpItem->RemoveDlpConnectionState(dlpAbility, dlpData); 513eace7efcSopenharmony_ci} 514eace7efcSopenharmony_ci#endif // WITH_DLP 515eace7efcSopenharmony_ci 516eace7efcSopenharmony_civoid ConnectionStateManager::InitAppStateObserver() 517eace7efcSopenharmony_ci{ 518eace7efcSopenharmony_ci if (appStateObserver_) { 519eace7efcSopenharmony_ci return; 520eace7efcSopenharmony_ci } 521eace7efcSopenharmony_ci 522eace7efcSopenharmony_ci sptr<OHOS::AppExecFwk::IAppMgr> appManager = GetAppMgr(); 523eace7efcSopenharmony_ci if (!appManager) { 524eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "null appManager, retry:%{public}d", retry_); 525eace7efcSopenharmony_ci if (retry_ < MAX_RETRY && handler_) { 526eace7efcSopenharmony_ci auto initConnectionStateManagerTask = [weak = weak_from_this()]() { 527eace7efcSopenharmony_ci auto self = weak.lock(); 528eace7efcSopenharmony_ci if (!self) { 529eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::CONNECTION, "invalid self pointer"); 530eace7efcSopenharmony_ci return; 531eace7efcSopenharmony_ci } 532eace7efcSopenharmony_ci self->InitAppStateObserver(); 533eace7efcSopenharmony_ci }; 534eace7efcSopenharmony_ci handler_->SubmitTask(initConnectionStateManagerTask, "InitConnectionStateManager", DELAY_TIME); 535eace7efcSopenharmony_ci retry_++; 536eace7efcSopenharmony_ci } 537eace7efcSopenharmony_ci return; 538eace7efcSopenharmony_ci } 539eace7efcSopenharmony_ci 540eace7efcSopenharmony_ci appStateObserver_ = new (std::nothrow)InnerAppStateObserver([](int32_t pid) { 541eace7efcSopenharmony_ci DelayedSingleton<ConnectionStateManager>::GetInstance()->HandleAppDied(pid); 542eace7efcSopenharmony_ci }); 543eace7efcSopenharmony_ci int32_t err = appManager->RegisterApplicationStateObserver(appStateObserver_); 544eace7efcSopenharmony_ci if (err != 0) { 545eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::CONNECTION, "register to appmgr err:%{public}d", err); 546eace7efcSopenharmony_ci appStateObserver_ = nullptr; 547eace7efcSopenharmony_ci return; 548eace7efcSopenharmony_ci } 549eace7efcSopenharmony_ci} 550eace7efcSopenharmony_ci} // namespace AAFwk 551eace7efcSopenharmony_ci} // namespace OHOS 552