ipc/chromium/src/base/message_pump.h

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) 2006-2008 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 #ifndef BASE_MESSAGE_PUMP_H_
michael@0 6 #define BASE_MESSAGE_PUMP_H_
michael@0 7
michael@0 8 #include "base/ref_counted.h"
michael@0 9
michael@0 10 namespace base {
michael@0 11
michael@0 12 class TimeTicks;
michael@0 13
michael@0 14 class MessagePump : public RefCountedThreadSafe<MessagePump> {
michael@0 15 public:
michael@0 16 // Please see the comments above the Run method for an illustration of how
michael@0 17 // these delegate methods are used.
michael@0 18 class Delegate {
michael@0 19 public:
michael@0 20 virtual ~Delegate() {}
michael@0 21
michael@0 22 // Called from within Run in response to ScheduleWork or when the message
michael@0 23 // pump would otherwise call DoDelayedWork. Returns true to indicate that
michael@0 24 // work was done. DoDelayedWork will not be called if DoWork returns true.
michael@0 25 virtual bool DoWork() = 0;
michael@0 26
michael@0 27 // Called from within Run in response to ScheduleDelayedWork or when the
michael@0 28 // message pump would otherwise sleep waiting for more work. Returns true
michael@0 29 // to indicate that delayed work was done. DoIdleWork will not be called
michael@0 30 // if DoDelayedWork returns true. Upon return |next_delayed_work_time|
michael@0 31 // indicates the time when DoDelayedWork should be called again. If
michael@0 32 // |next_delayed_work_time| is null (per Time::is_null), then the queue of
michael@0 33 // future delayed work (timer events) is currently empty, and no additional
michael@0 34 // calls to this function need to be scheduled.
michael@0 35 virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) = 0;
michael@0 36
michael@0 37 // Called from within Run just before the message pump goes to sleep.
michael@0 38 // Returns true to indicate that idle work was done.
michael@0 39 virtual bool DoIdleWork() = 0;
michael@0 40 };
michael@0 41
michael@0 42 virtual ~MessagePump() {}
michael@0 43
michael@0 44 // The Run method is called to enter the message pump's run loop.
michael@0 45 //
michael@0 46 // Within the method, the message pump is responsible for processing native
michael@0 47 // messages as well as for giving cycles to the delegate periodically. The
michael@0 48 // message pump should take care to mix delegate callbacks with native
michael@0 49 // message processing so neither type of event starves the other of cycles.
michael@0 50 //
michael@0 51 // The anatomy of a typical run loop:
michael@0 52 //
michael@0 53 // for (;;) {
michael@0 54 // bool did_work = DoInternalWork();
michael@0 55 // if (should_quit_)
michael@0 56 // break;
michael@0 57 //
michael@0 58 // did_work |= delegate_->DoWork();
michael@0 59 // if (should_quit_)
michael@0 60 // break;
michael@0 61 //
michael@0 62 // did_work |= delegate_->DoDelayedWork();
michael@0 63 // if (should_quit_)
michael@0 64 // break;
michael@0 65 //
michael@0 66 // if (did_work)
michael@0 67 // continue;
michael@0 68 //
michael@0 69 // did_work = delegate_->DoIdleWork();
michael@0 70 // if (should_quit_)
michael@0 71 // break;
michael@0 72 //
michael@0 73 // if (did_work)
michael@0 74 // continue;
michael@0 75 //
michael@0 76 // WaitForWork();
michael@0 77 // }
michael@0 78 //
michael@0 79 // Here, DoInternalWork is some private method of the message pump that is
michael@0 80 // responsible for dispatching the next UI message or notifying the next IO
michael@0 81 // completion (for example). WaitForWork is a private method that simply
michael@0 82 // blocks until there is more work of any type to do.
michael@0 83 //
michael@0 84 // Notice that the run loop cycles between calling DoInternalWork, DoWork,
michael@0 85 // and DoDelayedWork methods. This helps ensure that neither work queue
michael@0 86 // starves the other. This is important for message pumps that are used to
michael@0 87 // drive animations, for example.
michael@0 88 //
michael@0 89 // Notice also that after each callout to foreign code, the run loop checks
michael@0 90 // to see if it should quit. The Quit method is responsible for setting this
michael@0 91 // flag. No further work is done once the quit flag is set.
michael@0 92 //
michael@0 93 // NOTE: Care must be taken to handle Run being called again from within any
michael@0 94 // of the callouts to foreign code. Native message pumps may also need to
michael@0 95 // deal with other native message pumps being run outside their control
michael@0 96 // (e.g., the MessageBox API on Windows pumps UI messages!). To be specific,
michael@0 97 // the callouts (DoWork and DoDelayedWork) MUST still be provided even in
michael@0 98 // nested sub-loops that are "seemingly" outside the control of this message
michael@0 99 // pump. DoWork in particular must never be starved for time slices unless
michael@0 100 // it returns false (meaning it has run out of things to do).
michael@0 101 //
michael@0 102 virtual void Run(Delegate* delegate) = 0;
michael@0 103
michael@0 104 // Quit immediately from the most recently entered run loop. This method may
michael@0 105 // only be used on the thread that called Run.
michael@0 106 virtual void Quit() = 0;
michael@0 107
michael@0 108 // Schedule a DoWork callback to happen reasonably soon. Does nothing if a
michael@0 109 // DoWork callback is already scheduled. This method may be called from any
michael@0 110 // thread. Once this call is made, DoWork should not be "starved" at least
michael@0 111 // until it returns a value of false.
michael@0 112 virtual void ScheduleWork() = 0;
michael@0 113
michael@0 114 // This method may only called from the thread that called Run.
michael@0 115 //
michael@0 116 // Ensure that DoWork will be called if a nested loop is entered.
michael@0 117 // If a MessagePump can already guarantee that DoWork will be called
michael@0 118 // "reasonably soon", this method can be a no-op to avoid expensive
michael@0 119 // atomic tests and/or syscalls required for ScheduleWork().
michael@0 120 virtual void ScheduleWorkForNestedLoop() { ScheduleWork(); };
michael@0 121
michael@0 122 // Schedule a DoDelayedWork callback to happen at the specified time,
michael@0 123 // cancelling any pending DoDelayedWork callback. This method may only be
michael@0 124 // used on the thread that called Run.
michael@0 125 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) = 0;
michael@0 126 };
michael@0 127
michael@0 128 } // namespace base
michael@0 129
michael@0 130 #endif // BASE_MESSAGE_PUMP_H_

mercurial