OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.sync; | 5 package org.chromium.chrome.browser.sync; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.app.Application; | 8 import android.app.Application; |
9 import android.content.AbstractThreadedSyncAdapter; | 9 import android.content.AbstractThreadedSyncAdapter; |
10 import android.content.ContentProviderClient; | 10 import android.content.ContentProviderClient; |
11 import android.content.Context; | 11 import android.content.Context; |
12 import android.content.SyncResult; | 12 import android.content.SyncResult; |
13 import android.os.Bundle; | 13 import android.os.Bundle; |
14 import android.os.Handler; | 14 import android.os.Handler; |
15 import android.util.Log; | 15 import android.util.Log; |
16 | 16 |
17 import com.google.common.annotations.VisibleForTesting; | 17 import com.google.common.annotations.VisibleForTesting; |
| 18 import com.google.protos.ipc.invalidation.Types; |
18 | 19 |
19 import org.chromium.base.ThreadUtils; | 20 import org.chromium.base.ThreadUtils; |
20 import org.chromium.content.browser.BrowserStartupController; | 21 import org.chromium.content.browser.BrowserStartupController; |
21 | 22 |
22 import java.util.concurrent.Semaphore; | 23 import java.util.concurrent.Semaphore; |
23 | 24 |
24 /** | 25 /** |
25 * A sync adapter for Chromium. | 26 * A sync adapter for Chromium. |
26 */ | 27 */ |
27 public abstract class ChromiumSyncAdapter extends AbstractThreadedSyncAdapter { | 28 public abstract class ChromiumSyncAdapter extends AbstractThreadedSyncAdapter { |
28 private static final String TAG = "ChromiumSyncAdapter"; | 29 private static final String TAG = "ChromiumSyncAdapter"; |
29 | 30 |
30 // TODO(nyquist) Make these fields package protected once downstream sync ad
apter tests are | 31 // TODO(nyquist) Make these fields package protected once downstream sync ad
apter tests are |
31 // removed. | 32 // removed. |
32 @VisibleForTesting | 33 @VisibleForTesting |
| 34 public static final String INVALIDATION_OBJECT_SOURCE_KEY = "objectSource"; |
| 35 @VisibleForTesting |
33 public static final String INVALIDATION_OBJECT_ID_KEY = "objectId"; | 36 public static final String INVALIDATION_OBJECT_ID_KEY = "objectId"; |
34 @VisibleForTesting | 37 @VisibleForTesting |
35 public static final String INVALIDATION_VERSION_KEY = "version"; | 38 public static final String INVALIDATION_VERSION_KEY = "version"; |
36 @VisibleForTesting | 39 @VisibleForTesting |
37 public static final String INVALIDATION_PAYLOAD_KEY = "payload"; | 40 public static final String INVALIDATION_PAYLOAD_KEY = "payload"; |
38 | 41 |
39 private final Application mApplication; | 42 private final Application mApplication; |
40 private final boolean mAsyncStartup; | 43 private final boolean mAsyncStartup; |
41 | 44 |
42 public ChromiumSyncAdapter(Context context, Application application) { | 45 public ChromiumSyncAdapter(Context context, Application application) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 callback.onFailure(); | 120 callback.onFailure(); |
118 } | 121 } |
119 }); | 122 }); |
120 } | 123 } |
121 } | 124 } |
122 | 125 |
123 private BrowserStartupController.StartupCallback getStartupCallback( | 126 private BrowserStartupController.StartupCallback getStartupCallback( |
124 final Context context, final Account acct, Bundle extras, | 127 final Context context, final Account acct, Bundle extras, |
125 final SyncResult syncResult, final Semaphore semaphore) { | 128 final SyncResult syncResult, final Semaphore semaphore) { |
126 final boolean syncAllTypes = extras.getString(INVALIDATION_OBJECT_ID_KEY
) == null; | 129 final boolean syncAllTypes = extras.getString(INVALIDATION_OBJECT_ID_KEY
) == null; |
| 130 final int objectSource = syncAllTypes ? 0 : extras.getInt(INVALIDATION_O
BJECT_SOURCE_KEY); |
127 final String objectId = syncAllTypes ? "" : extras.getString(INVALIDATIO
N_OBJECT_ID_KEY); | 131 final String objectId = syncAllTypes ? "" : extras.getString(INVALIDATIO
N_OBJECT_ID_KEY); |
128 final long version = syncAllTypes ? 0 : extras.getLong(INVALIDATION_VERS
ION_KEY); | 132 final long version = syncAllTypes ? 0 : extras.getLong(INVALIDATION_VERS
ION_KEY); |
129 final String payload = syncAllTypes ? "" : extras.getString(INVALIDATION
_PAYLOAD_KEY); | 133 final String payload = syncAllTypes ? "" : extras.getString(INVALIDATION
_PAYLOAD_KEY); |
130 | 134 |
131 return new BrowserStartupController.StartupCallback() { | 135 return new BrowserStartupController.StartupCallback() { |
132 @Override | 136 @Override |
133 public void onSuccess(boolean alreadyStarted) { | 137 public void onSuccess(boolean alreadyStarted) { |
134 // Startup succeeded, so we can tickle the sync engine. | 138 // Startup succeeded, so we can tickle the sync engine. |
135 if (syncAllTypes) { | 139 if (syncAllTypes) { |
136 Log.v(TAG, "Received sync tickle for all types."); | 140 Log.v(TAG, "Received sync tickle for all types."); |
137 requestSyncForAllTypes(); | 141 requestSyncForAllTypes(); |
138 } else { | 142 } else { |
139 Log.v(TAG, "Received sync tickle for " + objectId + "."); | 143 // Invalidations persisted before objectSource was added sho
uld be assumed to be |
| 144 // for Sync objects. TODO(stepco): Remove this check once al
l persisted |
| 145 // invalidations can be expected to have the objectSource. |
| 146 int resolvedSource = objectSource; |
| 147 if (resolvedSource == 0) { |
| 148 resolvedSource = Types.ObjectSource.Type.CHROME_SYNC.getNu
mber(); |
| 149 } |
| 150 Log.v(TAG, "Received sync tickle for " + resolvedSource + "
" + objectId + "."); |
| 151 requestSync(resolvedSource, objectId, version, payload); |
| 152 // Call legacy requestSync. See comment on the method. |
140 requestSync(objectId, version, payload); | 153 requestSync(objectId, version, payload); |
141 } | 154 } |
142 semaphore.release(); | 155 semaphore.release(); |
143 } | 156 } |
144 | 157 |
145 @Override | 158 @Override |
146 public void onFailure() { | 159 public void onFailure() { |
147 // The startup failed, so we reset the delayed sync state. | 160 // The startup failed, so we reset the delayed sync state. |
148 DelayedSyncController.getInstance().setDelayedSync(context, acct
.name); | 161 DelayedSyncController.getInstance().setDelayedSync(context, acct
.name); |
149 // Using numIoExceptions so Android will treat this as a soft er
ror. | 162 // Using numIoExceptions so Android will treat this as a soft er
ror. |
150 syncResult.stats.numIoExceptions++; | 163 syncResult.stats.numIoExceptions++; |
151 semaphore.release(); | 164 semaphore.release(); |
152 } | 165 } |
153 }; | 166 }; |
154 } | 167 } |
155 | 168 |
| 169 /** |
| 170 * Legacy requestSync method present to retain compatibility with a test whi
ch overrides it. |
| 171 * TODO(stepco): Remove this method once the dependent test is updated to ov
erride the new |
| 172 * requestSync method. |
| 173 */ |
| 174 public void requestSync(String objectId, long version, String payload) {} |
| 175 |
156 @VisibleForTesting | 176 @VisibleForTesting |
157 public void requestSync(String objectId, long version, String payload) { | 177 public void requestSync(int objectSource, String objectId, long version, Str
ing payload) { |
158 ProfileSyncService.get(mApplication) | 178 ProfileSyncService.get(mApplication) |
159 .requestSyncFromNativeChrome(objectId, version, payload); | 179 .requestSyncFromNativeChrome(objectSource, objectId, version, pa
yload); |
160 } | 180 } |
161 | 181 |
162 @VisibleForTesting | 182 @VisibleForTesting |
163 public void requestSyncForAllTypes() { | 183 public void requestSyncForAllTypes() { |
164 ProfileSyncService.get(mApplication).requestSyncFromNativeChromeForAllTy
pes(); | 184 ProfileSyncService.get(mApplication).requestSyncFromNativeChromeForAllTy
pes(); |
165 } | 185 } |
166 } | 186 } |
OLD | NEW |