diff --git a/Downloading XPC Service/SUXPCURLDownload.m b/Downloading XPC Service/SUXPCURLDownload.m index ebf22340..5d648631 100644 --- a/Downloading XPC Service/SUXPCURLDownload.m +++ b/Downloading XPC Service/SUXPCURLDownload.m @@ -205,8 +205,6 @@ static NSString * const kSUFetchFolderName = @"fetch.XXXXXXXX"; assert(delegate_queue != NULL); _delegateConnection = xpc_connection_create(NULL, delegate_queue); - xpc_connection_set_context(_delegateConnection, delegate_queue); - xpc_connection_set_finalizer_f(_delegateConnection, (xpc_finalizer_t)&dispatch_release); xpc_connection_set_event_handler(_delegateConnection, ^(xpc_object_t event) { xpc_type_t type = xpc_get_type(event); @@ -232,9 +230,6 @@ static NSString * const kSUFetchFolderName = @"fetch.XXXXXXXX"; free(queue_name); xpc_connection_set_target_queue(peer, peer_event_queue); - xpc_connection_set_context(peer, peer_event_queue); - xpc_connection_set_finalizer_f(peer, (xpc_finalizer_t)&dispatch_release); - xpc_connection_set_event_handler(peer, ^(xpc_object_t nevent) { xpc_type_t ntype = xpc_get_type(nevent); @@ -243,6 +238,8 @@ static NSString * const kSUFetchFolderName = @"fetch.XXXXXXXX"; if (ntype != XPC_TYPE_DICTIONARY) return; + __block xpc_object_t answer = xpc_dictionary_create_reply(nevent); + xpc_dictionary_apply(nevent, ^bool(const char *key, xpc_object_t value) { NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init]; @@ -353,8 +350,6 @@ static NSString * const kSUFetchFolderName = @"fetch.XXXXXXXX"; } else if (0 == strcmp(key, SUDownloadServiceMIMETypeToDecodeKey)) { - xpc_object_t answer = xpc_dictionary_create_reply(nevent); - void (^block)(void) = ^{ BOOL shouldDecode = NO; if ([_delegate respondsToSelector:@selector(download:shouldDecodeSourceDataOfMIMEType:)]) @@ -366,15 +361,15 @@ static NSString * const kSUFetchFolderName = @"fetch.XXXXXXXX"; xpc_dictionary_set_bool(answer, SUDownloadServiceShouldDecodeMIMETypeKey, YES); }; [self performBlock:block onThread:_startedThread synchronous:YES]; - - xpc_connection_send_message(peer, answer); - xpc_release(answer); } [localPool release]; return true; }); + + xpc_connection_send_message(peer, answer); + xpc_release(answer); }); xpc_connection_resume(peer); } diff --git a/Downloading XPC Service/download_service.m b/Downloading XPC Service/download_service.m index e5403cee..3f0edd09 100644 --- a/Downloading XPC Service/download_service.m +++ b/Downloading XPC Service/download_service.m @@ -78,8 +78,9 @@ static void fetch_process_request(xpc_object_t request, xpc_object_t reply) callBacks.downloadDidReceiveData = ^(SUDSDownloader *downloader, NSUInteger dataLength) { xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); xpc_dictionary_set_int64(message, SUDownloadServiceReceivedDataLengthKey, (int64_t)dataLength); - xpc_connection_send_message(connection, message); + xpc_object_t answer = xpc_connection_send_message_with_reply_sync(connection, message); xpc_release(message); + xpc_release(answer); }; callBacks.downloadDidReceiveResponse = ^(SUDSDownloader *downloader, NSURLResponse *response) { @@ -87,22 +88,25 @@ static void fetch_process_request(xpc_object_t request, xpc_object_t reply) xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); xpc_dictionary_set_data(message, SUDownloadServiceReceivedResponseDataKey, [responseData bytes], [responseData length]); - xpc_connection_send_message(connection, message); + xpc_object_t answer = xpc_connection_send_message_with_reply_sync(connection, message); xpc_release(message); + xpc_release(answer); }; callBacks.downloadDidCreateDestination = ^(SUDSDownloader *downloader, NSString *destinationPath) { xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); xpc_dictionary_set_string(message, SUDownloadServiceCreatedDestinationPathKey, [destinationPath cStringUsingEncoding:NSUTF8StringEncoding]); - xpc_connection_send_message(connection, message); + xpc_object_t answer = xpc_connection_send_message_with_reply_sync(connection, message); xpc_release(message); + xpc_release(answer); }; callBacks.downloadDidBegin = ^(SUDSDownloader *downloader) { xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); xpc_dictionary_set_bool(message, SUDownloadServiceDidBeginDownloadingKey, true); - xpc_connection_send_message(connection, message); + xpc_object_t answer = xpc_connection_send_message_with_reply_sync(connection, message); xpc_release(message); + xpc_release(answer); }; callBacks.downloadDidFail = ^(SUDSDownloader *downloader, NSError *error) { @@ -113,15 +117,17 @@ static void fetch_process_request(xpc_object_t request, xpc_object_t reply) xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); xpc_dictionary_set_data(message, SUDownloadServiceReceivedFailErrorKey, [errorData bytes], [errorData length]); - xpc_connection_send_message(connection, message); + xpc_object_t answer = xpc_connection_send_message_with_reply_sync(connection, message); xpc_release(message); + xpc_release(answer); }; callBacks.downloadDidFinish = ^(SUDSDownloader *downloader) { xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); xpc_dictionary_set_bool(message, SUDownloadServiceDidFinishDownloadingKey, true); - xpc_connection_send_message(connection, message); + xpc_object_t answer = xpc_connection_send_message_with_reply_sync(connection, message); xpc_release(message); + xpc_release(answer); }; callBacks.downloadShouldDecodeSourceData = ^BOOL(SUDSDownloader *downloader, NSString *MIMEType) { @@ -130,9 +136,12 @@ static void fetch_process_request(xpc_object_t request, xpc_object_t reply) xpc_object_t answer = xpc_connection_send_message_with_reply_sync(connection, message); xpc_release(message); + BOOL result = NO; if (xpc_get_type(answer) == XPC_TYPE_DICTIONARY) - return (BOOL)xpc_dictionary_get_bool(answer, SUDownloadServiceShouldDecodeMIMETypeKey); - return NO; + result = (BOOL)xpc_dictionary_get_bool(answer, SUDownloadServiceShouldDecodeMIMETypeKey); + xpc_release(answer); + + return result; }; [downloader setCallBacks:callBacks]; @@ -221,8 +230,6 @@ static void fetch_event_handler(xpc_connection_t peer) // Set the target queue for connection. xpc_connection_set_target_queue(peer, peer_event_queue); - xpc_connection_set_context(peer, peer_event_queue); - xpc_connection_set_finalizer_f(peer, (xpc_finalizer_t)&dispatch_release); // Set the handler block for connection. xpc_connection_set_event_handler(peer, ^(xpc_object_t event) {