Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 123 additions & 74 deletions Classes/Network/PonyDebugger/FLEXNetworkObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Comment on lines -592 to +596
Copy link
Collaborator

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?

};

IMP implementation = imp_implementationWithBlock(swizzleBlock);
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

});

class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalResume));
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These as well, if I am reading correctly and these are on NSURLConnection

}
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
);
}
}];
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}

};

Expand Down