Skip to content

Completely rewrite CredentialsApiHelper.java and hopefully fix memory leak #566

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

Merged
merged 11 commits into from
Feb 2, 2017
Merged

Conversation

SUPERCILEX
Copy link
Collaborator

@SUPERCILEX SUPERCILEX commented Feb 1, 2017

@samtstern This should fix the memory leak because we aren't storing the activity anywhere.

#561


public static int getSafeAutoManageId() {
IDS.add(null);
return IDS.size() - 1;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sooooo much better than my previous solution. 😄

* API.
*/
@Deprecated
public class OldCredentialsApiHelper {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to look here, I just copied this from the old CredentialsApiHelper.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pick a better name for this, maybe ActivityCredentialsApiHelper or something. "Old" as a prefix does not help to show what was wrong with it.

Also I'd argue that we should keep this class named CredentialsApiHelper and the new class is the one that should have a new name since it reduces churn.

*/
@Deprecated
public Task<Void> signOut(@NonNull Activity activity) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me so happy: because our new signOut method uses FragmentActivity which extends Activity, people will be automatically upgraded. How's that for customer service! 😄

import java.util.List;

public class GoogleApiHelper {
private static final List<Void> IDS = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would do better as an AtomicInteger and then your static function would just be wrapping incrementAndGet()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samtstern Never knew that existed, sounds awesome!

*/
@Deprecated
public Task<Void> delete(@NonNull Activity activity) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell the difference between delete(FragmentActivity) and delete(Activity) is just that the former uses CredentialApiHelper and the latter uses OldCredentialApiHelper.

Could this all be simplified by having both of the CredentialApiHelper classes implement a common interface? I think it would look like this:

interface CredentialApiHelper {

  Task<Status> delete(Credential credential);

  Task<Status> disableAuthSignIn();

}

And then these two delete() functions would just be a matter of constructing the proper Helper and then calling to a private Task<Void> delete(CredentialApiHelper helper)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samtstern To address this comment and this one, I didn't add an interface or a good name because my intention for us to throw this away in FBUI v2.0. Do you disagree with that? If so, then we should definitely clean things up and maybe undeprecate thoses classes. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.0 could be a while away, let's try and keep things clean in the meantime.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samtstern SGTM, I'll leave things deprecated so we know to remove that stuff if we go that route, but I keep things clean in the meantime.

Signed-off-by: Alex Saveau <[email protected]>
@SUPERCILEX SUPERCILEX changed the title Completely rewrite CredentialsApiHelper.java and hopefully fix memory leak [DON'T MERGE] Completely rewrite CredentialsApiHelper.java and hopefully fix memory leak Feb 1, 2017
Signed-off-by: Alex Saveau <[email protected]>
@SUPERCILEX SUPERCILEX changed the title [DON'T MERGE] Completely rewrite CredentialsApiHelper.java and hopefully fix memory leak Completely rewrite CredentialsApiHelper.java and hopefully fix memory leak Feb 1, 2017
@SUPERCILEX
Copy link
Collaborator Author

@samtstern I'm happy with it now, PTAL.

CredentialApiHelper credentialHelper = CredentialsApiHelper.getInstance(gacHelper);

// Get all SmartLock credentials associated with the user
List<Credential> credentials = SmartLockBase.credentialsFromFirebaseUser(firebaseUser);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From here down to the end of the function seems duplicated between the two delete() methods. Can we abstract into a common private method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, forgot to refactor that.

Signed-off-by: Alex Saveau <[email protected]>
@@ -31,7 +31,8 @@
* A {@link com.google.android.gms.tasks.Task Task} based wrapper for the Smart Lock for Passwords
* API.
*/
public class CredentialsApiHelper {
@Deprecated
public class CredentialsApiHelper implements CredentialApiHelper {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last (annoying) comment: it's pretty confusing to have these two names only differ by an 's' in the middle. Let's change the name of the interface? Maybe CredentialTaskApi or something like that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samtstern SGTM, I just can't wait for us to throw this stuff away anyway.


public Task<Status> signOut() {
final TaskCompletionSource<Status> statusTask = new TaskCompletionSource<>();
getConnectedApiTask().addOnSuccessListener(new OnSuccessListener<Bundle>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this, but shouldn't we also add an onFailureListener to getConnectedApiTask() that propagates the failure to statusTask through setException?

credentialsApiHelper.delete(mCredential)
.addOnCompleteListener(new OnCompleteListener<Status>() {
AuthUI.getInstance()
.delete(getActivity())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now also deletes the underlying Firebase Auth account in addition to deleting the credentials. This has two new properties:

  • Will fail if there is no currently signed in user
  • Will fail if we don't have recent authentication (last 5 minutes)

Did you intend to change this behavior?

Signed-off-by: Alex Saveau <[email protected]>
Signed-off-by: Alex Saveau <[email protected]>
@SUPERCILEX
Copy link
Collaborator Author

@samtstern Sorry about those mistakes, please let me take another look at this PR to make sure I didn't mess up again. And maybe you could take another look too? (I've been distracted by my other PRs 😄)

Signed-off-by: Alex Saveau <[email protected]>
@SUPERCILEX
Copy link
Collaborator Author

@samtstern Ok, I think it's all good now.

@@ -26,7 +25,7 @@ public static GoogleSignInHelper getInstance(FragmentActivity activity) {

public Task<Status> signOut() {
final TaskCompletionSource<Status> statusTask = new TaskCompletionSource<>();
getConnectedApiTask().addOnSuccessListener(new OnSuccessListener<Bundle>() {
getConnectedApiTask().addOnSuccessListener(new ExceptionForwarder<Bundle>(statusTask) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work, it's still setting the ExceptionForwarder using addOnSuccessListener so even though it implements onFailure that will never be called.

You may want to make this an onCompleteListener instance instead so you'll get notified of success or failure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samtstern Today's not my day with tasks! 😵 PTAL at 15fbed3, I'm pretty sure that will work.


public Task<Status> signOut() {
final TaskCompletionSource<Status> statusTask = new TaskCompletionSource<>();
getConnectedApiTask().addOnSuccessListener(new ExceptionForwarder<Bundle>(statusTask) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work, it's still setting the ExceptionForwarder using addOnSuccessListener so even though it implements onFailure that will never be called.

You may want to make this an onCompleteListener instance instead so you'll get notified of success or failure.

Signed-off-by: Alex Saveau <[email protected]>
@samtstern
Copy link
Contributor

Woohoo looks good! I will merge once tests pass.

@SUPERCILEX
Copy link
Collaborator Author

@samtstern OMG finally! Thanks for sticking with me on this one! 😄

@samtstern samtstern merged commit 85d7b70 into firebase:version-1.2.0-dev Feb 2, 2017
@samtstern samtstern added this to the 1.2.0 milestone Feb 2, 2017
@SUPERCILEX SUPERCILEX deleted the asav/#561 branch February 2, 2017 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants