Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 332 lines (269 sloc) 11.401 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
//
// LinkBackServer.m
// LinkBack
//
// Created by Charles Jolley on Tue Jun 15 2004.
// Copyright (c) 2004, Nisus Software, Inc.
// All rights reserved.

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// Neither the name of the Nisus Software, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#import "LinkBackServer.h"
#import "LinkBack.h"

#import <objc/message.h>

NSString* MakeLinkBackServerName(NSString* bundleIdentifier, NSString* name)
{
    return [bundleIdentifier stringByAppendingFormat: @":%@",name] ;
}

NSMutableDictionary* LinkBackServers = nil ;

@implementation LinkBackServer

+ (void)initialize
{
    static BOOL inited = NO ;
    if (inited) return ;

    [super initialize] ;
    inited = YES ;
    
    if (!LinkBackServers) LinkBackServers = [[NSMutableDictionary alloc] init];
}

+ (LinkBackServer*)LinkBackServerWithName:(NSString*)aName
{
    return [self LinkBackServerWithName:aName bundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
}

+ (LinkBackServer*)LinkBackServerWithName:(NSString*)aName bundleIdentifier:(NSString *)bundleID
{
    return [LinkBackServers objectForKey:MakeLinkBackServerName(bundleID, aName)];
}

+ (BOOL)publishServerWithName:(NSString*)aName delegate:(id<LinkBackServerDelegate>)del
{
    LinkBackServer* serv = [[LinkBackServer alloc] initWithName:aName bundleIdentifier:[[NSBundle mainBundle] bundleIdentifier] delegate:del] ;
    BOOL ret = [serv publish] ; // retains if successful
    [serv release] ;
    return ret ;
}

+ (BOOL)publishServerWithName:(NSString*)aName bundleIdentifier:(NSString *)bundleID delegate:(id<LinkBackServerDelegate>)del ;
{
    LinkBackServer* serv = [[LinkBackServer alloc] initWithName:aName bundleIdentifier:bundleID delegate:del] ;
    BOOL ret = [serv publish] ; // retains if successful
    [serv release] ;
    return ret ;
}

static BOOL LinkBackServerIsSupported(NSString* name, id supportedServers)
{
BOOL ret = NO ;
NSUInteger idx ;
NSString* curServer = supportedServers ;

// NOTE: supportedServers may be nil, an NSArray, or NSString.
if (supportedServers) {
if ([supportedServers isKindOfClass: [NSArray class]]) {
idx = [supportedServers count] ;
while((NO==ret) && idx--) {
curServer = [supportedServers objectAtIndex: idx] ;
ret = [curServer isEqualToString: name] ;
}
} else ret = [curServer isEqualToString: name] ;
}

return ret ;
}

static NSString* FindLinkBackServer(NSString* bundleIdentifier, NSString* serverName, NSString* dir, int level, NSString **altServerPathPtr)
{
NSString* ret = nil ;

// resolve any symlinks, expand tildes.
dir = [dir stringByStandardizingPath] ;
    
NSFileManager* fm = [NSFileManager defaultManager] ;
    
    NSError *error = nil;
    NSArray *contents = [fm subpathsOfDirectoryAtPath: dir error:&error];
    if (!contents) {
NSLog(@"Unable to get subpaths of '%@' - %@", dir, error);
return nil;
    }
    
NSUInteger idx ;

#ifdef DEBUG_FindLinkBackServer
NSLog(@"searching for %@ (%@) in folder: %@", serverName, bundleIdentifier, dir) ;
#endif // DEBUG_FindLinkBackServer
        
// working info
NSString* cpath ;
NSBundle* cbundle ;
NSString* cbundleIdentifier ;
id supportedServers ;

// find all .app bundles in the directory and test them.
idx = (contents) ? [contents count] : 0 ;
while((nil==ret) && idx--) {
cpath = [contents objectAtIndex: idx] ;

if ([[cpath pathExtension] isEqualToString: @"app"]) {
cpath = [dir stringByAppendingPathComponent: cpath] ;
cbundle = [NSBundle bundleWithPath: cpath] ;
cbundleIdentifier = [cbundle bundleIdentifier] ;

supportedServers = [[cbundle infoDictionary] objectForKey: @"LinkBackServer"] ;
NSString* serverPath = (LinkBackServerIsSupported(serverName, supportedServers)) ? cpath : nil ;
if (nil != serverPath) {
if ([cbundleIdentifier isEqualToString: bundleIdentifier]) {
ret = serverPath ;
} else if ((NULL != altServerPathPtr) && (nil == *altServerPathPtr)) {
*altServerPathPtr = serverPath ;
}
}
}
}

// if the app was not found, descend into non-app dirs. only descend 4 levels to avoid taking forever.
if ((nil==ret) && (level<4)) {
idx = (contents) ? [contents count] : 0 ;
while((nil==ret) && idx--) {
BOOL isdir ;

cpath = [dir stringByAppendingPathComponent:[contents objectAtIndex: idx]] ;
[fm fileExistsAtPath: cpath isDirectory: &isdir] ;
if (isdir && (![[cpath pathExtension] isEqualToString: @"app"])) {
ret = FindLinkBackServer(bundleIdentifier, serverName, cpath, level+1, altServerPathPtr) ;
}
}
}

return ret ;
}

static void LinkBackRunAppNotFoundPanel(NSString* appName, NSURL* url)
{
NSInteger result ;

// strings for panel
NSBundle* b = [NSBundle bundleForClass: [LinkBack class]] ;
NSString* title ;
NSString* msg ;
NSString* ok ;
NSString* urlstr ;

title = NSLocalizedStringFromTableInBundle(@"_AppNotFoundTitle", @"Localized", b, @"app not found title") ;
ok = NSLocalizedStringFromTableInBundle(@"_OK", @"Localized", b, @"ok") ;

msg = (url) ? NSLocalizedStringFromTableInBundle(@"_AppNotFoundMessageWithURL", @"Localized", b, @"app not found msg") : NSLocalizedStringFromTableInBundle(@"_AppNotFoundMessageNoURL", @"Localized", b, @"app not found msg") ;

urlstr = (url) ? NSLocalizedStringFromTableInBundle(@"_GetApplication", @"Localized", b, @"Get application") : nil ;

title = [NSString stringWithFormat: title, appName] ;

result = NSRunCriticalAlertPanel(title, msg, ok, urlstr, nil) ;
if (NSAlertAlternateReturn == result) {
[[NSWorkspace sharedWorkspace] openURL: url] ;
}
}

+ (LinkBackServer*)LinkBackServerWithName:(NSString*)aName inApplication:(NSString*)bundleIdentifier launchIfNeeded:(BOOL)flag fallbackURL:(NSURL*)url appName:(NSString*)appName ;
{
NSString* serverName = MakeLinkBackServerName(bundleIdentifier, aName) ;
    id ret = nil ;

// Is this our own server?
    ret = [LinkBackServers objectForKey:serverName];
    if (ret != nil)
        return ret;

// Try to connect
ret = [NSConnection rootProxyForConnectionWithRegisteredName: serverName host: nil] ;
BOOL connect = YES ;

    // if launchIfNeeded, and the connection was not available, try to launch.
if((!ret) && (flag)) {
NSString* appPath ;
NSString* altAppPath = nil ;
id linkBackServers ;

// first, try to find the app with the bundle identifier
appPath = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier: bundleIdentifier] ;
linkBackServers = [[[NSBundle bundleWithPath: appPath] infoDictionary] objectForKey: @"LinkBackServer"] ;
appPath = (LinkBackServerIsSupported(aName, linkBackServers)) ? appPath : nil ;

// if the found app is not supported, we will need to search for the app ourselves.
if (nil==appPath) {
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSAllDomainsMask, NO); // Don't need to expand tildes as FindLinkBackServer() standardizes the path passed to it
NSUInteger pathCount = [searchPaths count];
NSUInteger pathIndex;
for (pathIndex = 0; pathIndex < pathCount; pathIndex++) {
NSString *searchPath = [searchPaths objectAtIndex:pathIndex];
appPath = FindLinkBackServer(bundleIdentifier, aName, searchPath, 0, &altAppPath);
if (nil!=appPath)
break;
}
}

if (nil==appPath) appPath = altAppPath;
                
// if app path has been found, launch the app.
if (appPath) {
[[NSWorkspace sharedWorkspace] launchApplication: appPath] ;
} else if (![[NSWorkspace sharedWorkspace] launchApplication: appName]) {
LinkBackRunAppNotFoundPanel(appName, url) ;
connect = NO ;
}
}
    
    // if needed, try to connect.
// retry connection for a while if we did not succeed at first. This gives the app time to launch.
if (connect && (nil==ret)) {
NSTimeInterval tryMark = [NSDate timeIntervalSinceReferenceDate] ;
do {
ret = [NSConnection rootProxyForConnectionWithRegisteredName: serverName host: nil] ;
} while ((!ret) && (([NSDate timeIntervalSinceReferenceDate]-tryMark)<10)) ;

}

// setup protocol and return
    if (ret) [ret setProtocolForProxy: @protocol(LinkBackServer)] ;
    return ret ;
}

- (id)initWithName:(NSString*)aName bundleIdentifier:(NSString *)anIdentifier delegate:(id<LinkBackServerDelegate>)aDel
{
    if (!(self = [super init]))
        return nil;

    bundleIdentifier = [anIdentifier copy];
    name = [aName copy] ;
    delegate = aDel ;
    listener = nil ;
    
    return self ;
}

- (void)dealloc
{
    if (listener)
        [self retract];
    [name release];
    [bundleIdentifier release];
    [super dealloc];
}

- (BOOL)publish
{
    NSString* serverName = MakeLinkBackServerName(bundleIdentifier, name) ;
    BOOL ret = YES ;
    
    // create listener and connect
    NSPort* port = [NSPort port] ;
    listener = [NSConnection connectionWithReceivePort: port sendPort:port] ;
    [listener setRootObject: self] ;
    ret = [listener registerName: serverName] ;
    
    // if successful, retain connection and add self to list of servers.
    if (ret) {
        [listener retain] ;
    } else listener = nil ; // listener will dealloc on its own.
    
    [LinkBackServers setObject: self forKey: serverName] ; // Always keep track of our published servers
    return ret ;
}

- (void)retract
{
    if (listener) {
        [listener invalidate] ;
        [listener release] ;
        listener = nil ;
    }
    
    [LinkBackServers removeObjectForKey: MakeLinkBackServerName(bundleIdentifier, name)] ;
}

- (LinkBack*)initiateLinkBackFromClient:(LinkBack*)clientLinkBack
{
    LinkBack* ret = [[LinkBack alloc] initServerWithClient: clientLinkBack delegate: delegate] ;
    
    // NOTE: we do not release because LinkBack will release itself when it the link closes. (caj)
    
    // But we need to pretend to release to hack around clang:
    void (*imp)(id, SEL) = (typeof(imp))objc_msgSend;
    imp(ret, @selector(retain));
    [ret autorelease];
    
    return ret ;
}

@end
Something went wrong with that request. Please try again.