Wed, 31 Dec 2014 06:09:35 +0100
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.background.fxa; |
michael@0 | 6 | |
michael@0 | 7 | import java.net.URI; |
michael@0 | 8 | import java.net.URISyntaxException; |
michael@0 | 9 | import java.util.HashMap; |
michael@0 | 10 | |
michael@0 | 11 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 12 | import org.mozilla.gecko.sync.net.Resource; |
michael@0 | 13 | |
michael@0 | 14 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 15 | import ch.boye.httpclientandroidlib.HttpHeaders; |
michael@0 | 16 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 17 | import ch.boye.httpclientandroidlib.impl.cookie.DateParseException; |
michael@0 | 18 | import ch.boye.httpclientandroidlib.impl.cookie.DateUtils; |
michael@0 | 19 | |
michael@0 | 20 | public class SkewHandler { |
michael@0 | 21 | private static final String LOG_TAG = "SkewHandler"; |
michael@0 | 22 | protected volatile long skewMillis = 0L; |
michael@0 | 23 | protected final String hostname; |
michael@0 | 24 | |
michael@0 | 25 | private static final HashMap<String, SkewHandler> skewHandlers = new HashMap<String, SkewHandler>(); |
michael@0 | 26 | |
michael@0 | 27 | public static SkewHandler getSkewHandlerForResource(final Resource resource) { |
michael@0 | 28 | return getSkewHandlerForHostname(resource.getHostname()); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | public static SkewHandler getSkewHandlerFromEndpointString(final String url) throws URISyntaxException { |
michael@0 | 32 | if (url == null) { |
michael@0 | 33 | throw new IllegalArgumentException("url must not be null."); |
michael@0 | 34 | } |
michael@0 | 35 | URI u = new URI(url); |
michael@0 | 36 | return getSkewHandlerForHostname(u.getHost()); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | public static synchronized SkewHandler getSkewHandlerForHostname(final String hostname) { |
michael@0 | 40 | SkewHandler handler = skewHandlers.get(hostname); |
michael@0 | 41 | if (handler == null) { |
michael@0 | 42 | handler = new SkewHandler(hostname); |
michael@0 | 43 | skewHandlers.put(hostname, handler); |
michael@0 | 44 | } |
michael@0 | 45 | return handler; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | public static synchronized void clearSkewHandlers() { |
michael@0 | 49 | skewHandlers.clear(); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | public SkewHandler(final String hostname) { |
michael@0 | 53 | this.hostname = hostname; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | public boolean updateSkewFromServerMillis(long millis, long now) { |
michael@0 | 57 | skewMillis = millis - now; |
michael@0 | 58 | Logger.debug(LOG_TAG, "Updated skew: " + skewMillis + "ms for hostname " + this.hostname); |
michael@0 | 59 | return true; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | public boolean updateSkewFromHTTPDateString(String date, long now) { |
michael@0 | 63 | try { |
michael@0 | 64 | final long millis = DateUtils.parseDate(date).getTime(); |
michael@0 | 65 | return updateSkewFromServerMillis(millis, now); |
michael@0 | 66 | } catch (DateParseException e) { |
michael@0 | 67 | Logger.warn(LOG_TAG, "Unexpected: invalid Date header from " + this.hostname); |
michael@0 | 68 | return false; |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | public boolean updateSkewFromDateHeader(Header header, long now) { |
michael@0 | 73 | String date = header.getValue(); |
michael@0 | 74 | if (null == date) { |
michael@0 | 75 | Logger.warn(LOG_TAG, "Unexpected: null Date header from " + this.hostname); |
michael@0 | 76 | return false; |
michael@0 | 77 | } |
michael@0 | 78 | return updateSkewFromHTTPDateString(date, now); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * Update our tracked skew value to account for the local clock differing from |
michael@0 | 83 | * the server's. |
michael@0 | 84 | * |
michael@0 | 85 | * @param response |
michael@0 | 86 | * the received HTTP response. |
michael@0 | 87 | * @param now |
michael@0 | 88 | * the current time in milliseconds. |
michael@0 | 89 | * @return true if the skew value was updated, false otherwise. |
michael@0 | 90 | */ |
michael@0 | 91 | public boolean updateSkew(HttpResponse response, long now) { |
michael@0 | 92 | Header header = response.getFirstHeader(HttpHeaders.DATE); |
michael@0 | 93 | if (null == header) { |
michael@0 | 94 | Logger.warn(LOG_TAG, "Unexpected: missing Date header from " + this.hostname); |
michael@0 | 95 | return false; |
michael@0 | 96 | } |
michael@0 | 97 | return updateSkewFromDateHeader(header, now); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | public long getSkewInMillis() { |
michael@0 | 101 | return skewMillis; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | public long getSkewInSeconds() { |
michael@0 | 105 | return skewMillis / 1000; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | public void resetSkew() { |
michael@0 | 109 | skewMillis = 0L; |
michael@0 | 110 | } |
michael@0 | 111 | } |