Skip to content

Commit 8047f2c

Browse files
charlotteliangCorrob
authored andcommitted
Remove reflection call to get checkin info from InstanceID (#2825)
1 parent bb111fb commit 8047f2c

20 files changed

+94
-346
lines changed

Example/Messaging/Tests/FIRMessagingClientTest.m

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
#import <XCTest/XCTest.h>
1818

19+
#import <FirebaseInstanceID/FIRInstanceID_Private.h>
1920
#import <OCMock/OCMock.h>
2021

2122
#import "Protos/GtalkCore.pbobjc.h"
2223

23-
#import "FIRMessagingCheckinService.h"
2424
#import "FIRMessagingClient.h"
2525
#import "FIRMessagingConnection.h"
2626
#import "FIRMessagingDataMessageManager.h"
@@ -44,9 +44,9 @@
4444
static NSString *const kFIRMessagingAppIDToken = @"1234xyzdef56789";
4545
static NSString *const kTopicToSubscribeTo = @"/topics/abcdef/hello-world";
4646

47-
@interface FIRMessagingRegistrar ()
47+
@interface FIRInstanceID (exposedForTests)
4848

49-
@property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
49+
+ (FIRInstanceID *)instanceIDForTests;
5050

5151
@end
5252

@@ -91,6 +91,8 @@ @interface FIRMessagingClientTest : XCTestCase
9191
@property(nonatomic, readwrite, strong) id mockClientDelegate;
9292
@property(nonatomic, readwrite, strong) id mockDataMessageManager;
9393
@property(nonatomic, readwrite, strong) id mockRegistrar;
94+
@property(nonatomic, readwrite, strong) id mockInstanceID;
95+
9496

9597
// argument callback blocks
9698
@property(nonatomic, readwrite, copy) FIRMessagingConnectCompletionHandler connectCompletion;
@@ -131,6 +133,7 @@ - (void)tearDown {
131133
- (void)tearDownMocksAndHandlers {
132134
self.connectCompletion = nil;
133135
self.subscribeCompletion = nil;
136+
[self.mockInstanceID stopMocking];
134137
}
135138

136139
- (void)setupConnectionWithFakeLoginResult:(BOOL)loginResult
@@ -296,12 +299,10 @@ - (void)setupFakeConnectionWithClass:(Class)connectionClass
296299
}
297300

298301
- (void)addFIRMessagingPreferenceKeysToUserDefaults {
299-
id mockCheckinService = OCMClassMock([FIRMessagingCheckinService class]);
300-
[[[mockCheckinService stub] andReturn:kDeviceAuthId] deviceAuthID];
301-
[[[mockCheckinService stub] andReturn:kSecretToken] secretToken];
302-
[[[mockCheckinService stub] andReturnValue:@YES] hasValidCheckinInfo];
303-
304-
[[[self.mockRegistrar stub] andReturn:mockCheckinService] checkinService];
302+
self.mockInstanceID = OCMPartialMock([FIRInstanceID instanceIDForTests]);
303+
OCMStub([self.mockInstanceID tryToLoadValidCheckinInfo]).andReturn(YES);
304+
OCMStub([self.mockInstanceID deviceAuthID]).andReturn(kDeviceAuthId);
305+
OCMStub([self.mockInstanceID secretToken]).andReturn(kSecretToken);
305306
}
306307

307308
@end

Example/Messaging/Tests/FIRMessagingRegistrarTest.m

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#import <OCMock/OCMock.h>
2121

22-
#import "FIRMessagingCheckinService.h"
2322
#import "FIRMessagingPubSubRegistrar.h"
2423
#import "FIRMessagingRegistrar.h"
2524
#import "FIRMessagingUtilities.h"
@@ -38,15 +37,13 @@
3837
@interface FIRMessagingRegistrar ()
3938

4039
@property(nonatomic, readwrite, strong) FIRMessagingPubSubRegistrar *pubsubRegistrar;
41-
@property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
4240

4341
@end
4442

4543
@interface FIRMessagingRegistrarTest : XCTestCase
4644

4745
@property(nonatomic, readwrite, strong) FIRMessagingRegistrar *registrar;
4846
@property(nonatomic, readwrite, strong) id mockRegistrar;
49-
@property(nonatomic, readwrite, strong) id mockCheckin;
5047
@property(nonatomic, readwrite, strong) id mockPubsubRegistrar;
5148

5249
@end
@@ -57,15 +54,11 @@ - (void)setUp {
5754
[super setUp];
5855
_registrar = [[FIRMessagingRegistrar alloc] init];
5956
_mockRegistrar = OCMPartialMock(_registrar);
60-
_mockCheckin = OCMPartialMock(_registrar.checkinService);
61-
_registrar.checkinService = _mockCheckin;
6257
_registrar.pubsubRegistrar = OCMClassMock([FIRMessagingPubSubRegistrar class]);
6358
_mockPubsubRegistrar = _registrar.pubsubRegistrar;
6459
}
6560

6661
- (void)testUpdateSubscriptionWithValidCheckinData {
67-
[self stubCheckinService];
68-
6962
[self.registrar updateSubscriptionToTopic:kTopicToSubscribeTo
7063
withToken:kFIRMessagingAppIDToken
7164
options:nil
@@ -81,8 +74,6 @@ - (void)testUpdateSubscriptionWithValidCheckinData {
8174
}
8275

8376
- (void)testUpdateSubscription {
84-
[self stubCheckinService];
85-
8677
__block FIRMessagingTopicOperationCompletion pubsubCompletion;
8778
[[[self.mockPubsubRegistrar stub] andDo:^(NSInvocation *invocation) {
8879
pubsubCompletion(nil);
@@ -102,26 +93,4 @@ - (void)testUpdateSubscription {
10293
XCTAssertNil(error);
10394
}];
10495
}
105-
106-
- (void)testFailedUpdateSubscriptionWithNoCheckin {
107-
// Mock checkin service to always return NO for hasValidCheckinInfo
108-
[[[self.mockCheckin stub] andReturnValue:@NO] hasValidCheckinInfo];
109-
// This should not create a network request since we don't have checkin info
110-
[self.registrar updateSubscriptionToTopic:kTopicToSubscribeTo
111-
withToken:kFIRMessagingAppIDToken
112-
options:nil
113-
shouldDelete:NO
114-
handler:^(NSError *error) {
115-
XCTAssertNotNil(error);
116-
}];
117-
}
118-
119-
#pragma mark - Private Helpers
120-
121-
- (void)stubCheckinService {
122-
[[[self.mockCheckin stub] andReturn:kDeviceAuthId] deviceAuthID];
123-
[[[self.mockCheckin stub] andReturn:kSecretToken] secretToken];
124-
[[[self.mockCheckin stub] andReturnValue:@YES] hasValidCheckinInfo];
125-
}
126-
12796
@end

Firebase/InstanceID/FIRInstanceID+Private.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
// TODO(chliangGoogle) Rename this to Internal.
2525
@interface FIRInstanceID (Private)
2626

27-
/**
28-
* Return the cached checkin preferences on the disk. This is used internally only by Messaging.
29-
*
30-
* @return The cached checkin preferences on the client.
31-
*/
32-
- (nullable FIRInstanceIDCheckinPreferences *)cachedCheckinPreferences;
33-
3427
/**
3528
* Fetches checkin info for the app. If the app has valid cached checkin preferences
3629
* they are returned instead of making a network request.

Firebase/InstanceID/FIRInstanceID+Private.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ @interface FIRInstanceID ()
2929

3030
@implementation FIRInstanceID (Private)
3131

32-
- (FIRInstanceIDCheckinPreferences *)cachedCheckinPreferences {
33-
return [self.tokenManager.authService checkinPreferences];
34-
}
35-
3632
// This method just wraps our pre-configured auth service to make the request.
3733
// This method is only needed by first-party users, like Remote Config.
3834
- (void)fetchCheckinInfoWithHandler:(FIRInstanceIDDeviceCheckinCompletion)handler {

Firebase/InstanceID/FIRInstanceID.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#import <GoogleUtilities/GULAppEnvironmentUtil.h>
2525
#import "FIRInstanceID+Private.h"
2626
#import "FIRInstanceIDAuthService.h"
27+
#import "FIRInstanceIDCheckinPreferences.h"
2728
#import "FIRInstanceIDCombinedHandler.h"
2829
#import "FIRInstanceIDConstants.h"
2930
#import "FIRInstanceIDDefines.h"
@@ -611,6 +612,26 @@ - (void)deleteIdentityWithHandler:(FIRInstanceIDDeleteHandler)handler {
611612
}];
612613
}
613614

615+
#pragma mark - Checkin
616+
617+
- (BOOL)tryToLoadValidCheckinInfo {
618+
FIRInstanceIDCheckinPreferences *checkinPreferences =
619+
[self.tokenManager.authService checkinPreferences];
620+
return [checkinPreferences hasValidCheckinInfo];
621+
}
622+
623+
- (NSString *)deviceAuthID {
624+
return [self.tokenManager.authService checkinPreferences].deviceID;
625+
}
626+
627+
- (NSString *)secretToken {
628+
return [self.tokenManager.authService checkinPreferences].secretToken;
629+
}
630+
631+
- (NSString *)versionInfo {
632+
return [self.tokenManager.authService checkinPreferences].versionInfo;
633+
}
634+
614635
#pragma mark - Config
615636

616637
+ (void)load {

Firebase/InstanceID/FIRInstanceIDCheckinPreferences.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
@property(nonatomic, readonly, copy) NSString *deviceDataVersion;
3737
@property(nonatomic, readonly, copy) NSString *digest;
3838
@property(nonatomic, readonly, copy) NSString *versionInfo;
39-
@property(nonatomic, readonly, strong) NSMutableDictionary *gServicesData;
4039
@property(nonatomic, readonly, assign) int64_t lastCheckinTimestampMillis;
4140

4241
/**

Firebase/InstanceID/Private/FIRInstanceID_Private.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@
1616

1717
#import <FirebaseInstanceID/FIRInstanceID.h>
1818

19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
@class FIRInstanceIDCheckinPreferences;
1922
/**
2023
* Private API used by other Firebase SDKs.
2124
*/
2225
@interface FIRInstanceID ()
2326

27+
@property(nonatomic, readonly, strong) NSString *deviceAuthID;
28+
@property(nonatomic, readonly, strong) NSString *secretToken;
29+
@property(nonatomic, readonly, strong) NSString *versionInfo;
30+
2431
/**
2532
* Private initializer.
2633
*/
27-
- (nonnull instancetype)initPrivately;
34+
- (instancetype)initPrivately;
2835

2936
/**
3037
* Returns a Firebase Messaging scoped token for the firebase app.
@@ -34,4 +41,27 @@
3441
*/
3542
- (nullable NSString *)token;
3643

44+
/**
45+
* Verify if valid checkin preferences have been loaded in memory.
46+
*
47+
* @return YES if valid checkin preferences exist in memory else NO.
48+
*/
49+
- (BOOL)hasValidCheckinInfo;
50+
51+
/**
52+
* Try to load prefetched checkin preferences from the cache. This supports the use case where
53+
* InstanceID library has already obtained a valid checkin and we should be using that.
54+
*
55+
* This should be used as a last gasp effort to retreive any cached checkin preferences before
56+
* hitting the FIRMessaging backend to retrieve new preferences.
57+
*
58+
* Note this is only required because InstanceID and FIRMessaging both require checkin preferences
59+
* which need to be synced with each other.
60+
*
61+
* @return YES if successfully loaded cached checkin preferences into memory else NO.
62+
*/
63+
- (BOOL)tryToLoadValidCheckinInfo;
64+
3765
@end
66+
67+
NS_ASSUME_NONNULL_END

Firebase/Messaging/FIRMessagingCheckinService.h

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)