-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add condition to Selector call #694
Open
sng1996
wants to merge
1
commit into
FLEXTool:master
Choose a base branch
from
sng1996:fix/safe-selector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -589,9 +589,11 @@ + (void)injectIntoNSURLConnectionCancel { | |
|
||
void (^swizzleBlock)(NSURLConnection *) = ^(NSURLConnection *slf) { | ||
[FLEXNetworkObserver.sharedObserver connectionWillCancel:slf]; | ||
((void(*)(id, SEL))objc_msgSend)( | ||
slf, swizzledSelector | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL))objc_msgSend)( | ||
slf, swizzledSelector | ||
); | ||
} | ||
}; | ||
|
||
IMP implementation = imp_implementationWithBlock(swizzleBlock); | ||
|
@@ -680,10 +682,11 @@ + (void)swizzleResumeSelector:(SEL)selector forClass:(Class)class { | |
|
||
[FLEXNetworkObserver.sharedObserver URLSessionTaskWillResume:slf]; | ||
} | ||
|
||
((void(*)(id, SEL))objc_msgSend)( | ||
slf, swizzledSelector | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL))objc_msgSend)( | ||
slf, swizzledSelector | ||
); | ||
} | ||
Comment on lines
-683
to
+689
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
}); | ||
|
||
class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalResume)); | ||
|
@@ -745,13 +748,17 @@ typedef void (^SendAsyncRequestBlock)( | |
completion(response, data, error); | ||
} | ||
}; | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, request, queue, wrapper | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, request, queue, wrapper | ||
); | ||
} | ||
} else { | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, request, queue, completion | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, request, queue, completion | ||
); | ||
} | ||
Comment on lines
-748
to
+761
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These as well, if I am reading correctly and these are on |
||
} | ||
}; | ||
|
||
|
@@ -786,9 +793,12 @@ + (void)injectIntoNSURLConnectionSynchronousClassMethod { | |
[FLEXNetworkRecorder.defaultRecorder recordMechanism:mechanism forRequestID:requestID]; | ||
NSError *temporaryError = nil; | ||
NSURLResponse *temporaryResponse = nil; | ||
data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)( | ||
slf, swizzledSelector, request, &temporaryResponse, &temporaryError | ||
); | ||
|
||
if ([slf respondsToSelector:swizzledSelector]) { | ||
data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)( | ||
slf, swizzledSelector, request, &temporaryResponse, &temporaryError | ||
); | ||
} | ||
|
||
[FLEXNetworkRecorder.defaultRecorder | ||
recordResponseReceivedWithRequestID:requestID | ||
|
@@ -818,9 +828,11 @@ + (void)injectIntoNSURLConnectionSynchronousClassMethod { | |
*response = temporaryResponse; | ||
} | ||
} else { | ||
data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)( | ||
slf, swizzledSelector, request, response, error | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)( | ||
slf, swizzledSelector, request, response, error | ||
); | ||
} | ||
} | ||
|
||
return data; | ||
|
@@ -878,16 +890,21 @@ + (void)injectIntoNSURLSessionAsyncDataAndDownloadTaskMethods:(Class)sessionClas | |
]; | ||
|
||
// Call the original method | ||
task = ((id(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, argument, completionWrapper | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
task = ((id(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, argument, completionWrapper | ||
); | ||
} | ||
[self setRequestID:requestID forConnectionOrTask:task]; | ||
} else { | ||
// Network observer disabled or no callback provided, | ||
// just pass through to the original method | ||
task = ((id(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, argument, completion | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
task = ((id(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, argument, completion | ||
); | ||
} | ||
|
||
} | ||
return task; | ||
}; | ||
|
@@ -942,14 +959,18 @@ + (void)injectIntoNSURLSessionAsyncUploadTaskMethods:(Class)sessionClass { | |
completion:completion | ||
]; | ||
|
||
task = ((id(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, request, argument, completionWrapper | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
task = ((id(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, request, argument, completionWrapper | ||
); | ||
} | ||
[self setRequestID:requestID forConnectionOrTask:task]; | ||
} else { | ||
task = ((id(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, request, argument, completion | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
task = ((id(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, request, argument, completion | ||
); | ||
} | ||
} | ||
return task; | ||
}; | ||
|
@@ -1043,9 +1064,11 @@ + (void)injectWillSendRequestIntoDelegateClass:(Class)cls { | |
[self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, connection, request, response); | ||
} originalImplementationBlock:^{ | ||
returnValue = ((id(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, connection, request, response | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
returnValue = ((id(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, connection, request, response | ||
); | ||
} | ||
}]; | ||
return returnValue; | ||
}; | ||
|
@@ -1087,9 +1110,11 @@ typedef void (^DidReceiveResponseBlock)( | |
[self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, connection, response); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, connection, response | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, connection, response | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1130,9 +1155,11 @@ typedef void (^DidReceiveDataBlock)( | |
[self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, connection, data); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, connection, data | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, connection, data | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1165,9 +1192,11 @@ + (void)injectDidFinishLoadingIntoDelegateClass:(Class)cls { | |
[self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, connection); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id))objc_msgSend)( | ||
slf, swizzledSelector, connection | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id))objc_msgSend)( | ||
slf, swizzledSelector, connection | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1205,9 +1234,11 @@ typedef void (^DidFailWithErrorBlock)( | |
[self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, connection, error); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, connection, error | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, connection, error | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1265,9 +1296,11 @@ typedef void (^HTTPRedirectionBlock)(id<NSURLSessionTaskDelegate> slf, | |
delegate:slf | ||
]; | ||
} originalImplementationBlock:^{ | ||
((id(*)(id, SEL, id, id, id, id, void(^)(NSURLRequest *)))objc_msgSend)( | ||
slf, swizzledSelector, session, task, response, newRequest, completionHandler | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((id(*)(id, SEL, id, id, id, id, void(^)(NSURLRequest *)))objc_msgSend)( | ||
slf, swizzledSelector, session, task, response, newRequest, completionHandler | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1308,9 +1341,11 @@ typedef void (^DidReceiveDataBlock)(id<NSURLSessionDataDelegate> slf, | |
[self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, session, dataTask, data); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, session, dataTask, data | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, session, dataTask, data | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1352,9 +1387,11 @@ typedef void (^DidBecomeDownloadTaskBlock)(id<NSURLSessionDataDelegate> slf, | |
[self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, session, dataTask, downloadTask); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, session, dataTask, downloadTask | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, session, dataTask, downloadTask | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1410,9 +1447,11 @@ typedef void (^DidReceiveResponseBlock)(id<NSURLSessionDelegate> slf, | |
delegate:slf | ||
]; | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id, id, void(^)(NSURLSessionResponseDisposition)))objc_msgSend)( | ||
slf, swizzledSelector, session, dataTask, response, completion | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id, id, void(^)(NSURLSessionResponseDisposition)))objc_msgSend)( | ||
slf, swizzledSelector, session, dataTask, response, completion | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1455,9 +1494,11 @@ typedef void (^DidCompleteWithErrorBlock)(id<NSURLSessionTaskDelegate> slf, | |
[self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, session, task, error); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, session, task, error | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, session, task, error | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1530,9 +1571,11 @@ typedef void (^DidFinishDownloadingBlock)(id<NSURLSessionTaskDelegate> slf, | |
[self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{ | ||
undefinedBlock(slf, session, task, location); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, session, task, location | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id, id))objc_msgSend)( | ||
slf, swizzledSelector, session, task, location | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1586,10 +1629,12 @@ typedef void (^DidWriteDataBlock)(id<NSURLSessionTaskDelegate> slf, | |
totalBytesWritten, totalBytesExpectedToWrite | ||
); | ||
} originalImplementationBlock:^{ | ||
((void(*)(id, SEL, id, id, int64_t, int64_t, int64_t))objc_msgSend)( | ||
slf, swizzledSelector, session, task, bytesWritten, | ||
totalBytesWritten, totalBytesExpectedToWrite | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id, int64_t, int64_t, int64_t))objc_msgSend)( | ||
slf, swizzledSelector, session, task, bytesWritten, | ||
totalBytesWritten, totalBytesExpectedToWrite | ||
); | ||
} | ||
}]; | ||
}; | ||
|
||
|
@@ -1631,9 +1676,11 @@ typedef void (^SendMessageBlock)( | |
} | ||
}; | ||
|
||
((void(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, message, completionHook | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id, id))objc_msgSend)( | ||
slf, swizzledSelector, message, completionHook | ||
); | ||
} | ||
}; | ||
|
||
[FLEXUtility replaceImplementationOfKnownSelector:selector | ||
|
@@ -1663,9 +1710,11 @@ typedef void (^SendMessageBlock)( | |
completion(message, error); | ||
}; | ||
|
||
((void(*)(id, SEL, id))objc_msgSend)( | ||
slf, swizzledSelector, completionHook | ||
); | ||
if ([slf respondsToSelector:swizzledSelector]) { | ||
((void(*)(id, SEL, id))objc_msgSend)( | ||
slf, swizzledSelector, completionHook | ||
); | ||
} | ||
|
||
}; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this check is necessary, since
NSURLConnection
will always implement-cancel
. Do you agree?