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

Side by Side Diff: base/message_pump_mac.mm

Issue 10689161: Adds MessageLoopUIApplication for use on iOS. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: ^ and $. Created 8 years, 5 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 unified diff | Download patch
« no previous file with comments | « base/message_pump_mac.h ('k') | base/run_loop.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_pump_mac.h" 5 #import "base/message_pump_mac.h"
6 6
7 #import <AppKit/AppKit.h>
8 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
9 8
10 #include <limits> 9 #include <limits>
11 10
12 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/run_loop.h"
13 #include "base/time.h" 13 #include "base/time.h"
14 14
15 #if !defined(OS_IOS)
16 #import <AppKit/AppKit.h>
17 #endif // !defined(OS_IOS)
18
15 namespace { 19 namespace {
16 20
17 void NoOp(void* info) { 21 void NoOp(void* info) {
18 } 22 }
19 23
20 const CFTimeInterval kCFTimeIntervalMax = 24 const CFTimeInterval kCFTimeIntervalMax =
21 std::numeric_limits<CFTimeInterval>::max(); 25 std::numeric_limits<CFTimeInterval>::max();
22 26
23 // Set to true if MessagePumpMac::Create() is called before NSApp is 27 // Set to true if MessagePumpMac::Create() is called before NSApp is
24 // initialized. Only accessed from the main thread. 28 // initialized. Only accessed from the main thread.
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 } 157 }
154 158
155 // Must be called on the run loop thread. 159 // Must be called on the run loop thread.
156 void MessagePumpCFRunLoopBase::Run(Delegate* delegate) { 160 void MessagePumpCFRunLoopBase::Run(Delegate* delegate) {
157 // nesting_level_ will be incremented in EnterExitRunLoop, so set 161 // nesting_level_ will be incremented in EnterExitRunLoop, so set
158 // run_nesting_level_ accordingly. 162 // run_nesting_level_ accordingly.
159 int last_run_nesting_level = run_nesting_level_; 163 int last_run_nesting_level = run_nesting_level_;
160 run_nesting_level_ = nesting_level_ + 1; 164 run_nesting_level_ = nesting_level_ + 1;
161 165
162 Delegate* last_delegate = delegate_; 166 Delegate* last_delegate = delegate_;
167 SetDelegate(delegate);
168
169 DoRun(delegate);
170
171 // Restore the previous state of the object.
172 SetDelegate(last_delegate);
173 run_nesting_level_ = last_run_nesting_level;
174 }
175
176 void MessagePumpCFRunLoopBase::SetDelegate(Delegate* delegate) {
163 delegate_ = delegate; 177 delegate_ = delegate;
164 178
165 if (delegate) { 179 if (delegate) {
166 // If any work showed up but could not be dispatched for want of a 180 // If any work showed up but could not be dispatched for want of a
167 // delegate, set it up for dispatch again now that a delegate is 181 // delegate, set it up for dispatch again now that a delegate is
168 // available. 182 // available.
169 if (delegateless_work_) { 183 if (delegateless_work_) {
170 CFRunLoopSourceSignal(work_source_); 184 CFRunLoopSourceSignal(work_source_);
171 delegateless_work_ = false; 185 delegateless_work_ = false;
172 } 186 }
173 if (delegateless_idle_work_) { 187 if (delegateless_idle_work_) {
174 CFRunLoopSourceSignal(idle_work_source_); 188 CFRunLoopSourceSignal(idle_work_source_);
175 delegateless_idle_work_ = false; 189 delegateless_idle_work_ = false;
176 } 190 }
177 } 191 }
178
179 DoRun(delegate);
180
181 // Restore the previous state of the object.
182 delegate_ = last_delegate;
183 run_nesting_level_ = last_run_nesting_level;
184 } 192 }
185 193
186 // May be called on any thread. 194 // May be called on any thread.
187 void MessagePumpCFRunLoopBase::ScheduleWork() { 195 void MessagePumpCFRunLoopBase::ScheduleWork() {
188 CFRunLoopSourceSignal(work_source_); 196 CFRunLoopSourceSignal(work_source_);
189 CFRunLoopWakeUp(run_loop_); 197 CFRunLoopWakeUp(run_loop_);
190 } 198 }
191 199
192 // Must be called on the run loop thread. 200 // Must be called on the run loop thread.
193 void MessagePumpCFRunLoopBase::ScheduleDelayedWork( 201 void MessagePumpCFRunLoopBase::ScheduleDelayedWork(
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 520
513 keep_running_ = true; 521 keep_running_ = true;
514 } 522 }
515 523
516 void MessagePumpNSRunLoop::Quit() { 524 void MessagePumpNSRunLoop::Quit() {
517 keep_running_ = false; 525 keep_running_ = false;
518 CFRunLoopSourceSignal(quit_source_); 526 CFRunLoopSourceSignal(quit_source_);
519 CFRunLoopWakeUp(run_loop()); 527 CFRunLoopWakeUp(run_loop());
520 } 528 }
521 529
530 #if defined(OS_IOS)
531 MessagePumpUIApplication::MessagePumpUIApplication()
532 : run_loop_(NULL) {
533 }
534
535 MessagePumpUIApplication::~MessagePumpUIApplication() {}
536
537 void MessagePumpUIApplication::DoRun(Delegate* delegate) {
538 NOTREACHED();
539 }
540
541 void MessagePumpUIApplication::Quit() {
542 NOTREACHED();
543 }
544
545 void MessagePumpUIApplication::Attach(Delegate* delegate) {
546 DCHECK(!run_loop_);
547 run_loop_ = new base::RunLoop();
548 CHECK(run_loop_->BeforeRun());
549 SetDelegate(delegate);
550 }
551
552 #else
553
522 MessagePumpNSApplication::MessagePumpNSApplication() 554 MessagePumpNSApplication::MessagePumpNSApplication()
523 : keep_running_(true), 555 : keep_running_(true),
524 running_own_loop_(false) { 556 running_own_loop_(false) {
525 } 557 }
526 558
527 MessagePumpNSApplication::~MessagePumpNSApplication() {} 559 MessagePumpNSApplication::~MessagePumpNSApplication() {}
528 560
529 void MessagePumpNSApplication::DoRun(Delegate* delegate) { 561 void MessagePumpNSApplication::DoRun(Delegate* delegate) {
530 bool last_running_own_loop_ = running_own_loop_; 562 bool last_running_own_loop_ = running_own_loop_;
531 563
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 // CrApplication is responsible for setting handlingSendEvent to true just 645 // CrApplication is responsible for setting handlingSendEvent to true just
614 // before it sends the event through the event handling mechanism, and 646 // before it sends the event through the event handling mechanism, and
615 // returning it to its previous value once the event has been sent. 647 // returning it to its previous value once the event has been sent.
616 NSAutoreleasePool* MessagePumpCrApplication::CreateAutoreleasePool() { 648 NSAutoreleasePool* MessagePumpCrApplication::CreateAutoreleasePool() {
617 if (MessagePumpMac::IsHandlingSendEvent()) 649 if (MessagePumpMac::IsHandlingSendEvent())
618 return nil; 650 return nil;
619 return MessagePumpNSApplication::CreateAutoreleasePool(); 651 return MessagePumpNSApplication::CreateAutoreleasePool();
620 } 652 }
621 653
622 // static 654 // static
623 MessagePump* MessagePumpMac::Create() {
624 if ([NSThread isMainThread]) {
625 if ([NSApp conformsToProtocol:@protocol(CrAppProtocol)])
626 return new MessagePumpCrApplication;
627
628 // The main-thread MessagePump implementations REQUIRE an NSApp.
629 // Executables which have specific requirements for their
630 // NSApplication subclass should initialize appropriately before
631 // creating an event loop.
632 [NSApplication sharedApplication];
633 not_using_crapp = true;
634 return new MessagePumpNSApplication;
635 }
636
637 return new MessagePumpNSRunLoop;
638 }
639
640 // static
641 bool MessagePumpMac::UsingCrApp() { 655 bool MessagePumpMac::UsingCrApp() {
642 DCHECK([NSThread isMainThread]); 656 DCHECK([NSThread isMainThread]);
643 657
644 // If NSApp is still not initialized, then the subclass used cannot 658 // If NSApp is still not initialized, then the subclass used cannot
645 // be determined. 659 // be determined.
646 DCHECK(NSApp); 660 DCHECK(NSApp);
647 661
648 // The pump was created using MessagePumpNSApplication. 662 // The pump was created using MessagePumpNSApplication.
649 if (not_using_crapp) 663 if (not_using_crapp)
650 return false; 664 return false;
651 665
652 return [NSApp conformsToProtocol:@protocol(CrAppProtocol)]; 666 return [NSApp conformsToProtocol:@protocol(CrAppProtocol)];
653 } 667 }
654 668
655 // static 669 // static
656 bool MessagePumpMac::IsHandlingSendEvent() { 670 bool MessagePumpMac::IsHandlingSendEvent() {
657 DCHECK([NSApp conformsToProtocol:@protocol(CrAppProtocol)]); 671 DCHECK([NSApp conformsToProtocol:@protocol(CrAppProtocol)]);
658 NSObject<CrAppProtocol>* app = static_cast<NSObject<CrAppProtocol>*>(NSApp); 672 NSObject<CrAppProtocol>* app = static_cast<NSObject<CrAppProtocol>*>(NSApp);
659 return [app isHandlingSendEvent]; 673 return [app isHandlingSendEvent];
660 } 674 }
675 #endif // !defined(OS_IOS)
676
677 // static
678 MessagePump* MessagePumpMac::Create() {
679 if ([NSThread isMainThread]) {
680 #if defined(OS_IOS)
681 return new MessagePumpUIApplication;
682 #else
683 if ([NSApp conformsToProtocol:@protocol(CrAppProtocol)])
684 return new MessagePumpCrApplication;
685
686 // The main-thread MessagePump implementations REQUIRE an NSApp.
687 // Executables which have specific requirements for their
688 // NSApplication subclass should initialize appropriately before
689 // creating an event loop.
690 [NSApplication sharedApplication];
691 not_using_crapp = true;
692 return new MessagePumpNSApplication;
693 #endif
694 }
695
696 return new MessagePumpNSRunLoop;
697 }
661 698
662 } // namespace base 699 } // namespace base
OLDNEW
« no previous file with comments | « base/message_pump_mac.h ('k') | base/run_loop.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698