Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2800)

Unified Diff: base/message_loop/message_pump_mac.mm

Issue 2889293002: Filter run loop modes in message_pump_mac.mm (Closed)
Patch Set: selfnits Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/message_loop/message_pump_mac.mm
diff --git a/base/message_loop/message_pump_mac.mm b/base/message_loop/message_pump_mac.mm
index 370e351ec9cd449c9c281da177dedf0db8aac7c0..2eacb4ec740105f7251948ae1dd541de399d64ef 100644
--- a/base/message_loop/message_pump_mac.mm
+++ b/base/message_loop/message_pump_mac.mm
@@ -44,39 +44,8 @@ const CFStringRef kAllModes[] = {
CFSTR("NSUnhighlightMenuRunLoopMode"),
};
-void CFRunLoopAddSourceToAllModes(CFRunLoopRef rl, CFRunLoopSourceRef source) {
- for (const CFStringRef& mode : kAllModes)
- CFRunLoopAddSource(rl, source, mode);
-}
-
-void CFRunLoopRemoveSourceFromAllModes(CFRunLoopRef rl,
- CFRunLoopSourceRef source) {
- for (const CFStringRef& mode : kAllModes)
- CFRunLoopRemoveSource(rl, source, mode);
-}
-
-void CFRunLoopAddTimerToAllModes(CFRunLoopRef rl, CFRunLoopTimerRef timer) {
- for (const CFStringRef& mode : kAllModes)
- CFRunLoopAddTimer(rl, timer, mode);
-}
-
-void CFRunLoopRemoveTimerFromAllModes(CFRunLoopRef rl,
- CFRunLoopTimerRef timer) {
- for (const CFStringRef& mode : kAllModes)
- CFRunLoopRemoveTimer(rl, timer, mode);
-}
-
-void CFRunLoopAddObserverToAllModes(CFRunLoopRef rl,
- CFRunLoopObserverRef observer) {
- for (const CFStringRef& mode : kAllModes)
- CFRunLoopAddObserver(rl, observer, mode);
-}
-
-void CFRunLoopRemoveObserverFromAllModes(CFRunLoopRef rl,
- CFRunLoopObserverRef observer) {
- for (const CFStringRef& mode : kAllModes)
- CFRunLoopRemoveObserver(rl, observer, mode);
-}
+// Mask that determines which modes in |kAllModes| to use.
+enum { kCommonModeMask = 0x1, kAllModesMask = ~0 };
void NoOp(void* info) {
}
@@ -196,8 +165,9 @@ void MessagePumpCFRunLoopBase::ChromeCFRunLoopTimerSetValid(
}
// Must be called on the run loop thread.
-MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
- : delegate_(NULL),
+MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase(int mode_mask)
+ : mode_mask_(mode_mask),
+ delegate_(NULL),
delayed_work_fire_time_(kCFTimeIntervalMax),
timer_slack_(base::TIMER_SLACK_NONE),
nesting_level_(0),
@@ -220,7 +190,7 @@ MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
0, // priority
RunDelayedWorkTimer,
&timer_context);
- CFRunLoopAddTimerToAllModes(run_loop_, delayed_work_timer_);
+ InvokeForAllModes(&CFRunLoopAddTimer, delayed_work_timer_);
CFRunLoopSourceContext source_context = CFRunLoopSourceContext();
source_context.info = this;
@@ -228,19 +198,19 @@ MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
work_source_ = CFRunLoopSourceCreate(NULL, // allocator
1, // priority
&source_context);
- CFRunLoopAddSourceToAllModes(run_loop_, work_source_);
+ InvokeForAllModes(&CFRunLoopAddSource, work_source_);
source_context.perform = RunIdleWorkSource;
idle_work_source_ = CFRunLoopSourceCreate(NULL, // allocator
2, // priority
&source_context);
- CFRunLoopAddSourceToAllModes(run_loop_, idle_work_source_);
+ InvokeForAllModes(&CFRunLoopAddSource, idle_work_source_);
source_context.perform = RunNestingDeferredWorkSource;
nesting_deferred_work_source_ = CFRunLoopSourceCreate(NULL, // allocator
0, // priority
&source_context);
- CFRunLoopAddSourceToAllModes(run_loop_, nesting_deferred_work_source_);
+ InvokeForAllModes(&CFRunLoopAddSource, nesting_deferred_work_source_);
CFRunLoopObserverContext observer_context = CFRunLoopObserverContext();
observer_context.info = this;
@@ -250,7 +220,7 @@ MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
0, // priority
PreWaitObserver,
&observer_context);
- CFRunLoopAddObserverToAllModes(run_loop_, pre_wait_observer_);
+ InvokeForAllModes(&CFRunLoopAddObserver, pre_wait_observer_);
pre_source_observer_ = CFRunLoopObserverCreate(NULL, // allocator
kCFRunLoopBeforeSources,
@@ -258,7 +228,7 @@ MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
0, // priority
PreSourceObserver,
&observer_context);
- CFRunLoopAddObserverToAllModes(run_loop_, pre_source_observer_);
+ InvokeForAllModes(&CFRunLoopAddObserver, pre_source_observer_);
enter_exit_observer_ = CFRunLoopObserverCreate(NULL, // allocator
kCFRunLoopEntry |
@@ -267,34 +237,25 @@ MessagePumpCFRunLoopBase::MessagePumpCFRunLoopBase()
0, // priority
EnterExitObserver,
&observer_context);
- CFRunLoopAddObserverToAllModes(run_loop_, enter_exit_observer_);
+ InvokeForAllModes(&CFRunLoopAddObserver, enter_exit_observer_);
}
// Ideally called on the run loop thread. If other run loops were running
// lower on the run loop thread's stack when this object was created, the
// same number of run loops must be running when this object is destroyed.
MessagePumpCFRunLoopBase::~MessagePumpCFRunLoopBase() {
- CFRunLoopRemoveObserverFromAllModes(run_loop_, enter_exit_observer_);
- CFRelease(enter_exit_observer_);
-
- CFRunLoopRemoveObserverFromAllModes(run_loop_, pre_source_observer_);
- CFRelease(pre_source_observer_);
-
- CFRunLoopRemoveObserverFromAllModes(run_loop_, pre_wait_observer_);
- CFRelease(pre_wait_observer_);
-
- CFRunLoopRemoveSourceFromAllModes(run_loop_, nesting_deferred_work_source_);
- CFRelease(nesting_deferred_work_source_);
-
- CFRunLoopRemoveSourceFromAllModes(run_loop_, idle_work_source_);
- CFRelease(idle_work_source_);
-
- CFRunLoopRemoveSourceFromAllModes(run_loop_, work_source_);
- CFRelease(work_source_);
-
- CFRunLoopRemoveTimerFromAllModes(run_loop_, delayed_work_timer_);
+ for (const CFRunLoopObserverRef& observer :
+ {enter_exit_observer_, pre_source_observer_, pre_wait_observer_}) {
+ InvokeForAllModes(&CFRunLoopRemoveObserver, observer);
+ CFRelease(observer);
+ }
+ for (const CFRunLoopSourceRef& source :
+ {nesting_deferred_work_source_, idle_work_source_, work_source_}) {
+ InvokeForAllModes(&CFRunLoopRemoveSource, source);
+ CFRelease(source);
+ }
+ InvokeForAllModes(&CFRunLoopRemoveTimer, delayed_work_timer_);
CFRelease(delayed_work_timer_);
-
CFRelease(run_loop_);
}
@@ -367,6 +328,17 @@ void MessagePumpCFRunLoopBase::SetTimerSlack(TimerSlack timer_slack) {
timer_slack_ = timer_slack;
}
+template <typename Argument>
+void MessagePumpCFRunLoopBase::InvokeForAllModes(void method(CFRunLoopRef,
+ Argument,
+ CFStringRef),
+ Argument argument) {
+ for (size_t i = 0; i < arraysize(kAllModes); ++i) {
+ if (mode_mask_ & (0x1 << i))
+ method(run_loop_, argument, kAllModes[i]);
+ }
+}
+
// Called from the run loop.
// static
void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer,
@@ -645,8 +617,7 @@ AutoreleasePoolType* MessagePumpCFRunLoopBase::CreateAutoreleasePool() {
}
MessagePumpCFRunLoop::MessagePumpCFRunLoop()
- : quit_pending_(false) {
-}
+ : MessagePumpCFRunLoopBase(kCommonModeMask), quit_pending_(false) {}
MessagePumpCFRunLoop::~MessagePumpCFRunLoop() {}
@@ -697,17 +668,17 @@ void MessagePumpCFRunLoop::EnterExitRunLoop(CFRunLoopActivity activity) {
}
MessagePumpNSRunLoop::MessagePumpNSRunLoop()
- : keep_running_(true) {
+ : MessagePumpCFRunLoopBase(kCommonModeMask), keep_running_(true) {
CFRunLoopSourceContext source_context = CFRunLoopSourceContext();
source_context.perform = NoOp;
quit_source_ = CFRunLoopSourceCreate(NULL, // allocator
0, // priority
&source_context);
- CFRunLoopAddSourceToAllModes(run_loop(), quit_source_);
+ InvokeForAllModes(&CFRunLoopAddSource, quit_source_);
}
MessagePumpNSRunLoop::~MessagePumpNSRunLoop() {
- CFRunLoopRemoveSourceFromAllModes(run_loop(), quit_source_);
+ InvokeForAllModes(&CFRunLoopRemoveSource, quit_source_);
CFRelease(quit_source_);
}
@@ -729,8 +700,7 @@ void MessagePumpNSRunLoop::Quit() {
#if defined(OS_IOS)
MessagePumpUIApplication::MessagePumpUIApplication()
- : run_loop_(NULL) {
-}
+ : MessagePumpCFRunLoopBase(kCommonModeMask), run_loop_(NULL) {}
MessagePumpUIApplication::~MessagePumpUIApplication() {}
@@ -752,9 +722,9 @@ void MessagePumpUIApplication::Attach(Delegate* delegate) {
#else
MessagePumpNSApplication::MessagePumpNSApplication()
- : keep_running_(true),
- running_own_loop_(false) {
-}
+ : MessagePumpCFRunLoopBase(kAllModesMask),
+ keep_running_(true),
+ running_own_loop_(false) {}
MessagePumpNSApplication::~MessagePumpNSApplication() {}
« base/message_loop/message_pump_mac.h ('K') | « base/message_loop/message_pump_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698