Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.fxa.activities; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.UnsupportedEncodingException; |
michael@0 | 8 | import java.util.concurrent.CountDownLatch; |
michael@0 | 9 | |
michael@0 | 10 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 11 | import org.mozilla.gecko.background.fxa.FxAccountClient; |
michael@0 | 12 | import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate; |
michael@0 | 13 | import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse; |
michael@0 | 14 | import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException; |
michael@0 | 15 | import org.mozilla.gecko.background.fxa.PasswordStretcher; |
michael@0 | 16 | import org.mozilla.gecko.fxa.activities.FxAccountSetupTask.InnerRequestDelegate; |
michael@0 | 17 | |
michael@0 | 18 | import android.content.Context; |
michael@0 | 19 | import android.os.AsyncTask; |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * An <code>AsyncTask</code> wrapper around signing up for, and signing in to, a |
michael@0 | 23 | * Firefox Account. |
michael@0 | 24 | * <p> |
michael@0 | 25 | * It's strange to add explicit blocking to callback-threading code, but we do |
michael@0 | 26 | * it here to take advantage of Android's built in support for background work. |
michael@0 | 27 | * We really want to avoid making a threading mistake that brings down the whole |
michael@0 | 28 | * process. |
michael@0 | 29 | */ |
michael@0 | 30 | abstract class FxAccountSetupTask<T> extends AsyncTask<Void, Void, InnerRequestDelegate<T>> { |
michael@0 | 31 | private static final String LOG_TAG = FxAccountSetupTask.class.getSimpleName(); |
michael@0 | 32 | |
michael@0 | 33 | public interface ProgressDisplay { |
michael@0 | 34 | public void showProgress(); |
michael@0 | 35 | public void dismissProgress(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | protected final Context context; |
michael@0 | 39 | protected final FxAccountClient client; |
michael@0 | 40 | protected final ProgressDisplay progressDisplay; |
michael@0 | 41 | |
michael@0 | 42 | // Initialized lazily. |
michael@0 | 43 | protected byte[] quickStretchedPW; |
michael@0 | 44 | |
michael@0 | 45 | // AsyncTask's are one-time-use, so final members are fine. |
michael@0 | 46 | protected final CountDownLatch latch = new CountDownLatch(1); |
michael@0 | 47 | protected final InnerRequestDelegate<T> innerDelegate = new InnerRequestDelegate<T>(latch); |
michael@0 | 48 | |
michael@0 | 49 | protected final RequestDelegate<T> delegate; |
michael@0 | 50 | |
michael@0 | 51 | public FxAccountSetupTask(Context context, ProgressDisplay progressDisplay, FxAccountClient client, RequestDelegate<T> delegate) { |
michael@0 | 52 | this.context = context; |
michael@0 | 53 | this.client = client; |
michael@0 | 54 | this.delegate = delegate; |
michael@0 | 55 | this.progressDisplay = progressDisplay; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | @Override |
michael@0 | 59 | protected void onPreExecute() { |
michael@0 | 60 | if (progressDisplay != null) { |
michael@0 | 61 | progressDisplay.showProgress(); |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | @Override |
michael@0 | 66 | protected void onPostExecute(InnerRequestDelegate<T> result) { |
michael@0 | 67 | if (progressDisplay != null) { |
michael@0 | 68 | progressDisplay.dismissProgress(); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | // We are on the UI thread, and need to invoke these callbacks here to allow UI updating. |
michael@0 | 72 | if (innerDelegate.failure != null) { |
michael@0 | 73 | delegate.handleFailure(innerDelegate.failure); |
michael@0 | 74 | } else if (innerDelegate.exception != null) { |
michael@0 | 75 | delegate.handleError(innerDelegate.exception); |
michael@0 | 76 | } else { |
michael@0 | 77 | delegate.handleSuccess(result.response); |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | @Override |
michael@0 | 82 | protected void onCancelled(InnerRequestDelegate<T> result) { |
michael@0 | 83 | if (progressDisplay != null) { |
michael@0 | 84 | progressDisplay.dismissProgress(); |
michael@0 | 85 | } |
michael@0 | 86 | delegate.handleError(new IllegalStateException("Task was cancelled.")); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | protected static class InnerRequestDelegate<T> implements RequestDelegate<T> { |
michael@0 | 90 | protected final CountDownLatch latch; |
michael@0 | 91 | public T response = null; |
michael@0 | 92 | public Exception exception = null; |
michael@0 | 93 | public FxAccountClientRemoteException failure = null; |
michael@0 | 94 | |
michael@0 | 95 | protected InnerRequestDelegate(CountDownLatch latch) { |
michael@0 | 96 | this.latch = latch; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | @Override |
michael@0 | 100 | public void handleError(Exception e) { |
michael@0 | 101 | Logger.error(LOG_TAG, "Got exception."); |
michael@0 | 102 | this.exception = e; |
michael@0 | 103 | latch.countDown(); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | @Override |
michael@0 | 107 | public void handleFailure(FxAccountClientRemoteException e) { |
michael@0 | 108 | Logger.warn(LOG_TAG, "Got failure."); |
michael@0 | 109 | this.failure = e; |
michael@0 | 110 | latch.countDown(); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | @Override |
michael@0 | 114 | public void handleSuccess(T result) { |
michael@0 | 115 | Logger.info(LOG_TAG, "Got success."); |
michael@0 | 116 | this.response = result; |
michael@0 | 117 | latch.countDown(); |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | public static class FxAccountCreateAccountTask extends FxAccountSetupTask<LoginResponse> { |
michael@0 | 122 | private static final String LOG_TAG = FxAccountCreateAccountTask.class.getSimpleName(); |
michael@0 | 123 | |
michael@0 | 124 | protected final byte[] emailUTF8; |
michael@0 | 125 | protected final PasswordStretcher passwordStretcher; |
michael@0 | 126 | |
michael@0 | 127 | public FxAccountCreateAccountTask(Context context, ProgressDisplay progressDisplay, String email, PasswordStretcher passwordStretcher, FxAccountClient client, RequestDelegate<LoginResponse> delegate) throws UnsupportedEncodingException { |
michael@0 | 128 | super(context, progressDisplay, client, delegate); |
michael@0 | 129 | this.emailUTF8 = email.getBytes("UTF-8"); |
michael@0 | 130 | this.passwordStretcher = passwordStretcher; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | @Override |
michael@0 | 134 | protected InnerRequestDelegate<LoginResponse> doInBackground(Void... arg0) { |
michael@0 | 135 | try { |
michael@0 | 136 | client.createAccountAndGetKeys(emailUTF8, passwordStretcher, innerDelegate); |
michael@0 | 137 | latch.await(); |
michael@0 | 138 | return innerDelegate; |
michael@0 | 139 | } catch (Exception e) { |
michael@0 | 140 | Logger.error(LOG_TAG, "Got exception logging in.", e); |
michael@0 | 141 | delegate.handleError(e); |
michael@0 | 142 | } |
michael@0 | 143 | return null; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | public static class FxAccountSignInTask extends FxAccountSetupTask<LoginResponse> { |
michael@0 | 148 | protected static final String LOG_TAG = FxAccountSignInTask.class.getSimpleName(); |
michael@0 | 149 | |
michael@0 | 150 | protected final byte[] emailUTF8; |
michael@0 | 151 | protected final PasswordStretcher passwordStretcher; |
michael@0 | 152 | |
michael@0 | 153 | public FxAccountSignInTask(Context context, ProgressDisplay progressDisplay, String email, PasswordStretcher passwordStretcher, FxAccountClient client, RequestDelegate<LoginResponse> delegate) throws UnsupportedEncodingException { |
michael@0 | 154 | super(context, progressDisplay, client, delegate); |
michael@0 | 155 | this.emailUTF8 = email.getBytes("UTF-8"); |
michael@0 | 156 | this.passwordStretcher = passwordStretcher; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | @Override |
michael@0 | 160 | protected InnerRequestDelegate<LoginResponse> doInBackground(Void... arg0) { |
michael@0 | 161 | try { |
michael@0 | 162 | client.loginAndGetKeys(emailUTF8, passwordStretcher, innerDelegate); |
michael@0 | 163 | latch.await(); |
michael@0 | 164 | return innerDelegate; |
michael@0 | 165 | } catch (Exception e) { |
michael@0 | 166 | Logger.error(LOG_TAG, "Got exception signing in.", e); |
michael@0 | 167 | delegate.handleError(e); |
michael@0 | 168 | } |
michael@0 | 169 | return null; |
michael@0 | 170 | } |
michael@0 | 171 | } |
michael@0 | 172 | } |