FPRInstrumentation.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/FPRInstrumentation.h"
  15. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  16. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/Network/FPRNSURLConnectionInstrument.h"
  18. #import "FirebasePerformance/Sources/Instrumentation/Network/FPRNSURLSessionInstrument.h"
  19. #import "FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.h"
  20. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  21. // The instrumentation group keys.
  22. NSString *const kFPRInstrumentationGroupNetworkKey = @"network";
  23. NSString *const kFPRInstrumentationGroupUIKitKey = @"uikit";
  24. /** Use ivars instead of properties to reduce message sending overhead. */
  25. @interface FPRInstrumentation () {
  26. // A dictionary of the instrument groups.
  27. NSDictionary<NSString *, NSMutableArray *> *_instrumentGroups;
  28. }
  29. /** Registers an instrument in the given group.
  30. *
  31. * @param instrument The instrument to register.
  32. * @param group The group to register the instrument in.
  33. */
  34. - (void)registerInstrument:(FPRInstrument *)instrument group:(NSString *)group;
  35. @end
  36. @implementation FPRInstrumentation
  37. - (instancetype)init {
  38. self = [super init];
  39. if (self) {
  40. _instrumentGroups = @{
  41. kFPRInstrumentationGroupNetworkKey : [[NSMutableArray alloc] init],
  42. kFPRInstrumentationGroupUIKitKey : [[NSMutableArray alloc] init]
  43. };
  44. }
  45. return self;
  46. }
  47. - (void)registerInstrument:(FPRInstrument *)instrument group:(NSString *)group {
  48. FPRAssert(instrument, @"Instrument must be non-nil.");
  49. FPRAssert(_instrumentGroups[group], @"groups and group must be non-nil, and groups[group] must be"
  50. "non-nil.");
  51. if (instrument != nil) {
  52. [_instrumentGroups[group] addObject:instrument];
  53. }
  54. [instrument registerInstrumentors];
  55. }
  56. - (NSUInteger)registerInstrumentGroup:(NSString *)group {
  57. FPRAssert(_instrumentGroups[group], @"The group key does not exist", group);
  58. FPRAssert(_instrumentGroups[group].count == 0, @"This group is already instrumented");
  59. if ([group isEqualToString:kFPRInstrumentationGroupNetworkKey]) {
  60. [self registerInstrument:[[FPRNSURLSessionInstrument alloc] init] group:group];
  61. [self registerInstrument:[[FPRNSURLConnectionInstrument alloc] init] group:group];
  62. }
  63. if ([group isEqualToString:kFPRInstrumentationGroupUIKitKey]) {
  64. [self registerInstrument:[[FPRUIViewControllerInstrument alloc] init] group:group];
  65. }
  66. return _instrumentGroups[group].count;
  67. }
  68. - (BOOL)deregisterInstrumentGroup:(NSString *)group {
  69. FPRAssert(_instrumentGroups[group], @"You're attempting to deregister an invalid group key.");
  70. for (FPRInstrument *instrument in _instrumentGroups[group]) {
  71. [instrument deregisterInstrumentors];
  72. }
  73. [_instrumentGroups[group] removeAllObjects];
  74. return _instrumentGroups[group].count == 0;
  75. }
  76. @end