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.sync.repositories; |
michael@0 | 6 | |
michael@0 | 7 | import java.util.Collection; |
michael@0 | 8 | import java.util.Iterator; |
michael@0 | 9 | |
michael@0 | 10 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 11 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionBeginDelegate; |
michael@0 | 12 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFinishDelegate; |
michael@0 | 13 | import org.mozilla.gecko.sync.repositories.domain.Record; |
michael@0 | 14 | |
michael@0 | 15 | public abstract class StoreTrackingRepositorySession extends RepositorySession { |
michael@0 | 16 | private static final String LOG_TAG = "StoreTrackSession"; |
michael@0 | 17 | protected StoreTracker storeTracker; |
michael@0 | 18 | |
michael@0 | 19 | protected static StoreTracker createStoreTracker() { |
michael@0 | 20 | return new HashSetStoreTracker(); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | public StoreTrackingRepositorySession(Repository repository) { |
michael@0 | 24 | super(repository); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | @Override |
michael@0 | 28 | public void begin(RepositorySessionBeginDelegate delegate) throws InvalidSessionTransitionException { |
michael@0 | 29 | RepositorySessionBeginDelegate deferredDelegate = delegate.deferredBeginDelegate(delegateQueue); |
michael@0 | 30 | try { |
michael@0 | 31 | super.sharedBegin(); |
michael@0 | 32 | } catch (InvalidSessionTransitionException e) { |
michael@0 | 33 | deferredDelegate.onBeginFailed(e); |
michael@0 | 34 | return; |
michael@0 | 35 | } |
michael@0 | 36 | // Or do this in your own subclass. |
michael@0 | 37 | storeTracker = createStoreTracker(); |
michael@0 | 38 | deferredDelegate.onBeginSucceeded(this); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | @Override |
michael@0 | 42 | protected synchronized void trackGUID(String guid) { |
michael@0 | 43 | if (this.storeTracker == null) { |
michael@0 | 44 | throw new IllegalStateException("Store tracker not yet initialized!"); |
michael@0 | 45 | } |
michael@0 | 46 | this.storeTracker.trackRecordForExclusion(guid); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | @Override |
michael@0 | 50 | protected synchronized void untrackGUID(String guid) { |
michael@0 | 51 | if (this.storeTracker == null) { |
michael@0 | 52 | throw new IllegalStateException("Store tracker not yet initialized!"); |
michael@0 | 53 | } |
michael@0 | 54 | this.storeTracker.untrackStoredForExclusion(guid); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | @Override |
michael@0 | 58 | protected synchronized void untrackGUIDs(Collection<String> guids) { |
michael@0 | 59 | if (this.storeTracker == null) { |
michael@0 | 60 | throw new IllegalStateException("Store tracker not yet initialized!"); |
michael@0 | 61 | } |
michael@0 | 62 | if (guids == null) { |
michael@0 | 63 | return; |
michael@0 | 64 | } |
michael@0 | 65 | for (String guid : guids) { |
michael@0 | 66 | this.storeTracker.untrackStoredForExclusion(guid); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | protected void trackRecord(Record record) { |
michael@0 | 71 | |
michael@0 | 72 | Logger.debug(LOG_TAG, "Tracking record " + record.guid + |
michael@0 | 73 | " (" + record.lastModified + ") to avoid re-upload."); |
michael@0 | 74 | // Future: we care about the timestamp… |
michael@0 | 75 | trackGUID(record.guid); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | protected void untrackRecord(Record record) { |
michael@0 | 79 | Logger.debug(LOG_TAG, "Un-tracking record " + record.guid + "."); |
michael@0 | 80 | untrackGUID(record.guid); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | @Override |
michael@0 | 84 | public Iterator<String> getTrackedRecordIDs() { |
michael@0 | 85 | if (this.storeTracker == null) { |
michael@0 | 86 | throw new IllegalStateException("Store tracker not yet initialized!"); |
michael@0 | 87 | } |
michael@0 | 88 | return this.storeTracker.recordsTrackedForExclusion(); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | @Override |
michael@0 | 92 | public void abort(RepositorySessionFinishDelegate delegate) { |
michael@0 | 93 | this.storeTracker = null; |
michael@0 | 94 | super.abort(delegate); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | @Override |
michael@0 | 98 | public void finish(RepositorySessionFinishDelegate delegate) throws InactiveSessionException { |
michael@0 | 99 | super.finish(delegate); |
michael@0 | 100 | this.storeTracker = null; |
michael@0 | 101 | } |
michael@0 | 102 | } |