-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathqmessagepump.cpp
133 lines (109 loc) · 3 KB
/
qmessagepump.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#define LOG_COMPONENT "MessagePumpQt"
#include <QTimer>
#include <QEvent>
#include <QThread>
#include <QAbstractEventDispatcher>
#include <QGuiApplication>
#include "qmessagepump.h"
#include "mozilla/embedlite/EmbedLiteMessagePump.h"
#include "mozilla/embedlite/EmbedLiteApp.h"
using namespace mozilla::embedlite;
// Cached QEvent user type, registered for our event system
static int sPokeEvent = -1;
MessagePumpQt::MessagePumpQt(EmbedLiteApp *aApp)
: mApp(aApp)
, mTimer(new QTimer(this))
, mState(nullptr)
, mLastDelayedWorkTime(-1)
{
mEventLoopPrivate = mApp->CreateEmbedLiteMessagePump(this);
// Register our custom event type, to use in qApp event loop
if (sPokeEvent == -1) {
sPokeEvent = QEvent::registerEventType();
}
connect(mTimer, &QTimer::timeout, this, &MessagePumpQt::dispatchDelayed);
mTimer->setSingleShot(true);
}
MessagePumpQt::~MessagePumpQt()
{
mTimer->stop();
delete mTimer;
delete mState;
delete mEventLoopPrivate;
}
bool MessagePumpQt::event(QEvent *e)
{
if (e->type() == sPokeEvent) {
handleDispatch();
return true;
}
return QObject::event(e);
}
void MessagePumpQt::handleDispatch()
{
if (mState->should_quit) {
return;
}
if (mEventLoopPrivate->DoWork(mState->delegate)) {
// there might be more, see more_work_is_plausible
// variable above, that's why we ScheduleWork() to keep going.
scheduleWorkLocal();
}
if (mState->should_quit) {
return;
}
bool doIdleWork = !mEventLoopPrivate->DoDelayedWork(mState->delegate);
scheduleDelayedIfNeeded();
if (doIdleWork) {
if (mEventLoopPrivate->DoIdleWork(mState->delegate)) {
scheduleWorkLocal();
}
}
}
void MessagePumpQt::scheduleWorkLocal()
{
QCoreApplication::postEvent(this, new QEvent((QEvent::Type)sPokeEvent));
}
void MessagePumpQt::scheduleDelayedIfNeeded()
{
if (mLastDelayedWorkTime == -1) {
return;
}
if (mTimer->isActive()) {
mTimer->stop();
}
mTimer->start(mLastDelayedWorkTime >= 0 ? mLastDelayedWorkTime : 0);
}
void MessagePumpQt::dispatchDelayed()
{
handleDispatch();
}
void MessagePumpQt::Run(void *delegate)
{
RunState *state = new RunState();
state->delegate = delegate;
state->should_quit = false;
state->run_depth = mState ? mState->run_depth + 1 : 1;
mState = state;
handleDispatch();
}
void MessagePumpQt::Quit()
{
if (mState) {
mState->should_quit = true;
mState->delegate = nullptr;
}
}
void MessagePumpQt::ScheduleWork()
{
scheduleWorkLocal();
}
void MessagePumpQt::ScheduleDelayedWork(const int aDelay)
{
mLastDelayedWorkTime = aDelay;
scheduleDelayedIfNeeded();
}