OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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.sync.notifier; | 5 package org.chromium.sync.notifier; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.app.PendingIntent; | 8 import android.app.PendingIntent; |
9 import android.content.ContentResolver; | 9 import android.content.ContentResolver; |
10 import android.content.Intent; | 10 import android.content.Intent; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 ensureClientStartState(); | 88 ensureClientStartState(); |
89 | 89 |
90 // Handle the intent. | 90 // Handle the intent. |
91 if (IntentProtocol.isStop(intent) && sIsClientStarted) { | 91 if (IntentProtocol.isStop(intent) && sIsClientStarted) { |
92 // If the intent requests that the client be stopped, stop it. | 92 // If the intent requests that the client be stopped, stop it. |
93 stopClient(); | 93 stopClient(); |
94 } else if (IntentProtocol.isRegisteredTypesChange(intent)) { | 94 } else if (IntentProtocol.isRegisteredTypesChange(intent)) { |
95 // If the intent requests a change in registrations, change them. | 95 // If the intent requests a change in registrations, change them. |
96 List<String> regTypes = | 96 List<String> regTypes = |
97 intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTER
ED_TYPES); | 97 intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTER
ED_TYPES); |
98 setRegisteredTypes(new HashSet<String>(regTypes)); | 98 setRegisteredTypes(regTypes != null ? new HashSet<String>(regTypes)
: null, |
| 99 IntentProtocol.getRegisteredObjectIds(intent)); |
99 } else { | 100 } else { |
100 // Otherwise, we don't recognize the intent. Pass it to the notifica
tion client service. | 101 // Otherwise, we don't recognize the intent. Pass it to the notifica
tion client service. |
101 super.onHandleIntent(intent); | 102 super.onHandleIntent(intent); |
102 } | 103 } |
103 } | 104 } |
104 | 105 |
105 @Override | 106 @Override |
106 public void invalidate(Invalidation invalidation, byte[] ackHandle) { | 107 public void invalidate(Invalidation invalidation, byte[] ackHandle) { |
107 byte[] payload = invalidation.getPayload(); | 108 byte[] payload = invalidation.getPayload(); |
108 String payloadStr = (payload == null) ? null : new String(payload); | 109 String payloadStr = (payload == null) ? null : new String(payload); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 InvalidationPreferences invPrefs = new InvalidationPreferences(this); | 282 InvalidationPreferences invPrefs = new InvalidationPreferences(this); |
282 EditContext editContext = invPrefs.edit(); | 283 EditContext editContext = invPrefs.edit(); |
283 invPrefs.setAccount(editContext, owningAccount); | 284 invPrefs.setAccount(editContext, owningAccount); |
284 invPrefs.commit(editContext); | 285 invPrefs.commit(editContext); |
285 } | 286 } |
286 | 287 |
287 /** | 288 /** |
288 * Reads the saved sync types from storage (if any) and returns a set contai
ning the | 289 * Reads the saved sync types from storage (if any) and returns a set contai
ning the |
289 * corresponding object ids. | 290 * corresponding object ids. |
290 */ | 291 */ |
291 @VisibleForTesting | 292 private Set<ObjectId> readSyncRegistrationsFromPrefs() { |
292 Set<ObjectId> readRegistrationsFromPrefs() { | |
293 Set<String> savedTypes = new InvalidationPreferences(this).getSavedSynce
dTypes(); | 293 Set<String> savedTypes = new InvalidationPreferences(this).getSavedSynce
dTypes(); |
294 if (savedTypes == null) return Collections.emptySet(); | 294 if (savedTypes == null) return Collections.emptySet(); |
295 else return ModelType.syncTypesToObjectIds(savedTypes); | 295 else return ModelType.syncTypesToObjectIds(savedTypes); |
296 } | 296 } |
297 | 297 |
298 /** | 298 /** |
| 299 * Reads the saved non-sync object ids from storage (if any) and returns a s
et containing the |
| 300 * corresponding object ids. |
| 301 */ |
| 302 private Set<ObjectId> readNonSyncRegistrationsFromPrefs() { |
| 303 Set<ObjectId> objectIds = new InvalidationPreferences(this).getSavedObje
ctIds(); |
| 304 if (objectIds == null) return Collections.emptySet(); |
| 305 else return objectIds; |
| 306 } |
| 307 |
| 308 /** |
| 309 * Reads the object registrations from storage (if any) and returns a set co
ntaining the |
| 310 * corresponding object ids. |
| 311 */ |
| 312 @VisibleForTesting |
| 313 Set<ObjectId> readRegistrationsFromPrefs() { |
| 314 return joinRegistrations(readSyncRegistrationsFromPrefs(), |
| 315 readNonSyncRegistrationsFromPrefs()); |
| 316 } |
| 317 |
| 318 /** |
| 319 * Join Sync object registrations with non-Sync object registrations to get
the full set of |
| 320 * desired object registrations. |
| 321 */ |
| 322 private static Set<ObjectId> joinRegistrations(Set<ObjectId> syncRegistratio
ns, |
| 323 Set<ObjectId> nonSyncRegistra
tions) { |
| 324 if (nonSyncRegistrations.isEmpty()) { |
| 325 return syncRegistrations; |
| 326 } |
| 327 if (syncRegistrations.isEmpty()) { |
| 328 return nonSyncRegistrations; |
| 329 } |
| 330 Set<ObjectId> registrations = new HashSet<ObjectId>( |
| 331 syncRegistrations.size() + nonSyncRegistrations.size()); |
| 332 registrations.addAll(syncRegistrations); |
| 333 registrations.addAll(nonSyncRegistrations); |
| 334 return registrations; |
| 335 } |
| 336 |
| 337 /** |
299 * Sets the types for which notifications are required to {@code syncTypes}.
{@code syncTypes} | 338 * Sets the types for which notifications are required to {@code syncTypes}.
{@code syncTypes} |
300 * is either a list of specific types or the special wildcard type | 339 * is either a list of specific types or the special wildcard type |
301 * {@link ModelType#ALL_TYPES_TYPE}. | 340 * {@link ModelType#ALL_TYPES_TYPE}. Also registers for additional objects s
pecified by |
| 341 * {@code objectIds}. Either parameter may be null if the corresponding regi
strations are not |
| 342 * changing. |
302 * <p> | 343 * <p> |
303 * @param syncTypes | 344 * @param syncTypes |
304 */ | 345 */ |
305 private void setRegisteredTypes(Set<String> syncTypes) { | 346 private void setRegisteredTypes(Set<String> syncTypes, Set<ObjectId> objectI
ds) { |
306 // If we have a ready client and will be making registration change call
s on it, then | 347 // If we have a ready client and will be making registration change call
s on it, then |
307 // read the current registrations from preferences before we write the n
ew values, so that | 348 // read the current registrations from preferences before we write the n
ew values, so that |
308 // we can take the diff of the two registration sets and determine which
registration change | 349 // we can take the diff of the two registration sets and determine which
registration change |
309 // calls to make. | 350 // calls to make. |
310 Set<ObjectId> existingRegistrations = (sClientId == null) ? | 351 Set<ObjectId> existingSyncRegistrations = (sClientId == null) ? |
311 null : readRegistrationsFromPrefs(); | 352 null : readSyncRegistrationsFromPrefs(); |
| 353 Set<ObjectId> existingNonSyncRegistrations = (sClientId == null) ? |
| 354 null : readNonSyncRegistrationsFromPrefs(); |
312 | 355 |
313 // Write the new sync types to preferences. We do not expand the syncTyp
es to take into | 356 // Write the new sync types/object ids to preferences. We do not expand
the syncTypes to |
314 // account the ALL_TYPES_TYPE at this point; we want to persist the wild
card unexpanded. | 357 // take into account the ALL_TYPES_TYPE at this point; we want to persis
t the wildcard |
| 358 // unexpanded. |
315 InvalidationPreferences prefs = new InvalidationPreferences(this); | 359 InvalidationPreferences prefs = new InvalidationPreferences(this); |
316 EditContext editContext = prefs.edit(); | 360 EditContext editContext = prefs.edit(); |
317 prefs.setSyncTypes(editContext, syncTypes); | 361 if (syncTypes != null) { |
| 362 prefs.setSyncTypes(editContext, syncTypes); |
| 363 } |
| 364 if (objectIds != null) { |
| 365 prefs.setObjectIds(editContext, objectIds); |
| 366 } |
318 prefs.commit(editContext); | 367 prefs.commit(editContext); |
319 | 368 |
320 // If we do not have a ready invalidation client, we cannot change its r
egistrations, so | 369 // If we do not have a ready invalidation client, we cannot change its r
egistrations, so |
321 // return. Later, when the client is ready, we will supply the new regis
trations. | 370 // return. Later, when the client is ready, we will supply the new regis
trations. |
322 if (sClientId == null) { | 371 if (sClientId == null) { |
323 return; | 372 return; |
324 } | 373 } |
325 | 374 |
326 // We do have a ready client. Unregister any existing registrations not
present in the | 375 // We do have a ready client. Unregister any existing registrations not
present in the |
327 // new set and register any elements in the new set not already present.
This call does | 376 // new set and register any elements in the new set not already present.
This call does |
328 // expansion of the ALL_TYPES_TYPE wildcard. | 377 // expansion of the ALL_TYPES_TYPE wildcard. |
329 // NOTE: syncTypes MUST NOT be used below this line, since it contains a
n unexpanded | 378 // NOTE: syncTypes MUST NOT be used below this line, since it contains a
n unexpanded |
330 // wildcard. | 379 // wildcard. |
| 380 // When computing the desired set of object ids, if only sync types were
provided, then |
| 381 // keep the existing non-sync types, and vice-versa. |
| 382 Set<ObjectId> desiredSyncRegistrations = syncTypes != null ? |
| 383 ModelType.syncTypesToObjectIds(syncTypes) : existingSyncRegistra
tions; |
| 384 Set<ObjectId> desiredNonSyncRegistrations = objectIds != null ? |
| 385 objectIds : existingNonSyncRegistrations; |
| 386 Set<ObjectId> desiredRegistrations = joinRegistrations(desiredNonSyncReg
istrations, |
| 387 desiredSyncRegistrations); |
| 388 Set<ObjectId> existingRegistrations = joinRegistrations(existingNonSyncR
egistrations, |
| 389 existingSyncRegistrations); |
| 390 |
331 Set<ObjectId> unregistrations = new HashSet<ObjectId>(); | 391 Set<ObjectId> unregistrations = new HashSet<ObjectId>(); |
332 Set<ObjectId> registrations = new HashSet<ObjectId>(); | 392 Set<ObjectId> registrations = new HashSet<ObjectId>(); |
333 computeRegistrationOps(existingRegistrations, | 393 computeRegistrationOps(existingRegistrations, desiredRegistrations, |
334 ModelType.syncTypesToObjectIds(syncTypes), | |
335 registrations, unregistrations); | 394 registrations, unregistrations); |
336 unregister(sClientId, unregistrations); | 395 unregister(sClientId, unregistrations); |
337 register(sClientId, registrations); | 396 register(sClientId, registrations); |
338 } | 397 } |
339 | 398 |
340 /** | 399 /** |
341 * Computes the set of (un)registrations to perform so that the registration
s active in the | 400 * Computes the set of (un)registrations to perform so that the registration
s active in the |
342 * Ticl will be {@code desiredRegs}, given that {@existingRegs} already exis
t. | 401 * Ticl will be {@code desiredRegs}, given that {@existingRegs} already exis
t. |
343 * | 402 * |
344 * @param regAccumulator registrations to perform | 403 * @param regAccumulator registrations to perform |
(...skipping 20 matching lines...) Expand all Loading... |
365 * @param payload the payload of the change, if known. | 424 * @param payload the payload of the change, if known. |
366 */ | 425 */ |
367 private void requestSync(@Nullable ObjectId objectId, @Nullable Long version
, | 426 private void requestSync(@Nullable ObjectId objectId, @Nullable Long version
, |
368 @Nullable String payload) { | 427 @Nullable String payload) { |
369 // Construct the bundle to supply to the native sync code. | 428 // Construct the bundle to supply to the native sync code. |
370 Bundle bundle = new Bundle(); | 429 Bundle bundle = new Bundle(); |
371 if (objectId == null && version == null && payload == null) { | 430 if (objectId == null && version == null && payload == null) { |
372 // Use an empty bundle in this case for compatibility with the v1 im
plementation. | 431 // Use an empty bundle in this case for compatibility with the v1 im
plementation. |
373 } else { | 432 } else { |
374 if (objectId != null) { | 433 if (objectId != null) { |
| 434 bundle.putInt("objectSource", objectId.getSource()); |
375 bundle.putString("objectId", new String(objectId.getName())); | 435 bundle.putString("objectId", new String(objectId.getName())); |
376 } | 436 } |
377 // We use "0" as the version if we have an unknown-version invalidat
ion. This is OK | 437 // We use "0" as the version if we have an unknown-version invalidat
ion. This is OK |
378 // because the native sync code special-cases zero and always syncs
for invalidations at | 438 // because the native sync code special-cases zero and always syncs
for invalidations at |
379 // that version (Tango defines a special UNKNOWN_VERSION constant wi
th this value). | 439 // that version (Tango defines a special UNKNOWN_VERSION constant wi
th this value). |
380 bundle.putLong("version", (version == null) ? 0 : version); | 440 bundle.putLong("version", (version == null) ? 0 : version); |
381 bundle.putString("payload", (payload == null) ? "" : payload); | 441 bundle.putString("payload", (payload == null) ? "" : payload); |
382 } | 442 } |
383 Account account = ChromeSigninController.get(this).getSignedInUser(); | 443 Account account = ChromeSigninController.get(this).getSignedInUser(); |
384 String contractAuthority = SyncStatusHelper.get(this).getContractAuthori
ty(); | 444 String contractAuthority = SyncStatusHelper.get(this).getContractAuthori
ty(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 } | 507 } |
448 | 508 |
449 private static void setClientId(byte[] clientId) { | 509 private static void setClientId(byte[] clientId) { |
450 sClientId = clientId; | 510 sClientId = clientId; |
451 } | 511 } |
452 | 512 |
453 private static void setIsClientStarted(boolean isStarted) { | 513 private static void setIsClientStarted(boolean isStarted) { |
454 sIsClientStarted = isStarted; | 514 sIsClientStarted = isStarted; |
455 } | 515 } |
456 } | 516 } |
OLD | NEW |