ipc/chromium/src/base/message_pump_qt.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include <qabstracteventdispatcher.h>
michael@0 6 #include <qevent.h>
michael@0 7 #include <QCoreApplication>
michael@0 8 #include <QThread>
michael@0 9 #include <qtimer.h>
michael@0 10
michael@0 11 #include "base/message_pump_qt.h"
michael@0 12
michael@0 13 #include <fcntl.h>
michael@0 14 #include <limits>
michael@0 15 #include <math.h>
michael@0 16
michael@0 17 #include "base/eintr_wrapper.h"
michael@0 18 #include "base/lazy_instance.h"
michael@0 19 #include "base/logging.h"
michael@0 20 #include "base/platform_thread.h"
michael@0 21
michael@0 22 namespace {
michael@0 23 // Cached QEvent user type, registered for our event system
michael@0 24 static int sPokeEvent;
michael@0 25 } // namespace
michael@0 26
michael@0 27 namespace base {
michael@0 28
michael@0 29 MessagePumpForUI::MessagePumpForUI()
michael@0 30 : state_(NULL),
michael@0 31 qt_pump(*this)
michael@0 32 {
michael@0 33 }
michael@0 34
michael@0 35 MessagePumpForUI::~MessagePumpForUI() {
michael@0 36 }
michael@0 37
michael@0 38 MessagePumpQt::MessagePumpQt(MessagePumpForUI &aPump)
michael@0 39 : pump(aPump), mTimer(new QTimer(this))
michael@0 40 {
michael@0 41 // Register our custom event type, to use in qApp event loop
michael@0 42 sPokeEvent = QEvent::registerEventType();
michael@0 43 connect(mTimer, SIGNAL(timeout()), this, SLOT(dispatchDelayed()));
michael@0 44 mTimer->setSingleShot(true);
michael@0 45 }
michael@0 46
michael@0 47 MessagePumpQt::~MessagePumpQt()
michael@0 48 {
michael@0 49 mTimer->stop();
michael@0 50 delete mTimer;
michael@0 51 }
michael@0 52
michael@0 53 bool
michael@0 54 MessagePumpQt::event(QEvent *e)
michael@0 55 {
michael@0 56 if (e->type() == sPokeEvent) {
michael@0 57 pump.HandleDispatch();
michael@0 58 return true;
michael@0 59 }
michael@0 60 return false;
michael@0 61 }
michael@0 62
michael@0 63 void
michael@0 64 MessagePumpQt::scheduleDelayedIfNeeded(const TimeTicks& delayed_work_time)
michael@0 65 {
michael@0 66 if (delayed_work_time.is_null()) {
michael@0 67 return;
michael@0 68 }
michael@0 69
michael@0 70 if (mTimer->isActive()) {
michael@0 71 mTimer->stop();
michael@0 72 }
michael@0 73
michael@0 74 TimeDelta later = delayed_work_time - TimeTicks::Now();
michael@0 75 // later.InMilliseconds() returns an int64_t, QTimer only accepts int's for start(),
michael@0 76 // std::min only works on exact same types.
michael@0 77 int laterMsecs = later.InMilliseconds() > std::numeric_limits<int>::max() ?
michael@0 78 std::numeric_limits<int>::max() : later.InMilliseconds();
michael@0 79 mTimer->start(laterMsecs > 0 ? laterMsecs : 0);
michael@0 80 }
michael@0 81
michael@0 82 void
michael@0 83 MessagePumpQt::dispatchDelayed()
michael@0 84 {
michael@0 85 pump.HandleDispatch();
michael@0 86 }
michael@0 87
michael@0 88 void MessagePumpForUI::Run(Delegate* delegate) {
michael@0 89 RunState state;
michael@0 90 state.delegate = delegate;
michael@0 91 state.should_quit = false;
michael@0 92 state.run_depth = state_ ? state_->run_depth + 1 : 1;
michael@0 93 // We really only do a single task for each iteration of the loop. If we
michael@0 94 // have done something, assume there is likely something more to do. This
michael@0 95 // will mean that we don't block on the message pump until there was nothing
michael@0 96 // more to do. We also set this to true to make sure not to block on the
michael@0 97 // first iteration of the loop, so RunAllPending() works correctly.
michael@0 98 bool more_work_is_plausible = true;
michael@0 99
michael@0 100 RunState* previous_state = state_;
michael@0 101 state_ = &state;
michael@0 102
michael@0 103 for(;;) {
michael@0 104 QEventLoop::ProcessEventsFlags block = QEventLoop::AllEvents;
michael@0 105 if (!more_work_is_plausible) {
michael@0 106 block |= QEventLoop::WaitForMoreEvents;
michael@0 107 }
michael@0 108
michael@0 109 QAbstractEventDispatcher* dispatcher =
michael@0 110 QAbstractEventDispatcher::instance(QThread::currentThread());
michael@0 111 // An assertion seems too much here, as during startup,
michael@0 112 // the dispatcher might not be ready yet.
michael@0 113 if (!dispatcher) {
michael@0 114 return;
michael@0 115 }
michael@0 116
michael@0 117 // processEvent's returns true if an event has been processed.
michael@0 118 more_work_is_plausible = dispatcher->processEvents(block);
michael@0 119
michael@0 120 if (state_->should_quit) {
michael@0 121 break;
michael@0 122 }
michael@0 123
michael@0 124 more_work_is_plausible |= state_->delegate->DoWork();
michael@0 125 if (state_->should_quit) {
michael@0 126 break;
michael@0 127 }
michael@0 128
michael@0 129 more_work_is_plausible |=
michael@0 130 state_->delegate->DoDelayedWork(&delayed_work_time_);
michael@0 131 if (state_->should_quit) {
michael@0 132 break;
michael@0 133 }
michael@0 134
michael@0 135 qt_pump.scheduleDelayedIfNeeded(delayed_work_time_);
michael@0 136
michael@0 137 if (more_work_is_plausible) {
michael@0 138 continue;
michael@0 139 }
michael@0 140
michael@0 141 more_work_is_plausible = state_->delegate->DoIdleWork();
michael@0 142 if (state_->should_quit) {
michael@0 143 break;
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 state_ = previous_state;
michael@0 148 }
michael@0 149
michael@0 150 void MessagePumpForUI::HandleDispatch() {
michael@0 151 if (state_->should_quit) {
michael@0 152 return;
michael@0 153 }
michael@0 154
michael@0 155 if (state_->delegate->DoWork()) {
michael@0 156 // there might be more, see more_work_is_plausible
michael@0 157 // variable above, that's why we ScheduleWork() to keep going.
michael@0 158 ScheduleWork();
michael@0 159 }
michael@0 160
michael@0 161 if (state_->should_quit) {
michael@0 162 return;
michael@0 163 }
michael@0 164
michael@0 165 state_->delegate->DoDelayedWork(&delayed_work_time_);
michael@0 166 qt_pump.scheduleDelayedIfNeeded(delayed_work_time_);
michael@0 167 }
michael@0 168
michael@0 169 void MessagePumpForUI::Quit() {
michael@0 170 if (state_) {
michael@0 171 state_->should_quit = true;
michael@0 172 } else {
michael@0 173 NOTREACHED() << "Quit called outside Run!";
michael@0 174 }
michael@0 175 }
michael@0 176
michael@0 177 void MessagePumpForUI::ScheduleWork() {
michael@0 178 QCoreApplication::postEvent(&qt_pump,
michael@0 179 new QEvent((QEvent::Type) sPokeEvent));
michael@0 180 }
michael@0 181
michael@0 182 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
michael@0 183 // On GLib implementation, a work source is defined which explicitly checks the
michael@0 184 // time that has passed. Here, on Qt we can use a QTimer that enqueues our
michael@0 185 // event signal in an event queue.
michael@0 186 delayed_work_time_ = delayed_work_time;
michael@0 187 qt_pump.scheduleDelayedIfNeeded(delayed_work_time_);
michael@0 188 }
michael@0 189
michael@0 190 } // namespace base

mercurial