mobile/android/base/sync/repositories/StoreTrackingRepositorySession.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.sync.repositories;
     7 import java.util.Collection;
     8 import java.util.Iterator;
    10 import org.mozilla.gecko.background.common.log.Logger;
    11 import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionBeginDelegate;
    12 import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFinishDelegate;
    13 import org.mozilla.gecko.sync.repositories.domain.Record;
    15 public abstract class StoreTrackingRepositorySession extends RepositorySession {
    16   private static final String LOG_TAG = "StoreTrackSession";
    17   protected StoreTracker storeTracker;
    19   protected static StoreTracker createStoreTracker() {
    20     return new HashSetStoreTracker();
    21   }
    23   public StoreTrackingRepositorySession(Repository repository) {
    24     super(repository);
    25   }
    27   @Override
    28   public void begin(RepositorySessionBeginDelegate delegate) throws InvalidSessionTransitionException {
    29     RepositorySessionBeginDelegate deferredDelegate = delegate.deferredBeginDelegate(delegateQueue);
    30     try {
    31       super.sharedBegin();
    32     } catch (InvalidSessionTransitionException e) {
    33       deferredDelegate.onBeginFailed(e);
    34       return;
    35     }
    36     // Or do this in your own subclass.
    37     storeTracker = createStoreTracker();
    38     deferredDelegate.onBeginSucceeded(this);
    39   }
    41   @Override
    42   protected synchronized void trackGUID(String guid) {
    43     if (this.storeTracker == null) {
    44       throw new IllegalStateException("Store tracker not yet initialized!");
    45     }
    46     this.storeTracker.trackRecordForExclusion(guid);
    47   }
    49   @Override
    50   protected synchronized void untrackGUID(String guid) {
    51     if (this.storeTracker == null) {
    52       throw new IllegalStateException("Store tracker not yet initialized!");
    53     }
    54     this.storeTracker.untrackStoredForExclusion(guid);
    55   }
    57   @Override
    58   protected synchronized void untrackGUIDs(Collection<String> guids) {
    59     if (this.storeTracker == null) {
    60       throw new IllegalStateException("Store tracker not yet initialized!");
    61     }
    62     if (guids == null) {
    63       return;
    64     }
    65     for (String guid : guids) {
    66       this.storeTracker.untrackStoredForExclusion(guid);
    67     }
    68   }
    70   protected void trackRecord(Record record) {
    72     Logger.debug(LOG_TAG, "Tracking record " + record.guid +
    73                            " (" + record.lastModified + ") to avoid re-upload.");
    74     // Future: we care about the timestamp…
    75     trackGUID(record.guid);
    76   }
    78   protected void untrackRecord(Record record) {
    79     Logger.debug(LOG_TAG, "Un-tracking record " + record.guid + ".");
    80     untrackGUID(record.guid);
    81   }
    83   @Override
    84   public Iterator<String> getTrackedRecordIDs() {
    85     if (this.storeTracker == null) {
    86       throw new IllegalStateException("Store tracker not yet initialized!");
    87     }
    88     return this.storeTracker.recordsTrackedForExclusion();
    89   }
    91   @Override
    92   public void abort(RepositorySessionFinishDelegate delegate) {
    93     this.storeTracker = null;
    94     super.abort(delegate);
    95   }
    97   @Override
    98   public void finish(RepositorySessionFinishDelegate delegate) throws InactiveSessionException {
    99     super.finish(delegate);
   100     this.storeTracker = null;
   101   }
   102 }

mercurial