FPRNSURLConnectionDelegate.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  47. trace.requestSize = totalBytesWritten;
  48. }
  49. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  50. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  51. [trace didCompleteRequestWithResponse:nil error:nil];
  52. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  53. }
  54. - (void)connection:(NSURLConnection *)connection
  55. didWriteData:(long long)bytesWritten
  56. totalBytesWritten:(long long)totalBytesWritten
  57. expectedTotalBytes:(long long)expectedTotalBytes {
  58. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  59. trace.requestSize = totalBytesWritten;
  60. }
  61. - (void)connectionDidFinishDownloading:(NSURLConnection *)connection
  62. destinationURL:(NSURL *)destinationURL {
  63. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  64. [trace didReceiveFileURL:destinationURL];
  65. [trace didCompleteRequestWithResponse:nil error:nil];
  66. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  67. }
  68. @end