Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "base/message_loop/message_pump_mac.h" | 5 #import "base/message_loop/message_pump_mac.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
| 8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 }); | 61 }); |
| 62 | 62 |
| 63 if (settimertolerance_function_ptr) | 63 if (settimertolerance_function_ptr) |
| 64 settimertolerance_function_ptr(timer, tolerance); | 64 settimertolerance_function_ptr(timer, tolerance); |
| 65 } | 65 } |
| 66 | 66 |
| 67 } // namespace | 67 } // namespace |
| 68 | 68 |
| 69 namespace base { | 69 namespace base { |
| 70 | 70 |
| 71 // static | |
| 72 const CFStringRef kMessageLoopExclusiveRunLoopMode = | |
| 73 CFSTR("kMessageLoopExclusiveRunLoopMode"); | |
| 74 | |
| 71 // A scoper for autorelease pools created from message pump run loops. | 75 // A scoper for autorelease pools created from message pump run loops. |
| 72 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare | 76 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare |
| 73 // case where an autorelease pool needs to be passed in. | 77 // case where an autorelease pool needs to be passed in. |
| 74 class MessagePumpScopedAutoreleasePool { | 78 class MessagePumpScopedAutoreleasePool { |
| 75 public: | 79 public: |
| 76 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) : | 80 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) : |
| 77 pool_(pump->CreateAutoreleasePool()) { | 81 pool_(pump->CreateAutoreleasePool()) { |
| 78 } | 82 } |
| 79 ~MessagePumpScopedAutoreleasePool() { | 83 ~MessagePumpScopedAutoreleasePool() { |
| 80 [pool_ drain]; | 84 [pool_ drain]; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 | 121 |
| 118 CFRunLoopTimerContext timer_context = { .info = this }; | 122 CFRunLoopTimerContext timer_context = { .info = this }; |
| 119 timer_.reset(CFRunLoopTimerCreate( | 123 timer_.reset(CFRunLoopTimerCreate( |
| 120 NULL, // allocator | 124 NULL, // allocator |
| 121 (Time::Now() + sampling_interval_).ToCFAbsoluteTime(), | 125 (Time::Now() + sampling_interval_).ToCFAbsoluteTime(), |
| 122 sampling_interval_.InSecondsF(), | 126 sampling_interval_.InSecondsF(), |
| 123 0, // flags | 127 0, // flags |
| 124 0, // order | 128 0, // order |
| 125 &MessagePumpInstrumentation::TimerFired, | 129 &MessagePumpInstrumentation::TimerFired, |
| 126 &timer_context)); | 130 &timer_context)); |
| 127 CFRunLoopAddTimer(CFRunLoopGetCurrent(), | 131 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer_, kCFRunLoopCommonModes); |
| 128 timer_, | 132 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer_, |
| 129 kCFRunLoopCommonModes); | 133 kMessageLoopExclusiveRunLoopMode); |
| 130 } | 134 } |
| 131 | 135 |
| 132 // Used to track kCFRunLoopEntry. | 136 // Used to track kCFRunLoopEntry. |
| 133 void LoopEntered() { | 137 void LoopEntered() { |
| 134 loop_run_times_.push(TimeTicks::Now()); | 138 loop_run_times_.push(TimeTicks::Now()); |
| 135 } | 139 } |
| 136 | 140 |
| 137 // Used to track kCFRunLoopExit. | 141 // Used to track kCFRunLoopExit. |
| 138 void LoopExited() { | 142 void LoopExited() { |
| 139 TimeDelta duration = TimeTicks::Now() - loop_run_times_.top(); | 143 TimeDelta duration = TimeTicks::Now() - loop_run_times_.top(); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 CFRunLoopTimerContext timer_context = CFRunLoopTimerContext(); | 330 CFRunLoopTimerContext timer_context = CFRunLoopTimerContext(); |
| 327 timer_context.info = this; | 331 timer_context.info = this; |
| 328 delayed_work_timer_ = CFRunLoopTimerCreate(NULL, // allocator | 332 delayed_work_timer_ = CFRunLoopTimerCreate(NULL, // allocator |
| 329 kCFTimeIntervalMax, // fire time | 333 kCFTimeIntervalMax, // fire time |
| 330 kCFTimeIntervalMax, // interval | 334 kCFTimeIntervalMax, // interval |
| 331 0, // flags | 335 0, // flags |
| 332 0, // priority | 336 0, // priority |
| 333 RunDelayedWorkTimer, | 337 RunDelayedWorkTimer, |
| 334 &timer_context); | 338 &timer_context); |
| 335 CFRunLoopAddTimer(run_loop_, delayed_work_timer_, kCFRunLoopCommonModes); | 339 CFRunLoopAddTimer(run_loop_, delayed_work_timer_, kCFRunLoopCommonModes); |
| 340 CFRunLoopAddTimer(run_loop_, delayed_work_timer_, | |
|
Mark Mentovai
2014/06/12 16:17:13
There’s enough duplication now that I think we sho
ccameron
2014/06/12 17:10:40
Yeah, I was on the bubble about which way to go --
| |
| 341 kMessageLoopExclusiveRunLoopMode); | |
| 336 | 342 |
| 337 CFRunLoopSourceContext source_context = CFRunLoopSourceContext(); | 343 CFRunLoopSourceContext source_context = CFRunLoopSourceContext(); |
| 338 source_context.info = this; | 344 source_context.info = this; |
| 339 source_context.perform = RunWorkSource; | 345 source_context.perform = RunWorkSource; |
| 340 work_source_ = CFRunLoopSourceCreate(NULL, // allocator | 346 work_source_ = CFRunLoopSourceCreate(NULL, // allocator |
| 341 1, // priority | 347 1, // priority |
| 342 &source_context); | 348 &source_context); |
| 343 CFRunLoopAddSource(run_loop_, work_source_, kCFRunLoopCommonModes); | 349 CFRunLoopAddSource(run_loop_, work_source_, kCFRunLoopCommonModes); |
| 350 CFRunLoopAddSource(run_loop_, work_source_, | |
| 351 kMessageLoopExclusiveRunLoopMode); | |
| 344 | 352 |
| 345 source_context.perform = RunIdleWorkSource; | 353 source_context.perform = RunIdleWorkSource; |
| 346 idle_work_source_ = CFRunLoopSourceCreate(NULL, // allocator | 354 idle_work_source_ = CFRunLoopSourceCreate(NULL, // allocator |
| 347 2, // priority | 355 2, // priority |
| 348 &source_context); | 356 &source_context); |
| 349 CFRunLoopAddSource(run_loop_, idle_work_source_, kCFRunLoopCommonModes); | 357 CFRunLoopAddSource(run_loop_, idle_work_source_, kCFRunLoopCommonModes); |
| 358 CFRunLoopAddSource( | |
| 359 run_loop_, idle_work_source_, kCFRunLoopCommonModes); | |
| 350 | 360 |
| 351 source_context.perform = RunNestingDeferredWorkSource; | 361 source_context.perform = RunNestingDeferredWorkSource; |
| 352 nesting_deferred_work_source_ = CFRunLoopSourceCreate(NULL, // allocator | 362 nesting_deferred_work_source_ = CFRunLoopSourceCreate(NULL, // allocator |
| 353 0, // priority | 363 0, // priority |
| 354 &source_context); | 364 &source_context); |
| 355 CFRunLoopAddSource(run_loop_, nesting_deferred_work_source_, | 365 CFRunLoopAddSource(run_loop_, nesting_deferred_work_source_, |
| 356 kCFRunLoopCommonModes); | 366 kCFRunLoopCommonModes); |
| 367 CFRunLoopAddSource(run_loop_, nesting_deferred_work_source_, | |
| 368 kMessageLoopExclusiveRunLoopMode); | |
| 357 | 369 |
| 358 CFRunLoopObserverContext observer_context = CFRunLoopObserverContext(); | 370 CFRunLoopObserverContext observer_context = CFRunLoopObserverContext(); |
| 359 observer_context.info = this; | 371 observer_context.info = this; |
| 360 pre_wait_observer_ = CFRunLoopObserverCreate(NULL, // allocator | 372 pre_wait_observer_ = CFRunLoopObserverCreate(NULL, // allocator |
| 361 kCFRunLoopBeforeWaiting | | 373 kCFRunLoopBeforeWaiting | |
| 362 kCFRunLoopAfterWaiting, | 374 kCFRunLoopAfterWaiting, |
| 363 true, // repeat | 375 true, // repeat |
| 364 0, // priority | 376 0, // priority |
| 365 StartOrEndWaitObserver, | 377 StartOrEndWaitObserver, |
| 366 &observer_context); | 378 &observer_context); |
| 367 CFRunLoopAddObserver(run_loop_, pre_wait_observer_, kCFRunLoopCommonModes); | 379 CFRunLoopAddObserver(run_loop_, pre_wait_observer_, kCFRunLoopCommonModes); |
| 380 CFRunLoopAddObserver(run_loop_, pre_wait_observer_, | |
| 381 kMessageLoopExclusiveRunLoopMode); | |
| 368 | 382 |
| 369 pre_source_observer_ = CFRunLoopObserverCreate(NULL, // allocator | 383 pre_source_observer_ = CFRunLoopObserverCreate(NULL, // allocator |
| 370 kCFRunLoopBeforeSources, | 384 kCFRunLoopBeforeSources, |
| 371 true, // repeat | 385 true, // repeat |
| 372 0, // priority | 386 0, // priority |
| 373 PreSourceObserver, | 387 PreSourceObserver, |
| 374 &observer_context); | 388 &observer_context); |
| 375 CFRunLoopAddObserver(run_loop_, pre_source_observer_, kCFRunLoopCommonModes); | 389 CFRunLoopAddObserver(run_loop_, pre_source_observer_, kCFRunLoopCommonModes); |
| 390 CFRunLoopAddObserver(run_loop_, pre_source_observer_, | |
| 391 kMessageLoopExclusiveRunLoopMode); | |
| 376 | 392 |
| 377 enter_exit_observer_ = CFRunLoopObserverCreate(NULL, // allocator | 393 enter_exit_observer_ = CFRunLoopObserverCreate(NULL, // allocator |
| 378 kCFRunLoopEntry | | 394 kCFRunLoopEntry | |
| 379 kCFRunLoopExit, | 395 kCFRunLoopExit, |
| 380 true, // repeat | 396 true, // repeat |
| 381 0, // priority | 397 0, // priority |
| 382 EnterExitObserver, | 398 EnterExitObserver, |
| 383 &observer_context); | 399 &observer_context); |
| 384 CFRunLoopAddObserver(run_loop_, enter_exit_observer_, kCFRunLoopCommonModes); | 400 CFRunLoopAddObserver(run_loop_, enter_exit_observer_, kCFRunLoopCommonModes); |
| 401 CFRunLoopAddObserver(run_loop_, enter_exit_observer_, | |
| 402 kMessageLoopExclusiveRunLoopMode); | |
| 385 } | 403 } |
| 386 | 404 |
| 387 // Ideally called on the run loop thread. If other run loops were running | 405 // Ideally called on the run loop thread. If other run loops were running |
| 388 // lower on the run loop thread's stack when this object was created, the | 406 // lower on the run loop thread's stack when this object was created, the |
| 389 // same number of run loops must be running when this object is destroyed. | 407 // same number of run loops must be running when this object is destroyed. |
| 390 MessagePumpCFRunLoopBase::~MessagePumpCFRunLoopBase() { | 408 MessagePumpCFRunLoopBase::~MessagePumpCFRunLoopBase() { |
| 391 CFRunLoopRemoveObserver(run_loop_, enter_exit_observer_, | 409 CFRunLoopRemoveObserver(run_loop_, enter_exit_observer_, |
| 392 kCFRunLoopCommonModes); | 410 kCFRunLoopCommonModes); |
| 411 CFRunLoopRemoveObserver(run_loop_, enter_exit_observer_, | |
| 412 kMessageLoopExclusiveRunLoopMode); | |
| 393 CFRelease(enter_exit_observer_); | 413 CFRelease(enter_exit_observer_); |
| 394 | 414 |
| 395 CFRunLoopRemoveObserver(run_loop_, pre_source_observer_, | 415 CFRunLoopRemoveObserver(run_loop_, pre_source_observer_, |
| 396 kCFRunLoopCommonModes); | 416 kCFRunLoopCommonModes); |
| 417 CFRunLoopRemoveObserver(run_loop_, pre_source_observer_, | |
| 418 kMessageLoopExclusiveRunLoopMode); | |
| 397 CFRelease(pre_source_observer_); | 419 CFRelease(pre_source_observer_); |
| 398 | 420 |
| 399 CFRunLoopRemoveObserver(run_loop_, pre_wait_observer_, | 421 CFRunLoopRemoveObserver(run_loop_, pre_wait_observer_, |
| 400 kCFRunLoopCommonModes); | 422 kCFRunLoopCommonModes); |
| 423 CFRunLoopRemoveObserver(run_loop_, pre_wait_observer_, | |
| 424 kMessageLoopExclusiveRunLoopMode); | |
| 401 CFRelease(pre_wait_observer_); | 425 CFRelease(pre_wait_observer_); |
| 402 | 426 |
| 403 CFRunLoopRemoveSource(run_loop_, nesting_deferred_work_source_, | 427 CFRunLoopRemoveSource(run_loop_, nesting_deferred_work_source_, |
| 404 kCFRunLoopCommonModes); | 428 kCFRunLoopCommonModes); |
| 429 CFRunLoopRemoveSource(run_loop_, nesting_deferred_work_source_, | |
| 430 kMessageLoopExclusiveRunLoopMode); | |
| 405 CFRelease(nesting_deferred_work_source_); | 431 CFRelease(nesting_deferred_work_source_); |
| 406 | 432 |
| 407 CFRunLoopRemoveSource(run_loop_, idle_work_source_, kCFRunLoopCommonModes); | 433 CFRunLoopRemoveSource(run_loop_, idle_work_source_, kCFRunLoopCommonModes); |
| 434 CFRunLoopRemoveSource(run_loop_, idle_work_source_, | |
| 435 kMessageLoopExclusiveRunLoopMode); | |
| 408 CFRelease(idle_work_source_); | 436 CFRelease(idle_work_source_); |
| 409 | 437 |
| 410 CFRunLoopRemoveSource(run_loop_, work_source_, kCFRunLoopCommonModes); | 438 CFRunLoopRemoveSource(run_loop_, work_source_, kCFRunLoopCommonModes); |
| 439 CFRunLoopRemoveSource(run_loop_, work_source_, | |
| 440 kMessageLoopExclusiveRunLoopMode); | |
| 411 CFRelease(work_source_); | 441 CFRelease(work_source_); |
| 412 | 442 |
| 413 CFRunLoopRemoveTimer(run_loop_, delayed_work_timer_, kCFRunLoopCommonModes); | 443 CFRunLoopRemoveTimer(run_loop_, delayed_work_timer_, kCFRunLoopCommonModes); |
| 444 CFRunLoopRemoveTimer(run_loop_, delayed_work_timer_, | |
| 445 kMessageLoopExclusiveRunLoopMode); | |
| 414 CFRelease(delayed_work_timer_); | 446 CFRelease(delayed_work_timer_); |
| 415 | 447 |
| 416 CFRelease(run_loop_); | 448 CFRelease(run_loop_); |
| 417 } | 449 } |
| 418 | 450 |
| 419 // Must be called on the run loop thread. | 451 // Must be called on the run loop thread. |
| 420 void MessagePumpCFRunLoopBase::Run(Delegate* delegate) { | 452 void MessagePumpCFRunLoopBase::Run(Delegate* delegate) { |
| 421 // nesting_level_ will be incremented in EnterExitRunLoop, so set | 453 // nesting_level_ will be incremented in EnterExitRunLoop, so set |
| 422 // run_nesting_level_ accordingly. | 454 // run_nesting_level_ accordingly. |
| 423 int last_run_nesting_level = run_nesting_level_; | 455 int last_run_nesting_level = run_nesting_level_; |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 793 } | 825 } |
| 794 | 826 |
| 795 MessagePumpNSRunLoop::MessagePumpNSRunLoop() | 827 MessagePumpNSRunLoop::MessagePumpNSRunLoop() |
| 796 : keep_running_(true) { | 828 : keep_running_(true) { |
| 797 CFRunLoopSourceContext source_context = CFRunLoopSourceContext(); | 829 CFRunLoopSourceContext source_context = CFRunLoopSourceContext(); |
| 798 source_context.perform = NoOp; | 830 source_context.perform = NoOp; |
| 799 quit_source_ = CFRunLoopSourceCreate(NULL, // allocator | 831 quit_source_ = CFRunLoopSourceCreate(NULL, // allocator |
| 800 0, // priority | 832 0, // priority |
| 801 &source_context); | 833 &source_context); |
| 802 CFRunLoopAddSource(run_loop(), quit_source_, kCFRunLoopCommonModes); | 834 CFRunLoopAddSource(run_loop(), quit_source_, kCFRunLoopCommonModes); |
| 835 CFRunLoopAddSource(run_loop(), quit_source_, | |
| 836 kMessageLoopExclusiveRunLoopMode); | |
| 803 } | 837 } |
| 804 | 838 |
| 805 MessagePumpNSRunLoop::~MessagePumpNSRunLoop() { | 839 MessagePumpNSRunLoop::~MessagePumpNSRunLoop() { |
| 806 CFRunLoopRemoveSource(run_loop(), quit_source_, kCFRunLoopCommonModes); | 840 CFRunLoopRemoveSource(run_loop(), quit_source_, kCFRunLoopCommonModes); |
| 841 CFRunLoopRemoveSource(run_loop(), quit_source_, | |
| 842 kMessageLoopExclusiveRunLoopMode); | |
| 807 CFRelease(quit_source_); | 843 CFRelease(quit_source_); |
| 808 } | 844 } |
| 809 | 845 |
| 810 void MessagePumpNSRunLoop::DoRun(Delegate* delegate) { | 846 void MessagePumpNSRunLoop::DoRun(Delegate* delegate) { |
| 811 while (keep_running_) { | 847 while (keep_running_) { |
| 812 // NSRunLoop manages autorelease pools itself. | 848 // NSRunLoop manages autorelease pools itself. |
| 813 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | 849 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode |
| 814 beforeDate:[NSDate distantFuture]]; | 850 beforeDate:[NSDate distantFuture]]; |
| 815 } | 851 } |
| 816 | 852 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 993 [NSApplication sharedApplication]; | 1029 [NSApplication sharedApplication]; |
| 994 g_not_using_cr_app = true; | 1030 g_not_using_cr_app = true; |
| 995 return new MessagePumpNSApplication; | 1031 return new MessagePumpNSApplication; |
| 996 #endif | 1032 #endif |
| 997 } | 1033 } |
| 998 | 1034 |
| 999 return new MessagePumpNSRunLoop; | 1035 return new MessagePumpNSRunLoop; |
| 1000 } | 1036 } |
| 1001 | 1037 |
| 1002 } // namespace base | 1038 } // namespace base |
| OLD | NEW |