ipc/chromium/src/base/message_pump_mac.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) 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 // The basis for all native run loops on the Mac is the CFRunLoop. It can be
michael@0 6 // used directly, it can be used as the driving force behind the similar
michael@0 7 // Foundation NSRunLoop, and it can be used to implement higher-level event
michael@0 8 // loops such as the NSApplication event loop.
michael@0 9 //
michael@0 10 // This file introduces a basic CFRunLoop-based implementation of the
michael@0 11 // MessagePump interface called CFRunLoopBase. CFRunLoopBase contains all
michael@0 12 // of the machinery necessary to dispatch events to a delegate, but does not
michael@0 13 // implement the specific run loop. Concrete subclasses must provide their
michael@0 14 // own DoRun and Quit implementations.
michael@0 15 //
michael@0 16 // A concrete subclass that just runs a CFRunLoop loop is provided in
michael@0 17 // MessagePumpCFRunLoop. For an NSRunLoop, the similar MessagePumpNSRunLoop
michael@0 18 // is provided.
michael@0 19 //
michael@0 20 // For the application's event loop, an implementation based on AppKit's
michael@0 21 // NSApplication event system is provided in MessagePumpNSApplication.
michael@0 22 //
michael@0 23 // Typically, MessagePumpNSApplication only makes sense on a Cocoa
michael@0 24 // application's main thread. If a CFRunLoop-based message pump is needed on
michael@0 25 // any other thread, one of the other concrete subclasses is preferrable.
michael@0 26 // MessagePumpMac::Create is defined, which returns a new NSApplication-based
michael@0 27 // or NSRunLoop-based MessagePump subclass depending on which thread it is
michael@0 28 // called on.
michael@0 29
michael@0 30 #ifndef BASE_MESSAGE_PUMP_MAC_H_
michael@0 31 #define BASE_MESSAGE_PUMP_MAC_H_
michael@0 32
michael@0 33 #include "base/message_pump.h"
michael@0 34
michael@0 35 #include <CoreFoundation/CoreFoundation.h>
michael@0 36 #include <IOKit/IOKitLib.h>
michael@0 37
michael@0 38 #if defined(__OBJC__)
michael@0 39 @class NSAutoreleasePool;
michael@0 40 #else // defined(__OBJC__)
michael@0 41 class NSAutoreleasePool;
michael@0 42 #endif // defined(__OBJC__)
michael@0 43
michael@0 44 namespace base {
michael@0 45
michael@0 46 class TimeTicks;
michael@0 47
michael@0 48 class MessagePumpCFRunLoopBase : public MessagePump {
michael@0 49 // Needs access to CreateAutoreleasePool.
michael@0 50 friend class MessagePumpScopedAutoreleasePool;
michael@0 51 public:
michael@0 52 MessagePumpCFRunLoopBase();
michael@0 53 virtual ~MessagePumpCFRunLoopBase();
michael@0 54
michael@0 55 // Subclasses should implement the work they need to do in MessagePump::Run
michael@0 56 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly.
michael@0 57 // This arrangement is used because MessagePumpCFRunLoopBase needs to set
michael@0 58 // up and tear down things before and after the "meat" of DoRun.
michael@0 59 virtual void Run(Delegate* delegate);
michael@0 60 virtual void DoRun(Delegate* delegate) = 0;
michael@0 61
michael@0 62 virtual void ScheduleWork();
michael@0 63 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
michael@0 64
michael@0 65 protected:
michael@0 66 // Accessors for private data members to be used by subclasses.
michael@0 67 CFRunLoopRef run_loop() const { return run_loop_; }
michael@0 68 int nesting_level() const { return nesting_level_; }
michael@0 69 int run_nesting_level() const { return run_nesting_level_; }
michael@0 70
michael@0 71 // Return an autorelease pool to wrap around any work being performed.
michael@0 72 // In some cases, CreateAutoreleasePool may return nil intentionally to
michael@0 73 // preventing an autorelease pool from being created, allowing any
michael@0 74 // objects autoreleased by work to fall into the current autorelease pool.
michael@0 75 virtual NSAutoreleasePool* CreateAutoreleasePool();
michael@0 76
michael@0 77 private:
michael@0 78 // Timer callback scheduled by ScheduleDelayedWork. This does not do any
michael@0 79 // work, but it signals delayed_work_source_ so that delayed work can be
michael@0 80 // performed within the appropriate priority constraints.
michael@0 81 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info);
michael@0 82
michael@0 83 // Perform highest-priority work. This is associated with work_source_
michael@0 84 // signalled by ScheduleWork. The static method calls the instance method;
michael@0 85 // the instance method returns true if work was done.
michael@0 86 static void RunWorkSource(void* info);
michael@0 87 bool RunWork();
michael@0 88
michael@0 89 // Perform delayed-priority work. This is associated with
michael@0 90 // delayed_work_source_ signalled by RunDelayedWorkTimer, and is responsible
michael@0 91 // for calling ScheduleDelayedWork again if appropriate. The static method
michael@0 92 // calls the instance method; the instance method returns true if more
michael@0 93 // delayed work is available.
michael@0 94 static void RunDelayedWorkSource(void* info);
michael@0 95 bool RunDelayedWork();
michael@0 96
michael@0 97 // Perform idle-priority work. This is normally called by PreWaitObserver,
michael@0 98 // but is also associated with idle_work_source_. When this function
michael@0 99 // actually does perform idle work, it will resignal that source. The
michael@0 100 // static method calls the instance method; the instance method returns
michael@0 101 // true if idle work was done.
michael@0 102 static void RunIdleWorkSource(void* info);
michael@0 103 bool RunIdleWork();
michael@0 104
michael@0 105 // Perform work that may have been deferred because it was not runnable
michael@0 106 // within a nested run loop. This is associated with
michael@0 107 // nesting_deferred_work_source_ and is signalled by
michael@0 108 // MaybeScheduleNestingDeferredWork when returning from a nested loop,
michael@0 109 // so that an outer loop will be able to perform the necessary tasks if it
michael@0 110 // permits nestable tasks.
michael@0 111 static void RunNestingDeferredWorkSource(void* info);
michael@0 112 bool RunNestingDeferredWork();
michael@0 113
michael@0 114 // Schedules possible nesting-deferred work to be processed before the run
michael@0 115 // loop goes to sleep, exits, or begins processing sources at the top of its
michael@0 116 // loop. If this function detects that a nested loop had run since the
michael@0 117 // previous attempt to schedule nesting-deferred work, it will schedule a
michael@0 118 // call to RunNestingDeferredWorkSource.
michael@0 119 void MaybeScheduleNestingDeferredWork();
michael@0 120
michael@0 121 // Observer callback responsible for performing idle-priority work, before
michael@0 122 // the run loop goes to sleep. Associated with idle_work_observer_.
michael@0 123 static void PreWaitObserver(CFRunLoopObserverRef observer,
michael@0 124 CFRunLoopActivity activity, void* info);
michael@0 125
michael@0 126 // Observer callback called before the run loop processes any sources.
michael@0 127 // Associated with pre_source_observer_.
michael@0 128 static void PreSourceObserver(CFRunLoopObserverRef observer,
michael@0 129 CFRunLoopActivity activity, void* info);
michael@0 130
michael@0 131 // Observer callback called when the run loop starts and stops, at the
michael@0 132 // beginning and end of calls to CFRunLoopRun. This is used to maintain
michael@0 133 // nesting_level_. Associated with enter_exit_observer_.
michael@0 134 static void EnterExitObserver(CFRunLoopObserverRef observer,
michael@0 135 CFRunLoopActivity activity, void* info);
michael@0 136
michael@0 137 // Called by EnterExitObserver after performing maintenance on nesting_level_.
michael@0 138 // This allows subclasses an opportunity to perform additional processing on
michael@0 139 // the basis of run loops starting and stopping.
michael@0 140 virtual void EnterExitRunLoop(CFRunLoopActivity activity);
michael@0 141
michael@0 142 // IOKit power state change notification callback, called when the system
michael@0 143 // enters and leaves the sleep state.
michael@0 144 static void PowerStateNotification(void* info, io_service_t service,
michael@0 145 uint32_t message_type,
michael@0 146 void* message_argument);
michael@0 147
michael@0 148 // The thread's run loop.
michael@0 149 CFRunLoopRef run_loop_;
michael@0 150
michael@0 151 // The timer, sources, and observers are described above alongside their
michael@0 152 // callbacks.
michael@0 153 CFRunLoopTimerRef delayed_work_timer_;
michael@0 154 CFRunLoopSourceRef work_source_;
michael@0 155 CFRunLoopSourceRef delayed_work_source_;
michael@0 156 CFRunLoopSourceRef idle_work_source_;
michael@0 157 CFRunLoopSourceRef nesting_deferred_work_source_;
michael@0 158 CFRunLoopObserverRef pre_wait_observer_;
michael@0 159 CFRunLoopObserverRef pre_source_observer_;
michael@0 160 CFRunLoopObserverRef enter_exit_observer_;
michael@0 161
michael@0 162 // Objects used for power state notification. See PowerStateNotification.
michael@0 163 io_connect_t root_power_domain_;
michael@0 164 IONotificationPortRef power_notification_port_;
michael@0 165 io_object_t power_notification_object_;
michael@0 166
michael@0 167 // (weak) Delegate passed as an argument to the innermost Run call.
michael@0 168 Delegate* delegate_;
michael@0 169
michael@0 170 // The time that delayed_work_timer_ is scheduled to fire. This is tracked
michael@0 171 // independently of CFRunLoopTimerGetNextFireDate(delayed_work_timer_)
michael@0 172 // to be able to reset the timer properly after waking from system sleep.
michael@0 173 // See PowerStateNotification.
michael@0 174 CFAbsoluteTime delayed_work_fire_time_;
michael@0 175
michael@0 176 // The recursion depth of the currently-executing CFRunLoopRun loop on the
michael@0 177 // run loop's thread. 0 if no run loops are running inside of whatever scope
michael@0 178 // the object was created in.
michael@0 179 int nesting_level_;
michael@0 180
michael@0 181 // The recursion depth (calculated in the same way as nesting_level_) of the
michael@0 182 // innermost executing CFRunLoopRun loop started by a call to Run.
michael@0 183 int run_nesting_level_;
michael@0 184
michael@0 185 // The deepest (numerically highest) recursion depth encountered since the
michael@0 186 // most recent attempt to run nesting-deferred work.
michael@0 187 int deepest_nesting_level_;
michael@0 188
michael@0 189 // "Delegateless" work flags are set when work is ready to be performed but
michael@0 190 // must wait until a delegate is available to process it. This can happen
michael@0 191 // when a MessagePumpCFRunLoopBase is instantiated and work arrives without
michael@0 192 // any call to Run on the stack. The Run method will check for delegateless
michael@0 193 // work on entry and redispatch it as needed once a delegate is available.
michael@0 194 bool delegateless_work_;
michael@0 195 bool delegateless_delayed_work_;
michael@0 196 bool delegateless_idle_work_;
michael@0 197
michael@0 198 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase);
michael@0 199 };
michael@0 200
michael@0 201 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase {
michael@0 202 public:
michael@0 203 MessagePumpCFRunLoop();
michael@0 204
michael@0 205 virtual void DoRun(Delegate* delegate);
michael@0 206 virtual void Quit();
michael@0 207
michael@0 208 private:
michael@0 209 virtual void EnterExitRunLoop(CFRunLoopActivity activity);
michael@0 210
michael@0 211 // True if Quit is called to stop the innermost MessagePump
michael@0 212 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_)
michael@0 213 // is running inside the MessagePump's innermost Run call.
michael@0 214 bool quit_pending_;
michael@0 215
michael@0 216 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoop);
michael@0 217 };
michael@0 218
michael@0 219 class MessagePumpNSRunLoop : public MessagePumpCFRunLoopBase {
michael@0 220 public:
michael@0 221 MessagePumpNSRunLoop();
michael@0 222 virtual ~MessagePumpNSRunLoop();
michael@0 223
michael@0 224 virtual void DoRun(Delegate* delegate);
michael@0 225 virtual void Quit();
michael@0 226
michael@0 227 private:
michael@0 228 // A source that doesn't do anything but provide something signalable
michael@0 229 // attached to the run loop. This source will be signalled when Quit
michael@0 230 // is called, to cause the loop to wake up so that it can stop.
michael@0 231 CFRunLoopSourceRef quit_source_;
michael@0 232
michael@0 233 // False after Quit is called.
michael@0 234 bool keep_running_;
michael@0 235
michael@0 236 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop);
michael@0 237 };
michael@0 238
michael@0 239 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase {
michael@0 240 public:
michael@0 241 MessagePumpNSApplication();
michael@0 242
michael@0 243 virtual void DoRun(Delegate* delegate);
michael@0 244 virtual void Quit();
michael@0 245
michael@0 246 protected:
michael@0 247 // Returns nil if NSApp is currently in the middle of calling -sendEvent.
michael@0 248 virtual NSAutoreleasePool* CreateAutoreleasePool();
michael@0 249
michael@0 250 private:
michael@0 251 // False after Quit is called.
michael@0 252 bool keep_running_;
michael@0 253
michael@0 254 // True if DoRun is managing its own run loop as opposed to letting
michael@0 255 // -[NSApplication run] handle it. The outermost run loop in the application
michael@0 256 // is managed by -[NSApplication run], inner run loops are handled by a loop
michael@0 257 // in DoRun.
michael@0 258 bool running_own_loop_;
michael@0 259
michael@0 260 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication);
michael@0 261 };
michael@0 262
michael@0 263 class MessagePumpMac {
michael@0 264 public:
michael@0 265 // Returns a new instance of MessagePumpNSApplication if called on the main
michael@0 266 // thread. Otherwise, returns a new instance of MessagePumpNSRunLoop.
michael@0 267 static MessagePump* Create();
michael@0 268
michael@0 269 private:
michael@0 270 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac);
michael@0 271 };
michael@0 272
michael@0 273 } // namespace base
michael@0 274
michael@0 275 #endif // BASE_MESSAGE_PUMP_MAC_H_

mercurial