1 /* 2 * libjingle 3 * Copyright 2012, Google Inc 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #import "talk/base/maccocoasocketserver.h" 28 29 #import <Foundation/Foundation.h> 30 #import <AppKit/AppKit.h> 31 #include <assert.h> 32 33 #include "talk/base/scoped_autorelease_pool.h" 34 35 // MacCocoaSocketServerHelper serves as a delegate to NSMachPort or a target for 36 // a timeout. 37 @interface MacCocoaSocketServerHelper : NSObject { 38 // This is a weak reference. This works fine since the 39 // talk_base::MacCocoaSocketServer owns this object. 40 talk_base::MacCocoaSocketServer* socketServer_; // Weak. 41 } 42 @end 43 44 @implementation MacCocoaSocketServerHelper 45 - (id)initWithSocketServer:(talk_base::MacCocoaSocketServer*)ss { 46 self = [super init]; 47 if (self) { 48 socketServer_ = ss; 49 } 50 return self; 51 } 52 53 - (void)timerFired:(NSTimer*)timer { 54 socketServer_->WakeUp(); 55 } 56 @end 57 58 namespace talk_base { 59 60 MacCocoaSocketServer::MacCocoaSocketServer() { 61 helper_ = [[MacCocoaSocketServerHelper alloc] initWithSocketServer:this]; 62 timer_ = nil; 63 64 // Initialize the shared NSApplication 65 [NSApplication sharedApplication]; 66 } 67 68 MacCocoaSocketServer::~MacCocoaSocketServer() { 69 [timer_ invalidate]; 70 [timer_ release]; 71 [helper_ release]; 72 } 73 74 bool MacCocoaSocketServer::Wait(int cms, bool process_io) { 75 talk_base::ScopedAutoreleasePool pool; 76 if (!process_io && cms == 0) { 77 // No op. 78 return true; 79 } 80 81 if (!process_io) { 82 // No way to listen to common modes and not get socket events, unless 83 // we disable each one's callbacks. 84 EnableSocketCallbacks(false); 85 } 86 87 if (kForever != cms) { 88 // Install a timer that fires wakeup after cms has elapsed. 89 timer_ = 90 [NSTimer scheduledTimerWithTimeInterval:cms / 1000.0 91 target:helper_ 92 selector:@selector(timerFired:) 93 userInfo:nil 94 repeats:NO]; 95 [timer_ retain]; 96 } 97 98 // Run until WakeUp is called, which will call stop and exit this loop. 99 [NSApp run]; 100 101 if (!process_io) { 102 // Reenable them. Hopefully this won't cause spurious callbacks or 103 // missing ones while they were disabled. 104 EnableSocketCallbacks(true); 105 } 106 107 return true; 108 } 109 110 void MacCocoaSocketServer::WakeUp() { 111 // Timer has either fired or shortcutted. 112 [timer_ invalidate]; 113 [timer_ release]; 114 timer_ = nil; 115 [NSApp stop:nil]; 116 117 // NSApp stop only exits after finishing processing of the 118 // current event. Since we're potentially in a timer callback 119 // and not an NSEvent handler, we need to trigger a dummy one 120 // and turn the loop over. We may be able to skip this if we're 121 // on the ss' thread and not inside the app loop already. 122 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined 123 location:NSMakePoint(0,0) 124 modifierFlags:0 125 timestamp:0 126 windowNumber:0 127 context:nil 128 subtype:1 129 data1:1 130 data2:1]; 131 [NSApp postEvent:event atStart:YES]; 132 } 133 134 } // namespace talk_base 135