2012-04-12 20:15:53 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-24 13:17:46 -07:00
|
|
|
* 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.util.HashMap;
|
2012-05-04 12:27:06 -07:00
|
|
|
import java.util.Map;
|
2012-01-14 09:20:31 -08:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Set;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
import org.mozilla.gecko.sync.delegates.InfoCollectionsDelegate;
|
|
|
|
import org.mozilla.gecko.sync.net.SyncStorageRecordRequest;
|
|
|
|
import org.mozilla.gecko.sync.net.SyncStorageRequestDelegate;
|
|
|
|
import org.mozilla.gecko.sync.net.SyncStorageResponse;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
public class InfoCollections implements SyncStorageRequestDelegate {
|
|
|
|
private static final String LOG_TAG = "InfoCollections";
|
|
|
|
protected String infoURL;
|
|
|
|
protected String credentials;
|
|
|
|
|
2012-05-04 12:27:06 -07:00
|
|
|
/**
|
|
|
|
* Fields fetched from the server, or <code>null</code> if not yet fetched.
|
|
|
|
* <p>
|
|
|
|
* Rather than storing decimal/double timestamps, as provided by the server,
|
|
|
|
* we convert immediately to milliseconds since epoch.
|
|
|
|
*/
|
|
|
|
private Map<String, Long> timestamps = null;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-05-04 12:27:06 -07:00
|
|
|
/**
|
|
|
|
* Return the timestamp for the given collection, or null if the timestamps
|
|
|
|
* have not been fetched or the given collection does not have a timestamp.
|
|
|
|
*
|
|
|
|
* @param collection
|
|
|
|
* The collection to inspect.
|
|
|
|
* @return the timestamp in milliseconds since epoch.
|
|
|
|
*/
|
2012-01-14 09:20:31 -08:00
|
|
|
public Long getTimestamp(String collection) {
|
2012-05-04 12:27:06 -07:00
|
|
|
if (timestamps == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return timestamps.get(collection);
|
2012-01-14 09:20:31 -08:00
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-04-12 20:15:53 -07:00
|
|
|
/**
|
|
|
|
* Test if a given collection needs to be updated.
|
|
|
|
*
|
|
|
|
* @param collection
|
|
|
|
* The collection to test.
|
|
|
|
* @param lastModified
|
|
|
|
* Timestamp when local record was last modified.
|
|
|
|
*/
|
|
|
|
public boolean updateNeeded(String collection, long lastModified) {
|
|
|
|
Logger.trace(LOG_TAG, "Testing " + collection + " for updateNeeded. Local last modified is " + lastModified + ".");
|
|
|
|
|
|
|
|
// No local record of modification time? Need an update.
|
|
|
|
if (lastModified <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No meta/global on the server? We need an update. The server fetch will fail and
|
|
|
|
// then we will upload a fresh meta/global.
|
|
|
|
Long serverLastModified = getTimestamp(collection);
|
|
|
|
if (serverLastModified == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need an update if our modification time is stale.
|
|
|
|
return (serverLastModified.longValue() > lastModified);
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Temporary location to store our callback.
|
|
|
|
private InfoCollectionsDelegate callback;
|
|
|
|
|
|
|
|
public InfoCollections(String metaURL, String credentials) {
|
|
|
|
this.infoURL = metaURL;
|
|
|
|
this.credentials = credentials;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void fetch(InfoCollectionsDelegate callback) {
|
2012-05-04 12:27:06 -07:00
|
|
|
this.callback = callback;
|
|
|
|
this.doFetch();
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void doFetch() {
|
|
|
|
try {
|
2012-01-14 09:20:31 -08:00
|
|
|
final SyncStorageRecordRequest r = new SyncStorageRecordRequest(this.infoURL);
|
2011-12-21 08:44:08 -08:00
|
|
|
r.delegate = this;
|
2012-01-14 09:20:31 -08:00
|
|
|
// TODO: it might be nice to make Resource include its
|
|
|
|
// own thread pool, and automatically run asynchronously.
|
|
|
|
ThreadPool.run(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2012-03-29 16:48:55 -07:00
|
|
|
try {
|
|
|
|
r.get();
|
|
|
|
} catch (Exception e) {
|
|
|
|
callback.handleError(e);
|
|
|
|
}
|
2012-01-14 09:20:31 -08:00
|
|
|
}});
|
2012-03-29 16:48:55 -07:00
|
|
|
} catch (Exception e) {
|
2011-12-21 08:44:08 -08:00
|
|
|
callback.handleError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2012-04-12 20:15:53 -07:00
|
|
|
public void setFromRecord(ExtendedJSONObject record) throws IllegalStateException, IOException, ParseException, NonObjectJSONException {
|
|
|
|
Log.i(LOG_TAG, "info/collections is " + record.toJSONString());
|
2012-01-14 09:20:31 -08:00
|
|
|
HashMap<String, Long> map = new HashMap<String, Long>();
|
|
|
|
|
2012-04-12 20:15:53 -07:00
|
|
|
Set<Entry<String, Object>> entrySet = record.object.entrySet();
|
2012-01-14 09:20:31 -08:00
|
|
|
for (Entry<String, Object> entry : entrySet) {
|
|
|
|
// These objects are most likely going to be Doubles. Regardless, we
|
|
|
|
// want to get them in a more sane time format.
|
|
|
|
String key = entry.getKey();
|
|
|
|
Object value = entry.getValue();
|
|
|
|
if (value instanceof Double) {
|
|
|
|
map.put(key, Utils.decimalSecondsToMilliseconds((Double) value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (value instanceof Long) {
|
|
|
|
map.put(key, Utils.decimalSecondsToMilliseconds((Long) value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (value instanceof Integer) {
|
|
|
|
map.put(key, Utils.decimalSecondsToMilliseconds((Integer) value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Log.w(LOG_TAG, "Skipping info/collections entry for " + key);
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
this.timestamps = map;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncStorageRequestDelegate methods for fetching.
|
|
|
|
public String credentials() {
|
|
|
|
return this.credentials;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String ifUnmodifiedSince() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void handleRequestSuccess(SyncStorageResponse response) {
|
|
|
|
if (response.wasSuccessful()) {
|
|
|
|
try {
|
2012-04-12 20:15:53 -07:00
|
|
|
this.setFromRecord(response.jsonObjectBody());
|
2011-12-21 08:44:08 -08:00
|
|
|
this.callback.handleSuccess(this);
|
|
|
|
this.callback = null;
|
|
|
|
} catch (Exception e) {
|
|
|
|
this.callback.handleError(e);
|
|
|
|
this.callback = null;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.callback.handleFailure(response);
|
|
|
|
this.callback = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void handleRequestFailure(SyncStorageResponse response) {
|
|
|
|
this.callback.handleFailure(response);
|
|
|
|
this.callback = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void handleRequestError(Exception e) {
|
|
|
|
this.callback.handleError(e);
|
|
|
|
this.callback = null;
|
|
|
|
}
|
|
|
|
}
|