FPRNSURLConnectionDelegate.m 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/FPRNSURLConnectionDelegate.h"
  15. #import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
  16. @implementation FPRNSURLConnectionDelegate
  17. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  18. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  19. [trace didCompleteRequestWithResponse:nil error:error];
  20. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  21. }
  22. - (NSURLRequest *)connection:(NSURLConnection *)connection
  23. willSendRequest:(nonnull NSURLRequest *)request
  24. redirectResponse:(nullable NSURLResponse *)response {
  25. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  26. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  27. return request;
  28. }
  29. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  30. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  31. if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
  32. trace.responseCode = (int32_t)((NSHTTPURLResponse *)response).statusCode;
  33. }
  34. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  35. }
  36. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  37. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  38. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  39. trace.responseSize += data.length;
  40. }
  41. - (void)connection:(NSURLConnection *)connection
  42. didSendBodyData:(NSInteger)bytesWritten
  43. totalBytesWritten:(NSInteger)totalBytesWritten
  44. totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
  45. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  46. trace.requestSize = totalBytesWritten;
  47. if (totalBytesWritten >= totalBytesExpectedToWrite) {
  48. [trace checkpointState:FPRNetworkTraceCheckpointStateRequestCompleted];
  49. }
  50. }
  51. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  52. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  53. [trace didCompleteRequestWithResponse:nil error:nil];
  54. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  55. }
  56. - (void)connection:(NSURLConnection *)connection
  57. didWriteData:(long long)bytesWritten
  58. totalBytesWritten:(long long)totalBytesWritten
  59. expectedTotalBytes:(long long)expectedTotalBytes {
  60. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  61. trace.requestSize = totalBytesWritten;
  62. }
  63. - (void)connectionDidFinishDownloading:(NSURLConnection *)connection
  64. destinationURL:(NSURL *)destinationURL {
  65. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  66. [trace didReceiveFileURL:destinationURL];
  67. [trace didCompleteRequestWithResponse:nil error:nil];
  68. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  69. }
  70. @end