summaryrefslogtreecommitdiffstats
path: root/src/core/api/qwebenginemessagepumpscheduler.cpp
blob: a435e2c0c5a81078e279cbe04266bfd7ed3c2e65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qwebenginemessagepumpscheduler_p.h"

#include <QAbstractEventDispatcher>
#include <QCoreApplication>
#include <QTimerEvent>

QWebEngineMessagePumpScheduler::QWebEngineMessagePumpScheduler(std::function<void()> callback)
    : m_callback(std::move(callback))
{}

void QWebEngineMessagePumpScheduler::scheduleImmediateWork()
{
    QCoreApplication::postEvent(this, new QTimerEvent(0), Qt::NormalEventPriority);
}

void QWebEngineMessagePumpScheduler::scheduleIdleWork()
{
    QCoreApplication::postEvent(this, new QTimerEvent(0), Qt::LowEventPriority);
}

void QWebEngineMessagePumpScheduler::scheduleDelayedWork(int delay)
{
    if (delay < 0) {
        killTimer(m_timerId);
        m_timerId = 0;
    } else if (!m_timerId || delay < QAbstractEventDispatcher::instance()->remainingTime(m_timerId)) {
        killTimer(m_timerId);
        m_timerId = startTimer(delay);
    }
}

void QWebEngineMessagePumpScheduler::timerEvent(QTimerEvent *ev)
{
    Q_ASSERT(!ev->timerId() || m_timerId == ev->timerId());
    killTimer(m_timerId);
    m_timerId = 0;
    m_callback();
}

#include "moc_qwebenginemessagepumpscheduler_p.cpp"