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 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.testhelpers; |
michael@0 | 5 | |
michael@0 | 6 | import java.util.Map.Entry; |
michael@0 | 7 | import java.util.concurrent.ConcurrentHashMap; |
michael@0 | 8 | import java.util.concurrent.ExecutorService; |
michael@0 | 9 | import java.util.concurrent.Executors; |
michael@0 | 10 | |
michael@0 | 11 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 12 | import org.mozilla.gecko.sync.repositories.InactiveSessionException; |
michael@0 | 13 | import org.mozilla.gecko.sync.repositories.InvalidSessionTransitionException; |
michael@0 | 14 | import org.mozilla.gecko.sync.repositories.NoStoreDelegateException; |
michael@0 | 15 | import org.mozilla.gecko.sync.repositories.RecordFilter; |
michael@0 | 16 | import org.mozilla.gecko.sync.repositories.Repository; |
michael@0 | 17 | import org.mozilla.gecko.sync.repositories.StoreTrackingRepositorySession; |
michael@0 | 18 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionBeginDelegate; |
michael@0 | 19 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCreationDelegate; |
michael@0 | 20 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFetchRecordsDelegate; |
michael@0 | 21 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFinishDelegate; |
michael@0 | 22 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionGuidsSinceDelegate; |
michael@0 | 23 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionWipeDelegate; |
michael@0 | 24 | import org.mozilla.gecko.sync.repositories.domain.Record; |
michael@0 | 25 | |
michael@0 | 26 | import android.content.Context; |
michael@0 | 27 | |
michael@0 | 28 | public class WBORepository extends Repository { |
michael@0 | 29 | |
michael@0 | 30 | public class WBORepositoryStats { |
michael@0 | 31 | public long created = -1; |
michael@0 | 32 | public long begun = -1; |
michael@0 | 33 | public long fetchBegan = -1; |
michael@0 | 34 | public long fetchCompleted = -1; |
michael@0 | 35 | public long storeBegan = -1; |
michael@0 | 36 | public long storeCompleted = -1; |
michael@0 | 37 | public long finished = -1; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | public static final String LOG_TAG = "WBORepository"; |
michael@0 | 41 | |
michael@0 | 42 | // Access to stats is not guarded. |
michael@0 | 43 | public WBORepositoryStats stats; |
michael@0 | 44 | |
michael@0 | 45 | // Whether or not to increment the timestamp of stored records. |
michael@0 | 46 | public final boolean bumpTimestamps; |
michael@0 | 47 | |
michael@0 | 48 | public class WBORepositorySession extends StoreTrackingRepositorySession { |
michael@0 | 49 | |
michael@0 | 50 | protected WBORepository wboRepository; |
michael@0 | 51 | protected ExecutorService delegateExecutor = Executors.newSingleThreadExecutor(); |
michael@0 | 52 | public ConcurrentHashMap<String, Record> wbos; |
michael@0 | 53 | |
michael@0 | 54 | public WBORepositorySession(WBORepository repository) { |
michael@0 | 55 | super(repository); |
michael@0 | 56 | |
michael@0 | 57 | wboRepository = repository; |
michael@0 | 58 | wbos = new ConcurrentHashMap<String, Record>(); |
michael@0 | 59 | stats = new WBORepositoryStats(); |
michael@0 | 60 | stats.created = now(); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | @Override |
michael@0 | 64 | protected synchronized void trackGUID(String guid) { |
michael@0 | 65 | if (wboRepository.shouldTrack()) { |
michael@0 | 66 | super.trackGUID(guid); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | @Override |
michael@0 | 71 | public void guidsSince(long timestamp, |
michael@0 | 72 | RepositorySessionGuidsSinceDelegate delegate) { |
michael@0 | 73 | throw new RuntimeException("guidsSince not implemented."); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | @Override |
michael@0 | 77 | public void fetchSince(long timestamp, |
michael@0 | 78 | RepositorySessionFetchRecordsDelegate delegate) { |
michael@0 | 79 | long fetchBegan = now(); |
michael@0 | 80 | stats.fetchBegan = fetchBegan; |
michael@0 | 81 | RecordFilter filter = storeTracker.getFilter(); |
michael@0 | 82 | |
michael@0 | 83 | for (Entry<String, Record> entry : wbos.entrySet()) { |
michael@0 | 84 | Record record = entry.getValue(); |
michael@0 | 85 | if (record.lastModified >= timestamp) { |
michael@0 | 86 | if (filter != null && |
michael@0 | 87 | filter.excludeRecord(record)) { |
michael@0 | 88 | Logger.debug(LOG_TAG, "Excluding record " + record.guid); |
michael@0 | 89 | continue; |
michael@0 | 90 | } |
michael@0 | 91 | delegate.deferredFetchDelegate(delegateExecutor).onFetchedRecord(record); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | long fetchCompleted = now(); |
michael@0 | 95 | stats.fetchCompleted = fetchCompleted; |
michael@0 | 96 | delegate.deferredFetchDelegate(delegateExecutor).onFetchCompleted(fetchCompleted); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | @Override |
michael@0 | 100 | public void fetch(final String[] guids, |
michael@0 | 101 | final RepositorySessionFetchRecordsDelegate delegate) { |
michael@0 | 102 | long fetchBegan = now(); |
michael@0 | 103 | stats.fetchBegan = fetchBegan; |
michael@0 | 104 | for (String guid : guids) { |
michael@0 | 105 | if (wbos.containsKey(guid)) { |
michael@0 | 106 | delegate.deferredFetchDelegate(delegateExecutor).onFetchedRecord(wbos.get(guid)); |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | long fetchCompleted = now(); |
michael@0 | 110 | stats.fetchCompleted = fetchCompleted; |
michael@0 | 111 | delegate.deferredFetchDelegate(delegateExecutor).onFetchCompleted(fetchCompleted); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | @Override |
michael@0 | 115 | public void fetchAll(final RepositorySessionFetchRecordsDelegate delegate) { |
michael@0 | 116 | long fetchBegan = now(); |
michael@0 | 117 | stats.fetchBegan = fetchBegan; |
michael@0 | 118 | for (Entry<String, Record> entry : wbos.entrySet()) { |
michael@0 | 119 | Record record = entry.getValue(); |
michael@0 | 120 | delegate.deferredFetchDelegate(delegateExecutor).onFetchedRecord(record); |
michael@0 | 121 | } |
michael@0 | 122 | long fetchCompleted = now(); |
michael@0 | 123 | stats.fetchCompleted = fetchCompleted; |
michael@0 | 124 | delegate.deferredFetchDelegate(delegateExecutor).onFetchCompleted(fetchCompleted); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | @Override |
michael@0 | 128 | public void store(final Record record) throws NoStoreDelegateException { |
michael@0 | 129 | if (delegate == null) { |
michael@0 | 130 | throw new NoStoreDelegateException(); |
michael@0 | 131 | } |
michael@0 | 132 | final long now = now(); |
michael@0 | 133 | if (stats.storeBegan < 0) { |
michael@0 | 134 | stats.storeBegan = now; |
michael@0 | 135 | } |
michael@0 | 136 | Record existing = wbos.get(record.guid); |
michael@0 | 137 | Logger.debug(LOG_TAG, "Existing record is " + (existing == null ? "<null>" : (existing.guid + ", " + existing))); |
michael@0 | 138 | if (existing != null && |
michael@0 | 139 | existing.lastModified > record.lastModified) { |
michael@0 | 140 | Logger.debug(LOG_TAG, "Local record is newer. Not storing."); |
michael@0 | 141 | delegate.deferredStoreDelegate(delegateExecutor).onRecordStoreSucceeded(record.guid); |
michael@0 | 142 | return; |
michael@0 | 143 | } |
michael@0 | 144 | if (existing != null) { |
michael@0 | 145 | Logger.debug(LOG_TAG, "Replacing local record."); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | // Store a copy of the record with an updated modified time. |
michael@0 | 149 | Record toStore = record.copyWithIDs(record.guid, record.androidID); |
michael@0 | 150 | if (bumpTimestamps) { |
michael@0 | 151 | toStore.lastModified = now; |
michael@0 | 152 | } |
michael@0 | 153 | wbos.put(record.guid, toStore); |
michael@0 | 154 | |
michael@0 | 155 | trackRecord(toStore); |
michael@0 | 156 | delegate.deferredStoreDelegate(delegateExecutor).onRecordStoreSucceeded(record.guid); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | @Override |
michael@0 | 160 | public void wipe(final RepositorySessionWipeDelegate delegate) { |
michael@0 | 161 | if (!isActive()) { |
michael@0 | 162 | delegate.onWipeFailed(new InactiveSessionException(null)); |
michael@0 | 163 | return; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | Logger.info(LOG_TAG, "Wiping WBORepositorySession."); |
michael@0 | 167 | this.wbos = new ConcurrentHashMap<String, Record>(); |
michael@0 | 168 | |
michael@0 | 169 | // Wipe immediately for the convenience of test code. |
michael@0 | 170 | wboRepository.wbos = new ConcurrentHashMap<String, Record>(); |
michael@0 | 171 | delegate.deferredWipeDelegate(delegateExecutor).onWipeSucceeded(); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | @Override |
michael@0 | 175 | public void finish(RepositorySessionFinishDelegate delegate) throws InactiveSessionException { |
michael@0 | 176 | Logger.info(LOG_TAG, "Finishing WBORepositorySession: handing back " + this.wbos.size() + " WBOs."); |
michael@0 | 177 | wboRepository.wbos = this.wbos; |
michael@0 | 178 | stats.finished = now(); |
michael@0 | 179 | super.finish(delegate); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | @Override |
michael@0 | 183 | public void begin(RepositorySessionBeginDelegate delegate) throws InvalidSessionTransitionException { |
michael@0 | 184 | this.wbos = wboRepository.cloneWBOs(); |
michael@0 | 185 | stats.begun = now(); |
michael@0 | 186 | super.begin(delegate); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | @Override |
michael@0 | 190 | public void storeDone(long end) { |
michael@0 | 191 | // TODO: this is not guaranteed to be called after all of the record |
michael@0 | 192 | // store callbacks have completed! |
michael@0 | 193 | if (stats.storeBegan < 0) { |
michael@0 | 194 | stats.storeBegan = end; |
michael@0 | 195 | } |
michael@0 | 196 | stats.storeCompleted = end; |
michael@0 | 197 | delegate.deferredStoreDelegate(delegateExecutor).onStoreCompleted(end); |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | public ConcurrentHashMap<String, Record> wbos; |
michael@0 | 202 | |
michael@0 | 203 | public WBORepository(boolean bumpTimestamps) { |
michael@0 | 204 | super(); |
michael@0 | 205 | this.bumpTimestamps = bumpTimestamps; |
michael@0 | 206 | this.wbos = new ConcurrentHashMap<String, Record>(); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | public WBORepository() { |
michael@0 | 210 | this(false); |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | public synchronized boolean shouldTrack() { |
michael@0 | 214 | return false; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | @Override |
michael@0 | 218 | public void createSession(RepositorySessionCreationDelegate delegate, |
michael@0 | 219 | Context context) { |
michael@0 | 220 | delegate.deferredCreationDelegate().onSessionCreated(new WBORepositorySession(this)); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | public ConcurrentHashMap<String, Record> cloneWBOs() { |
michael@0 | 224 | ConcurrentHashMap<String, Record> out = new ConcurrentHashMap<String, Record>(); |
michael@0 | 225 | for (Entry<String, Record> entry : wbos.entrySet()) { |
michael@0 | 226 | out.put(entry.getKey(), entry.getValue()); // Assume that records are |
michael@0 | 227 | // immutable. |
michael@0 | 228 | } |
michael@0 | 229 | return out; |
michael@0 | 230 | } |
michael@0 | 231 | } |