Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert NSLog statements from the communication code to NotificationH… #4

Merged
merged 1 commit into from
Feb 20, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions src/ios/BEMCommunicationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "AuthCompletionHandler.h"
#import "BEMConnectionSettings.h"
#import "BEMConstants.h"
#import "LocalNotificationManager.h"

#import <GoogleOpenSource/GoogleOpenSource.h>

Expand Down Expand Up @@ -128,15 +129,17 @@ -(id)initPost:(NSURL*)url data:(NSMutableDictionary*)jsonDict completionHandler:
}

-(void)execute {
NSLog(@"CommunicationHelper.execute called!");
[LocalNotificationManager addNotification:@"CommunicationHelper.execute called!" showUI:FALSE];
// First, we parse the dictionary because we need the data to call the completion function anyway
// Note that this data does not contain the user token, and should not be sent to the server
NSError *parseError;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self.mJsonDict
options:kNilOptions
error:&parseError];
if (parseError != NULL) {
NSLog(@"parseError = %@, calling completion handler", parseError);
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"parseError = %@, calling completion handler",
parseError]];
self.mCompletionHandler(jsonData, NULL, parseError);
return;
}
Expand All @@ -152,29 +155,35 @@ -(void)execute {
// so it is not in memory, and the ID token is not stored in the keychain. It is a real pain to store the ID token
// in the keychain through subclassing, so let's just try to refresh the token anyway
expired = expired || ([AuthCompletionHandler sharedInstance].getIdToken == NULL);
NSLog(@"currAuth = %@, canAuthorize = %@, expiresIn = %@, expirationDate = %@, expired = %@",
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"currAuth = %@, canAuthorize = %@, expiresIn = %@, expirationDate = %@, expired = %@",
currAuth, NSStringFromBOOL(currAuth.canAuthorize), currAuth.expiresIn, currAuth.expirationDate,
NSStringFromBOOL(expired));
NSStringFromBOOL(expired)] showUI:FALSE];
if (currAuth.canAuthorize != YES) {
NSLog(@"Unable to refresh token, trying to re-authenticate");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Unable to refresh token, trying to re-authenticate"] showUI:FALSE];
// Not sure why we would get canAuthorize be null, but I assume that we re-login in that case
[self tryToAuthenticate:jsonData];
} else {
if (expired) {
NSLog(@"Existing auth token expired, refreshing...");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Existing auth token expired, refreshing"] showUI:FALSE];
// Need to refresh the token
[currAuth authorizeRequest:NULL completionHandler:^(NSError *error) {
if (error != NULL) {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Error %@ while refreshing token, need to notify user", error] showUI:FALSE];
// modify some kind of error count and notify that user needs to sign in again
NSLog(@"Error while refreshing token, need to modify error count");
self.mCompletionHandler(jsonData, nil, error);
} else {
NSLog(@"Refresh completion block called, refreshed token is %@", currAuth);
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Refresh completion block called, refreshed token is %@", currAuth] showUI:FALSE];
BOOL stillExpired = ([currAuth.expirationDate compare:[NSDate date]] == NSOrderedAscending);
if (stillExpired) {
// Although we called refresh, the token is still expired. Let's try to call a different
// refresh method
NSLog(@"Existing auth token still expired after first refresh attempt, trying different attempt...");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Existing auth token still expired after first refresh attempt, notifying user"] showUI:FALSE];
/*
[currAuth authorizeRequest:NULL
delegate:self
Expand All @@ -189,14 +198,16 @@ -(void)execute {
NSError *refreshError = [NSError errorWithDomain:errorDomain code:authFailedNeedUserInput userInfo:userInfo];
self.mCompletionHandler(NULL, NULL, refreshError);
} else {
NSLog(@"Refresh is really done, posting to host");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Refresh is realy done, posting to host"] showUI:FALSE];
assert(error == NULL);
[self postToHost];
}
}
}];
} else {
NSLog(@"Existing auth token not expired, posting to host");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Existing auth token not expired, posting to host"] showUI:FALSE];
assert(expired == FALSE);
[self postToHost];
}
Expand All @@ -205,11 +216,13 @@ -(void)execute {
}

- (void)tryToAuthenticate:(NSData*)jsonData {
NSLog(@"tryToAuthenticate called");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"tryToAuthenticate called"] showUI:FALSE];
[[AuthCompletionHandler sharedInstance] registerFinishDelegate:self];
BOOL silentAuthResult = [[AuthCompletionHandler sharedInstance] trySilentAuthentication];
if (silentAuthResult == NO) {
NSLog(@"Need user input for authentication, need to signal user somehow");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Need user input for authentication, need to signal user somehow"] showUI:FALSE];
[[AuthCompletionHandler sharedInstance] unregisterFinishDelegate:self];
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"User authentication failed.", nil),
Expand All @@ -220,6 +233,9 @@ - (void)tryToAuthenticate:(NSData*)jsonData {
NSError *authError = [NSError errorWithDomain:errorDomain code:authFailedNeedUserInput userInfo:userInfo];
self.mCompletionHandler(jsonData, NULL, authError);
} else {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"callback should be called, we will deal with it there"]
showUI:FALSE];
NSLog(@"callback should be called, we will deal with it there");
// So far, callback has not taken a long time...
// But callback may take a long time. In that case, we may want to return early.
Expand All @@ -233,7 +249,8 @@ - (void)tryToAuthenticate:(NSData*)jsonData {
}

- (void)postToHost {
NSLog(@"postToHost called with url = %@", self.mUrl);
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"postToHost called with url = %@", self.mUrl] showUI:FALSE];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:self.mUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:500];
Expand Down Expand Up @@ -286,17 +303,20 @@ - (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
- (void)finishRefreshSelector:(GTMOAuth2Authentication *)auth
request:(NSMutableURLRequest *)request
finishedWithError:(NSError *)error {
NSLog(@"CommunicationHelper.finishRefreshSelector called with auth = %@, request = %@, error = %@",
auth, request, error);
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"CommunicationHelper.finishRefreshSelector called with auth = %@, request = %@, error = %@", auth, request, error] showUI:FALSE];

if (error != NULL) {
self.mCompletionHandler(NULL, NULL, error);
} else {
BOOL stillExpired = ([auth.expirationDate compare:[NSDate date]] == NSOrderedAscending);
if (stillExpired) {
NSLog(@"No more methods to try, I GIVE UP!");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"No more methods to try, I GIVE UP!"] showUI:TRUE];
} else {
// Check to see whether the sharedInstance auth token has been updated
NSLog(@"Auth token in the shared instance is %@", [AuthCompletionHandler sharedInstance].currAuth);
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Auth token in the shared instance is %@, posting to host", [AuthCompletionHandler sharedInstance].currAuth] showUI:FALSE];
[self postToHost];
}
}
Expand Down