Files
firebase-ios-sdk/FirebaseSegmentation/Sources/SEGContentManager.m
T
69bfe3fd3c Merge FLoC SDK into master branch. (#6466)
* Create Segmentation SDK structure for source and unit tests. (#3214)

* Create Segmentation SDK structure for source and unit tests.

* Review changes: Remove unnecessary files in test folder. Add md-floc-master to CI

* Rename Segmentation directory to FirebaseSegmentation directory. Update podspec to include search header path.

* Add core support with interop for Segmentation SDK.  (#3430)

* Add core support with interop for Segmentation SDK. Also update headers to be under sources folder.

* Review fixes.

* Minor changes.

* Fix style.

* Fix style.

* Style changes.

* Fix whitespace in travis.yml

* Fix style.

* Travis CI is stuck..try updating the travis.yml

* Undo travis.yml change.

* Working drop of Segmentation SDK along with test app and unit tests. (#4574)

* Working drop of Segmentation SDK along with sample app and unit tests.

* Update if_changed.sh to include FirebaseSegmentation.

* Complete NS_ASSUME_NON_NULL_START with NS_ASSUME_NON_NULL_END in header file.

* Fix unit tests.

* Fix style.

* Fixes after running XCode's static analyzer.

* Fix style.

* fix style.

* 'pod lib lint' fixes.

* Fix analyzer errors.

* Address review comments.

* Minor changes for review comments.

* Address review comments.

* Address review comments.

* stop mocking in tear down method for tests.

* Minor update to sample app project.

* Fix trailing whitespace in Podfile.

* Add set -x to check.sh

* update segmetation dependency version

* migrate FloC SDK to depend on FIS SDK directly

* format floc

* format

* using customized FIRapp

* remove test plist file

* refactor to capture weakself

* format

* replace partial mock with class mock

* minor refactoring

* use subscript to manipulate dictionary instance

* fix import error

* minor refoctoring, addressing comments

* address comments

* fix configurations

* format

Co-authored-by: dmandar <dmandar@users.noreply.github.com>
Co-authored-by: ChaoqunCHEN <cqchen93@gmail.com>
2020-10-01 15:10:32 -07:00

173 lines
7.5 KiB
Objective-C

// Copyright 2019 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "FirebaseSegmentation/Sources/SEGContentManager.h"
#import "FirebaseCore/Sources/Private/FIRAppInternal.h"
#import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FirebaseInstallations.h"
#import "FirebaseSegmentation/Sources/Public/FIRSegmentation.h"
#import "FirebaseSegmentation/Sources/SEGDatabaseManager.h"
#import "FirebaseSegmentation/Sources/SEGNetworkManager.h"
#import "FirebaseSegmentation/Sources/SEGSegmentationConstants.h"
NSString *const kErrorDescription = @"ErrorDescription";
@interface SEGContentManager () {
NSMutableDictionary<NSString *, id> *_associationData;
NSString *_installationIdentifier;
NSString *_installationIdentifierToken;
SEGDatabaseManager *_databaseManager;
SEGNetworkManager *_networkManager;
}
@end
@implementation SEGContentManager
+ (instancetype)sharedInstanceWithOptions:(FIROptions *)options {
static dispatch_once_t onceToken;
static SEGContentManager *sharedInstance;
dispatch_once(&onceToken, ^{
sharedInstance = [[SEGContentManager alloc]
initWithDatabaseManager:[SEGDatabaseManager sharedInstance]
networkManager:[[SEGNetworkManager alloc] initWithOptions:options]];
});
return sharedInstance;
}
- (instancetype)initWithDatabaseManager:databaseManager networkManager:networkManager {
self = [super init];
if (self) {
// Initialize the database manager.
_databaseManager = databaseManager;
// Initialize the network manager.
_networkManager = networkManager;
// Load all data from the database.
[_databaseManager createOrOpenDatabaseWithCompletion:^(BOOL success, NSDictionary *result) {
self->_associationData = [result mutableCopy];
}];
// TODO(dmandar) subscribe to FIS notifications once integrated.
}
return self;
}
- (FIRInstallations *)installationForApp:(NSString *)firebaseApp {
return [FIRInstallations installationsWithApp:[FIRApp appNamed:firebaseApp]];
}
- (void)associateCustomInstallationIdentiferNamed:(NSString *)customInstallationID
firebaseApp:(NSString *)firebaseApp
completion:(SEGRequestCompletion)completionHandler {
// Get the latest installation identifier
FIRInstallations *installation = [self installationForApp:firebaseApp];
if (installation == nil) {
completionHandler(NO, @{kErrorDescription : @"Firebase Installations SDK not available"});
}
__weak SEGContentManager *weakSelf = self;
[installation
installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
SEGContentManager *strongSelf = weakSelf;
if (!strongSelf) {
completionHandler(NO, @{kErrorDescription : @"Internal Error getting installation ID."});
return;
}
[strongSelf associateInstallationWithLatestIdentifier:identifier
installation:installation
customizedIdentifier:customInstallationID
firebaseApp:firebaseApp
error:error
completion:completionHandler];
}];
}
- (void)associateInstallationWithLatestIdentifier:(NSString *_Nullable)identifier
installation:(FIRInstallations *)installation
customizedIdentifier:(NSString *)customInstallationID
firebaseApp:(NSString *)firebaseApp
error:(NSError *_Nullable)error
completion:(SEGRequestCompletion)completionHandler {
if (!identifier || error) {
NSString *errorMessage = @"Error getting installation ID.";
if (error) {
errorMessage = [errorMessage stringByAppendingString:error.description];
}
NSDictionary *errorDictionary = @{kErrorDescription : errorMessage};
completionHandler(NO, errorDictionary);
return;
}
_installationIdentifier = identifier;
__weak SEGContentManager *weakSelf = self;
[installation authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
NSError *_Nullable error) {
SEGContentManager *strongSelf = weakSelf;
if (!strongSelf) {
completionHandler(NO, @{kErrorDescription : @"Internal Error getting installation token."});
return;
}
[strongSelf associateInstallationWithToken:tokenResult
customizedIdentifier:customInstallationID
firebaseApp:firebaseApp
error:error
completion:completionHandler];
}];
}
- (void)associateInstallationWithToken:(FIRInstallationsAuthTokenResult *_Nullable)tokenResult
customizedIdentifier:(NSString *)customInstallationID
firebaseApp:(NSString *)firebaseApp
error:(NSError *_Nullable)error
completion:(SEGRequestCompletion)completionHandler {
if (!tokenResult || error) {
NSString *errorMessage = @"Error getting AuthToken.";
if (error) {
errorMessage = [errorMessage stringByAppendingString:error.description];
}
NSDictionary *errorDictionary = @{kErrorDescription : errorMessage};
completionHandler(NO, errorDictionary);
return;
}
_installationIdentifierToken = tokenResult.authToken;
NSMutableDictionary<NSString *, NSString *> *appAssociationData =
[[NSMutableDictionary alloc] init];
appAssociationData[kSEGCustomInstallationIdentifierKey] = customInstallationID;
appAssociationData[kSEGFirebaseInstallationIdentifierKey] = _installationIdentifier;
appAssociationData[kSEGAssociationStatusKey] = kSEGAssociationStatusPending;
_associationData[firebaseApp] = appAssociationData;
// Update the database async.
// TODO(mandard) The database write and corresponding completion handler needs to be wired up
// once we support listening to FID changes.
[_databaseManager insertMainTableApplicationNamed:firebaseApp
customInstanceIdentifier:customInstallationID
firebaseInstanceIdentifier:_installationIdentifier
associationStatus:kSEGAssociationStatusPending
completionHandler:nil];
// Send the change up to the backend. Also add the token.
[_networkManager
makeAssociationRequestToBackendWithData:appAssociationData
token:_installationIdentifierToken
completion:^(BOOL status, NSDictionary<NSString *, id> *result) {
// TODO: log, update database.
completionHandler(status, result);
}];
}
@end