refact: use lambda in identifyWindowByPidEnv

-
This commit is contained in:
black-desk 2023-04-17 16:18:14 +08:00 committed by deepin-bot[bot]
parent 4e43ef0cdb
commit 5f9beaa1c2

View File

@ -233,29 +233,34 @@ AppInfo *WindowIdentify::identifyWindowByPidEnv(Dock *_dock, WindowInfoX *winInf
int launchedDesktopFilePid = launchedDesktopFilePidStr.toInt(); int launchedDesktopFilePid = launchedDesktopFilePidStr.toInt();
qInfo() << "launchedDesktopFilePid=" << launchedDesktopFilePid << " launchedDesktopFile=" << launchedDesktopFile; qInfo() << "launchedDesktopFilePid=" << launchedDesktopFilePid << " launchedDesktopFile=" << launchedDesktopFile;
// 以下 2 种情况下,才能信任环境变量 GIO_LAUNCHED_DESKTOP_FILE。 auto pidIsSh = [](int pid) -> bool {
// 1. 当窗口 pid 和 launchedDesktopFilePid 相同时; Process parentProcess(pid);
// 2. 当窗口的进程的父进程 id即 ppid和 launchedDesktopFilePid 相同,
// 并且该父进程是 sh 或 bash 时。
bool needTry = false;
if (pid == launchedDesktopFilePid) {
needTry = true;
} else if (process->getPpid() && process->getPpid() == launchedDesktopFilePid) {
Process parentProcess(launchedDesktopFilePid);
auto parentCmdLine = parentProcess.getCmdLine(); auto parentCmdLine = parentProcess.getCmdLine();
if (parentCmdLine.size() > 0) { if (parentCmdLine.size() <= 0) {
qInfo() << "ppid equal " << "parentCmdLine[0]:" << parentCmdLine[0].c_str(); return false;
QString cmd0 = parentCmdLine[0].c_str();
int pos = cmd0.lastIndexOf('/');
if (pos > 0)
cmd0 = cmd0.remove(0, pos + 1);
if (cmd0 == "sh" || cmd0 == "bash")
needTry = true;
} }
}
if (needTry) { qInfo() << "ppid equal" << "parentCmdLine[0]:" << parentCmdLine[0].c_str();
QString cmd0 = parentCmdLine[0].c_str();
int pos = cmd0.lastIndexOf('/');
if (pos > 0)
cmd0 = cmd0.remove(0, pos + 1);
if (cmd0 == "sh" || cmd0 == "bash"){
return true;
}
return false;
};
// 以下几种情况下,才能信任环境变量 GIO_LAUNCHED_DESKTOP_FILE。
if (pid == launchedDesktopFilePid || // 当窗口pid和launchedDesktopFilePid相同时
( process->getPpid() &&
process->getPpid() == launchedDesktopFilePid &&
pidIsSh(process->getPpid())
) // 当窗口的进程的父进程id即ppid和launchedDesktopFilePid相同并且该父进程是sh或bash时。
) {
ret = new AppInfo(launchedDesktopFile); ret = new AppInfo(launchedDesktopFile);
innerId = ret->getInnerId(); innerId = ret->getInnerId();
} }