|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.synchronizer; |
|
6 |
|
7 import org.mozilla.gecko.background.common.log.Logger; |
|
8 import org.mozilla.gecko.sync.SynchronizerConfiguration; |
|
9 import org.mozilla.gecko.sync.repositories.Repository; |
|
10 import org.mozilla.gecko.sync.repositories.RepositorySessionBundle; |
|
11 |
|
12 import android.content.Context; |
|
13 |
|
14 /** |
|
15 * I perform a sync. |
|
16 * |
|
17 * Initialize me by calling `load` with a SynchronizerConfiguration. |
|
18 * |
|
19 * Start synchronizing by calling `synchronize` with a SynchronizerDelegate. I |
|
20 * provide coarse-grained feedback by calling my delegate's callback methods. |
|
21 * |
|
22 * I always call exactly one of my delegate's `onSynchronized` or |
|
23 * `onSynchronizeFailed` callback methods. In addition, I call |
|
24 * `onSynchronizeAborted` before `onSynchronizeFailed` when I encounter a fetch, |
|
25 * store, or session error while synchronizing. |
|
26 * |
|
27 * After synchronizing, call `save` to get back a SynchronizerConfiguration with |
|
28 * updated bundle information. |
|
29 */ |
|
30 public class Synchronizer implements SynchronizerSessionDelegate { |
|
31 public static final String LOG_TAG = "SyncDelSDelegate"; |
|
32 |
|
33 protected String configSyncID; // Used to pass syncID from load() back into save(). |
|
34 |
|
35 protected SynchronizerDelegate synchronizerDelegate; |
|
36 |
|
37 protected SynchronizerSession session = null; |
|
38 |
|
39 public SynchronizerSession getSynchronizerSession() { |
|
40 return session; |
|
41 } |
|
42 |
|
43 @Override |
|
44 public void onInitialized(SynchronizerSession session) { |
|
45 session.synchronize(); |
|
46 } |
|
47 |
|
48 @Override |
|
49 public void onSynchronized(SynchronizerSession synchronizerSession) { |
|
50 Logger.debug(LOG_TAG, "Got onSynchronized."); |
|
51 Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate."); |
|
52 this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer()); |
|
53 } |
|
54 |
|
55 @Override |
|
56 public void onSynchronizeSkipped(SynchronizerSession synchronizerSession) { |
|
57 Logger.debug(LOG_TAG, "Got onSynchronizeSkipped."); |
|
58 Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate as if on success."); |
|
59 this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer()); |
|
60 } |
|
61 |
|
62 @Override |
|
63 public void onSynchronizeFailed(SynchronizerSession session, |
|
64 Exception lastException, String reason) { |
|
65 this.synchronizerDelegate.onSynchronizeFailed(session.getSynchronizer(), lastException, reason); |
|
66 } |
|
67 |
|
68 public Repository repositoryA; |
|
69 public Repository repositoryB; |
|
70 public RepositorySessionBundle bundleA; |
|
71 public RepositorySessionBundle bundleB; |
|
72 |
|
73 /** |
|
74 * Fetch a synchronizer session appropriate for this <code>Synchronizer</code> |
|
75 */ |
|
76 protected SynchronizerSession newSynchronizerSession() { |
|
77 return new SynchronizerSession(this, this); |
|
78 } |
|
79 |
|
80 /** |
|
81 * Start synchronizing, calling delegate's callback methods. |
|
82 */ |
|
83 public void synchronize(Context context, SynchronizerDelegate delegate) { |
|
84 this.synchronizerDelegate = delegate; |
|
85 this.session = newSynchronizerSession(); |
|
86 this.session.init(context, bundleA, bundleB); |
|
87 } |
|
88 |
|
89 public SynchronizerConfiguration save() { |
|
90 return new SynchronizerConfiguration(configSyncID, bundleA, bundleB); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Set my repository session bundles from a SynchronizerConfiguration. |
|
95 * |
|
96 * This method is not thread-safe. |
|
97 * |
|
98 * @param config |
|
99 */ |
|
100 public void load(SynchronizerConfiguration config) { |
|
101 bundleA = config.remoteBundle; |
|
102 bundleB = config.localBundle; |
|
103 configSyncID = config.syncID; |
|
104 } |
|
105 } |