CB-6059 iOS: Add necessary @synchronized blocks for newly introduced multi-threading.

This commit is contained in:
Andrew Grieve
2014-02-19 11:49:24 -05:00
parent 49b4774e7e
commit 0f8467bdfe
+40 -30
View File
@@ -75,6 +75,10 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
@implementation CDVFileTransfer @implementation CDVFileTransfer
@synthesize activeTransfers; @synthesize activeTransfers;
- (void)pluginInitialize {
activeTransfers = [[NSMutableDictionary alloc] init];
}
- (NSString*)escapePathComponentForUrlString:(NSString*)urlString - (NSString*)escapePathComponentForUrlString:(NSString*)urlString
{ {
NSRange schemeAndHostRange = [urlString rangeOfString:@"://.*?/" options:NSRegularExpressionSearch]; NSRange schemeAndHostRange = [urlString rangeOfString:@"://.*?/" options:NSRegularExpressionSearch];
@@ -327,29 +331,34 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
return; return;
} }
CDVFileTransferDelegate* delegate = [self delegateForUploadCommand:command]; CDVFileTransferDelegate* delegate = [self delegateForUploadCommand:command];
[NSURLConnection connectionWithRequest:req delegate:delegate]; delegate.connection = [[NSURLConnection alloc] initWithRequest:req delegate:delegate startImmediately:NO];
if (self.queue == nil) {
self.queue = [[NSOperationQueue alloc] init];
}
[delegate.connection setDelegateQueue:self.queue];
// sets a background task ID for the transfer object. // sets a background task ID for the transfer object.
delegate.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ delegate.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[delegate cancelTransfer:delegate.connection]; [delegate cancelTransfer:delegate.connection];
}]; }];
if (activeTransfers == nil) { @synchronized (activeTransfers) {
activeTransfers = [[NSMutableDictionary alloc] init]; activeTransfers[delegate.objectId] = delegate;
} }
[delegate.connection start];
[activeTransfers setObject:delegate forKey:delegate.objectId];
} }
- (void)abort:(CDVInvokedUrlCommand*)command - (void)abort:(CDVInvokedUrlCommand*)command
{ {
NSString* objectId = [command.arguments objectAtIndex:0]; NSString* objectId = [command.arguments objectAtIndex:0];
CDVFileTransferDelegate* delegate = [activeTransfers objectForKey:objectId]; @synchronized (activeTransfers) {
CDVFileTransferDelegate* delegate = activeTransfers[objectId];
if (delegate != nil) { if (delegate != nil) {
[delegate cancelTransfer:delegate.connection]; [delegate cancelTransfer:delegate.connection];
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createFileTransferError:CONNECTION_ABORTED AndSource:delegate.source AndTarget:delegate.target]]; CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createFileTransferError:CONNECTION_ABORTED AndSource:delegate.source AndTarget:delegate.target]];
[self.commandDelegate sendPluginResult:result callbackId:delegate.callbackId]; [self.commandDelegate sendPluginResult:result callbackId:delegate.callbackId];
}
} }
} }
@@ -419,10 +428,9 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
} }
[delegate.connection setDelegateQueue:self.queue]; [delegate.connection setDelegateQueue:self.queue];
if (activeTransfers == nil) { @synchronized (activeTransfers) {
activeTransfers = [[NSMutableDictionary alloc] init]; activeTransfers[delegate.objectId] = delegate;
} }
[activeTransfers setObject:delegate forKey:delegate.objectId];
[delegate.connection start]; [delegate.connection start];
} }
@@ -467,13 +475,13 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
return result; return result;
} }
- (void)onReset - (void)onReset {
{ @synchronized (activeTransfers) {
for (CDVFileTransferDelegate* delegate in [activeTransfers allValues]) { while ([activeTransfers count] > 0) {
[delegate.connection cancel]; CDVFileTransferDelegate* delegate = [activeTransfers allValues][0];
[delegate cancelTransfer:delegate.connection];
}
} }
[activeTransfers removeAllObjects];
} }
@end @end
@@ -564,11 +572,12 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
[self.command.commandDelegate sendPluginResult:result callbackId:callbackId]; [self.command.commandDelegate sendPluginResult:result callbackId:callbackId];
// remove connection for activeTransfers // remove connection for activeTransfers
[command.activeTransfers removeObjectForKey:objectId]; @synchronized (command.activeTransfers) {
[command.activeTransfers removeObjectForKey:objectId];
// remove background id task in case our upload was done in the background // remove background id task in case our upload was done in the background
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID]; [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
self.backgroundTaskID = UIBackgroundTaskInvalid; self.backgroundTaskID = UIBackgroundTaskInvalid;
}
} }
- (void)removeTargetFile - (void)removeTargetFile
@@ -581,12 +590,13 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
- (void)cancelTransfer:(NSURLConnection*)connection - (void)cancelTransfer:(NSURLConnection*)connection
{ {
[connection cancel]; [connection cancel];
@synchronized (self.command.activeTransfers) {
CDVFileTransferDelegate* delegate = self.command.activeTransfers[self.objectId];
[self.command.activeTransfers removeObjectForKey:self.objectId];
[[UIApplication sharedApplication] endBackgroundTask:delegate.backgroundTaskID];
delegate.backgroundTaskID = UIBackgroundTaskInvalid;
}
CDVFileTransferDelegate* delegate = [self.command.activeTransfers objectForKey:self.objectId];
[[UIApplication sharedApplication] endBackgroundTask:delegate.backgroundTaskID];
delegate.backgroundTaskID = UIBackgroundTaskInvalid;
[self.command.activeTransfers removeObjectForKey:self.objectId];
[self removeTargetFile]; [self removeTargetFile];
} }