mobile/android/base/fxa/login/FxAccountLoginStateMachine.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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.login;
michael@0 6
michael@0 7 import java.security.NoSuchAlgorithmException;
michael@0 8 import java.util.HashSet;
michael@0 9 import java.util.Set;
michael@0 10
michael@0 11 import org.mozilla.gecko.background.fxa.FxAccountClient;
michael@0 12 import org.mozilla.gecko.browserid.BrowserIDKeyPair;
michael@0 13 import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.Transition;
michael@0 14 import org.mozilla.gecko.fxa.login.State.StateLabel;
michael@0 15
michael@0 16 public class FxAccountLoginStateMachine {
michael@0 17 public static final String LOG_TAG = FxAccountLoginStateMachine.class.getSimpleName();
michael@0 18
michael@0 19 public interface LoginStateMachineDelegate {
michael@0 20 public FxAccountClient getClient();
michael@0 21 public long getCertificateDurationInMilliseconds();
michael@0 22 public long getAssertionDurationInMilliseconds();
michael@0 23 public void handleTransition(Transition transition, State state);
michael@0 24 public void handleFinal(State state);
michael@0 25 public BrowserIDKeyPair generateKeyPair() throws NoSuchAlgorithmException;
michael@0 26 }
michael@0 27
michael@0 28 public static class ExecuteDelegate {
michael@0 29 protected final LoginStateMachineDelegate delegate;
michael@0 30 protected final StateLabel desiredStateLabel;
michael@0 31 // It's as difficult to detect arbitrary cycles as repeated states.
michael@0 32 protected final Set<StateLabel> stateLabelsSeen = new HashSet<StateLabel>();
michael@0 33
michael@0 34 protected ExecuteDelegate(StateLabel initialStateLabel, StateLabel desiredStateLabel, LoginStateMachineDelegate delegate) {
michael@0 35 this.delegate = delegate;
michael@0 36 this.desiredStateLabel = desiredStateLabel;
michael@0 37 this.stateLabelsSeen.add(initialStateLabel);
michael@0 38 }
michael@0 39
michael@0 40 public FxAccountClient getClient() {
michael@0 41 return delegate.getClient();
michael@0 42 }
michael@0 43
michael@0 44 public long getCertificateDurationInMilliseconds() {
michael@0 45 return delegate.getCertificateDurationInMilliseconds();
michael@0 46 }
michael@0 47
michael@0 48 public long getAssertionDurationInMilliseconds() {
michael@0 49 return delegate.getAssertionDurationInMilliseconds();
michael@0 50 }
michael@0 51
michael@0 52 public BrowserIDKeyPair generateKeyPair() throws NoSuchAlgorithmException {
michael@0 53 return delegate.generateKeyPair();
michael@0 54 }
michael@0 55
michael@0 56 public void handleTransition(Transition transition, State state) {
michael@0 57 // Always trigger the transition callback.
michael@0 58 delegate.handleTransition(transition, state);
michael@0 59
michael@0 60 // Possibly trigger the final callback. We trigger if we're at our desired
michael@0 61 // state, or if we've seen this state before.
michael@0 62 StateLabel stateLabel = state.getStateLabel();
michael@0 63 if (stateLabel == desiredStateLabel || stateLabelsSeen.contains(stateLabel)) {
michael@0 64 delegate.handleFinal(state);
michael@0 65 return;
michael@0 66 }
michael@0 67
michael@0 68 // If this wasn't the last state, leave a bread crumb and move on to the
michael@0 69 // next state.
michael@0 70 stateLabelsSeen.add(stateLabel);
michael@0 71 state.execute(this);
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 public void advance(State initialState, final StateLabel desiredStateLabel, final LoginStateMachineDelegate delegate) {
michael@0 76 if (initialState.getStateLabel() == desiredStateLabel) {
michael@0 77 // We're already where we want to be!
michael@0 78 delegate.handleFinal(initialState);
michael@0 79 return;
michael@0 80 }
michael@0 81 ExecuteDelegate executeDelegate = new ExecuteDelegate(initialState.getStateLabel(), desiredStateLabel, delegate);
michael@0 82 initialState.execute(executeDelegate);
michael@0 83 }
michael@0 84 }

mercurial