Skip to content

Commit 0707694

Browse files
authored
Make Firebase Performance initialize using Component system. (#10692)
1 parent 9f90bc8 commit 0707694

File tree

3 files changed

+71
-20
lines changed

3 files changed

+71
-20
lines changed

FirebasePerformance/Sources/FPRClient+Private.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
@class FPRConfigurations;
2020
@class FIRInstallations;
2121

22+
/// Protocol to define the Firebase performance provider for the component framework.
23+
@protocol FIRPerformanceProvider <NSObject>
24+
25+
@end
26+
2227
/**
2328
* Extension that is added on top of the class FPRClient to make the private properties visible
2429
* between the implementation file and the unit tests.

FirebasePerformance/Sources/FPRClient.m

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
#import "FirebaseCore/Extension/FirebaseCoreInternal.h"
3737

38-
@interface FPRClient ()
38+
@interface FPRClient () <FIRLibrary, FIRPerformanceProvider>
3939

4040
/** The original configuration object used to initialize the client. */
4141
@property(nonatomic, strong) FPRConfiguration *config;
@@ -48,22 +48,26 @@ @interface FPRClient ()
4848
@implementation FPRClient
4949

5050
+ (void)load {
51-
__weak NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
52-
__block id listener;
53-
54-
void (^observerBlock)(NSNotification *) = ^(NSNotification *aNotification) {
55-
NSDictionary *appInfoDict = aNotification.userInfo;
56-
NSNumber *isDefaultApp = appInfoDict[kFIRAppIsDefaultAppKey];
57-
if (![isDefaultApp boolValue]) {
58-
return;
51+
[FIRApp registerInternalLibrary:[FPRClient class]
52+
withName:@"fire-perf"
53+
withVersion:[NSString stringWithUTF8String:kFPRSDKVersion]];
54+
}
55+
56+
#pragma mark - Component registration system
57+
58+
+ (nonnull NSArray<FIRComponent *> *)componentsToRegister {
59+
FIRComponentCreationBlock creationBlock =
60+
^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
61+
if (!container.app.isDefaultApp) {
62+
return nil;
5963
}
6064

61-
NSString *appName = appInfoDict[kFIRAppNameKey];
65+
NSString *appName = container.app.name;
6266
FIRApp *app = [FIRApp appNamed:appName];
6367
FIROptions *options = app.options;
6468
NSError *error = nil;
6569

66-
// Based on the environment variable SDK decides if events are dispatchd to Autopush or Prod.
70+
// Based on the environment variable SDK decides if events are dispatched to Autopush or Prod.
6771
// By default, events are sent to Prod.
6872
BOOL useAutoPush = NO;
6973
NSDictionary<NSString *, NSString *> *environment = [NSProcessInfo processInfo].environment;
@@ -79,17 +83,18 @@ + (void)load {
7983
FPRLogError(kFPRClientInitialize, @"Failed to initialize the client with error: %@.", error);
8084
}
8185

82-
[notificationCenter removeObserver:listener];
83-
listener = nil;
86+
*isCacheable = YES;
87+
88+
return [self sharedInstance];
8489
};
8590

86-
// Register the Perf library for Firebase Core tracking.
87-
[FIRApp registerLibrary:@"fire-perf" // From go/firebase-sdk-platform-info
88-
withVersion:[NSString stringWithUTF8String:kFPRSDKVersion]];
89-
listener = [notificationCenter addObserverForName:kFIRAppReadyToConfigureSDKNotification
90-
object:[FIRApp class]
91-
queue:nil
92-
usingBlock:observerBlock];
91+
FIRComponent *component =
92+
[FIRComponent componentWithProtocol:@protocol(FIRPerformanceProvider)
93+
instantiationTiming:FIRInstantiationTimingEagerInDefaultApp
94+
dependencies:@[]
95+
creationBlock:creationBlock];
96+
97+
return @[ component ];
9398
}
9499

95100
+ (FPRClient *)sharedInstance {

FirebasePerformance/Tests/Unit/FPRClientTest.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#import <XCTest/XCTest.h>
1616

17+
#import "FirebaseCore/Extension/FirebaseCoreInternal.h"
18+
1719
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
1820
#import "FirebasePerformance/Sources/FPRClient+Private.h"
1921
#import "FirebasePerformance/Sources/FPRClient.h"
@@ -252,4 +254,43 @@ - (void)testValidateClearcutLogDirectoryCleanupIfNotExists {
252254
XCTAssertNoThrow([FPRClient cleanupClearcutCacheDirectory]);
253255
}
254256

257+
#pragma mark - Component Registration Tests
258+
259+
- (void)testRegistersAsLibrary {
260+
// Configure a test FIRApp.
261+
NSString *appName = @"__FIRAPP_DEFAULT";
262+
[FIRApp configureWithName:appName options:[self fakeOptions]];
263+
FIRApp *app = [FIRApp appNamed:appName];
264+
265+
// Attempt to fetch the component and verify it's a valid instance.
266+
id<FIRPerformanceProvider> provider = FIR_COMPONENT(FIRPerformanceProvider, app.container);
267+
XCTAssertNotNil(provider);
268+
269+
// Ensure that the instance that comes from the container is cached.
270+
id<FIRPerformanceProvider> sameProvider = FIR_COMPONENT(FIRPerformanceProvider, app.container);
271+
XCTAssertNotNil(sameProvider);
272+
XCTAssertEqual(provider, sameProvider);
273+
}
274+
275+
- (void)testFailsRegistrationForNonDefaultApp {
276+
// Configure a test FIRApp.
277+
NSString *appName = @"some_random_app";
278+
[FIRApp configureWithName:appName options:[self fakeOptions]];
279+
FIRApp *app = [FIRApp appNamed:appName];
280+
281+
// Attempt to fetch the component and verify it's a valid instance.
282+
id<FIRPerformanceProvider> provider = FIR_COMPONENT(FIRPerformanceProvider, app.container);
283+
XCTAssertNil(provider);
284+
}
285+
286+
#pragma mark - Helpers
287+
288+
- (FIROptions *)fakeOptions {
289+
FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
290+
GCMSenderID:@"correct_gcm_sender_id"];
291+
options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
292+
options.projectID = @"project-id";
293+
return options;
294+
}
295+
255296
@end

0 commit comments

Comments
 (0)