1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2023-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 "app_debug_manager.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h" 19eace7efcSopenharmony_ci#include "ipc_types.h" 20eace7efcSopenharmony_ci 21eace7efcSopenharmony_cinamespace OHOS { 22eace7efcSopenharmony_cinamespace AppExecFwk { 23eace7efcSopenharmony_ciint32_t AppDebugManager::RegisterAppDebugListener(const sptr<IAppDebugListener> &listener) 24eace7efcSopenharmony_ci{ 25eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "called"); 26eace7efcSopenharmony_ci if (listener == nullptr) { 27eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "null listener"); 28eace7efcSopenharmony_ci return ERR_INVALID_DATA; 29eace7efcSopenharmony_ci } 30eace7efcSopenharmony_ci 31eace7efcSopenharmony_ci std::unique_lock<std::mutex> lock(mutex_); 32eace7efcSopenharmony_ci const auto &finder = 33eace7efcSopenharmony_ci std::find_if(listeners_.begin(), listeners_.end(), [&listener](const sptr<IAppDebugListener> &element) { 34eace7efcSopenharmony_ci if (element != nullptr && listener != nullptr) { 35eace7efcSopenharmony_ci return element->AsObject() == listener->AsObject(); 36eace7efcSopenharmony_ci } 37eace7efcSopenharmony_ci return false; 38eace7efcSopenharmony_ci }); 39eace7efcSopenharmony_ci if (finder == listeners_.end()) { 40eace7efcSopenharmony_ci listeners_.emplace(listener); 41eace7efcSopenharmony_ci } 42eace7efcSopenharmony_ci 43eace7efcSopenharmony_ci if (!debugInfos_.empty()) { 44eace7efcSopenharmony_ci listener->OnAppDebugStarted(debugInfos_); 45eace7efcSopenharmony_ci } 46eace7efcSopenharmony_ci return ERR_OK; 47eace7efcSopenharmony_ci} 48eace7efcSopenharmony_ci 49eace7efcSopenharmony_ciint32_t AppDebugManager::UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener) 50eace7efcSopenharmony_ci{ 51eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "called"); 52eace7efcSopenharmony_ci if (listener == nullptr) { 53eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "null listener"); 54eace7efcSopenharmony_ci return ERR_INVALID_DATA; 55eace7efcSopenharmony_ci } 56eace7efcSopenharmony_ci 57eace7efcSopenharmony_ci std::unique_lock<std::mutex> lock(mutex_); 58eace7efcSopenharmony_ci const auto &finder = 59eace7efcSopenharmony_ci std::find_if(listeners_.begin(), listeners_.end(), [&listener](const sptr<IAppDebugListener> &element) { 60eace7efcSopenharmony_ci if (element != nullptr && listener != nullptr) { 61eace7efcSopenharmony_ci return element->AsObject() == listener->AsObject(); 62eace7efcSopenharmony_ci } 63eace7efcSopenharmony_ci return false; 64eace7efcSopenharmony_ci }); 65eace7efcSopenharmony_ci if (finder != listeners_.end()) { 66eace7efcSopenharmony_ci listeners_.erase(finder); 67eace7efcSopenharmony_ci } 68eace7efcSopenharmony_ci return ERR_OK; 69eace7efcSopenharmony_ci} 70eace7efcSopenharmony_ci 71eace7efcSopenharmony_civoid AppDebugManager::StartDebug(const std::vector<AppDebugInfo> &infos) 72eace7efcSopenharmony_ci{ 73eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "called"); 74eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 75eace7efcSopenharmony_ci std::vector<AppDebugInfo> incrementInfos; 76eace7efcSopenharmony_ci GetIncrementAppDebugInfos(infos, incrementInfos); 77eace7efcSopenharmony_ci if (incrementInfos.empty()) { 78eace7efcSopenharmony_ci return; 79eace7efcSopenharmony_ci } 80eace7efcSopenharmony_ci 81eace7efcSopenharmony_ci for (const auto &listener : listeners_) { 82eace7efcSopenharmony_ci if (listener == nullptr) { 83eace7efcSopenharmony_ci continue; 84eace7efcSopenharmony_ci } 85eace7efcSopenharmony_ci listener->OnAppDebugStarted(incrementInfos); 86eace7efcSopenharmony_ci } 87eace7efcSopenharmony_ci} 88eace7efcSopenharmony_ci 89eace7efcSopenharmony_civoid AppDebugManager::StopDebug(const std::vector<AppDebugInfo> &infos) 90eace7efcSopenharmony_ci{ 91eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "called"); 92eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 93eace7efcSopenharmony_ci std::vector<AppDebugInfo> debugInfos; 94eace7efcSopenharmony_ci for (auto &it : infos) { 95eace7efcSopenharmony_ci auto isExist = [this, it](const AppDebugInfo &info) { 96eace7efcSopenharmony_ci return (info.bundleName == it.bundleName && info.pid == it.pid && 97eace7efcSopenharmony_ci info.uid == it.uid && info.isDebugStart == it.isDebugStart); 98eace7efcSopenharmony_ci }; 99eace7efcSopenharmony_ci 100eace7efcSopenharmony_ci auto finder = std::find_if(debugInfos_.begin(), debugInfos_.end(), isExist); 101eace7efcSopenharmony_ci if (finder != debugInfos_.end()) { 102eace7efcSopenharmony_ci debugInfos_.erase(finder); 103eace7efcSopenharmony_ci debugInfos.emplace_back(it); 104eace7efcSopenharmony_ci } 105eace7efcSopenharmony_ci } 106eace7efcSopenharmony_ci 107eace7efcSopenharmony_ci if (!debugInfos.empty()) { 108eace7efcSopenharmony_ci for (const auto &listener : listeners_) { 109eace7efcSopenharmony_ci if (listener == nullptr) { 110eace7efcSopenharmony_ci continue; 111eace7efcSopenharmony_ci } 112eace7efcSopenharmony_ci listener->OnAppDebugStoped(debugInfos); 113eace7efcSopenharmony_ci } 114eace7efcSopenharmony_ci } 115eace7efcSopenharmony_ci} 116eace7efcSopenharmony_ci 117eace7efcSopenharmony_cibool AppDebugManager::IsAttachDebug(const std::string &bundleName) 118eace7efcSopenharmony_ci{ 119eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 120eace7efcSopenharmony_ci for (auto &iter : debugInfos_) { 121eace7efcSopenharmony_ci if (iter.bundleName == bundleName && !iter.isDebugStart) { 122eace7efcSopenharmony_ci return true; 123eace7efcSopenharmony_ci } 124eace7efcSopenharmony_ci } 125eace7efcSopenharmony_ci return false; 126eace7efcSopenharmony_ci} 127eace7efcSopenharmony_ci 128eace7efcSopenharmony_civoid AppDebugManager::GetIncrementAppDebugInfos( 129eace7efcSopenharmony_ci const std::vector<AppDebugInfo> &infos, std::vector<AppDebugInfo> &incrementInfos) 130eace7efcSopenharmony_ci{ 131eace7efcSopenharmony_ci for (auto &it : infos) { 132eace7efcSopenharmony_ci auto isExist = [this, it](const AppDebugInfo &info) { 133eace7efcSopenharmony_ci return (info.bundleName == it.bundleName && info.pid == it.pid && info.uid == it.uid); 134eace7efcSopenharmony_ci }; 135eace7efcSopenharmony_ci 136eace7efcSopenharmony_ci auto finder = std::find_if(debugInfos_.begin(), debugInfos_.end(), isExist); 137eace7efcSopenharmony_ci if (finder == debugInfos_.end()) { 138eace7efcSopenharmony_ci incrementInfos.emplace_back(it); 139eace7efcSopenharmony_ci } else { 140eace7efcSopenharmony_ci if (!finder->isDebugStart && it.isDebugStart) { 141eace7efcSopenharmony_ci finder->isDebugStart = it.isDebugStart; 142eace7efcSopenharmony_ci } 143eace7efcSopenharmony_ci } 144eace7efcSopenharmony_ci } 145eace7efcSopenharmony_ci 146eace7efcSopenharmony_ci debugInfos_.insert(debugInfos_.end(), incrementInfos.begin(), incrementInfos.end()); 147eace7efcSopenharmony_ci} 148eace7efcSopenharmony_ci 149eace7efcSopenharmony_civoid AppDebugManager::RemoveAppDebugInfo(const AppDebugInfo &info) 150eace7efcSopenharmony_ci{ 151eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "called"); 152eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 153eace7efcSopenharmony_ci auto isExist = [this, info](const AppDebugInfo &debugInfo) { 154eace7efcSopenharmony_ci return (debugInfo.bundleName == info.bundleName && debugInfo.pid == info.pid && 155eace7efcSopenharmony_ci debugInfo.uid == info.uid && debugInfo.isDebugStart == info.isDebugStart); 156eace7efcSopenharmony_ci }; 157eace7efcSopenharmony_ci 158eace7efcSopenharmony_ci auto finder = std::find_if(debugInfos_.begin(), debugInfos_.end(), isExist); 159eace7efcSopenharmony_ci if (finder == debugInfos_.end()) { 160eace7efcSopenharmony_ci return; 161eace7efcSopenharmony_ci } 162eace7efcSopenharmony_ci debugInfos_.erase(finder); 163eace7efcSopenharmony_ci 164eace7efcSopenharmony_ci std::vector<AppDebugInfo> debugInfos; 165eace7efcSopenharmony_ci debugInfos.emplace_back(info); 166eace7efcSopenharmony_ci for (const auto &listener : listeners_) { 167eace7efcSopenharmony_ci if (listener == nullptr) { 168eace7efcSopenharmony_ci continue; 169eace7efcSopenharmony_ci } 170eace7efcSopenharmony_ci listener->OnAppDebugStoped(debugInfos); 171eace7efcSopenharmony_ci } 172eace7efcSopenharmony_ci} 173eace7efcSopenharmony_ci} // namespace AppExecFwk 174eace7efcSopenharmony_ci} // namespace OHOS 175