Chromium Code Reviews| 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; |
| 11 import android.os.Bundle; | 11 import android.os.Bundle; |
| 12 import android.util.Log; | 12 import android.util.Log; |
| 13 | 13 |
| 14 import com.google.common.annotations.VisibleForTesting; | 14 import com.google.common.annotations.VisibleForTesting; |
| 15 import com.google.common.collect.Lists; | |
| 16 import com.google.common.collect.Sets; | |
| 17 import com.google.ipc.invalidation.external.client.InvalidationListener.Registra tionState; | 15 import com.google.ipc.invalidation.external.client.InvalidationListener.Registra tionState; |
| 18 import com.google.ipc.invalidation.external.client.contrib.AndroidListener; | 16 import com.google.ipc.invalidation.external.client.contrib.AndroidListener; |
| 19 import com.google.ipc.invalidation.external.client.types.ErrorInfo; | 17 import com.google.ipc.invalidation.external.client.types.ErrorInfo; |
| 20 import com.google.ipc.invalidation.external.client.types.Invalidation; | 18 import com.google.ipc.invalidation.external.client.types.Invalidation; |
| 21 import com.google.ipc.invalidation.external.client.types.ObjectId; | 19 import com.google.ipc.invalidation.external.client.types.ObjectId; |
| 22 import com.google.protos.ipc.invalidation.Types.ClientType; | 20 import com.google.protos.ipc.invalidation.Types.ClientType; |
| 23 | 21 |
| 24 import org.chromium.base.ActivityStatus; | 22 import org.chromium.base.ActivityStatus; |
| 25 import org.chromium.sync.internal_api.pub.base.ModelType; | 23 import org.chromium.sync.internal_api.pub.base.ModelType; |
| 26 import org.chromium.sync.notifier.InvalidationController.IntentProtocol; | 24 import org.chromium.sync.notifier.InvalidationController.IntentProtocol; |
| 27 import org.chromium.sync.notifier.InvalidationPreferences.EditContext; | 25 import org.chromium.sync.notifier.InvalidationPreferences.EditContext; |
| 28 import org.chromium.sync.signin.AccountManagerHelper; | 26 import org.chromium.sync.signin.AccountManagerHelper; |
| 29 import org.chromium.sync.signin.ChromeSigninController; | 27 import org.chromium.sync.signin.ChromeSigninController; |
| 30 | 28 |
| 29 import java.util.ArrayList; | |
| 31 import java.util.Collection; | 30 import java.util.Collection; |
| 32 import java.util.Collections; | 31 import java.util.Collections; |
| 32 import java.util.HashSet; | |
| 33 import java.util.List; | 33 import java.util.List; |
| 34 import java.util.Random; | 34 import java.util.Random; |
| 35 import java.util.Set; | 35 import java.util.Set; |
| 36 | 36 |
| 37 import javax.annotation.Nullable; | 37 import javax.annotation.Nullable; |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Service that controls notifications for sync. | 40 * Service that controls notifications for sync. |
| 41 * <p> | 41 * <p> |
| 42 * This service serves two roles. On the one hand, it is a client for the notifi cation system | 42 * This service serves two roles. On the one hand, it is a client for the notifi cation system |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 ensureClientStartState(); | 89 ensureClientStartState(); |
| 90 | 90 |
| 91 // Handle the intent. | 91 // Handle the intent. |
| 92 if (IntentProtocol.isStop(intent) && sIsClientStarted) { | 92 if (IntentProtocol.isStop(intent) && sIsClientStarted) { |
| 93 // If the intent requests that the client be stopped, stop it. | 93 // If the intent requests that the client be stopped, stop it. |
| 94 stopClient(); | 94 stopClient(); |
| 95 } else if (IntentProtocol.isRegisteredTypesChange(intent)) { | 95 } else if (IntentProtocol.isRegisteredTypesChange(intent)) { |
| 96 // If the intent requests a change in registrations, change them. | 96 // If the intent requests a change in registrations, change them. |
| 97 List<String> regTypes = | 97 List<String> regTypes = |
| 98 intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTER ED_TYPES); | 98 intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTER ED_TYPES); |
| 99 setRegisteredTypes(Sets.newHashSet(regTypes)); | 99 setRegisteredTypes(new HashSet<String>(regTypes)); |
| 100 } else { | 100 } else { |
| 101 // 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. |
| 102 super.onHandleIntent(intent); | 102 super.onHandleIntent(intent); |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 | 105 |
| 106 @Override | 106 @Override |
| 107 public void invalidate(Invalidation invalidation, byte[] ackHandle) { | 107 public void invalidate(Invalidation invalidation, byte[] ackHandle) { |
| 108 byte[] payload = invalidation.getPayload(); | 108 byte[] payload = invalidation.getPayload(); |
| 109 String payloadStr = (payload == null) ? null : new String(payload); | 109 String payloadStr = (payload == null) ? null : new String(payload); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 124 } | 124 } |
| 125 | 125 |
| 126 @Override | 126 @Override |
| 127 public void informRegistrationFailure( | 127 public void informRegistrationFailure( |
| 128 byte[] clientId, ObjectId objectId, boolean isTransient, String erro rMessage) { | 128 byte[] clientId, ObjectId objectId, boolean isTransient, String erro rMessage) { |
| 129 Log.w(TAG, "Registration failure on " + objectId + " ; transient = " + i sTransient | 129 Log.w(TAG, "Registration failure on " + objectId + " ; transient = " + i sTransient |
| 130 + ": " + errorMessage); | 130 + ": " + errorMessage); |
| 131 if (isTransient) { | 131 if (isTransient) { |
| 132 // Retry immediately on transient failures. The base AndroidListener w ill handle | 132 // Retry immediately on transient failures. The base AndroidListener w ill handle |
| 133 // exponential backoff if there are repeated failures. | 133 // exponential backoff if there are repeated failures. |
| 134 List<ObjectId> objectIdAsList = Lists.newArrayList(objectId); | 134 List<ObjectId> objectIdAsList = new ArrayList<ObjectId>(); |
|
nyquist
2013/08/20 19:01:57
CollectionUtil.newArrayList here and below.
Yaron
2013/08/20 23:41:31
Done.
| |
| 135 objectIdAsList.add(objectId); | |
| 135 if (readRegistrationsFromPrefs().contains(objectId)) { | 136 if (readRegistrationsFromPrefs().contains(objectId)) { |
| 136 register(clientId, objectIdAsList); | 137 register(clientId, objectIdAsList); |
| 137 } else { | 138 } else { |
| 138 unregister(clientId, objectIdAsList); | 139 unregister(clientId, objectIdAsList); |
| 139 } | 140 } |
| 140 } | 141 } |
| 141 } | 142 } |
| 142 | 143 |
| 143 @Override | 144 @Override |
| 144 public void informRegistrationStatus( | 145 public void informRegistrationStatus( |
| 145 byte[] clientId, ObjectId objectId, RegistrationState regState) { | 146 byte[] clientId, ObjectId objectId, RegistrationState regState) { |
| 146 Log.d(TAG, "Registration status for " + objectId + ": " + regState); | 147 Log.d(TAG, "Registration status for " + objectId + ": " + regState); |
| 147 List<ObjectId> objectIdAsList = Lists.newArrayList(objectId); | 148 List<ObjectId> objectIdAsList = new ArrayList<ObjectId>(); |
| 149 objectIdAsList.add(objectId); | |
| 148 boolean registrationisDesired = readRegistrationsFromPrefs().contains(ob jectId); | 150 boolean registrationisDesired = readRegistrationsFromPrefs().contains(ob jectId); |
| 149 if (regState == RegistrationState.REGISTERED) { | 151 if (regState == RegistrationState.REGISTERED) { |
| 150 if (!registrationisDesired) { | 152 if (!registrationisDesired) { |
| 151 Log.i(TAG, "Unregistering for object we're no longer interested in") ; | 153 Log.i(TAG, "Unregistering for object we're no longer interested in") ; |
| 152 unregister(clientId, objectIdAsList); | 154 unregister(clientId, objectIdAsList); |
| 153 } | 155 } |
| 154 } else { | 156 } else { |
| 155 if (registrationisDesired) { | 157 if (registrationisDesired) { |
| 156 Log.i(TAG, "Registering for an object"); | 158 Log.i(TAG, "Registering for an object"); |
| 157 register(clientId, objectIdAsList); | 159 register(clientId, objectIdAsList); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 // return. Later, when the client is ready, we will supply the new regis trations. | 324 // return. Later, when the client is ready, we will supply the new regis trations. |
| 323 if (sClientId == null) { | 325 if (sClientId == null) { |
| 324 return; | 326 return; |
| 325 } | 327 } |
| 326 | 328 |
| 327 // We do have a ready client. Unregister any existing registrations not present in the | 329 // We do have a ready client. Unregister any existing registrations not present in the |
| 328 // new set and register any elements in the new set not already present. This call does | 330 // new set and register any elements in the new set not already present. This call does |
| 329 // expansion of the ALL_TYPES_TYPE wildcard. | 331 // expansion of the ALL_TYPES_TYPE wildcard. |
| 330 // NOTE: syncTypes MUST NOT be used below this line, since it contains a n unexpanded | 332 // NOTE: syncTypes MUST NOT be used below this line, since it contains a n unexpanded |
| 331 // wildcard. | 333 // wildcard. |
| 332 List<ObjectId> unregistrations = Lists.newArrayList(); | 334 Set<ObjectId> unregistrations = new HashSet<ObjectId>(); |
| 333 List<ObjectId> registrations = Lists.newArrayList(); | 335 Set<ObjectId> registrations = new HashSet<ObjectId>(); |
| 334 computeRegistrationOps(existingRegistrations, | 336 computeRegistrationOps(existingRegistrations, |
| 335 ModelType.syncTypesToObjectIds(syncTypes), | 337 ModelType.syncTypesToObjectIds(syncTypes), |
| 336 registrations, unregistrations); | 338 registrations, unregistrations); |
| 337 unregister(sClientId, unregistrations); | 339 unregister(sClientId, unregistrations); |
| 338 register(sClientId, registrations); | 340 register(sClientId, registrations); |
| 339 } | 341 } |
| 340 | 342 |
| 341 /** | 343 /** |
| 342 * Computes the set of (un)registrations to perform so that the registration s active in the | 344 * Computes the set of (un)registrations to perform so that the registration s active in the |
| 343 * Ticl will be {@code desiredRegs}, given that {@existingRegs} already exis t. | 345 * Ticl will be {@code desiredRegs}, given that {@existingRegs} already exis t. |
| 344 * | 346 * |
| 345 * @param regAccumulator registrations to perform | 347 * @param regAccumulator registrations to perform |
| 346 * @param unregAccumulator unregistrations to perform. | 348 * @param unregAccumulator unregistrations to perform. |
| 347 */ | 349 */ |
| 348 @VisibleForTesting | 350 @VisibleForTesting |
| 349 static void computeRegistrationOps(Set<ObjectId> existingRegs, Set<ObjectId> desiredRegs, | 351 static void computeRegistrationOps(Set<ObjectId> existingRegs, Set<ObjectId> desiredRegs, |
| 350 Collection<ObjectId> regAccumulator, Collection<ObjectId> unregAccum ulator) { | 352 Set<ObjectId> regAccumulator, Set<ObjectId> unregAccumulator) { |
| 353 | |
| 351 // Registrations to do are elements in the new set but not the old set. | 354 // Registrations to do are elements in the new set but not the old set. |
| 352 regAccumulator.addAll(Sets.difference(desiredRegs, existingRegs)); | 355 regAccumulator.addAll(desiredRegs); |
| 356 regAccumulator.removeAll(existingRegs); | |
| 353 | 357 |
| 354 // Unregistrations to do are elements in the old set but not the new set . | 358 // Unregistrations to do are elements in the old set but not the new set . |
| 355 unregAccumulator.addAll(Sets.difference(existingRegs, desiredRegs)); | 359 unregAccumulator.addAll(existingRegs); |
| 360 unregAccumulator.removeAll(desiredRegs); | |
| 356 } | 361 } |
| 357 | 362 |
| 358 /** | 363 /** |
| 359 * Requests that the sync system perform a sync. | 364 * Requests that the sync system perform a sync. |
| 360 * | 365 * |
| 361 * @param objectId the object that changed, if known. | 366 * @param objectId the object that changed, if known. |
| 362 * @param version the version of the object that changed, if known. | 367 * @param version the version of the object that changed, if known. |
| 363 * @param payload the payload of the change, if known. | 368 * @param payload the payload of the change, if known. |
| 364 */ | 369 */ |
| 365 private void requestSync(@Nullable ObjectId objectId, @Nullable Long version , | 370 private void requestSync(@Nullable ObjectId objectId, @Nullable Long version , |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 } | 450 } |
| 446 | 451 |
| 447 private static void setClientId(byte[] clientId) { | 452 private static void setClientId(byte[] clientId) { |
| 448 sClientId = clientId; | 453 sClientId = clientId; |
| 449 } | 454 } |
| 450 | 455 |
| 451 private static void setIsClientStarted(boolean isStarted) { | 456 private static void setIsClientStarted(boolean isStarted) { |
| 452 sIsClientStarted = isStarted; | 457 sIsClientStarted = isStarted; |
| 453 } | 458 } |
| 454 } | 459 } |
| OLD | NEW |