Revision: 25120 http://sourceforge.net/p/bibdesk/svn/25120 Author: hofman Date: 2020-12-02 17:04:45 +0000 (Wed, 02 Dec 2020) Log Message: ----------- Fold session based downloader in downloader class and remove downloader classes for NSURLDownload and NSURLSession
Modified Paths: -------------- trunk/bibdesk/BDSKDownloader.h trunk/bibdesk/BDSKDownloader.m trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj Removed Paths: ------------- trunk/bibdesk/BDSKDownloaderDeprecated.h trunk/bibdesk/BDSKDownloaderDeprecated.m trunk/bibdesk/BDSKDownloaderSession.h trunk/bibdesk/BDSKDownloaderSession.m Modified: trunk/bibdesk/BDSKDownloader.h =================================================================== --- trunk/bibdesk/BDSKDownloader.h 2020-12-02 16:13:28 UTC (rev 25119) +++ trunk/bibdesk/BDSKDownloader.h 2020-12-02 17:04:45 UTC (rev 25120) @@ -48,8 +48,11 @@ BDSKDownloaderAuthChallengeRejectProtectionSpace = 3, }; -@interface BDSKDownloader : NSObject { +@interface BDSKDownloader : NSObject <NSURLSessionDelegate> { NSMapTable *downloads; + NSURLSession *session; + NSMapTable *delegates; + NSMutableSet *receivedExpectedBytes; } + (BDSKDownloader *)sharedDownloader; @@ -63,49 +66,23 @@ #pragma mark - -@interface BDSKDownloader (BDSKPrivate) - -+ (Class)downloadClass; - -- (BDSKDownload *)addDownloadWithTask:(id)download delegate:(id <BDSKDownloaderDelegate>)delegate; - -- (BDSKDownload *)downloadForTask:(id)download; - -- (void)cancelDownload:(BDSKDownload *)download; - -- (void)cleanupDownload:(BDSKDownload *)download; - -- (void)_download:(BDSKDownload *)download didFinishWithData:(NSData *)data; -- (void)_download:(BDSKDownload *)download didFailWithError:(NSError *)error; - -- (void)_download:(BDSKDownload *)download didReceiveExpectedContentLength:(int64_t)expectedContentLength; -- (void)_download:(BDSKDownload *)download didReceiveData:(NSData *)data ofLength:(uint64_t)length; - -- (void)_download:(BDSKDownload *)download didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(BDSKDownloaderAuthChallengeDisposition disposition, NSURLCredential * credential))completionHandler; - -- (void)_download:(BDSKDownload *)download decideDestinationWithSuggestedFilename:(NSString *)suggestedFileName completionHandler:(void (^)(NSURL *destinationURL, BOOL allowOverwrite))completionHandler; -- (void)_download:(BDSKDownload *)download didCreateDestination:(NSURL *)destinationURL; - -@end - -#pragma mark - - @interface BDSKDownload : NSObject { - id task; + NSURLSessionTask *task; NSMutableData *data; id <BDSKDownloaderDelegate> delegate; + BOOL receivedExpectedBytes; } @property (nonatomic, retain) id <BDSKDownloaderDelegate> delegate; -@property (nonatomic, retain) id task; +@property (nonatomic, retain) NSURLSessionTask *task; @property (nonatomic, readonly) NSURLResponse *response; @property (nonatomic, retain) NSMutableData *data; +@property (nonatomic) BOOL receivedExpectedBytes; - (void)cancel; @end - #pragma mark - @protocol BDSKDownloaderDelegate <NSObject> Modified: trunk/bibdesk/BDSKDownloader.m =================================================================== --- trunk/bibdesk/BDSKDownloader.m 2020-12-02 16:13:28 UTC (rev 25119) +++ trunk/bibdesk/BDSKDownloader.m 2020-12-02 17:04:45 UTC (rev 25120) @@ -37,29 +37,44 @@ */ #import "BDSKDownloader.h" -#import "BDSKDownloaderDeprecated.h" -#import "BDSKDownloaderSession.h" #import "BDSKAuthenticationHandler.h" #import "NSFileManager_BDSKExtensions.h" +@interface BDSKDownloader (BDSKPrivate) +- (BDSKDownload *)addDownloadWithTask:(id)download delegate:(id <BDSKDownloaderDelegate>)delegate; + +- (BDSKDownload *)downloadForTask:(id)download; + +- (void)cancelDownload:(BDSKDownload *)download; + +- (void)cleanupDownload:(BDSKDownload *)download; + +- (void)_download:(BDSKDownload *)download didFinishWithData:(NSData *)data; +- (void)_download:(BDSKDownload *)download didFailWithError:(NSError *)error; + +- (void)_download:(BDSKDownload *)download didReceiveExpectedContentLength:(int64_t)expectedContentLength; +- (void)_download:(BDSKDownload *)download didReceiveData:(NSData *)data ofLength:(uint64_t)length; + +- (void)_download:(BDSKDownload *)download didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(BDSKDownloaderAuthChallengeDisposition disposition, NSURLCredential * credential))completionHandler; + +- (void)_download:(BDSKDownload *)download decideDestinationWithSuggestedFilename:(NSString *)suggestedFileName completionHandler:(void (^)(NSURL *destinationURL, BOOL allowOverwrite))completionHandler; +- (void)_download:(BDSKDownload *)download didCreateDestination:(NSURL *)destinationURL; + +@end + +#pragma mark - + @implementation BDSKDownloader + (BDSKDownloader *)sharedDownloader{ static BDSKDownloader *sharedDownloader = nil; if (sharedDownloader == nil) { - if (NSClassFromString(@"NSURLSession")) - sharedDownloader = [[BDSKDownloaderSession alloc] init]; - else - sharedDownloader = [[BDSKDownloaderDeprecated alloc] init]; + sharedDownloader = [[self alloc] init]; } return sharedDownloader; } -+ (Class)downloadClass { - return [BDSKDownload class]; -} - - (id)init { self = [super init]; if (self) { @@ -68,23 +83,60 @@ return self; } -- (BDSKDownload *)startFileDownloadWithRequest:(NSURLRequest *)request delegate:(id <BDSKDownloaderDelegate>)aDelegate { - [self doesNotRecognizeSelector:_cmd]; - return nil; +- (NSURLSession *)session { + if (session == nil) { + session = [[NSURLSession + sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] + delegate:self + delegateQueue:[NSOperationQueue mainQueue]] retain]; + } + return session; } -- (BDSKDownload *)startDataDownloadWithRequest:(NSURLRequest *)request delegate:(id <BDSKDownloaderDelegate>)aDelegate { - [self doesNotRecognizeSelector:_cmd]; - return nil; +- (BDSKDownload *)startFileDownloadWithRequest:(NSURLRequest *)request delegate:(id <BDSKDownloaderDelegate>)delegate { + NSURLSessionTask *task = [[self session] downloadTaskWithRequest:request]; + BDSKDownload *download = [self addDownloadWithTask:task delegate:delegate]; + [task resume]; + return download; } +- (BDSKDownload *)startDataDownloadWithRequest:(NSURLRequest *)request delegate:(id <BDSKDownloaderDelegate>)delegate { + NSURLSessionTask *task = [[self session] dataTaskWithRequest:request]; + BDSKDownload *download = [self addDownloadWithTask:task delegate:delegate]; + [task resume]; + return download; +} + - (NSData *)downloadDataWithRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error { - [self doesNotRecognizeSelector:_cmd]; - return nil; + __block NSURLResponse *returnResponse = nil; + __block NSError *returnError = nil; + __block NSData *returnData = nil; + __block BOOL downloadFinished = NO; + + [[[self session] dataTaskWithRequest:request completionHandler:^(NSData *aData, NSURLResponse *aResponse, NSError *aError){ + if (aData) { + returnData = [aData retain]; + if (request) + returnResponse = [aResponse retain]; + } else if (error) { + returnError = [aError retain]; + } + downloadFinished = YES; + }] resume]; + + NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; + while (downloadFinished == NO && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]); + + if (response) + *response = [returnResponse autorelease]; + if (error) + *error = [returnError autorelease]; + + return [returnData autorelease]; } - (BDSKDownload *)addDownloadWithTask:(id)task delegate:(id <BDSKDownloaderDelegate>)delegate { - BDSKDownload *download = [[[[[self class] downloadClass] alloc] init] autorelease]; + BDSKDownload *download = [[[BDSKDownload alloc] init] autorelease]; [download setTask:task]; [download setDelegate:delegate]; [downloads setObject:download forKey:task]; @@ -167,6 +219,86 @@ } } +#pragma mark NSURLSessionDownloadTaskDelegate + +- (void)URLSession:(NSURLSession *)aSession downloadTask:(NSURLSessionDownloadTask *)task didFinishDownloadingToURL:(NSURL *)location { + BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; + NSString *suggestedFileName = [[task response] suggestedFilename] ?: [location lastPathComponent]; + + [self _download:download decideDestinationWithSuggestedFilename:suggestedFileName completionHandler:^(NSURL *destinationURL, BOOL allowOverwrite){ + NSError *error = nil; + NSFileManager *fm = [NSFileManager defaultManager]; + if ([destinationURL checkResourceIsReachableAndReturnError:NULL]) { + if (allowOverwrite) { + [fm removeItemAtURL:destinationURL error:NULL]; + } else { + destinationURL = [fm uniqueFileURL:destinationURL]; + } + } else if ([[destinationURL URLByDeletingLastPathComponent] checkResourceIsReachableAndReturnError:NULL] == NO) { + [fm createDirectoryAtPath:[[destinationURL URLByDeletingLastPathComponent] path] withIntermediateDirectories:YES attributes:nil error:NULL]; + } + if ([fm moveItemAtURL:location toURL:destinationURL error:&error]) { + [self _download:download didCreateDestination:destinationURL]; + [self _download:download didFinishWithData:nil]; + } else { + [self _download:download didFailWithError:error]; + [self cleanupDownload:download]; + } + }]; +} + +- (void)URLSession:(NSURLSession *)aSession downloadTask:(NSURLSessionDownloadTask *)task didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { + BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; + if ([download receivedExpectedBytes] == NO) { + [download setReceivedExpectedBytes:YES]; + [self _download:download didReceiveExpectedContentLength:totalBytesExpectedToWrite]; + } + + if (bytesWritten >= 0) { + [self _download:download didReceiveData:nil ofLength:(uint64_t)bytesWritten]; + } +} + +#pragma mark NSURLSessionDataTaskDelegate + +- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler { + BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; + [self _download:download didReceiveExpectedContentLength:[response expectedContentLength]]; + // the delegeate may have cancelled the task, in that case don't call the callback + if ([self downloadForTask:task]) + completionHandler(NSURLSessionResponseAllow); +} + +- (void)URLSession:(NSURLSession *)aSession dataTask:(NSURLSessionDataTask *)task didReceiveData:(NSData *)data { + BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; + [self _download:download didReceiveData:data ofLength:[data length]]; +} + +#pragma mark NSURLSessionTaskDelegate + +- (void)URLSession:(NSURLSession *)aSession task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { + BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; + [self _download:download didReceiveChallenge:challenge completionHandler:(void (^)(BDSKDownloaderAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler]; +} + +- (void)URLSession:(NSURLSession *)aSession task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { + BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; + if (error) { + [self _download:download didFailWithError:error]; + } else if ([task isKindOfClass:[NSURLSessionDataTask class]]) { + [self _download:download didFinishWithData:[download data]]; + } + [self cleanupDownload:download]; +} + +#pragma mark NSURLSessionDelegate + +// we need to implement this to avoid it being forwarded to the task specific delegate method +- (void)URLSession:(NSURLSession *)aSession didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { + completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); +} + + @end #pragma mark - @@ -173,11 +305,11 @@ @implementation BDSKDownload -@synthesize delegate, task, data; +@synthesize delegate, task, data, receivedExpectedBytes; @dynamic response; - (NSURLResponse *)response { - return nil; + return [[self task] response]; } - (void)cancel { Deleted: trunk/bibdesk/BDSKDownloaderDeprecated.h =================================================================== --- trunk/bibdesk/BDSKDownloaderDeprecated.h 2020-12-02 16:13:28 UTC (rev 25119) +++ trunk/bibdesk/BDSKDownloaderDeprecated.h 2020-12-02 17:04:45 UTC (rev 25120) @@ -1,47 +0,0 @@ -// -// BDSKDownloaderDeprecated.h -// BibDesk -// -// Created by Christiaan on 14/03/2019. -/* - This software is Copyright (c) 2019-2020 - Christiaan Hofman. 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 Christiaan Hofman nor the names of any - 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 <Cocoa/Cocoa.h> -#import "BDSKDownloader.h" - - -@interface BDSKDownloaderDeprecated : BDSKDownloader { - NSMapTable *downloadInfo; -} - -@end Deleted: trunk/bibdesk/BDSKDownloaderDeprecated.m =================================================================== --- trunk/bibdesk/BDSKDownloaderDeprecated.m 2020-12-02 16:13:28 UTC (rev 25119) +++ trunk/bibdesk/BDSKDownloaderDeprecated.m 2020-12-02 17:04:45 UTC (rev 25120) @@ -1,185 +0,0 @@ -// -// BDSKDownloaderDeprecated.m -// BibDesk -// -// Created by Christiaan on 14/03/2019. -/* - This software is Copyright (c) 2019-2020 - Christiaan Hofman. 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 Christiaan Hofman nor the names of any - 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 "BDSKDownloaderDeprecated.h" - -@interface BDSKDownloadDeprecated : BDSKDownload { - NSURLResponse *response; - NSURL *destinationURL; -} -@property (nonatomic, retain) NSURLResponse *response; -@end - -@implementation BDSKDownloadDeprecated -@synthesize response; -@end - -@interface BDSKDownloaderDeprecated () <NSURLDownloadDelegate> -@end - -@implementation BDSKDownloaderDeprecated - -+ (Class)downloadClass { - return [BDSKDownloadDeprecated class]; -} - -- (BDSKDownload *)startFileDownloadWithRequest:(NSURLRequest *)request delegate:(id <BDSKDownloaderDelegate>)delegate { - NSURLDownload *task = [[[NSURLDownload alloc] initWithRequest:request delegate:self] autorelease]; - return [self addDownloadWithTask:task delegate:delegate]; -} - -- (BDSKDownload *)startDataDownloadWithRequest:(NSURLRequest *)request delegate:(id <BDSKDownloaderDelegate>)delegate { - NSURLConnection *task = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease]; - return [self addDownloadWithTask:task delegate:delegate]; -} - -- (NSData *)downloadDataWithRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error { - return [NSURLConnection sendSynchronousRequest:request returningResponse:response error:error]; -} - -#pragma mark NSURLDownloadDelegate - -- (void)download:(NSURLDownload *)task didReceiveAuthenticationChallenge:(nonnull NSURLAuthenticationChallenge *)challenge { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - - [self _download:download didReceiveChallenge:challenge completionHandler:^(BDSKDownloaderAuthChallengeDisposition disposition, NSURLCredential *credential){ - id <NSURLAuthenticationChallengeSender> sender = [challenge sender]; - switch (disposition) { - case BDSKDownloaderAuthChallengeUseCredential: - [sender useCredential:credential forAuthenticationChallenge:challenge]; - break; - case BDSKDownloaderAuthChallengePerformDefaultHandling: - [sender performDefaultHandlingForAuthenticationChallenge:challenge]; - break; - case BDSKDownloaderAuthChallengeCancelAuthenticationChallenge: - [sender cancelAuthenticationChallenge:challenge]; - break; - case BDSKDownloaderAuthChallengeRejectProtectionSpace: - [sender rejectProtectionSpaceAndContinueWithChallenge:challenge]; - break; - } - }]; -} - -- (void)download:(NSURLDownload *)task decideDestinationWithSuggestedFilename:(NSString *)suggestedFileName { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download decideDestinationWithSuggestedFilename:suggestedFileName completionHandler:^(NSURL *destinationURL, BOOL allowOverwrite){ - [task setDestination:[destinationURL path] allowOverwrite:allowOverwrite]; - }]; -} - -- (void)download:(NSURLDownload *)task didCreateDestination:(NSString *)path { - BDSKDownloadDeprecated *download = (BDSKDownloadDeprecated *)[[[self downloadForTask:task] retain] autorelease]; - [self _download:download didCreateDestination:[NSURL fileURLWithPath:path isDirectory:NO]]; -} - -- (void)download:(NSURLDownload *)task didReceiveResponse:(NSURLResponse *)response { - BDSKDownloadDeprecated *download = (BDSKDownloadDeprecated *)[[[self downloadForTask:task] retain] autorelease]; - [download setResponse:response]; - [self _download:download didReceiveExpectedContentLength:[response expectedContentLength]]; -} - -- (void)download:(NSURLDownload *)task didReceiveDataOfLength:(NSUInteger)length { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - if (length >= 0) { - [self _download:download didReceiveData:nil ofLength:(uint64_t)length]; - } -} - -- (void)downloadDidFinish:(NSURLDownload *)task -{ - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download didFinishWithData:nil]; - [self cleanupDownload:download]; -} - -- (void)download:(NSURLDownload *)task didFailWithError:(NSError *)error -{ - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download didFailWithError:error]; - [self cleanupDownload:download]; -} - -#pragma mark NSURLConnectionDelegate - -- (void)connection:(NSURLConnection *)task didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - - [self _download:download didReceiveChallenge:challenge completionHandler:^(BDSKDownloaderAuthChallengeDisposition disposition, NSURLCredential *credential){ - id <NSURLAuthenticationChallengeSender> sender = [challenge sender]; - switch (disposition) { - case BDSKDownloaderAuthChallengeUseCredential: - [sender useCredential:credential forAuthenticationChallenge:challenge]; - break; - case BDSKDownloaderAuthChallengePerformDefaultHandling: - [sender performDefaultHandlingForAuthenticationChallenge:challenge]; - break; - case BDSKDownloaderAuthChallengeCancelAuthenticationChallenge: - [sender cancelAuthenticationChallenge:challenge]; - break; - case BDSKDownloaderAuthChallengeRejectProtectionSpace: - [sender rejectProtectionSpaceAndContinueWithChallenge:challenge]; - break; - } - }]; -} - -- (void)connection:(NSURLConnection *)task didReceiveResponse:(NSURLResponse *)response { - BDSKDownloadDeprecated *download = (BDSKDownloadDeprecated *)[[[self downloadForTask:task] retain] autorelease]; - [self _download:download didReceiveExpectedContentLength:[response expectedContentLength]]; - [download setResponse:response]; -} - -- (void)connection:(NSURLConnection *)task didReceiveData:(NSData *)data { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download didReceiveData:data ofLength:[data length]]; -} - -- (void)connectionDidFinishLoading:(NSURLConnection *)task { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download didFinishWithData:[download data]]; - [self cleanupDownload:download]; -} - -- (void)connection:(NSURLConnection *)task didFailWithError:(NSError *)error { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download didFailWithError:error]; - [self cleanupDownload:download]; -} - -@end Deleted: trunk/bibdesk/BDSKDownloaderSession.h =================================================================== --- trunk/bibdesk/BDSKDownloaderSession.h 2020-12-02 16:13:28 UTC (rev 25119) +++ trunk/bibdesk/BDSKDownloaderSession.h 2020-12-02 17:04:45 UTC (rev 25120) @@ -1,50 +0,0 @@ -// -// BDSKDownloaderSession.h -// BibDesk -// -// Created by Christiaan on 14/03/2019. -/* - This software is Copyright (c) 2019-2020 - Christiaan Hofman. 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 Christiaan Hofman nor the names of any - 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 <Cocoa/Cocoa.h> -#import "BDSKDownloader.h" - -@class NSURLSession, NSURLSessionTask; - -@interface BDSKDownloaderSession : BDSKDownloader { - NSURLSession *session; - NSMapTable *delegates; - NSMutableSet *receivedExpectedBytes; -} - -@end Deleted: trunk/bibdesk/BDSKDownloaderSession.m =================================================================== --- trunk/bibdesk/BDSKDownloaderSession.m 2020-12-02 16:13:28 UTC (rev 25119) +++ trunk/bibdesk/BDSKDownloaderSession.m 2020-12-02 17:04:45 UTC (rev 25120) @@ -1,202 +0,0 @@ -// -// BDSKDownloaderSession.m -// BibDesk -// -// Created by Christiaan on 14/03/2019. -/* - This software is Copyright (c) 2019-2020 - Christiaan Hofman. 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 Christiaan Hofman nor the names of any - 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 "BDSKDownloaderSession.h" -#import "NSFileManager_BDSKExtensions.h" -#import "NSURLSession_BDSKForwardDeclarations.h" - - -@interface BDSKDownloadSession : BDSKDownload { - BOOL receivedExpectedBytes; -} -@property (nonatomic) BOOL receivedExpectedBytes; -@end - -@implementation BDSKDownloadSession - -@synthesize receivedExpectedBytes; - -- (NSURLResponse *)response { - return [[self task] response]; -} - -@end - -#pragma mark - - -@interface BDSKDownloaderSession () <NSURLSessionDelegate> -@end - -@implementation BDSKDownloaderSession - -+ (Class)downloadClass { - return [BDSKDownloadSession class]; -} - -- (NSURLSession *)session { - if (session == nil) { - session = [[NSURLSession - sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] - delegate:self - delegateQueue:[NSOperationQueue mainQueue]] retain]; - } - return session; -} - -- (BDSKDownload *)startFileDownloadWithRequest:(NSURLRequest *)request delegate:(id <BDSKDownloaderDelegate>)delegate { - NSURLSessionTask *task = [[self session] downloadTaskWithRequest:request]; - BDSKDownload *download = [self addDownloadWithTask:task delegate:delegate]; - [task resume]; - return download; -} - -- (BDSKDownload *)startDataDownloadWithRequest:(NSURLRequest *)request delegate:(id <BDSKDownloaderDelegate>)delegate { - NSURLSessionTask *task = [[self session] dataTaskWithRequest:request]; - BDSKDownload *download = [self addDownloadWithTask:task delegate:delegate]; - [task resume]; - return download; -} - -- (NSData *)downloadDataWithRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error { - __block NSURLResponse *returnResponse = nil; - __block NSError *returnError = nil; - __block NSData *returnData = nil; - __block BOOL downloadFinished = NO; - - [[[self session] dataTaskWithRequest:request completionHandler:^(NSData *aData, NSURLResponse *aResponse, NSError *aError){ - if (aData) { - returnData = [aData retain]; - if (request) - returnResponse = [aResponse retain]; - } else if (error) { - returnError = [aError retain]; - } - downloadFinished = YES; - }] resume]; - - NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; - while (downloadFinished == NO && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]); - - if (response) - *response = [returnResponse autorelease]; - if (error) - *error = [returnError autorelease]; - - return [returnData autorelease]; -} - -#pragma mark NSURLSessionDownloadTaskDelegate - -- (void)URLSession:(NSURLSession *)aSession downloadTask:(NSURLSessionDownloadTask *)task didFinishDownloadingToURL:(NSURL *)location { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - NSString *suggestedFileName = [[task response] suggestedFilename] ?: [location lastPathComponent]; - - [self _download:download decideDestinationWithSuggestedFilename:suggestedFileName completionHandler:^(NSURL *destinationURL, BOOL allowOverwrite){ - NSError *error = nil; - NSFileManager *fm = [NSFileManager defaultManager]; - if ([destinationURL checkResourceIsReachableAndReturnError:NULL]) { - if (allowOverwrite) { - [fm removeItemAtURL:destinationURL error:NULL]; - } else { - destinationURL = [fm uniqueFileURL:destinationURL]; - } - } else if ([[destinationURL URLByDeletingLastPathComponent] checkResourceIsReachableAndReturnError:NULL] == NO) { - [fm createDirectoryAtPath:[[destinationURL URLByDeletingLastPathComponent] path] withIntermediateDirectories:YES attributes:nil error:NULL]; - } - if ([fm moveItemAtURL:location toURL:destinationURL error:&error]) { - [self _download:download didCreateDestination:destinationURL]; - [self _download:download didFinishWithData:nil]; - } else { - [self _download:download didFailWithError:error]; - [self cleanupDownload:download]; - } - }]; -} - -- (void)URLSession:(NSURLSession *)aSession downloadTask:(NSURLSessionDownloadTask *)task didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { - BDSKDownloadSession *download = (BDSKDownloadSession *)[[[self downloadForTask:task] retain] autorelease]; - if ([download receivedExpectedBytes] == NO) { - [download setReceivedExpectedBytes:YES]; - [self _download:download didReceiveExpectedContentLength:totalBytesExpectedToWrite]; - } - - if (bytesWritten >= 0) { - [self _download:download didReceiveData:nil ofLength:(uint64_t)bytesWritten]; - } -} - -#pragma mark NSURLSessionDataTaskDelegate - -- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download didReceiveExpectedContentLength:[response expectedContentLength]]; - // the delegeate may have cancelled the task, in that case don't call the callback - if ([self downloadForTask:task]) - completionHandler(NSURLSessionResponseAllow); -} - -- (void)URLSession:(NSURLSession *)aSession dataTask:(NSURLSessionDataTask *)task didReceiveData:(NSData *)data { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download didReceiveData:data ofLength:[data length]]; -} - -#pragma mark NSURLSessionTaskDelegate - -- (void)URLSession:(NSURLSession *)aSession task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - [self _download:download didReceiveChallenge:challenge completionHandler:(void (^)(BDSKDownloaderAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler]; -} - -- (void)URLSession:(NSURLSession *)aSession task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { - BDSKDownload *download = [[[self downloadForTask:task] retain] autorelease]; - if (error) { - [self _download:download didFailWithError:error]; - } else if ([task isKindOfClass:[NSURLSessionDataTask class]]) { - [self _download:download didFinishWithData:[download data]]; - } - [self cleanupDownload:download]; -} - -#pragma mark NSURLSessionDelegate - -// we need to implement this to avoid it being forwarded to the task specific delegate method -- (void)URLSession:(NSURLSession *)aSession didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { - completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); -} - -@end Modified: trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj =================================================================== --- trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj 2020-12-02 16:13:28 UTC (rev 25119) +++ trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj 2020-12-02 17:04:45 UTC (rev 25120) @@ -514,10 +514,6 @@ CE6775170A8506A7003CFC58 /* NSDictionary_BDSKExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = CE6775150A8506A7003CFC58 /* NSDictionary_BDSKExtensions.m */; }; CE6958F6223A9B94001239A6 /* BDSKDownloader.h in Headers */ = {isa = PBXBuildFile; fileRef = CE6958F4223A9B94001239A6 /* BDSKDownloader.h */; }; CE6958F7223A9B94001239A6 /* BDSKDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = CE6958F5223A9B94001239A6 /* BDSKDownloader.m */; }; - CE69590F223A9BBE001239A6 /* BDSKDownloaderDeprecated.h in Headers */ = {isa = PBXBuildFile; fileRef = CE69590D223A9BBE001239A6 /* BDSKDownloaderDeprecated.h */; }; - CE695910223A9BBE001239A6 /* BDSKDownloaderDeprecated.m in Sources */ = {isa = PBXBuildFile; fileRef = CE69590E223A9BBE001239A6 /* BDSKDownloaderDeprecated.m */; }; - CE695913223A9BD9001239A6 /* BDSKDownloaderSession.h in Headers */ = {isa = PBXBuildFile; fileRef = CE695911223A9BD9001239A6 /* BDSKDownloaderSession.h */; }; - CE695914223A9BD9001239A6 /* BDSKDownloaderSession.m in Sources */ = {isa = PBXBuildFile; fileRef = CE695912223A9BD9001239A6 /* BDSKDownloaderSession.m */; }; CE6C04450BEDFA2D007BF0B5 /* NSParagraphStyle_BDSKExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = CE6C04430BEDFA2D007BF0B5 /* NSParagraphStyle_BDSKExtensions.m */; }; CE6CF99B2038413C000B73D3 /* BDSKNumdamParser.m in Sources */ = {isa = PBXBuildFile; fileRef = CE6CF9992038413C000B73D3 /* BDSKNumdamParser.m */; }; CE6DA03211052C81001CD28E /* BDSKFormatStringFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = CE6DA03011052C81001CD28E /* BDSKFormatStringFormatter.m */; }; @@ -1523,10 +1519,6 @@ CE6775150A8506A7003CFC58 /* NSDictionary_BDSKExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSDictionary_BDSKExtensions.m; sourceTree = "<group>"; }; CE6958F4223A9B94001239A6 /* BDSKDownloader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BDSKDownloader.h; sourceTree = "<group>"; }; CE6958F5223A9B94001239A6 /* BDSKDownloader.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BDSKDownloader.m; sourceTree = "<group>"; }; - CE69590D223A9BBE001239A6 /* BDSKDownloaderDeprecated.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BDSKDownloaderDeprecated.h; sourceTree = "<group>"; }; - CE69590E223A9BBE001239A6 /* BDSKDownloaderDeprecated.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BDSKDownloaderDeprecated.m; sourceTree = "<group>"; }; - CE695911223A9BD9001239A6 /* BDSKDownloaderSession.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BDSKDownloaderSession.h; sourceTree = "<group>"; }; - CE695912223A9BD9001239A6 /* BDSKDownloaderSession.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BDSKDownloaderSession.m; sourceTree = "<group>"; }; CE6C04420BEDFA2D007BF0B5 /* NSParagraphStyle_BDSKExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSParagraphStyle_BDSKExtensions.h; sourceTree = "<group>"; }; CE6C04430BEDFA2D007BF0B5 /* NSParagraphStyle_BDSKExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSParagraphStyle_BDSKExtensions.m; sourceTree = "<group>"; }; CE6CF9982038413C000B73D3 /* BDSKNumdamParser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BDSKNumdamParser.h; sourceTree = "<group>"; }; @@ -2841,8 +2833,6 @@ children = ( 6CD26A220F928EEE0089FDFD /* BDSKBibDeskProtocol.m */, CE6958F5223A9B94001239A6 /* BDSKDownloader.m */, - CE69590E223A9BBE001239A6 /* BDSKDownloaderDeprecated.m */, - CE695912223A9BD9001239A6 /* BDSKDownloaderSession.m */, CE044A4312667EB500CE55C4 /* BDSKDownloadManager.m */, CE17A30822CA42D9002B64F8 /* BDSKItemDownload.m */, CEB4F47F22428F23008240EE /* BDSKSoapBinding.m */, @@ -3037,8 +3027,6 @@ CE7B88902045664200D6A648 /* BDSKDOIWebParser.h */, CE51616A22CD4E7D00832F3E /* BDSKDownloadCommand.h */, CE6958F4223A9B94001239A6 /* BDSKDownloader.h */, - CE69590D223A9BBE001239A6 /* BDSKDownloaderDeprecated.h */, - CE695911223A9BD9001239A6 /* BDSKDownloaderSession.h */, CE044A4212667EB500CE55C4 /* BDSKDownloadManager.h */, CEEC70DF093B6EC200A64F54 /* BDSKDragImageView.h */, CE26481A0B5AB4B700D4B5B8 /* BDSKDragTextField.h */, @@ -3413,7 +3401,6 @@ CE2A0AAF22459A4100A8F31C /* BibDocument_UI.h in Headers */, CE2A09BA2245997A00A8F31C /* BDSKArxivParser.h in Headers */, CE2A0A5A22459A0A00A8F31C /* BDSKSciFinderParser.h in Headers */, - CE695913223A9BD9001239A6 /* BDSKDownloaderSession.h in Headers */, CE2A0A05224599E900A8F31C /* BDSKFindController.h in Headers */, CE2A0A2D224599EF00A8F31C /* BDSKMARCParser.h in Headers */, CE2A0A6D22459A0A00A8F31C /* BDSKServerInfo.h in Headers */, @@ -3511,7 +3498,6 @@ CE2A0A0E224599E900A8F31C /* BDSKGroup.h in Headers */, CE2A09D5224599B300A8F31C /* BDSKComplexStringEditor.h in Headers */, CE2A09FC224599E100A8F31C /* BDSKFileMigrationController.h in Headers */, - CE69590F223A9BBE001239A6 /* BDSKDownloaderDeprecated.h in Headers */, CE2A0AC022459A4B00A8F31C /* CFString_BDSKExtensions.h in Headers */, CE2A0ABC22459A4500A8F31C /* BibPref_General.h in Headers */, CE2A0AAC22459A4100A8F31C /* BibDocument_Menus.h in Headers */, @@ -4575,7 +4561,6 @@ CD6295381140DE1C002E4751 /* BDSKSpringerParser.m in Sources */, CD86BA3A1141C023005EEFDA /* BDSKBibTeXWebParser.m in Sources */, CEFAD8BF1150F7A800A3D969 /* BDSKFontWell.m in Sources */, - CE695914223A9BD9001239A6 /* BDSKDownloaderSession.m in Sources */, CE17832E1158F0A000B2EBDF /* BDSKBookmarkSheetController.m in Sources */, CEF5366B1192EFE400027C3C /* BDSKNotesOutlineView.m in Sources */, CD50E5FB11A2190E00848756 /* BDSKJSTORWebParser.m in Sources */, @@ -4601,7 +4586,6 @@ 3B91DD54162225850088206D /* WokSearchService.m in Sources */, CEC46E311629743200B51A9D /* NSPasteboard_BDSKExtensions.m in Sources */, CE2153D416B169A4004EB59A /* BDSKAlias.m in Sources */, - CE695910223A9BBE001239A6 /* BDSKDownloaderDeprecated.m in Sources */, CECBFA45216124AD007565DE /* BDSKTypeInfo.m in Sources */, CE280D4816C6C51600344CC8 /* BDSKSaveAccessoryViewController.m in Sources */, CEBF7AE818F2EB6300012ECD /* WokSearchLiteService.m in Sources */, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit