Skip to content

Commit d4ca3f4

Browse files
ut003640deepin-bot[bot]
authored andcommitted
fix: optimize the logic for opening the authentication web page
1.Since deepin-service-manager does not exit after the current user logs out, logic for monitoring desktop entry and desktop exit has been added. The presence of the portal web page is checked only after entering the desktop, by detecting whether the SessionManager service is started. This ensures that the web page is always opened after the desktop session is ready. 2.When assigning a value to the portal, logic has been added to compare the current network with the last network. This allows the portal address to be updated immediately when switching networks rapidly. Log: Optimize the logic for opening the authentication web page Influence: portal web page PMS: TASK-360019 fix: 优化打开认证网页逻辑 1、由于deepin-service-manager在注销当前用户后不会退出,所以增加监视进入桌面和退出桌面的逻辑,在进入桌面后再检测是否存在portal网页,使用检测SessionManager服务是否启动的方式来判断,这样保证了打开网页始终在进入桌面后进行 2、在给portal赋值的情况下,增加判断当前网络和最后一次网络是否相同的逻辑,这样在快速切换网络的时候,portal地址会马上更新 Log: 优化打开认证网页逻辑 Influence: portal网页
1 parent 7f61ca2 commit d4ca3f4

File tree

6 files changed

+256
-212
lines changed

6 files changed

+256
-212
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: LGPL-3.0-or-later
4+
5+
#include "desktopmonitor.h"
6+
#include "constants.h"
7+
8+
#include <QDBusConnection>
9+
#include <QDBusInterface>
10+
#include <QDBusServiceWatcher>
11+
#include <QDBusConnectionInterface>
12+
#include <QTimer>
13+
14+
using namespace network::sessionservice;
15+
16+
#define SESSION_MANAG_SERVICE "com.deepin.SessionManager"
17+
18+
DesktopMonitor::DesktopMonitor(QObject *parent)
19+
: QObject (parent)
20+
, m_prepare(false)
21+
, m_checkFinished(false)
22+
, m_timer(nullptr)
23+
{
24+
init();
25+
}
26+
27+
bool DesktopMonitor::prepared() const
28+
{
29+
return m_prepare && (!m_displayEnvironment.isEmpty() || m_checkFinished);
30+
}
31+
32+
QString DesktopMonitor::displayEnvironment() const
33+
{
34+
return m_displayEnvironment;
35+
}
36+
37+
void DesktopMonitor::init()
38+
{
39+
m_prepare = QDBusConnection::sessionBus().interface()->isServiceRegistered(SESSION_MANAG_SERVICE);
40+
qCDebug(DSM) << SESSION_MANAG_SERVICE << " prepare status" << m_prepare;
41+
if (m_prepare) {
42+
m_displayEnvironment = getDisplayEnvironment();
43+
qCDebug(DSM) << "get display environment" << m_displayEnvironment;
44+
if (m_displayEnvironment.isEmpty()) {
45+
m_timer = new QTimer(this);
46+
m_timer->setInterval(1000);
47+
m_timer->setProperty("checktime", 0);
48+
connect(m_timer, &QTimer::timeout, this, &DesktopMonitor::onCheckTimeout);
49+
m_timer->start();
50+
}
51+
}
52+
53+
// 就算注销当前用户后,deepin-service-manager服务也不会退出,所以这里需要监控该服务的退出和启动的状态
54+
QDBusServiceWatcher *serviceWatcher = new QDBusServiceWatcher(this);
55+
serviceWatcher->setConnection(QDBusConnection::sessionBus());
56+
serviceWatcher->addWatchedService(SESSION_MANAG_SERVICE);
57+
connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, &DesktopMonitor::onServiceRegistered);
58+
QDBusConnection::sessionBus().connect("com.deepin.SessionManager", "/com/deepin/SessionManager", "com.deepin.SessionManager",
59+
"PrepareLogout", this, SLOT(onPrepareLogout()));
60+
}
61+
62+
QString DesktopMonitor::getDisplayEnvironment() const
63+
{
64+
QString displayenv = qgetenv("DISPLAY");
65+
if (!displayenv.isEmpty()) {
66+
qCDebug(DSM) << "get DISPLAY Environment from local env";
67+
return displayenv;
68+
}
69+
70+
qCDebug(DSM) << "get DISPLAY Environment from systemd1 service";
71+
// 有时候获取到的环境变量为空,此时就需要从systemd接口中来获取环境变量了
72+
QDBusInterface systemdInterface("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager");
73+
QStringList environments = systemdInterface.property("Environment").toStringList();
74+
for (const QString &env : environments) {
75+
QStringList envArray = env.split("=");
76+
if (envArray.size() < 2)
77+
continue;
78+
if (envArray.first() == "DISPLAY")
79+
return envArray[1];
80+
}
81+
82+
return QString();
83+
}
84+
85+
void DesktopMonitor::checkFinished()
86+
{
87+
m_checkFinished = true;
88+
m_timer->stop();
89+
m_timer->deleteLater();
90+
m_timer = nullptr;
91+
if (prepared()) {
92+
qCDebug(DSM) << "display environment check finished";
93+
emit desktopChanged(true);
94+
}
95+
}
96+
97+
void DesktopMonitor::onCheckTimeout()
98+
{
99+
m_displayEnvironment = getDisplayEnvironment();
100+
if (m_displayEnvironment.isEmpty()) {
101+
// 连接检测20次,如果20次还是没有这个环境变量,就说明这个环境变量有问题,直接尝试打开
102+
int checktime = m_timer->property("checktime").toInt();
103+
qCDebug(DSM) << "in " << checktime << "times, the display environment is empty";
104+
if (checktime >= 20) {
105+
checkFinished();
106+
} else {
107+
checktime++;
108+
m_timer->setProperty("checktime", checktime);
109+
}
110+
} else {
111+
qCDebug(DSM) << "the display environment is not empty" << m_displayEnvironment;
112+
checkFinished();
113+
}
114+
}
115+
116+
void DesktopMonitor::onServiceRegistered(const QString &service)
117+
{
118+
if (service != SESSION_MANAG_SERVICE)
119+
return;
120+
121+
if (m_prepare && !m_displayEnvironment.isEmpty()) {
122+
// 如果之前是已经准备好的,无需再次发送信号,这种情况一般发生在kill掉startdde进程或者重启SessionManager服务
123+
qCDebug(DSM) << "the prepare is true";
124+
return;
125+
}
126+
127+
m_prepare = true;
128+
m_displayEnvironment = getDisplayEnvironment();
129+
qCInfo(DSM) << SESSION_MANAG_SERVICE << "start success, display envionment:" << m_displayEnvironment;
130+
if (prepared()) {
131+
qCDebug(DSM) << "desktop prepared";
132+
emit desktopChanged(true);
133+
}
134+
}
135+
136+
void DesktopMonitor::onPrepareLogout()
137+
{
138+
// 注销当前账户
139+
m_prepare = false;
140+
qCDebug(DSM) << "current user is logout";
141+
emit desktopChanged(false);
142+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: LGPL-3.0-or-later
4+
5+
#ifndef DESKTOPMONITOR_H
6+
#define DESKTOPMONITOR_H
7+
8+
#include <QObject>
9+
10+
class QTimer;
11+
12+
namespace network {
13+
namespace sessionservice {
14+
/**
15+
* @brief The StartMonitor class
16+
* 用于监控环境是否准备好,桌面环境准备好了,才能进行下一步的动作
17+
*/
18+
class DesktopMonitor : public QObject
19+
{
20+
Q_OBJECT
21+
22+
public:
23+
DesktopMonitor(QObject *parent = Q_NULLPTR);
24+
~DesktopMonitor() = default;
25+
bool prepared() const; // 环境是否准备好
26+
QString displayEnvironment() const; // DISPLAY环境变量
27+
28+
signals:
29+
void desktopChanged(bool); // 桌面环境变化发出的信号,true为进入桌面后并且环境准备好了,false为注销当前环境
30+
31+
private:
32+
void init();
33+
QString getDisplayEnvironment() const;
34+
void checkFinished();
35+
36+
private slots:
37+
void onCheckTimeout();
38+
void onServiceRegistered(const QString &service);
39+
void onPrepareLogout();
40+
41+
private:
42+
bool m_prepare;
43+
bool m_checkFinished;
44+
QString m_displayEnvironment;
45+
QTimer *m_timer;
46+
};
47+
48+
}
49+
}
50+
51+
#endif

network-service-plugin/src/session/sessioncontainer.cpp

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include "sessioncontainer.h"
66
#include "sessionipconfilct.h"
77
#include "constants.h"
8-
#include "urlopenerhelper.h"
8+
#include "desktopmonitor.h"
99

1010
#include <QDBusConnection>
1111
#include <QDBusInterface>
12+
#include <QProcessEnvironment>
13+
#include <QProcess>
1214

1315
using namespace network::sessionservice;
1416

@@ -19,9 +21,10 @@ static QString networkInterface = "org.deepin.service.SystemNetwork";
1921
SessionContainer::SessionContainer(QObject *parent)
2022
: QObject (parent)
2123
, m_ipConflictHandler(new SessionIPConflict(this))
24+
, m_desktopMonitor(new DesktopMonitor(this))
2225
{
23-
initMember();
2426
initConnection();
27+
initEnvornment();
2528
}
2629

2730
SessionContainer::~SessionContainer()
@@ -39,9 +42,10 @@ void SessionContainer::initConnection()
3942
QDBusConnection::systemBus().connect(networkService, networkPath, networkInterface, "PortalDetected", this, SLOT(onPortalDetected(const QString &)));
4043
// 当系统代理发生变化的时候需要主动调用SystemNetwork服务的检查网络连通性的接口
4144
QDBusConnection::sessionBus().connect("org.deepin.dde.Network1", "/org/deepin/dde/Network1", "org.deepin.dde.Network1", "ProxyMethodChanged", this, SLOT(onProxyMethodChanged(const QString &)));
45+
connect(m_desktopMonitor, &DesktopMonitor::desktopChanged, this, &SessionContainer::onDesktopChanged);
4246
}
4347

44-
void SessionContainer::initMember()
48+
void SessionContainer::checkPortalUrl()
4549
{
4650
// 检测初始化的状态是否为门户认证,这种情况下需要先打开Url
4751
QDBusInterface dbusInter("org.deepin.service.SystemNetwork", "/org/deepin/service/SystemNetwork", "org.deepin.service.SystemNetwork", QDBusConnection::systemBus());
@@ -50,19 +54,59 @@ void SessionContainer::initMember()
5054
// 获取需要认证的网站的信息,并打开网页
5155
QString url = dbusInter.property("PortalUrl").toString();
5256
qCDebug(DSM) << "check portal url:" << url;
53-
onPortalDetected(url);
57+
openPortalUrl(url);
5458
}
5559
}
5660

61+
void SessionContainer::initEnvornment()
62+
{
63+
if (m_desktopMonitor->prepared())
64+
enterDesktop();
65+
}
66+
67+
void SessionContainer::enterDesktop()
68+
{
69+
qCInfo(DSM) << "enter desktop";
70+
// 进入桌面,检查本地portal连接
71+
checkPortalUrl();
72+
}
73+
74+
void SessionContainer::leaveDesktop()
75+
{
76+
qCInfo(DSM) << "leave desktop";
77+
// TODO: 注销后离开桌面
78+
}
79+
80+
void SessionContainer::openPortalUrl(const QString &url)
81+
{
82+
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
83+
QString displayEnvironment = m_desktopMonitor->displayEnvironment();
84+
if (!displayEnvironment.isEmpty())
85+
env.insert("DISPLAY", displayEnvironment);
86+
87+
QProcess process;
88+
process.setProcessEnvironment(env);
89+
process.start("xdg-open", QStringList() << url);
90+
process.waitForFinished();
91+
}
92+
5793
void SessionContainer::onIPConflictChanged(const QString &devicePath, const QString &ip, bool conflicted)
5894
{
5995
// TODO: 这里用于处理IP冲突,例如检测到IP冲突后,会给出提示的消息,等后期将任务栏种的相关处理删除后,再到这里来处理,目前此处暂时保留
96+
Q_UNUSED(devicePath)
97+
Q_UNUSED(ip)
98+
Q_UNUSED(conflicted)
6099
}
61100

62101
void SessionContainer::onPortalDetected(const QString &url)
63102
{
103+
if (!m_desktopMonitor->prepared()) {
104+
qCWarning(DSM) << "desktop is not login in";
105+
return;
106+
}
107+
64108
qCDebug(DSM) << "detacted portal url" << url;
65-
UrlOpenerHelper::openUrl(url);
109+
openPortalUrl(url);
66110
}
67111

68112
void SessionContainer::onProxyMethodChanged(const QString &method)
@@ -73,3 +117,8 @@ void SessionContainer::onProxyMethodChanged(const QString &method)
73117
QDBusPendingCall callReply = dbusInter.asyncCall("CheckConnectivity");
74118
callReply.waitForFinished();
75119
}
120+
121+
void SessionContainer::onDesktopChanged(bool isLogin)
122+
{
123+
isLogin ? enterDesktop() : leaveDesktop();
124+
}

network-service-plugin/src/session/sessioncontainer.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -11,6 +11,7 @@ namespace network {
1111
namespace sessionservice {
1212

1313
class SessionIPConflict;
14+
class DesktopMonitor;
1415

1516
class SessionContainer : public QObject
1617
{
@@ -23,15 +24,21 @@ class SessionContainer : public QObject
2324

2425
private:
2526
void initConnection();
26-
void initMember();
27+
void initEnvornment();
28+
void checkPortalUrl();
29+
void enterDesktop();
30+
void leaveDesktop();
31+
void openPortalUrl(const QString &url);
2732

2833
private slots:
2934
void onIPConflictChanged(const QString &devicePath, const QString &ip, bool conflicted);
3035
void onPortalDetected(const QString &url);
3136
void onProxyMethodChanged(const QString &method);
37+
void onDesktopChanged(bool isLogin);
3238

3339
private:
3440
SessionIPConflict *m_ipConflictHandler;
41+
DesktopMonitor *m_desktopMonitor;
3542
};
3643

3744
}

0 commit comments

Comments
 (0)