Skip to content

Conversation

@deepin-ci-robot
Copy link
Contributor

@deepin-ci-robot deepin-ci-robot commented Nov 4, 2025

Synchronize source files from linuxdeepin/dde-session-shell.

Source-pull-request: linuxdeepin/dde-session-shell#50

Summary by Sourcery

Sync upstream updates to remove the deprecated PowerManager interface, add virtual machine detection, refactor suspend/hibernate checks to rely on login1 and environment variables, and clean up obsolete XML and generated files

New Features:

  • Disable suspend and hibernate when running inside a virtual machine

Enhancements:

  • Replace the deprecated Deepin PowerManager DBus interface with canSuspend/canHibernate methods using org.freedesktop.login1 and environment flags
  • Introduce detectVirtualMachine() to drive suspend/hibernate availability checks
  • Refactor checkPowerInfo() to use the new canSuspend/canHibernate methods

Build:

  • Remove PowerManager1 XML from CMakeLists and delete generated interface files

@sourcery-ai
Copy link

sourcery-ai bot commented Nov 4, 2025

Reviewer's Guide

This PR syncs updates from linuxdeepin/dde-session-shell by removing the legacy PowerManagerInter dependency and related generated interfaces, refactoring suspend/hibernate checks to rely solely on login1 and configuration settings, and adding VM detection to prevent power actions in virtual environments.

Class diagram for updated AuthInterface (removal of PowerManagerInter, new methods, and attributes)

classDiagram
    class AuthInterface {
        +bool canSuspend()
        +bool canHibernate()
        +bool detectVirtualMachine()
        -PowerManagerInter* m_powerManagerInter
        +bool m_isVM
        SessionBaseModel* m_model
        QGSettings* m_gsettings
        uint m_lastLogoutUid
        uint m_currentUserUid
        std::list<uint> m_loginUserList
    }
    AuthInterface --> AccountsInter
    AuthInterface --> LoginedInter
    AuthInterface --> DBusLogin1Manager
    AuthInterface --> Login1SessionSelf
    AuthInterface --> DBusObjectInter
Loading

Class diagram for removal of PowerManagerInter type aliases

classDiagram
    class DSS_DBUS {
        -const QString powerManagerService
        -const QString powerManagerPath
    }
    class org_deepin_dde {
        -const QString powerManagerService
        -const QString powerManagerPath
    }
Loading

Class diagram for removed PowerManager1 generated interface files

classDiagram
    class PowerManager1Adaptor {
        <<removed>>
    }
Loading

Flow diagram for new suspend/hibernate logic in AuthInterface

flowchart TD
    A["User initiates suspend/hibernate action"] --> B["AuthInterface checks m_isVM"]
    B -- "If true" --> C["Action denied"]
    B -- "If false" --> D["Check environment variable (POWER_CAN_SLEEP/POWER_CAN_HIBERNATE)"]
    D -- "If 0" --> C
    D -- "If not 0" --> E["Check /sys/power/mem_sleep for suspend"]
    E -- "If missing" --> C
    E -- "If present" --> F["Query login1 for CanSuspend/CanHibernate"]
    F -- "If yes" --> G["Allow action"]
    F -- "If no" --> C
Loading

Flow diagram for VM detection in AuthInterface

flowchart TD
    A["detectVirtualMachine() called"] --> B["Run /usr/bin/systemd-detect-virt"]
    B --> C["Process exit code"]
    C -- "!= 0" --> D["Return false (not VM)"]
    C -- "== 0" --> E["Read output"]
    E -- "Output is 'none' or empty" --> D
    E -- "Output is VM type" --> F["Return true (is VM)"]
Loading

File-Level Changes

Change Details Files
Remove PowerManagerInter dependency and generated interfaces
  • Drop m_powerManagerInter member, constructor initialization, and all usage
  • Remove PowerManagerInter includes and using declarations
  • Delete powermanager D-Bus constants from dbusconstant.h
  • Remove powermanager1interface from CMakeLists and generated XML/adaptor files
src/session-widgets/authinterface.cpp
src/session-widgets/authinterface.h
src/global_util/dbusconstant.h
CMakeLists.txt
toolGenerate/qdbusxml2cpp/org.deepin.dde.PowerManager1Adaptor.cpp
toolGenerate/qdbusxml2cpp/org.deepin.dde.PowerManager1Adaptor.h
xml/snipe/org.deepin.dde.PowerManager1.xml
Refactor power suspend/hibernate checks to use login1 and settings
  • Replace environment-variable and PowerManagerInter checks in checkPowerInfo with getGSettings/getDconfigValue combined with canSuspend()/canHibernate()
  • Implement canSuspend() and canHibernate() methods using DBusLogin1Manager only
  • Update LockWorker to invoke canHibernate() instead of m_powerManagerInter->CanHibernate()
src/session-widgets/authinterface.cpp
src/session-widgets/authinterface.h
src/dde-lock/lockworker.cpp
Add virtualization detection to block power actions in VMs
  • Introduce detectVirtualMachine() helper and m_isVM flag
  • Initialize m_isVM in the AuthInterface constructor
  • Integrate m_isVM condition into canSuspend() and canHibernate()
src/session-widgets/authinterface.cpp
src/session-widgets/authinterface.h

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Calling detectVirtualMachine synchronously in the constructor can block startup; consider deferring VM detection or running it asynchronously.
  • In canSuspend, checking for /sys/power/mem_sleep existence alone may allow unsupported modes; you should inspect the file contents to confirm the presence of a valid sleep mode (e.g., 'deep' or 's2idle').
  • Replace raw getenv calls with QProcessEnvironment or qgetenv and centralize the POWER_CAN_* keys as static constants to improve consistency and null safety.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Calling detectVirtualMachine synchronously in the constructor can block startup; consider deferring VM detection or running it asynchronously.
- In canSuspend, checking for /sys/power/mem_sleep existence alone may allow unsupported modes; you should inspect the file contents to confirm the presence of a valid sleep mode (e.g., 'deep' or 's2idle').
- Replace raw getenv calls with QProcessEnvironment or qgetenv and centralize the POWER_CAN_* keys as static constants to improve consistency and null safety.

## Individual Comments

### Comment 1
<location> `src/session-widgets/authinterface.cpp:285` </location>
<code_context>
+
+bool AuthInterface::detectVirtualMachine() {
+    QProcess process;
+    process.start("/usr/bin/systemd-detect-virt", QStringList());
+    process.waitForFinished(-1);
+    
</code_context>

<issue_to_address>
**suggestion:** Hardcoding the path to systemd-detect-virt may reduce portability.

Using a hardcoded path assumes systemd-detect-virt is always at /usr/bin, which may not be true on all systems. To make this more robust, use QStandardPaths or search for the executable in $PATH.

Suggested implementation:

```cpp
bool AuthInterface::detectVirtualMachine() {
    QString detectVirtPath = QProcess::findExecutable("systemd-detect-virt");
    if (detectVirtPath.isEmpty()) {
        // Executable not found, assume not a VM
        return false;
    }
    QProcess process;

```

```cpp
    process.start(detectVirtPath, QStringList());
    process.waitForFinished(-1);

```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


bool AuthInterface::detectVirtualMachine() {
QProcess process;
process.start("/usr/bin/systemd-detect-virt", QStringList());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Hardcoding the path to systemd-detect-virt may reduce portability.

Using a hardcoded path assumes systemd-detect-virt is always at /usr/bin, which may not be true on all systems. To make this more robust, use QStandardPaths or search for the executable in $PATH.

Suggested implementation:

bool AuthInterface::detectVirtualMachine() {
    QString detectVirtPath = QProcess::findExecutable("systemd-detect-virt");
    if (detectVirtPath.isEmpty()) {
        // Executable not found, assume not a VM
        return false;
    }
    QProcess process;
    process.start(detectVirtPath, QStringList());
    process.waitForFinished(-1);

@deepin-ci-robot deepin-ci-robot force-pushed the sync-pr-50-nosync branch 3 times, most recently from 51a0131 to 0849c11 Compare November 4, 2025 07:46
Synchronize source files from linuxdeepin/dde-session-shell.

Source-pull-request: linuxdeepin/dde-session-shell#50
@deepin-ci-robot
Copy link
Contributor Author

deepin pr auto review

我来对这个diff进行全面的代码审查:

  1. 语法逻辑:
  • 代码修改整体逻辑清晰,移除了PowerManager相关的接口,改用login1接口来检查电源管理功能
  • 新增的canSuspend()和canHibernate()函数实现逻辑合理
  • detectVirtualMachine()函数实现正确,使用了systemd-detect-virt标准工具
  1. 代码质量:
  • 优点:
    • 移除了冗余的PowerManager接口,简化了代码结构
    • 添加了虚拟机检测功能,提高了代码的健壮性
    • 使用了标准的login1接口,提高了兼容性
  • 改进建议:
    • 在detectVirtualMachine()中,process.waitForFinished(3000)的超时时间应该定义为常量
    • 可以考虑将"/sys/power/mem_sleep"等路径定义为常量
    • canSuspend()和canHibernate()函数可以添加日志记录
  1. 代码性能:
  • 使用systemd-detect-virt检测虚拟机是一个轻量级的操作
  • QProcess的使用合理,设置了超时限制
  • 文件存在性检查直接使用QFile::exists,性能良好
  1. 代码安全:
  • 优点:
    • 对外部命令(systemd-detect-virt)的调用设置了超时限制
    • 检查了命令执行结果
  • 改进建议:
    • 在使用getenv()获取环境变量时,应该检查返回值是否为nullptr
    • 建议在process.start()之前检查命令是否存在
  1. 具体改进建议:
// 建议添加常量定义
namespace {
    constexpr int VM_DETECTION_TIMEOUT = 3000;
    const QString MEM_SLEEP_PATH = "/sys/power/mem_sleep";
}

bool AuthInterface::canSuspend()
{
    const char* sleepEnv = getenv("POWER_CAN_SLEEP");
    if (sleepEnv && QString(sleepEnv) == "0" || m_isVM) {
        return false; 
    }

    if (!QFile::exists(MEM_SLEEP_PATH)) {
        qWarning() << "Memory sleep support file not found:" << MEM_SLEEP_PATH;
        return false;
    }
    
    QString canSuspend = m_login1Inter->CanSuspend();
    bool result = canSuspend == "yes";
    qInfo() << "Suspend support check result:" << result;
    return result;
}

bool AuthInterface::detectVirtualMachine()
{
    const QString detectCmd = "/usr/bin/systemd-detect-virt";
    if (!QFile::exists(detectCmd)) {
        qWarning() << "Virtual machine detection command not found:" << detectCmd;
        return false;
    }

    QProcess process;
    process.start(detectCmd, QStringList());
    if (!process.waitForFinished(VM_DETECTION_TIMEOUT)) {
        qWarning() << "Timeout detecting virtual machine after" << VM_DETECTION_TIMEOUT << "ms";
        process.kill();
        return false;
    }
    
    if (process.exitCode() != 0) {
        qWarning() << "Failed to detect virtual machine, error:" << process.errorString();
        return false;
    }
    
    QString name = QString::fromUtf8(process.readAllStandardOutput()).trimmed();
    bool isVM = name != "none" && !name.isEmpty();
    qInfo() << "Virtual machine detection result:" << name << "isVM:" << isVM;
    return isVM;
}
  1. 其他建议:
  • 考虑添加单元测试来验证新增的canSuspend()、canHibernate()和detectVirtualMachine()函数
  • 可以考虑将环境变量检查和虚拟机检测的逻辑提取为单独的工具函数
  • 建议在CMakeLists.txt中添加对systemd-detect-virt的依赖检查

这些修改提高了代码的可维护性、安全性和可读性,同时保持了原有的功能完整性。

@deepin-ci-robot
Copy link
Contributor Author

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: deepin-ci-robot, fly602

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@fly602 fly602 merged commit cae4aa0 into master Nov 4, 2025
25 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants