FPRNSURLSessionDelegate.m 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebasePerformance/Sources/Instrumentation/Network/Delegates/FPRNSURLSessionDelegate.h"
  15. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  16. #import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
  17. @implementation FPRNSURLSessionDelegate
  18. - (void)URLSession:(NSURLSession *)session
  19. task:(NSURLSessionTask *)task
  20. didCompleteWithError:(NSError *)error {
  21. @try {
  22. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:task];
  23. [trace didCompleteRequestWithResponse:task.response error:error];
  24. [FPRNetworkTrace removeNetworkTraceFromObject:task];
  25. } @catch (NSException *exception) {
  26. FPRLogInfo(kFPRNetworkTraceNotTrackable, @"Unable to track network request.");
  27. }
  28. }
  29. - (void)URLSession:(NSURLSession *)session
  30. task:(NSURLSessionTask *)task
  31. didSendBodyData:(int64_t)bytesSent
  32. totalBytesSent:(int64_t)totalBytesSent
  33. totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  34. @try {
  35. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:task];
  36. trace.requestSize = totalBytesSent;
  37. if (totalBytesSent >= totalBytesExpectedToSend) {
  38. if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
  39. NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
  40. [trace didCompleteRequestWithResponse:response error:task.error];
  41. [FPRNetworkTrace removeNetworkTraceFromObject:task];
  42. }
  43. }
  44. } @catch (NSException *exception) {
  45. FPRLogInfo(kFPRNetworkTraceNotTrackable, @"Unable to track network request.");
  46. }
  47. }
  48. - (void)URLSession:(NSURLSession *)session
  49. dataTask:(NSURLSessionDataTask *)dataTask
  50. didReceiveData:(NSData *)data {
  51. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:dataTask];
  52. [trace didReceiveData:data];
  53. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  54. }
  55. - (void)URLSession:(NSURLSession *)session
  56. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  57. didFinishDownloadingToURL:(NSURL *)location {
  58. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:downloadTask];
  59. [trace didReceiveFileURL:location];
  60. [trace didCompleteRequestWithResponse:downloadTask.response error:downloadTask.error];
  61. [FPRNetworkTrace removeNetworkTraceFromObject:downloadTask];
  62. }
  63. - (void)URLSession:(NSURLSession *)session
  64. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  65. didWriteData:(int64_t)bytesWritten
  66. totalBytesWritten:(int64_t)totalBytesWritten
  67. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  68. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:downloadTask];
  69. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  70. trace.responseSize = totalBytesWritten;
  71. if (totalBytesWritten >= totalBytesExpectedToWrite) {
  72. if ([downloadTask.response isKindOfClass:[NSHTTPURLResponse class]]) {
  73. NSHTTPURLResponse *response = (NSHTTPURLResponse *)downloadTask.response;
  74. [trace didCompleteRequestWithResponse:response error:downloadTask.error];
  75. [FPRNetworkTrace removeNetworkTraceFromObject:downloadTask];
  76. }
  77. }
  78. }
  79. @end