message_pump_qt.h (2226B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 #ifndef BASE_MESSAGE_PUMP_QT_H_ 8 #define BASE_MESSAGE_PUMP_QT_H_ 9 10 #include <qobject.h> 11 12 #include "base/message_pump.h" 13 #include "base/time.h" 14 15 class QTimer; 16 17 namespace base { 18 19 class MessagePumpForUI; 20 21 class MessagePumpQt : public QObject { 22 Q_OBJECT 23 24 public: 25 MessagePumpQt(MessagePumpForUI& pump); 26 ~MessagePumpQt(); 27 28 virtual bool event(QEvent* e); 29 void scheduleDelayedIfNeeded(const TimeTicks& delayed_work_time); 30 31 public Q_SLOTS: 32 void dispatchDelayed(); 33 34 private: 35 base::MessagePumpForUI& pump; 36 QTimer* mTimer; 37 }; 38 39 // This class implements a MessagePump needed for TYPE_UI MessageLoops on 40 // XP_LINUX platforms using QApplication event loop 41 class MessagePumpForUI : public MessagePump { 42 public: 43 MessagePumpForUI(); 44 ~MessagePumpForUI(); 45 46 virtual void Run(Delegate* delegate); 47 virtual void Quit(); 48 virtual void ScheduleWork(); 49 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); 50 51 // Internal methods used for processing the pump callbacks. They are 52 // public for simplicity but should not be used directly. 53 // HandleDispatch is called after the poll has completed. 54 void HandleDispatch(); 55 56 private: 57 // We may make recursive calls to Run, so we save state that needs to be 58 // separate between them in this structure type. 59 struct RunState { 60 Delegate* delegate; 61 62 // Used to flag that the current Run() invocation should return ASAP. 63 bool should_quit; 64 65 // Used to count how many Run() invocations are on the stack. 66 int run_depth; 67 }; 68 69 RunState* state_; 70 71 // This is the time when we need to do delayed work. 72 TimeTicks delayed_work_time_; 73 74 // MessagePump implementation for Qt based on the GLib implement. 75 // On Qt we use a QObject base class and the 76 // default qApp in order to process events through QEventLoop. 77 MessagePumpQt qt_pump; 78 79 DISALLOW_COPY_AND_ASSIGN(MessagePumpForUI); 80 }; 81 82 } // namespace base 83 84 #endif // BASE_MESSAGE_PUMP_QT_H_