incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [6/7] ios commit: [CB-836] Storing connection delegates for aborting connections quicker
Date Fri, 21 Sep 2012 03:15:05 GMT
[CB-836] Storing connection delegates for aborting connections quicker


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/7c4fa19d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/7c4fa19d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/7c4fa19d

Branch: refs/heads/master
Commit: 7c4fa19d5988037b1a69b406d337d9a7e0158e36
Parents: de69937
Author: Cory Thompson <coryjthompson@gmail.com>
Authored: Wed Aug 22 21:04:17 2012 +1000
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Thu Sep 20 22:34:27 2012 -0400

----------------------------------------------------------------------
 CordovaLib/Classes/CDVFileTransfer.h |    2 +
 CordovaLib/Classes/CDVFileTransfer.m |   66 +++++++++++++++-------------
 2 files changed, 37 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/7c4fa19d/CordovaLib/Classes/CDVFileTransfer.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVFileTransfer.h b/CordovaLib/Classes/CDVFileTransfer.h
index 67e3bd0..63be7d6 100755
--- a/CordovaLib/Classes/CDVFileTransfer.h
+++ b/CordovaLib/Classes/CDVFileTransfer.h
@@ -53,6 +53,7 @@ extern NSString* const kOptionsKeyCookie;
                                   AndSource:(NSString*)source 
                                   AndTarget:(NSString*)target 
                                   AndHttpStatus:(int)httpStatus;
+@property (nonatomic, copy) NSMutableDictionary* activeTransfers;
 @end
 
 
@@ -62,6 +63,7 @@ extern NSString* const kOptionsKeyCookie;
 @property (strong) NSMutableData* responseData; // atomic
 @property (nonatomic, strong) CDVFileTransfer* command;
 @property (nonatomic, assign) CDVFileTransferDirection direction;
+@property (nonatomic, strong) NSURLConnection* connection;
 @property (nonatomic, copy) NSString* callbackId;
 @property (nonatomic, copy) NSString* objectId;
 @property (nonatomic, copy) NSString* source;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/7c4fa19d/CordovaLib/Classes/CDVFileTransfer.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVFileTransfer.m b/CordovaLib/Classes/CDVFileTransfer.m
index 773d34f..65fc01e 100755
--- a/CordovaLib/Classes/CDVFileTransfer.m
+++ b/CordovaLib/Classes/CDVFileTransfer.m
@@ -63,8 +63,8 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
{
     return totalBytesWritten;
 }
 
-static NSMutableArray* _abortTriggered = nil;
 @implementation CDVFileTransfer
+@synthesize activeTransfers;
 
 - (NSString*) escapePathComponentForUrlString:(NSString*)urlString
 {
@@ -270,19 +270,25 @@ static NSMutableArray* _abortTriggered = nil;
     // fileData and req are split into helper functions to ease the unit testing of delegateForUpload.
     NSData* fileData = [self fileDataForUploadCommand:command];
     NSURLRequest* req = [self requestForUploadCommand:command fileData:fileData];
-    if (req != nil) {
-        CDVFileTransferDelegate* delegate = [self delegateForUploadCommand:command];
-        [NSURLConnection connectionWithRequest:req delegate:delegate];
+    if (req == nil) {
+        return;
+    }
+    CDVFileTransferDelegate* delegate = [self delegateForUploadCommand:command];
+    [NSURLConnection connectionWithRequest:req delegate:delegate];
+
+    if (activeTransfers == nil) {
+        activeTransfers = [[NSMutableDictionary alloc] init];
     }
+    
+    [activeTransfers setObject:delegate forKey:delegate.objectId];
 }
 
 - (void) abort:(CDVInvokedUrlCommand*)command {
     NSString* objectId = [command.arguments objectAtIndex:0];
-    if(_abortTriggered == nil){
-        _abortTriggered = [NSMutableArray array];
-    }
-    [_abortTriggered addObject:objectId];
->>>>>>> Abort functionality added to FileTransfer
+    
+    CDVFileTransferDelegate *delegate = [activeTransfers objectForKey:objectId];
+    [delegate.connection cancel];
+    [activeTransfers removeObjectForKey:objectId];
 }
 
 - (void) download:(CDVInvokedUrlCommand*)command {
@@ -329,7 +335,14 @@ static NSMutableArray* _abortTriggered = nil;
     delegate.source = sourceUrl;
     delegate.target = filePath;
 	
-	[NSURLConnection connectionWithRequest:req delegate:delegate];
+    delegate.connection = [NSURLConnection connectionWithRequest:req delegate:delegate];
+    [activeTransfers setObject: delegate forKey:delegate.objectId];
+    
+    if (activeTransfers == nil) {
+        activeTransfers = [[NSMutableDictionary alloc] init];
+    }
+    
+    [activeTransfers setObject:delegate forKey:delegate.objectId];
 }
 
 -(NSMutableDictionary*) createFileTransferError:(int)code AndSource:(NSString*)source AndTarget:(NSString*)target
@@ -363,7 +376,7 @@ static NSMutableArray* _abortTriggered = nil;
 
 @implementation CDVFileTransferDelegate
 
-@synthesize callbackId, source, target, responseData, command, bytesWritten, direction, responseCode,
objectId;
+@synthesize callbackId, connection, source, target, responseData, command, bytesWritten,
direction, responseCode, objectId;
 
 
 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
@@ -379,7 +392,7 @@ static NSMutableArray* _abortTriggered = nil;
     CDVFile * file;
     
     NSLog(@"File Transfer Finished with response code %d", self.responseCode);
-        
+         
     if(self.direction == CDV_TRANSFER_UPLOAD)
     {
         if(self.responseCode >= 200 && self.responseCode < 300)
@@ -443,6 +456,9 @@ static NSMutableArray* _abortTriggered = nil;
     } else {
         [self.command writeJavascript:[result toErrorCallbackString: callbackId]];
     }
+    
+    //remove connection for activeTransfers
+    [command.activeTransfers removeObjectForKey:objectId];
 }
 
 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
@@ -455,37 +471,25 @@ static NSMutableArray* _abortTriggered = nil;
 {
     CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary:
[command createFileTransferError: CONNECTION_ERR AndSource:source AndTarget:target AndHttpStatus:
self.responseCode]];
     NSLog(@"File Transfer Error: %@", [error localizedDescription]);
+    
+    //remove connection for activeTransfers
+    [command.activeTransfers removeObjectForKey:objectId];
+    
     [self.command writeJavascript:[result toErrorCallbackString: callbackId]];
 }
 
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
-    if([_abortTriggered containsObject:self.objectId])
-    {
-        NSLog(@"Aborted File Transfer");
-        [connection cancel];
-        [_abortTriggered removeObject:self.objectId];
-        return;
-    }
-    [self.responseData appendData:data];
+        [self.responseData appendData:data];
 }
 - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
 {
-    if([_abortTriggered containsObject:self.objectId])
-    {
-        NSLog(@"Aborted File Transfer");
-        [connection cancel];
-        [_abortTriggered removeObject:self.objectId];
-        return;
-    }
-    
     NSMutableDictionary* uploadProgress = [NSMutableDictionary dictionaryWithCapacity:3];
-    CDVPluginResult* result;
-    
+   
     [uploadProgress setObject:[NSNumber numberWithBool: true] forKey:@"lengthComputable"];
     [uploadProgress setObject:[NSNumber numberWithInt: totalBytesWritten] forKey:@"loaded"];
     [uploadProgress setObject:[NSNumber numberWithInt: totalBytesExpectedToWrite] forKey:@"total"];
-    result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary:
uploadProgress ];
+    CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary:
uploadProgress ];
     [result setKeepCallbackAsBool:true]; 
     [self.command writeJavascript:[result toSuccessCallbackString: callbackId ]];
     


Mime
View raw message