1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/fxa/SkewHandler.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,111 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.fxa; 1.9 + 1.10 +import java.net.URI; 1.11 +import java.net.URISyntaxException; 1.12 +import java.util.HashMap; 1.13 + 1.14 +import org.mozilla.gecko.background.common.log.Logger; 1.15 +import org.mozilla.gecko.sync.net.Resource; 1.16 + 1.17 +import ch.boye.httpclientandroidlib.Header; 1.18 +import ch.boye.httpclientandroidlib.HttpHeaders; 1.19 +import ch.boye.httpclientandroidlib.HttpResponse; 1.20 +import ch.boye.httpclientandroidlib.impl.cookie.DateParseException; 1.21 +import ch.boye.httpclientandroidlib.impl.cookie.DateUtils; 1.22 + 1.23 +public class SkewHandler { 1.24 + private static final String LOG_TAG = "SkewHandler"; 1.25 + protected volatile long skewMillis = 0L; 1.26 + protected final String hostname; 1.27 + 1.28 + private static final HashMap<String, SkewHandler> skewHandlers = new HashMap<String, SkewHandler>(); 1.29 + 1.30 + public static SkewHandler getSkewHandlerForResource(final Resource resource) { 1.31 + return getSkewHandlerForHostname(resource.getHostname()); 1.32 + } 1.33 + 1.34 + public static SkewHandler getSkewHandlerFromEndpointString(final String url) throws URISyntaxException { 1.35 + if (url == null) { 1.36 + throw new IllegalArgumentException("url must not be null."); 1.37 + } 1.38 + URI u = new URI(url); 1.39 + return getSkewHandlerForHostname(u.getHost()); 1.40 + } 1.41 + 1.42 + public static synchronized SkewHandler getSkewHandlerForHostname(final String hostname) { 1.43 + SkewHandler handler = skewHandlers.get(hostname); 1.44 + if (handler == null) { 1.45 + handler = new SkewHandler(hostname); 1.46 + skewHandlers.put(hostname, handler); 1.47 + } 1.48 + return handler; 1.49 + } 1.50 + 1.51 + public static synchronized void clearSkewHandlers() { 1.52 + skewHandlers.clear(); 1.53 + } 1.54 + 1.55 + public SkewHandler(final String hostname) { 1.56 + this.hostname = hostname; 1.57 + } 1.58 + 1.59 + public boolean updateSkewFromServerMillis(long millis, long now) { 1.60 + skewMillis = millis - now; 1.61 + Logger.debug(LOG_TAG, "Updated skew: " + skewMillis + "ms for hostname " + this.hostname); 1.62 + return true; 1.63 + } 1.64 + 1.65 + public boolean updateSkewFromHTTPDateString(String date, long now) { 1.66 + try { 1.67 + final long millis = DateUtils.parseDate(date).getTime(); 1.68 + return updateSkewFromServerMillis(millis, now); 1.69 + } catch (DateParseException e) { 1.70 + Logger.warn(LOG_TAG, "Unexpected: invalid Date header from " + this.hostname); 1.71 + return false; 1.72 + } 1.73 + } 1.74 + 1.75 + public boolean updateSkewFromDateHeader(Header header, long now) { 1.76 + String date = header.getValue(); 1.77 + if (null == date) { 1.78 + Logger.warn(LOG_TAG, "Unexpected: null Date header from " + this.hostname); 1.79 + return false; 1.80 + } 1.81 + return updateSkewFromHTTPDateString(date, now); 1.82 + } 1.83 + 1.84 + /** 1.85 + * Update our tracked skew value to account for the local clock differing from 1.86 + * the server's. 1.87 + * 1.88 + * @param response 1.89 + * the received HTTP response. 1.90 + * @param now 1.91 + * the current time in milliseconds. 1.92 + * @return true if the skew value was updated, false otherwise. 1.93 + */ 1.94 + public boolean updateSkew(HttpResponse response, long now) { 1.95 + Header header = response.getFirstHeader(HttpHeaders.DATE); 1.96 + if (null == header) { 1.97 + Logger.warn(LOG_TAG, "Unexpected: missing Date header from " + this.hostname); 1.98 + return false; 1.99 + } 1.100 + return updateSkewFromDateHeader(header, now); 1.101 + } 1.102 + 1.103 + public long getSkewInMillis() { 1.104 + return skewMillis; 1.105 + } 1.106 + 1.107 + public long getSkewInSeconds() { 1.108 + return skewMillis / 1000; 1.109 + } 1.110 + 1.111 + public void resetSkew() { 1.112 + skewMillis = 0L; 1.113 + } 1.114 +} 1.115 \ No newline at end of file