FPRNSURLSessionDelegate.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. FPRLogWarning(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. [trace checkpointState:FPRNetworkTraceCheckpointStateRequestCompleted];
  39. }
  40. } @catch (NSException *exception) {
  41. FPRLogWarning(kFPRNetworkTraceNotTrackable, @"Unable to track network request.");
  42. }
  43. }
  44. - (void)URLSession:(NSURLSession *)session
  45. dataTask:(NSURLSessionDataTask *)dataTask
  46. didReceiveData:(NSData *)data {
  47. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:dataTask];
  48. [trace didReceiveData:data];
  49. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  50. }
  51. - (void)URLSession:(NSURLSession *)session
  52. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  53. didFinishDownloadingToURL:(NSURL *)location {
  54. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:downloadTask];
  55. [trace didReceiveFileURL:location];
  56. [trace didCompleteRequestWithResponse:downloadTask.response error:downloadTask.error];
  57. [FPRNetworkTrace removeNetworkTraceFromObject:downloadTask];
  58. }
  59. - (void)URLSession:(NSURLSession *)session
  60. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  61. didWriteData:(int64_t)bytesWritten
  62. totalBytesWritten:(int64_t)totalBytesWritten
  63. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  64. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:downloadTask];
  65. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  66. trace.responseSize = totalBytesWritten;
  67. if (totalBytesWritten >= totalBytesExpectedToWrite) {
  68. if ([downloadTask.response isKindOfClass:[NSHTTPURLResponse class]]) {
  69. NSHTTPURLResponse *response = (NSHTTPURLResponse *)downloadTask.response;
  70. [trace didCompleteRequestWithResponse:response error:downloadTask.error];
  71. [FPRNetworkTrace removeNetworkTraceFromObject:downloadTask];
  72. }
  73. }
  74. }
  75. @end