Skip to content

Commit

Permalink
Pass Context through to BaseResource
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jul 19, 2015
1 parent 6ff4359 commit 5f9c832
Show file tree
Hide file tree
Showing 51 changed files with 197 additions and 110 deletions.
12 changes: 8 additions & 4 deletions mobile/android/base/background/bagheera/BagheeraClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package org.mozilla.gecko.background.bagheera;

import android.content.Context;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
Expand Down Expand Up @@ -32,6 +34,7 @@
*/
public class BagheeraClient {

protected final Context mCtx;
protected final String serverURI;
protected final Executor executor;
protected static final Pattern URI_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]+$");
Expand All @@ -51,13 +54,14 @@ public class BagheeraClient {
* @param executor
* the executor which will be used to invoke delegate callbacks.
*/
public BagheeraClient(final String serverURI, final Executor executor) {
public BagheeraClient(final Context ctx, final String serverURI, final Executor executor) {
if (serverURI == null) {
throw new IllegalArgumentException("Must provide a server URI.");
}
if (executor == null) {
throw new IllegalArgumentException("Must provide a non-null executor.");
}
this.mCtx = ctx;
this.serverURI = serverURI.endsWith("/") ? serverURI : serverURI + "/";
this.executor = executor;
}
Expand All @@ -71,8 +75,8 @@ public BagheeraClient(final String serverURI, final Executor executor) {
* @param serverURI
* the destination server URI.
*/
public BagheeraClient(final String serverURI) {
this(serverURI, Executors.newSingleThreadExecutor());
public BagheeraClient(final Context ctx, final String serverURI) {
this(ctx, serverURI, Executors.newSingleThreadExecutor());
}

/**
Expand Down Expand Up @@ -147,7 +151,7 @@ protected BaseResource makeResource(final String namespace, final String id) thr

final String uri = this.serverURI + PROTOCOL_VERSION + SUBMIT_PATH +
namespace + "/" + id;
return new BaseResource(uri);
return new BaseResource(mCtx, uri);
}

public class BagheeraResourceDelegate extends BaseResourceDelegate {
Expand Down
9 changes: 7 additions & 2 deletions mobile/android/base/background/fxa/FxAccountClient10.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import javax.crypto.Mac;

import android.content.Context;

import org.json.simple.JSONObject;
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientMalformedResponseException;
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
Expand Down Expand Up @@ -77,6 +79,8 @@ public class FxAccountClient10 {
protected static final String[] requiredErrorStringFields = { JSON_KEY_ERROR, JSON_KEY_MESSAGE, JSON_KEY_INFO };
protected static final String[] requiredErrorLongFields = { JSON_KEY_CODE, JSON_KEY_ERRNO };

protected final Context mCtx;

/**
* The server's URI.
* <p>
Expand All @@ -87,13 +91,14 @@ public class FxAccountClient10 {

protected final Executor executor;

public FxAccountClient10(String serverURI, Executor executor) {
public FxAccountClient10(Context ctx, String serverURI, Executor executor) {
if (serverURI == null) {
throw new IllegalArgumentException("Must provide a server URI.");
}
if (executor == null) {
throw new IllegalArgumentException("Must provide a non-null executor.");
}
this.mCtx = ctx;
this.serverURI = serverURI.endsWith("/") ? serverURI : serverURI + "/";
if (!this.serverURI.endsWith("/")) {
throw new IllegalArgumentException("Constructed serverURI must end with a trailing slash: " + this.serverURI);
Expand Down Expand Up @@ -140,7 +145,7 @@ protected BaseResource getBaseResource(String path, String... queryParameters) t
sb.append(URLEncoder.encode(val, "UTF-8"));
}
}
return new BaseResource(new URI(sb.toString()));
return new BaseResource(mCtx, new URI(sb.toString()));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions mobile/android/base/background/fxa/FxAccountClient20.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.Map;
import java.util.concurrent.Executor;

import android.content.Context;

import org.json.simple.JSONObject;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
Expand All @@ -22,8 +24,8 @@ public class FxAccountClient20 extends FxAccountClient10 implements FxAccountCli
protected static final String[] LOGIN_RESPONSE_REQUIRED_STRING_FIELDS_KEYS = new String[] { JSON_KEY_UID, JSON_KEY_SESSIONTOKEN, JSON_KEY_KEYFETCHTOKEN, };
protected static final String[] LOGIN_RESPONSE_REQUIRED_BOOLEAN_FIELDS = new String[] { JSON_KEY_VERIFIED };

public FxAccountClient20(String serverURI, Executor executor) {
super(serverURI, executor);
public FxAccountClient20(Context ctx, String serverURI, Executor executor) {
super(ctx, serverURI, executor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.net.URISyntaxException;
import java.util.concurrent.Executor;

import android.content.Context;

import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.net.BaseResource;

Expand Down Expand Up @@ -59,10 +61,10 @@ public AuthorizationResponse(String access_token, String token_type, String scop
}

public void authorization(String client_id, String assertion, String state, String scope,
RequestDelegate<AuthorizationResponse> delegate) {
RequestDelegate<AuthorizationResponse> delegate, Context context) {
final BaseResource resource;
try {
resource = new BaseResource(new URI(serverURI + "authorization"));
resource = new BaseResource(context, new URI(serverURI + "authorization"));
} catch (URISyntaxException e) {
invokeHandleError(delegate, e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.net.URISyntaxException;
import java.util.concurrent.Executor;

import android.content.Context;

import org.mozilla.gecko.background.fxa.oauth.FxAccountAbstractClient;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.net.AuthHeaderProvider;
Expand All @@ -27,10 +29,10 @@ public FxAccountProfileClient10(String serverURI, Executor executor) {
super(serverURI, executor);
}

public void profile(final String token, RequestDelegate<ExtendedJSONObject> delegate) {
public void profile(final String token, RequestDelegate<ExtendedJSONObject> delegate, Context context) {
BaseResource resource;
try {
resource = new BaseResource(new URI(serverURI + "profile"));
resource = new BaseResource(context, new URI(serverURI + "profile"));
} catch (URISyntaxException e) {
invokeHandleError(delegate, e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected JSONObject generateDocument(final long localTime, final long last,
}

protected void uploadPayload(String id, String payload, Collection<String> oldIds, BagheeraRequestDelegate uploadDelegate) {
final BagheeraClient client = new BagheeraClient(getDocumentServerURI());
final BagheeraClient client = new BagheeraClient(context, getDocumentServerURI());

Logger.pii(LOG_TAG, "New health report has id " + id +
"and obsoletes " + (oldIds != null ? Integer.toString(oldIds.size()) : "no") + " old ids.");
Expand Down Expand Up @@ -186,7 +186,7 @@ protected SubmissionsTracker getSubmissionsTracker(final HealthReportStorage sto

@Override
public void delete(final long localTime, final String id, Delegate delegate) {
final BagheeraClient client = new BagheeraClient(getDocumentServerURI());
final BagheeraClient client = new BagheeraClient(context, getDocumentServerURI());

Logger.pii(LOG_TAG, "Deleting health report with id " + id + ".");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package org.mozilla.gecko.browserid.verifier;

import android.content.Context;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -25,12 +27,16 @@ public class BrowserIDRemoteVerifierClient10 extends AbstractBrowserIDRemoteVeri

public static final String DEFAULT_VERIFIER_URL = "https://verifier.login.persona.org/verify";

public BrowserIDRemoteVerifierClient10() throws URISyntaxException {
private final Context mCtx;

public BrowserIDRemoteVerifierClient10(Context ctx) throws URISyntaxException {
super(new URI(DEFAULT_VERIFIER_URL));
mCtx = ctx;
}

public BrowserIDRemoteVerifierClient10(URI verifierUri) {
public BrowserIDRemoteVerifierClient10(Context ctx, URI verifierUri) {
super(verifierUri);
mCtx = ctx;
}

@Override
Expand All @@ -45,7 +51,7 @@ public void verify(String audience, String assertion, final BrowserIDVerifierDel
throw new IllegalArgumentException("delegate cannot be null.");
}

BaseResource r = new BaseResource(verifierUri);
BaseResource r = new BaseResource(mCtx, verifierUri);

r.delegate = new RemoteVerifierResourceDelegate(r, delegate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import android.content.Context;

import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.net.BaseResource;
Expand All @@ -22,12 +23,16 @@ public class BrowserIDRemoteVerifierClient20 extends AbstractBrowserIDRemoteVeri
protected static final String JSON_KEY_ASSERTION = "assertion";
protected static final String JSON_KEY_AUDIENCE = "audience";

public BrowserIDRemoteVerifierClient20() throws URISyntaxException {
private final Context mCtx;

public BrowserIDRemoteVerifierClient20(Context ctx) throws URISyntaxException {
super(new URI(DEFAULT_VERIFIER_URL));
mCtx = ctx;
}

public BrowserIDRemoteVerifierClient20(URI verifierUri) {
public BrowserIDRemoteVerifierClient20(Context ctx, URI verifierUri) {
super(verifierUri);
mCtx = ctx;
}

@Override
Expand All @@ -42,7 +47,7 @@ public void verify(String audience, String assertion, final BrowserIDVerifierDel
throw new IllegalArgumentException("delegate cannot be null.");
}

BaseResource r = new BaseResource(verifierUri);
BaseResource r = new BaseResource(mCtx, verifierUri);
r.delegate = new RemoteVerifierResourceDelegate(r, delegate);

final ExtendedJSONObject requestBody = new ExtendedJSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void handleSuccess(LoginResponse result) {
public void updateCredentials(String email, String password) {
String serverURI = fxAccount.getAccountServerURI();
Executor executor = Executors.newSingleThreadExecutor();
FxAccountClient client = new FxAccountClient20(serverURI, executor);
FxAccountClient client = new FxAccountClient20(this, serverURI, executor);
PasswordStretcher passwordStretcher = makePasswordStretcher(password);
try {
hideRemoteError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public void handleFailure(FxAccountClientRemoteException e) {
};

Executor executor = Executors.newSingleThreadExecutor();
FxAccountClient client = new FxAccountClient20(serverURI, executor);
FxAccountClient client = new FxAccountClient20(this, serverURI, executor);
try {
hideRemoteError();
new FxAccountCreateAccountTask(this, this, email, passwordStretcher, client, getQueryParameters(), delegate).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void handleFailure(FxAccountClientRemoteException e) {
};

Executor executor = Executors.newSingleThreadExecutor();
FxAccountClient client = new FxAccountClient20(serverURI, executor);
FxAccountClient client = new FxAccountClient20(this, serverURI, executor);
try {
hideRemoteError();
new FxAccountSignInTask(this, this, email, passwordStretcher, client, getQueryParameters(), delegate).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public FxADefaultLoginStateMachineDelegate(Context context, AndroidFxAccount fxA
this.context = context;
this.fxAccount = fxAccount;
this.executor = Executors.newSingleThreadExecutor();
this.client = new FxAccountClient20(fxAccount.getAccountServerURI(), executor);
this.client = new FxAccountClient20(context, fxAccount.getAccountServerURI(), executor);
}

abstract public void handleNotMarried(State notMarried);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public FxADefaultLoginStateMachineDelegate(Context context, AndroidFxAccount fxA
this.context = context;
this.fxAccount = fxAccount;
this.executor = Executors.newSingleThreadExecutor();
this.client = new FxAccountClient20(fxAccount.getAccountServerURI(), executor);
this.client = new FxAccountClient20(context, fxAccount.getAccountServerURI(), executor);
}

@Override
Expand Down Expand Up @@ -253,7 +253,7 @@ public void handleError(Exception e) {
Logger.error(LOG_TAG, "OAuth error.", e);
responder.fail(e);
}
});
}, context);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions mobile/android/base/fxa/sync/FxAccountSyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void handleSuccess(final TokenServerToken token) {
final AuthHeaderProvider authHeaderProvider = new HawkAuthHeaderProvider(token.id, token.key.getBytes("UTF-8"), includePayloadVerificationHash, storageServerSkew);

final Context context = getContext();
final SyncConfiguration syncConfig = new SyncConfiguration(token.uid, authHeaderProvider, sharedPrefs, syncKeyBundle);
final SyncConfiguration syncConfig = new SyncConfiguration(token.uid, authHeaderProvider, context, sharedPrefs, syncKeyBundle);

Collection<String> knownStageNames = SyncConfiguration.validEngineNames();
syncConfig.stagesToSync = Utils.getStagesToSyncFromBundle(knownStageNames, extras);
Expand Down Expand Up @@ -365,7 +365,7 @@ private long delay(long delay) {
};

TokenServerClient tokenServerclient = new TokenServerClient(tokenServerEndpointURI, executor);
tokenServerclient.getTokenFromBrowserIDAssertion(assertion, true, clientState, delegate);
tokenServerclient.getTokenFromBrowserIDAssertion(assertion, true, clientState, delegate, getContext());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion mobile/android/base/fxa/tasks/FxAccountCodeResender.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static void resendCode(Context context, AndroidFxAccount fxAccount) {
}

Executor executor = Executors.newSingleThreadExecutor();
FxAccountClient client = new FxAccountClient20(fxAccount.getAccountServerURI(), executor);
FxAccountClient client = new FxAccountClient20(context, fxAccount.getAccountServerURI(), executor);
new FxAccountResendCodeTask(context, sessionToken, client, delegate).execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void resendUnlockCode(Context context, String authServerURI, byte[
}

final Executor executor = Executors.newSingleThreadExecutor();
final FxAccountClient client = new FxAccountClient20(authServerURI, executor);
final FxAccountClient client = new FxAccountClient20(context, authServerURI, executor);
new FxAccountUnlockCodeTask(context, emailUTF8, client, delegate).execute();
}
}
Loading

0 comments on commit 5f9c832

Please sign in to comment.