2012-03-12 19:17:56 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko.sync;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.util.HashMap;
|
2012-03-23 11:03:48 -07:00
|
|
|
import java.util.List;
|
2011-12-21 08:44:08 -08:00
|
|
|
import java.util.Map;
|
2012-03-27 18:41:34 -07:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
import org.mozilla.gecko.sync.crypto.CryptoException;
|
|
|
|
import org.mozilla.gecko.sync.crypto.KeyBundle;
|
2012-03-05 20:53:14 -08:00
|
|
|
import org.mozilla.gecko.sync.delegates.ClientsDataDelegate;
|
2011-12-21 08:44:08 -08:00
|
|
|
import org.mozilla.gecko.sync.delegates.FreshStartDelegate;
|
|
|
|
import org.mozilla.gecko.sync.delegates.GlobalSessionCallback;
|
|
|
|
import org.mozilla.gecko.sync.delegates.InfoCollectionsDelegate;
|
|
|
|
import org.mozilla.gecko.sync.delegates.KeyUploadDelegate;
|
|
|
|
import org.mozilla.gecko.sync.delegates.MetaGlobalDelegate;
|
|
|
|
import org.mozilla.gecko.sync.delegates.WipeServerDelegate;
|
2012-03-12 19:17:56 -07:00
|
|
|
import org.mozilla.gecko.sync.net.BaseResource;
|
2012-03-27 18:41:34 -07:00
|
|
|
import org.mozilla.gecko.sync.net.HttpResponseObserver;
|
|
|
|
import org.mozilla.gecko.sync.net.SyncResponse;
|
2011-12-21 08:44:08 -08:00
|
|
|
import org.mozilla.gecko.sync.net.SyncStorageRecordRequest;
|
|
|
|
import org.mozilla.gecko.sync.net.SyncStorageRequest;
|
|
|
|
import org.mozilla.gecko.sync.net.SyncStorageRequestDelegate;
|
|
|
|
import org.mozilla.gecko.sync.net.SyncStorageResponse;
|
|
|
|
import org.mozilla.gecko.sync.stage.AndroidBrowserBookmarksServerSyncStage;
|
|
|
|
import org.mozilla.gecko.sync.stage.AndroidBrowserHistoryServerSyncStage;
|
|
|
|
import org.mozilla.gecko.sync.stage.CheckPreconditionsStage;
|
|
|
|
import org.mozilla.gecko.sync.stage.CompletedStage;
|
|
|
|
import org.mozilla.gecko.sync.stage.EnsureClusterURLStage;
|
|
|
|
import org.mozilla.gecko.sync.stage.EnsureKeysStage;
|
2012-03-09 15:53:23 -08:00
|
|
|
import org.mozilla.gecko.sync.stage.FennecTabsServerSyncStage;
|
2011-12-21 08:44:08 -08:00
|
|
|
import org.mozilla.gecko.sync.stage.FetchInfoCollectionsStage;
|
|
|
|
import org.mozilla.gecko.sync.stage.FetchMetaGlobalStage;
|
|
|
|
import org.mozilla.gecko.sync.stage.GlobalSyncStage;
|
|
|
|
import org.mozilla.gecko.sync.stage.GlobalSyncStage.Stage;
|
|
|
|
import org.mozilla.gecko.sync.stage.NoSuchStageException;
|
2012-03-05 20:53:14 -08:00
|
|
|
import org.mozilla.gecko.sync.stage.SyncClientsEngineStage;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
import android.content.Context;
|
2012-01-14 09:20:31 -08:00
|
|
|
import android.content.SharedPreferences;
|
2011-12-21 08:44:08 -08:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
2012-02-15 22:05:52 -08:00
|
|
|
import ch.boye.httpclientandroidlib.HttpResponse;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-03-27 18:41:34 -07:00
|
|
|
public class GlobalSession implements CredentialsSource, PrefsSource, HttpResponseObserver {
|
2012-01-14 09:20:31 -08:00
|
|
|
private static final String LOG_TAG = "GlobalSession";
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
public static final String API_VERSION = "1.1";
|
|
|
|
public static final long STORAGE_VERSION = 5;
|
2012-01-14 09:20:31 -08:00
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
public SyncConfiguration config = null;
|
|
|
|
|
|
|
|
protected Map<Stage, GlobalSyncStage> stages;
|
|
|
|
public Stage currentState = Stage.idle;
|
|
|
|
|
2012-03-12 19:17:56 -07:00
|
|
|
public final GlobalSessionCallback callback;
|
2011-12-21 08:44:08 -08:00
|
|
|
private Context context;
|
2012-03-05 20:53:14 -08:00
|
|
|
private ClientsDataDelegate clientsDelegate;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Key accessors.
|
|
|
|
*/
|
|
|
|
public void setCollectionKeys(CollectionKeys k) {
|
|
|
|
config.setCollectionKeys(k);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public CollectionKeys getCollectionKeys() {
|
|
|
|
return config.collectionKeys;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public KeyBundle keyForCollection(String collection) throws NoCollectionKeysSetException {
|
|
|
|
return config.keyForCollection(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Config passthrough for convenience.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String credentials() {
|
|
|
|
return config.credentials();
|
|
|
|
}
|
|
|
|
|
|
|
|
public URI wboURI(String collection, String id) throws URISyntaxException {
|
|
|
|
return config.wboURI(collection, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validators.
|
|
|
|
*/
|
|
|
|
private static boolean isInvalidString(String s) {
|
|
|
|
return s == null ||
|
|
|
|
s.trim().length() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean anyInvalidStrings(String s, String...strings) {
|
|
|
|
if (isInvalidString(s)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (String str : strings) {
|
|
|
|
if (isInvalidString(str)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GlobalSession(String userAPI,
|
|
|
|
String serverURL,
|
|
|
|
String username,
|
|
|
|
String password,
|
2012-01-14 09:20:31 -08:00
|
|
|
String prefsPath,
|
2011-12-21 08:44:08 -08:00
|
|
|
KeyBundle syncKeyBundle,
|
|
|
|
GlobalSessionCallback callback,
|
|
|
|
Context context,
|
2012-03-05 20:53:14 -08:00
|
|
|
Bundle extras,
|
|
|
|
ClientsDataDelegate clientsDelegate)
|
2011-12-21 08:44:08 -08:00
|
|
|
throws SyncConfigurationException, IllegalArgumentException, IOException, ParseException, NonObjectJSONException {
|
|
|
|
if (callback == null) {
|
|
|
|
throw new IllegalArgumentException("Must provide a callback to GlobalSession constructor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anyInvalidStrings(username, password)) {
|
|
|
|
throw new SyncConfigurationException();
|
|
|
|
}
|
|
|
|
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.info(LOG_TAG, "GlobalSession initialized with bundle " + extras);
|
2011-12-21 08:44:08 -08:00
|
|
|
URI serverURI;
|
|
|
|
try {
|
|
|
|
serverURI = (serverURL == null) ? null : new URI(serverURL);
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
throw new SyncConfigurationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syncKeyBundle == null ||
|
|
|
|
syncKeyBundle.getEncryptionKey() == null ||
|
|
|
|
syncKeyBundle.getHMACKey() == null) {
|
|
|
|
throw new SyncConfigurationException();
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
this.callback = callback;
|
|
|
|
this.context = context;
|
2012-03-05 20:53:14 -08:00
|
|
|
this.clientsDelegate = clientsDelegate;
|
2012-01-14 09:20:31 -08:00
|
|
|
|
|
|
|
config = new SyncConfiguration(prefsPath, this);
|
2011-12-21 08:44:08 -08:00
|
|
|
config.userAPI = userAPI;
|
|
|
|
config.serverURL = serverURI;
|
|
|
|
config.username = username;
|
|
|
|
config.password = password;
|
|
|
|
config.syncKeyBundle = syncKeyBundle;
|
|
|
|
|
2012-03-23 11:03:48 -07:00
|
|
|
registerCommands();
|
2011-12-21 08:44:08 -08:00
|
|
|
prepareStages();
|
|
|
|
}
|
|
|
|
|
2012-03-23 11:03:48 -07:00
|
|
|
protected void registerCommands() {
|
|
|
|
CommandProcessor processor = CommandProcessor.getProcessor();
|
|
|
|
|
|
|
|
processor.registerCommand("resetEngine", new CommandRunner() {
|
|
|
|
@Override
|
|
|
|
public void executeCommand(List<String> args) {
|
|
|
|
resetClient(new String[] { args.get(0) });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
processor.registerCommand("resetAll", new CommandRunner() {
|
|
|
|
@Override
|
|
|
|
public void executeCommand(List<String> args) {
|
|
|
|
resetClient(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
protected void prepareStages() {
|
|
|
|
stages = new HashMap<Stage, GlobalSyncStage>();
|
|
|
|
stages.put(Stage.checkPreconditions, new CheckPreconditionsStage());
|
|
|
|
stages.put(Stage.ensureClusterURL, new EnsureClusterURLStage());
|
|
|
|
stages.put(Stage.fetchInfoCollections, new FetchInfoCollectionsStage());
|
|
|
|
stages.put(Stage.fetchMetaGlobal, new FetchMetaGlobalStage());
|
|
|
|
stages.put(Stage.ensureKeysStage, new EnsureKeysStage());
|
2012-03-05 20:53:14 -08:00
|
|
|
stages.put(Stage.syncClientsEngine, new SyncClientsEngineStage());
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
// TODO: more stages.
|
2012-03-09 15:53:23 -08:00
|
|
|
stages.put(Stage.syncTabs, new FennecTabsServerSyncStage());
|
2011-12-21 08:44:08 -08:00
|
|
|
stages.put(Stage.syncBookmarks, new AndroidBrowserBookmarksServerSyncStage());
|
|
|
|
stages.put(Stage.syncHistory, new AndroidBrowserHistoryServerSyncStage());
|
|
|
|
stages.put(Stage.completed, new CompletedStage());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected GlobalSyncStage getStageByName(Stage next) throws NoSuchStageException {
|
|
|
|
GlobalSyncStage stage = stages.get(next);
|
|
|
|
if (stage == null) {
|
|
|
|
throw new NoSuchStageException(next);
|
|
|
|
}
|
|
|
|
return stage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance and loop around the stages of a sync.
|
|
|
|
* @param current
|
|
|
|
* @return
|
2012-03-05 20:53:13 -08:00
|
|
|
* The next stage to execute.
|
2011-12-21 08:44:08 -08:00
|
|
|
*/
|
|
|
|
public static Stage nextStage(Stage current) {
|
|
|
|
int index = current.ordinal() + 1;
|
|
|
|
int max = Stage.completed.ordinal() + 1;
|
|
|
|
return Stage.values()[index % max];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move to the next stage in the syncing process.
|
|
|
|
*/
|
|
|
|
public void advance() {
|
2012-03-27 18:41:34 -07:00
|
|
|
// If we have a backoff, request a backoff and don't advance to next stage.
|
|
|
|
long existingBackoff = largestBackoffObserved.get();
|
|
|
|
if (existingBackoff > 0) {
|
|
|
|
this.abort(null, "Aborting sync because of backoff of " + existingBackoff + " milliseconds.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
this.callback.handleStageCompleted(this.currentState, this);
|
|
|
|
Stage next = nextStage(this.currentState);
|
|
|
|
GlobalSyncStage nextStage;
|
|
|
|
try {
|
|
|
|
nextStage = this.getStageByName(next);
|
|
|
|
} catch (NoSuchStageException e) {
|
|
|
|
this.abort(e, "No such stage " + next);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.currentState = next;
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.info(LOG_TAG, "Running next stage " + next + " (" + nextStage + ")...");
|
2011-12-21 08:44:08 -08:00
|
|
|
try {
|
|
|
|
nextStage.execute(this);
|
|
|
|
} catch (Exception ex) {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Caught exception " + ex + " running stage " + next);
|
2011-12-21 08:44:08 -08:00
|
|
|
this.abort(ex, "Uncaught exception in stage.");
|
2012-03-27 18:41:34 -07:00
|
|
|
return;
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getSyncID() {
|
|
|
|
return config.syncID;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String generateSyncID() {
|
|
|
|
config.syncID = Utils.generateGuid();
|
|
|
|
return config.syncID;
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
/*
|
|
|
|
* PrefsSource methods.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public SharedPreferences getPrefs(String name, int mode) {
|
|
|
|
return this.getContext().getSharedPreferences(name, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-12-21 08:44:08 -08:00
|
|
|
public Context getContext() {
|
|
|
|
return this.context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin a sync.
|
2012-03-27 18:41:34 -07:00
|
|
|
* <p>
|
2011-12-21 08:44:08 -08:00
|
|
|
* The caller is responsible for:
|
2012-03-27 18:41:34 -07:00
|
|
|
* <ul>
|
|
|
|
* <li>Verifying that any backoffs/minimum next sync requests are respected.</li>
|
|
|
|
* <li>Ensuring that the device is online.</li>
|
|
|
|
* <li>Ensuring that dependencies are ready.</li>
|
|
|
|
* </ul>
|
2011-12-21 08:44:08 -08:00
|
|
|
*
|
|
|
|
* @throws AlreadySyncingException
|
|
|
|
*/
|
|
|
|
public void start() throws AlreadySyncingException {
|
|
|
|
if (this.currentState != GlobalSyncStage.Stage.idle) {
|
|
|
|
throw new AlreadySyncingException(this.currentState);
|
|
|
|
}
|
2012-03-27 18:41:34 -07:00
|
|
|
installAsHttpResponseObserver(); // Uninstalled by completeSync or abort.
|
2011-12-21 08:44:08 -08:00
|
|
|
this.advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop this sync and start again.
|
|
|
|
* @throws AlreadySyncingException
|
|
|
|
*/
|
|
|
|
protected void restart() throws AlreadySyncingException {
|
|
|
|
this.currentState = GlobalSyncStage.Stage.idle;
|
2012-01-14 09:20:31 -08:00
|
|
|
if (callback.shouldBackOff()) {
|
|
|
|
this.callback.handleAborted(this, "Told to back off.");
|
|
|
|
return;
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
this.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void completeSync() {
|
2012-03-27 18:41:34 -07:00
|
|
|
uninstallAsHttpResponseObserver();
|
2011-12-21 08:44:08 -08:00
|
|
|
this.currentState = GlobalSyncStage.Stage.idle;
|
|
|
|
this.callback.handleSuccess(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void abort(Exception e, String reason) {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Aborting sync: " + reason, e);
|
2012-03-27 18:41:34 -07:00
|
|
|
uninstallAsHttpResponseObserver();
|
|
|
|
long existingBackoff = largestBackoffObserved.get();
|
|
|
|
if (existingBackoff > 0) {
|
|
|
|
callback.requestBackoff(existingBackoff);
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
this.callback.handleError(this, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void handleHTTPError(SyncStorageResponse response, String reason) {
|
|
|
|
// TODO: handling of 50x (backoff), 401 (node reassignment or auth error).
|
|
|
|
// Fall back to aborting.
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Aborting sync due to HTTP " + response.getStatusCode());
|
2012-01-14 09:20:31 -08:00
|
|
|
this.interpretHTTPFailure(response.httpResponse());
|
2011-12-21 08:44:08 -08:00
|
|
|
this.abort(new HTTPFailureException(response), reason);
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
/**
|
|
|
|
* Perform appropriate backoff etc. extraction.
|
|
|
|
*/
|
|
|
|
public void interpretHTTPFailure(HttpResponse response) {
|
|
|
|
// TODO: handle permanent rejection.
|
2012-03-27 18:41:34 -07:00
|
|
|
long responseBackoff = (new SyncResponse(response)).totalBackoffInMilliseconds();
|
|
|
|
if (responseBackoff > 0) {
|
|
|
|
callback.requestBackoff(responseBackoff);
|
2012-01-14 09:20:31 -08:00
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-03-12 19:17:56 -07:00
|
|
|
if (response.getStatusLine() != null && response.getStatusLine().getStatusCode() == 401) {
|
|
|
|
/*
|
|
|
|
* Alert our callback we have a 401 on a cluster URL. This GlobalSession
|
|
|
|
* will fail, but the next one will fetch a new cluster URL and will
|
|
|
|
* distinguish between "node reassignment" and "user password changed".
|
|
|
|
*/
|
|
|
|
callback.informUnauthorizedResponse(this, config.getClusterURL());
|
|
|
|
}
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
public void fetchMetaGlobal(MetaGlobalDelegate callback) throws URISyntaxException {
|
|
|
|
if (this.config.metaGlobal == null) {
|
|
|
|
this.config.metaGlobal = new MetaGlobal(config.metaURL(), credentials());
|
|
|
|
}
|
|
|
|
this.config.metaGlobal.fetch(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void fetchInfoCollections(InfoCollectionsDelegate callback) throws URISyntaxException {
|
|
|
|
if (this.config.infoCollections == null) {
|
|
|
|
this.config.infoCollections = new InfoCollections(config.infoURL(), credentials());
|
|
|
|
}
|
|
|
|
this.config.infoCollections.fetch(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void uploadKeys(CryptoRecord keysRecord,
|
|
|
|
final KeyUploadDelegate keyUploadDelegate) {
|
|
|
|
SyncStorageRecordRequest request;
|
2012-01-14 09:20:31 -08:00
|
|
|
final GlobalSession self = this;
|
2011-12-21 08:44:08 -08:00
|
|
|
try {
|
|
|
|
request = new SyncStorageRecordRequest(this.config.keysURI());
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
keyUploadDelegate.onKeyUploadFailed(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.delegate = new SyncStorageRequestDelegate() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String ifUnmodifiedSince() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleRequestSuccess(SyncStorageResponse response) {
|
2012-03-12 19:17:56 -07:00
|
|
|
BaseResource.consumeEntity(response); // We don't need the response at all.
|
2011-12-21 08:44:08 -08:00
|
|
|
keyUploadDelegate.onKeysUploaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleRequestFailure(SyncStorageResponse response) {
|
2012-01-14 09:20:31 -08:00
|
|
|
self.interpretHTTPFailure(response.httpResponse());
|
2012-03-12 19:17:56 -07:00
|
|
|
BaseResource.consumeEntity(response); // The exception thrown should not need the body of the response.
|
2011-12-21 08:44:08 -08:00
|
|
|
keyUploadDelegate.onKeyUploadFailed(new HTTPFailureException(response));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleRequestError(Exception ex) {
|
|
|
|
keyUploadDelegate.onKeyUploadFailed(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String credentials() {
|
2012-01-14 09:20:31 -08:00
|
|
|
return self.credentials();
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
keysRecord.setKeyBundle(config.syncKeyBundle);
|
|
|
|
try {
|
|
|
|
keysRecord.encrypt();
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
keyUploadDelegate.onKeyUploadFailed(e);
|
|
|
|
return;
|
|
|
|
} catch (CryptoException e) {
|
|
|
|
keyUploadDelegate.onKeyUploadFailed(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request.put(keysRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* meta/global callbacks.
|
|
|
|
*/
|
|
|
|
public void processMetaGlobal(MetaGlobal global) {
|
|
|
|
Long storageVersion = global.getStorageVersion();
|
|
|
|
if (storageVersion < STORAGE_VERSION) {
|
|
|
|
// Outdated server.
|
|
|
|
freshStart();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (storageVersion > STORAGE_VERSION) {
|
|
|
|
// Outdated client!
|
|
|
|
requiresUpgrade();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String remoteSyncID = global.getSyncID();
|
|
|
|
if (remoteSyncID == null) {
|
|
|
|
// Corrupt meta/global.
|
|
|
|
freshStart();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String localSyncID = this.getSyncID();
|
|
|
|
if (!remoteSyncID.equals(localSyncID)) {
|
|
|
|
// Sync ID has changed. Reset timestamps and fetch new keys.
|
2012-03-23 11:03:48 -07:00
|
|
|
resetClient(null);
|
2011-12-21 08:44:08 -08:00
|
|
|
if (config.collectionKeys != null) {
|
|
|
|
config.collectionKeys.clear();
|
|
|
|
}
|
|
|
|
config.syncID = remoteSyncID;
|
|
|
|
// TODO TODO TODO
|
|
|
|
}
|
2012-01-14 09:20:31 -08:00
|
|
|
config.persistToPrefs();
|
2011-12-21 08:44:08 -08:00
|
|
|
advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processMissingMetaGlobal(MetaGlobal global) {
|
|
|
|
freshStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do a fresh start then quietly finish the sync, starting another.
|
|
|
|
*/
|
|
|
|
protected void freshStart() {
|
|
|
|
final GlobalSession globalSession = this;
|
|
|
|
freshStart(this, new FreshStartDelegate() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFreshStartFailed(Exception e) {
|
|
|
|
globalSession.abort(e, "Fresh start failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFreshStart() {
|
|
|
|
try {
|
2012-01-14 09:20:31 -08:00
|
|
|
globalSession.config.persistToPrefs();
|
2011-12-21 08:44:08 -08:00
|
|
|
globalSession.restart();
|
|
|
|
} catch (Exception e) {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Got exception when restarting sync after freshStart.", e);
|
2011-12-21 08:44:08 -08:00
|
|
|
globalSession.abort(e, "Got exception after freshStart.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean the server, aborting the current sync.
|
|
|
|
*/
|
|
|
|
protected void freshStart(final GlobalSession session, final FreshStartDelegate freshStartDelegate) {
|
|
|
|
|
|
|
|
final String newSyncID = session.generateSyncID();
|
|
|
|
final String metaURL = session.config.metaURL();
|
|
|
|
final String credentials = session.credentials();
|
|
|
|
|
|
|
|
wipeServer(session, new WipeServerDelegate() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onWiped(long timestamp) {
|
2012-03-23 11:03:48 -07:00
|
|
|
session.resetClient(null);
|
2011-12-21 08:44:08 -08:00
|
|
|
session.config.collectionKeys.clear(); // TODO: make sure we clear our keys timestamp.
|
2012-01-14 09:20:31 -08:00
|
|
|
session.config.persistToPrefs();
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
MetaGlobal mg = new MetaGlobal(metaURL, credentials);
|
|
|
|
mg.setSyncID(newSyncID);
|
|
|
|
mg.setStorageVersion(STORAGE_VERSION);
|
|
|
|
|
|
|
|
// It would be good to set the X-If-Unmodified-Since header to `timestamp`
|
|
|
|
// for this PUT to ensure at least some level of transactionality.
|
|
|
|
// Unfortunately, the servers don't support it after a wipe right now
|
|
|
|
// (bug 693893), so we're going to defer this until bug 692700.
|
|
|
|
mg.upload(new MetaGlobalDelegate() {
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public void handleSuccess(MetaGlobal global, SyncStorageResponse response) {
|
2011-12-21 08:44:08 -08:00
|
|
|
session.config.metaGlobal = global;
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.info(LOG_TAG, "New meta/global uploaded with sync ID " + newSyncID);
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
// Generate and upload new keys.
|
|
|
|
try {
|
|
|
|
session.uploadKeys(CollectionKeys.generateCollectionKeys().asCryptoRecord(), new KeyUploadDelegate() {
|
|
|
|
@Override
|
|
|
|
public void onKeysUploaded() {
|
|
|
|
// Now we can download them.
|
|
|
|
freshStartDelegate.onFreshStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onKeyUploadFailed(Exception e) {
|
|
|
|
Log.e(LOG_TAG, "Got exception uploading new keys.", e);
|
|
|
|
freshStartDelegate.onFreshStartFailed(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (NoCollectionKeysSetException e) {
|
|
|
|
Log.e(LOG_TAG, "Got exception generating new keys.", e);
|
|
|
|
freshStartDelegate.onFreshStartFailed(e);
|
|
|
|
} catch (CryptoException e) {
|
|
|
|
Log.e(LOG_TAG, "Got exception generating new keys.", e);
|
|
|
|
freshStartDelegate.onFreshStartFailed(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public void handleMissing(MetaGlobal global, SyncStorageResponse response) {
|
2011-12-21 08:44:08 -08:00
|
|
|
// Shouldn't happen.
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Got 'missing' response uploading new meta/global.");
|
2011-12-21 08:44:08 -08:00
|
|
|
freshStartDelegate.onFreshStartFailed(new Exception("meta/global missing"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleFailure(SyncStorageResponse response) {
|
|
|
|
// TODO: respect backoffs etc.
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Got failure " + response.getStatusCode() + " uploading new meta/global.");
|
2012-01-14 09:20:31 -08:00
|
|
|
session.interpretHTTPFailure(response.httpResponse());
|
2011-12-21 08:44:08 -08:00
|
|
|
freshStartDelegate.onFreshStartFailed(new HTTPFailureException(response));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleError(Exception e) {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Got error uploading new meta/global.", e);
|
2011-12-21 08:44:08 -08:00
|
|
|
freshStartDelegate.onFreshStartFailed(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MetaGlobalDelegate deferred() {
|
|
|
|
final MetaGlobalDelegate self = this;
|
|
|
|
return new MetaGlobalDelegate() {
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public void handleSuccess(final MetaGlobal global, final SyncStorageResponse response) {
|
2011-12-21 08:44:08 -08:00
|
|
|
ThreadPool.run(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2012-01-14 09:20:31 -08:00
|
|
|
self.handleSuccess(global, response);
|
2011-12-21 08:44:08 -08:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public void handleMissing(final MetaGlobal global, final SyncStorageResponse response) {
|
2011-12-21 08:44:08 -08:00
|
|
|
ThreadPool.run(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2012-01-14 09:20:31 -08:00
|
|
|
self.handleMissing(global, response);
|
2011-12-21 08:44:08 -08:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleFailure(final SyncStorageResponse response) {
|
|
|
|
ThreadPool.run(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
self.handleFailure(response);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleError(final Exception e) {
|
|
|
|
ThreadPool.run(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
self.handleError(e);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MetaGlobalDelegate deferred() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onWipeFailed(Exception e) {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Wipe failed.");
|
2011-12-21 08:44:08 -08:00
|
|
|
freshStartDelegate.onFreshStartFailed(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void wipeServer(final CredentialsSource credentials, final WipeServerDelegate wipeDelegate) {
|
|
|
|
SyncStorageRequest request;
|
2012-01-14 09:20:31 -08:00
|
|
|
final GlobalSession self = this;
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
try {
|
|
|
|
request = new SyncStorageRequest(config.storageURL(false));
|
|
|
|
} catch (URISyntaxException ex) {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Invalid URI in wipeServer.");
|
2011-12-21 08:44:08 -08:00
|
|
|
wipeDelegate.onWipeFailed(ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.delegate = new SyncStorageRequestDelegate() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String ifUnmodifiedSince() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleRequestSuccess(SyncStorageResponse response) {
|
2012-03-12 19:17:56 -07:00
|
|
|
BaseResource.consumeEntity(response);
|
2011-12-21 08:44:08 -08:00
|
|
|
wipeDelegate.onWiped(response.normalizedWeaveTimestamp());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleRequestFailure(SyncStorageResponse response) {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Got request failure " + response.getStatusCode() + " in wipeServer.");
|
2012-01-14 09:20:31 -08:00
|
|
|
// Process HTTP failures here to pick up backoffs, etc.
|
|
|
|
self.interpretHTTPFailure(response.httpResponse());
|
2012-03-12 19:17:56 -07:00
|
|
|
BaseResource.consumeEntity(response); // The exception thrown should not need the body of the response.
|
2011-12-21 08:44:08 -08:00
|
|
|
wipeDelegate.onWipeFailed(new HTTPFailureException(response));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleRequestError(Exception ex) {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.warn(LOG_TAG, "Got exception in wipeServer.", ex);
|
2011-12-21 08:44:08 -08:00
|
|
|
wipeDelegate.onWipeFailed(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String credentials() {
|
|
|
|
return credentials.credentials();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
request.delete();
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
/**
|
|
|
|
* Reset our state. Clear our sync ID, reset each engine, drop any
|
|
|
|
* cached records.
|
|
|
|
*/
|
2012-03-23 11:03:48 -07:00
|
|
|
private void resetClient(String[] engines) {
|
|
|
|
if (engines == null) {
|
|
|
|
// Set `engines` to be *all* the engines.
|
|
|
|
}
|
2012-01-14 09:20:31 -08:00
|
|
|
// TODO: futz with config?!
|
|
|
|
// TODO: engines?!
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Suggest that your Sync client needs to be upgraded to work
|
|
|
|
* with this server.
|
|
|
|
*/
|
|
|
|
public void requiresUpgrade() {
|
2012-03-12 19:17:56 -07:00
|
|
|
Logger.info(LOG_TAG, "Client outdated storage version; requires update.");
|
2011-12-21 08:44:08 -08:00
|
|
|
// TODO: notify UI.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If meta/global is missing or malformed, throws a MetaGlobalException.
|
|
|
|
* Otherwise, returns true if there is an entry for this engine in the
|
|
|
|
* meta/global "engines" object.
|
|
|
|
*
|
|
|
|
* @param engineName
|
|
|
|
* @return
|
2012-03-05 20:53:13 -08:00
|
|
|
* true if the engine with the provided name is present in the
|
|
|
|
* meta/global "engines" object.
|
|
|
|
*
|
2011-12-21 08:44:08 -08:00
|
|
|
* @throws MetaGlobalException
|
|
|
|
*/
|
|
|
|
public boolean engineIsEnabled(String engineName) throws MetaGlobalException {
|
|
|
|
if (this.config.metaGlobal == null) {
|
|
|
|
throw new MetaGlobalNotSetException();
|
|
|
|
}
|
|
|
|
if (this.config.metaGlobal.engines == null) {
|
|
|
|
throw new MetaGlobalMissingEnginesException();
|
|
|
|
}
|
|
|
|
return this.config.metaGlobal.engines.get(engineName) != null;
|
|
|
|
}
|
2012-03-05 20:53:14 -08:00
|
|
|
|
|
|
|
public ClientsDataDelegate getClientsDelegate() {
|
|
|
|
return this.clientsDelegate;
|
|
|
|
}
|
2012-03-27 18:41:34 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The longest backoff observed to date; -1 means no backoff observed.
|
|
|
|
*/
|
|
|
|
protected final AtomicLong largestBackoffObserved = new AtomicLong(-1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset any observed backoff and start observing HTTP responses for backoff
|
|
|
|
* requests.
|
|
|
|
*/
|
|
|
|
protected void installAsHttpResponseObserver() {
|
|
|
|
Logger.debug(LOG_TAG, "Installing " + this + " as BaseResource HttpResponseObserver.");
|
|
|
|
BaseResource.setHttpResponseObserver(this);
|
|
|
|
largestBackoffObserved.set(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop observing HttpResponses for backoff requests.
|
|
|
|
*/
|
|
|
|
protected void uninstallAsHttpResponseObserver() {
|
|
|
|
Logger.debug(LOG_TAG, "Uninstalling " + this + " as BaseResource HttpResponseObserver.");
|
|
|
|
BaseResource.setHttpResponseObserver(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Observe all HTTP response for backoff requests on all status codes, not just errors.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void observeHttpResponse(HttpResponse response) {
|
|
|
|
long responseBackoff = (new SyncResponse(response)).totalBackoffInMilliseconds(); // TODO: don't allocate object?
|
|
|
|
if (responseBackoff <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug(LOG_TAG, "Observed " + responseBackoff + " millisecond backoff request.");
|
|
|
|
while (true) {
|
|
|
|
long existingBackoff = largestBackoffObserved.get();
|
|
|
|
if (existingBackoff >= responseBackoff) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (largestBackoffObserved.compareAndSet(existingBackoff, responseBackoff)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|