mobile/android/base/sync/synchronizer/Synchronizer.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/synchronizer/Synchronizer.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     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.sync.synchronizer;
     1.9 +
    1.10 +import org.mozilla.gecko.background.common.log.Logger;
    1.11 +import org.mozilla.gecko.sync.SynchronizerConfiguration;
    1.12 +import org.mozilla.gecko.sync.repositories.Repository;
    1.13 +import org.mozilla.gecko.sync.repositories.RepositorySessionBundle;
    1.14 +
    1.15 +import android.content.Context;
    1.16 +
    1.17 +/**
    1.18 + * I perform a sync.
    1.19 + *
    1.20 + * Initialize me by calling `load` with a SynchronizerConfiguration.
    1.21 + *
    1.22 + * Start synchronizing by calling `synchronize` with a SynchronizerDelegate. I
    1.23 + * provide coarse-grained feedback by calling my delegate's callback methods.
    1.24 + *
    1.25 + * I always call exactly one of my delegate's `onSynchronized` or
    1.26 + * `onSynchronizeFailed` callback methods. In addition, I call
    1.27 + * `onSynchronizeAborted` before `onSynchronizeFailed` when I encounter a fetch,
    1.28 + * store, or session error while synchronizing.
    1.29 + *
    1.30 + * After synchronizing, call `save` to get back a SynchronizerConfiguration with
    1.31 + * updated bundle information.
    1.32 + */
    1.33 +public class Synchronizer implements SynchronizerSessionDelegate {
    1.34 +  public static final String LOG_TAG = "SyncDelSDelegate";
    1.35 +
    1.36 +  protected String configSyncID; // Used to pass syncID from load() back into save().
    1.37 +
    1.38 +  protected SynchronizerDelegate synchronizerDelegate;
    1.39 +
    1.40 +  protected SynchronizerSession session = null;
    1.41 +
    1.42 +  public SynchronizerSession getSynchronizerSession() {
    1.43 +    return session;
    1.44 +  }
    1.45 +
    1.46 +  @Override
    1.47 +  public void onInitialized(SynchronizerSession session) {
    1.48 +    session.synchronize();
    1.49 +  }
    1.50 +
    1.51 +  @Override
    1.52 +  public void onSynchronized(SynchronizerSession synchronizerSession) {
    1.53 +    Logger.debug(LOG_TAG, "Got onSynchronized.");
    1.54 +    Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate.");
    1.55 +    this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer());
    1.56 +  }
    1.57 +
    1.58 +  @Override
    1.59 +  public void onSynchronizeSkipped(SynchronizerSession synchronizerSession) {
    1.60 +    Logger.debug(LOG_TAG, "Got onSynchronizeSkipped.");
    1.61 +    Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate as if on success.");
    1.62 +    this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer());
    1.63 +  }
    1.64 +
    1.65 +  @Override
    1.66 +  public void onSynchronizeFailed(SynchronizerSession session,
    1.67 +      Exception lastException, String reason) {
    1.68 +    this.synchronizerDelegate.onSynchronizeFailed(session.getSynchronizer(), lastException, reason);
    1.69 +  }
    1.70 +
    1.71 +  public Repository repositoryA;
    1.72 +  public Repository repositoryB;
    1.73 +  public RepositorySessionBundle bundleA;
    1.74 +  public RepositorySessionBundle bundleB;
    1.75 +
    1.76 +  /**
    1.77 +   * Fetch a synchronizer session appropriate for this <code>Synchronizer</code>
    1.78 +   */
    1.79 +  protected SynchronizerSession newSynchronizerSession() {
    1.80 +    return new SynchronizerSession(this, this);
    1.81 +  }
    1.82 +
    1.83 +  /**
    1.84 +   * Start synchronizing, calling delegate's callback methods.
    1.85 +   */
    1.86 +  public void synchronize(Context context, SynchronizerDelegate delegate) {
    1.87 +    this.synchronizerDelegate = delegate;
    1.88 +    this.session = newSynchronizerSession();
    1.89 +    this.session.init(context, bundleA, bundleB);
    1.90 +  }
    1.91 +
    1.92 +  public SynchronizerConfiguration save() {
    1.93 +    return new SynchronizerConfiguration(configSyncID, bundleA, bundleB);
    1.94 +  }
    1.95 +
    1.96 +  /**
    1.97 +   * Set my repository session bundles from a SynchronizerConfiguration.
    1.98 +   *
    1.99 +   * This method is not thread-safe.
   1.100 +   *
   1.101 +   * @param config
   1.102 +   */
   1.103 +  public void load(SynchronizerConfiguration config) {
   1.104 +    bundleA = config.remoteBundle;
   1.105 +    bundleB = config.localBundle;
   1.106 +    configSyncID  = config.syncID;
   1.107 +  }
   1.108 +}

mercurial