summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-12 15:36:23 +0000
committerrlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-12 15:36:23 +0000
commit268f79d2383e23866118e572aac3ff5d27f02486 (patch)
treeec195bf41a44a73bbc5fd49fc5ab53964789ee10
parent87d0e6ce87ee97a7936796b6c94ffd5ed1cf323a (diff)
downloadchromium_src-268f79d2383e23866118e572aac3ff5d27f02486.zip
chromium_src-268f79d2383e23866118e572aac3ff5d27f02486.tar.gz
chromium_src-268f79d2383e23866118e572aac3ff5d27f02486.tar.bz2
Support for shared invalidator client IDs
Allow the Java side of the Andorid invalidations implementation to share its client ID with its associated C++ classes. The InvalidationController class will prefer to use an ID from a provided UniqueIdentificationGeneratorFactory. This ID would have the advantage of persisting across restarts, which is necessary to ensure effective reflection blocking. If the UniqueIdentificationGeneratorFactory has not been provided, as may be the case in tests, the InvalidationController will generate its own temporary ID. This will effectievely disable reflection blocking, but it's better than returning a consistent ID (since that would risk blocking notifications that we actually need). This randomly generated ID is not much different from the randomly generated ID created by the InvalidationService prior to this CL. At the time of this writing, the code to set up the UniqueIDGeneratorFactory is not in place. This will be added in a later commit. Until then, reflection blocking will remain broken. BUG=172391 Review URL: https://codereview.chromium.org/54923003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@234524 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/android/java/src/org/chromium/chrome/browser/invalidation/InvalidationController.java53
-rw-r--r--chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationControllerTest.java60
-rw-r--r--chrome/browser/invalidation/invalidation_controller_android.cc23
-rw-r--r--chrome/browser/invalidation/invalidation_controller_android.h5
-rw-r--r--chrome/browser/invalidation/invalidation_service_android.cc3
-rw-r--r--chrome/browser/invalidation/invalidation_service_android_unittest.cc28
-rw-r--r--sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java5
-rw-r--r--sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java26
-rw-r--r--sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java91
9 files changed, 246 insertions, 48 deletions
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/invalidation/InvalidationController.java b/chrome/android/java/src/org/chromium/chrome/browser/invalidation/InvalidationController.java
index a6a56da..1383c17 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/invalidation/InvalidationController.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/invalidation/InvalidationController.java
@@ -7,18 +7,22 @@ package org.chromium.chrome.browser.invalidation;
import android.accounts.Account;
import android.content.Context;
import android.content.Intent;
+import android.util.Base64;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.chromium.base.ActivityStatus;
import org.chromium.base.CalledByNative;
+import org.chromium.chrome.browser.identity.UniqueIdentificationGeneratorFactory;
+import org.chromium.chrome.browser.identity.UniqueIdentificationGenerator;
import org.chromium.sync.internal_api.pub.base.ModelType;
import org.chromium.sync.notifier.InvalidationIntentProtocol;
import org.chromium.sync.notifier.InvalidationPreferences;
import org.chromium.sync.notifier.InvalidationService;
import org.chromium.sync.notifier.SyncStatusHelper;
+import java.util.Random;
import java.util.Set;
/**
@@ -28,10 +32,22 @@ import java.util.Set;
public class InvalidationController implements ActivityStatus.StateListener {
private static final Object LOCK = new Object();
+ // Key to use when initializing the UniqueIdentificationGeneratorFactory's entry for this class.
+ public static final String ID_GENERATOR = "INVALIDATION_CONTROLLER_ID_GENERATOR";
+
+ // Pref key to use for UUID-based generator.
+ public static final String INVALIDATIONS_UUID_PREF_KEY = "chromium.invalidations.uuid";
+
+ private static final Random RANDOM = new Random();
+
private static InvalidationController sInstance;
+ private final Object mLock = new Object();
+
private final Context mContext;
+ private byte[] mUniqueId;
+
/**
* Sets the types for which the client should register for notifications.
*
@@ -43,6 +59,8 @@ public class InvalidationController implements ActivityStatus.StateListener {
Intent registerIntent =
InvalidationIntentProtocol.createRegisterIntent(account, allTypes, types);
registerIntent.setClass(mContext, InvalidationService.class);
+ registerIntent.putExtra(
+ InvalidationIntentProtocol.EXTRA_CLIENT_NAME, getInvalidatorClientId());
mContext.startService(registerIntent);
}
@@ -77,6 +95,8 @@ public class InvalidationController implements ActivityStatus.StateListener {
InvalidationIntentProtocol.createRegisterIntent(
account, objectSources, objectNames);
registerIntent.setClass(mContext, InvalidationService.class);
+ registerIntent.putExtra(
+ InvalidationIntentProtocol.EXTRA_CLIENT_NAME, getInvalidatorClientId());
mContext.startService(registerIntent);
}
@@ -85,6 +105,8 @@ public class InvalidationController implements ActivityStatus.StateListener {
*/
public void start() {
Intent intent = new Intent(mContext, InvalidationService.class);
+ intent.putExtra(
+ InvalidationIntentProtocol.EXTRA_CLIENT_NAME, getInvalidatorClientId());
mContext.startService(intent);
}
@@ -94,6 +116,8 @@ public class InvalidationController implements ActivityStatus.StateListener {
public void stop() {
Intent intent = new Intent(mContext, InvalidationService.class);
intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
+ intent.putExtra(
+ InvalidationIntentProtocol.EXTRA_CLIENT_NAME, getInvalidatorClientId());
mContext.startService(intent);
}
@@ -131,4 +155,33 @@ public class InvalidationController implements ActivityStatus.StateListener {
}
}
}
+
+ @CalledByNative
+ public byte[] getInvalidatorClientId() {
+ synchronized(mLock) {
+ if (mUniqueId != null) {
+ return mUniqueId;
+ }
+
+ try {
+ UniqueIdentificationGenerator generator =
+ UniqueIdentificationGeneratorFactory.getInstance(ID_GENERATOR);
+ mUniqueId = generator.getUniqueId(null).getBytes();
+ } catch (RuntimeException e) {
+ // Tests won't have a generator available. We need to handle them differently. But
+ // it would be dangerous to return a hard-coded string, since that could break
+ // invalidations if it was ever called in a real browser instance.
+ //
+ // A randomly generated ID is less harmful. We'll add a "BadID" prefix to it so
+ // hopefully someone will notice and file a bug if we ever used it in practice.
+ byte[] randomBytes = new byte[8];
+ RANDOM.nextBytes(randomBytes);
+ String encoded =
+ Base64.encodeToString(randomBytes, 0, randomBytes.length, Base64.NO_WRAP);
+ String idString = "BadID" + encoded;
+ mUniqueId = idString.getBytes();
+ }
+ return mUniqueId;
+ }
+ }
}
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationControllerTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationControllerTest.java
index aada9f2..3e05ffb 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationControllerTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationControllerTest.java
@@ -19,6 +19,8 @@ import org.chromium.base.ActivityStatus;
import org.chromium.base.CollectionUtil;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.Feature;
+import org.chromium.chrome.browser.identity.UniqueIdentificationGenerator;
+import org.chromium.chrome.browser.identity.UniqueIdentificationGeneratorFactory;
import org.chromium.sync.internal_api.pub.base.ModelType;
import org.chromium.sync.notifier.InvalidationIntentProtocol;
import org.chromium.sync.notifier.InvalidationPreferences;
@@ -28,6 +30,7 @@ import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController;
import org.chromium.sync.test.util.MockSyncContentResolverDelegate;
+import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -35,6 +38,8 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
+import javax.annotation.Nullable;
+
/**
* Tests for the {@link InvalidationController}.
*/
@@ -60,7 +65,7 @@ public class InvalidationControllerTest extends InstrumentationTestCase {
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
- assertNull(intent.getExtras());
+ assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME));
}
@SmallTest
@@ -70,7 +75,8 @@ public class InvalidationControllerTest extends InstrumentationTestCase {
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
- assertEquals(1, intent.getExtras().size());
+ assertEquals(2, intent.getExtras().size());
+ assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME));
assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_STOP));
assertTrue(intent.getBooleanExtra(InvalidationIntentProtocol.EXTRA_STOP, false));
}
@@ -84,7 +90,6 @@ public class InvalidationControllerTest extends InstrumentationTestCase {
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
- assertNull(intent.getExtras());
}
@SmallTest
@@ -105,7 +110,8 @@ public class InvalidationControllerTest extends InstrumentationTestCase {
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
- assertEquals(1, intent.getExtras().size());
+ assertEquals(2, intent.getExtras().size());
+ assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME));
assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_STOP));
assertTrue(intent.getBooleanExtra(InvalidationIntentProtocol.EXTRA_STOP, false));
}
@@ -312,6 +318,41 @@ public class InvalidationControllerTest extends InstrumentationTestCase {
assertTrue(objectIds.contains(ObjectId.newInstance(2, "b".getBytes())));
}
+ @SmallTest
+ @Feature({"Sync"})
+ public void testFallbackClientId() {
+ // Test that the InvalidationController consistently returns the same ID even when it has to
+ // resort to its "fallback" ID generation code.
+ InvalidationController controller = new InvalidationController(mContext);
+ byte[] id1 = controller.getInvalidatorClientId();
+ byte[] id2 = controller.getInvalidatorClientId();
+
+ assertTrue(Arrays.equals(id1, id2));
+
+ // Even if initialize the generator late, the ID will remain consistent.
+ registerHardCodedIdGenerator();
+
+ byte[] id3 = controller.getInvalidatorClientId();
+ assertTrue(Arrays.equals(id2, id3));
+ }
+
+ @SmallTest
+ @Feature({"Sync"})
+ public void testPreRegisteredClientId() {
+ registerHardCodedIdGenerator();
+
+ UniqueIdentificationGenerator generator =
+ UniqueIdentificationGeneratorFactory.getInstance(InvalidationController.ID_GENERATOR);
+ String generatorId = generator.getUniqueId(null);
+
+ InvalidationController controller = new InvalidationController(mContext);
+ byte[] id = controller.getInvalidatorClientId();
+ byte[] id2 = controller.getInvalidatorClientId();
+
+ assertTrue(Arrays.equals(generatorId.getBytes(), id));
+ assertTrue(Arrays.equals(id, id2));
+ }
+
/**
* Asserts that {@code intent} is destined for the correct component.
*/
@@ -350,4 +391,15 @@ public class InvalidationControllerTest extends InstrumentationTestCase {
return getBaseContext().getPackageManager();
}
}
+
+ private static void registerHardCodedIdGenerator() {
+ UniqueIdentificationGeneratorFactory.registerGenerator(
+ InvalidationController.ID_GENERATOR,
+ new UniqueIdentificationGenerator() {
+ public String getUniqueId(@Nullable String salt) {
+ return "Testable ID";
+ }
+ },
+ true);
+ }
}
diff --git a/chrome/browser/invalidation/invalidation_controller_android.cc b/chrome/browser/invalidation/invalidation_controller_android.cc
index c153df1..021027c 100644
--- a/chrome/browser/invalidation/invalidation_controller_android.cc
+++ b/chrome/browser/invalidation/invalidation_controller_android.cc
@@ -45,6 +45,29 @@ void InvalidationControllerAndroid::SetRegisteredObjectIds(
base::android::ToJavaArrayOfStrings(env, names).obj());
}
+std::string InvalidationControllerAndroid::GetInvalidatorClientId() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+ if (invalidation_controller_.is_null()) {
+ invalidation_controller_.Reset(Java_InvalidationController_get(
+ env,
+ base::android::GetApplicationContext()));
+ }
+
+ // Ask the Java code to for the invalidator ID it's currently using.
+ base::android::ScopedJavaLocalRef<_jbyteArray*> id_bytes_java =
+ Java_InvalidationController_getInvalidatorClientId(
+ env,
+ invalidation_controller_.obj());
+
+ // Convert it into a more convenient format for C++.
+ std::vector<uint8_t> id_bytes;
+ base::android::JavaByteArrayToByteVector(env, id_bytes_java.obj(), &id_bytes);
+ std::string id(id_bytes.begin(), id_bytes.end());
+
+ return id;
+}
+
bool RegisterInvalidationController(JNIEnv* env) {
return RegisterNativesImpl(env);
}
diff --git a/chrome/browser/invalidation/invalidation_controller_android.h b/chrome/browser/invalidation/invalidation_controller_android.h
index 28fc0c5..05839ad 100644
--- a/chrome/browser/invalidation/invalidation_controller_android.h
+++ b/chrome/browser/invalidation/invalidation_controller_android.h
@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_INVALIDATION_INVALIDATION_CONTROLLER_ANDROID_H_
#define CHROME_BROWSER_INVALIDATION_INVALIDATION_CONTROLLER_ANDROID_H_
+#include <string>
+
#include "base/android/jni_android.h"
#include "base/android/jni_helper.h"
#include "sync/notifier/invalidation_util.h"
@@ -22,6 +24,9 @@ class InvalidationControllerAndroid {
// notification.
virtual void SetRegisteredObjectIds(const syncer::ObjectIdSet& ids);
+ // Asks the Java code to return the client ID it chose to use.
+ std::string GetInvalidatorClientId();
+
private:
// The Java invalidation controller.
base::android::ScopedJavaGlobalRef<jobject> invalidation_controller_;
diff --git a/chrome/browser/invalidation/invalidation_service_android.cc b/chrome/browser/invalidation/invalidation_service_android.cc
index 72a7046..4bada4b 100644
--- a/chrome/browser/invalidation/invalidation_service_android.cc
+++ b/chrome/browser/invalidation/invalidation_service_android.cc
@@ -59,8 +59,7 @@ InvalidationServiceAndroid::GetInvalidatorState() const {
std::string InvalidationServiceAndroid::GetInvalidatorClientId() const {
DCHECK(CalledOnValidThread());
- // TODO: Return a valid ID here. See crbug.com/172391.
- return "Bogus";
+ return invalidation_controller_->GetInvalidatorClientId();
}
void InvalidationServiceAndroid::Observe(
diff --git a/chrome/browser/invalidation/invalidation_service_android_unittest.cc b/chrome/browser/invalidation/invalidation_service_android_unittest.cc
index e97137b..d652bee 100644
--- a/chrome/browser/invalidation/invalidation_service_android_unittest.cc
+++ b/chrome/browser/invalidation/invalidation_service_android_unittest.cc
@@ -145,4 +145,32 @@ TEST_F(InvalidationServiceAndroidRegistrationTest, UpdateObjectRegistration) {
invalidation_service().UnregisterInvalidationHandler(&handler);
}
+#if defined(OS_ANDROID)
+
+class InvalidationServiceAndroidTest : public testing::Test {
+ public:
+ InvalidationServiceAndroidTest()
+ : invalidation_service_(&profile_, new InvalidationControllerAndroid()) {}
+ virtual ~InvalidationServiceAndroidTest() {}
+
+ InvalidationService& invalidation_service() {
+ return invalidation_service_;
+ }
+
+ private:
+ TestingProfile profile_;
+ InvalidationServiceAndroid invalidation_service_;
+};
+
+TEST_F(InvalidationServiceAndroidTest, FetchClientId) {
+ const std::string id1 = invalidation_service().GetInvalidatorClientId();
+ ASSERT_FALSE(id1.empty());
+
+ // If nothing else, the ID should be consistent.
+ const std::string id2 = invalidation_service().GetInvalidatorClientId();
+ ASSERT_EQ(id1, id2);
+}
+
+#endif
+
} // namespace invalidation
diff --git a/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java b/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java
index c87b894..e3d5135 100644
--- a/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java
+++ b/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java
@@ -35,6 +35,11 @@ public class InvalidationIntentProtocol {
public static final String EXTRA_ACCOUNT = "account";
/**
+ * byte[]-valued intent extra containing the unique client ID.
+ */
+ public static final String EXTRA_CLIENT_NAME = "client_name";
+
+ /**
* String-list-valued intent extra of the syncable types to sync.
*/
public static final String EXTRA_REGISTERED_TYPES = "registered_types";
diff --git a/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java b/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java
index ea3e73a..cd8b605 100644
--- a/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java
+++ b/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java
@@ -75,6 +75,11 @@ public class InvalidationService extends AndroidListener {
*/
@Nullable private static byte[] sClientId;
+ /**
+ * A ID that uniquely identifies this client. Used for reflection blocking.
+ */
+ @Nullable private byte[] mClientName;
+
@Override
public void onHandleIntent(Intent intent) {
// Ensure that a client is or is not running, as appropriate, and that it is for the
@@ -85,6 +90,17 @@ public class InvalidationService extends AndroidListener {
Account account = intent.hasExtra(InvalidationIntentProtocol.EXTRA_ACCOUNT) ?
(Account) intent.getParcelableExtra(InvalidationIntentProtocol.EXTRA_ACCOUNT)
: null;
+
+ // Any intents sent to the InvalidationService should include the EXTRA_CLIENT_NAME. The
+ // call to ensureClientStartState() might need a client name, and would break if we don't
+ // have one.
+ //
+ // Intents that are addressed to the AndroidListener portion of this class do not need to
+ // include the EXTRA_CLIENT_NAME.
+ if (intent.hasExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME)) {
+ mClientName = intent.getByteArrayExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME);
+ }
+
ensureAccount(account);
ensureClientStartState();
@@ -265,7 +281,8 @@ public class InvalidationService extends AndroidListener {
* {@link InvalidationPreferences#setAccount}.
*/
private void startClient() {
- Intent startIntent = AndroidListener.createStartIntent(this, CLIENT_TYPE, getClientName());
+ assert (mClientName != null);
+ Intent startIntent = AndroidListener.createStartIntent(this, CLIENT_TYPE, mClientName);
startService(startIntent);
setIsClientStarted(true);
}
@@ -496,13 +513,6 @@ public class InvalidationService extends AndroidListener {
return "oauth2:" + SyncStatusHelper.CHROME_SYNC_OAUTH2_SCOPE;
}
- /** Returns the client name used for the notification client. */
- private static byte[] getClientName() {
- // TODO(dsmyers): we should use the same client name as the native sync code.
- // Bug: https://code.google.com/p/chromium/issues/detail?id=172391
- return Long.toString(RANDOM.nextLong()).getBytes();
- }
-
private static void setClientId(byte[] clientId) {
sClientId = clientId;
}
diff --git a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java
index f0d021a..b4b536f 100644
--- a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java
+++ b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java
@@ -41,6 +41,9 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
/** Id used when creating clients. */
private static final byte[] CLIENT_ID = new byte[]{0, 4, 7};
+ /** Id used to uniquely name this client instance. */
+ private static final byte[] TEST_CLIENT_NAME = "UNIQUE_CLIENT_NAME".getBytes();
+
/** Intents provided to {@link #startService}. */
private List<Intent> mStartServiceIntents;
@@ -65,7 +68,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
@Override
public void tearDown() throws Exception {
if (InvalidationService.getIsClientStartedForTest()) {
- Intent stopIntent = new Intent().putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
+ Intent stopIntent = createStopIntent();
getService().onHandleIntent(stopIntent);
}
assertFalse(InvalidationService.getIsClientStartedForTest());
@@ -314,7 +317,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Client needs to be started for the permament error to trigger and stop.
getService().setShouldRunStates(true, true);
getService().onCreate();
- getService().onHandleIntent(new Intent());
+ getService().onHandleIntent(createStartIntent());
getService().mStartedServices.clear(); // Discard start intent.
// Transient error.
@@ -465,11 +468,11 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
getService().setShouldRunStates(true, true);
getService().onCreate();
- Intent startIntent = new Intent();
+ Intent startIntent = createStartIntent();
getService().onHandleIntent(startIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
- Intent stopIntent = new Intent().putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
+ Intent stopIntent = createStopIntent();
getService().onHandleIntent(stopIntent);
assertFalse(InvalidationService.getIsClientStartedForTest());
@@ -491,7 +494,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
getService().onCreate();
// Start the service.
- Intent startIntent = new Intent();
+ Intent startIntent = createStartIntent();
getService().onHandleIntent(startIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
@@ -523,8 +526,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
Set<ModelType> desiredRegistrations = CollectionUtil.newHashSet(
ModelType.BOOKMARK, ModelType.SESSION);
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
- Intent registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false,
- desiredRegistrations);
+ Intent registrationIntent = createRegisterIntent(account, false, desiredRegistrations);
getService().onHandleIntent(registrationIntent);
// Verify client started and state written.
@@ -539,8 +541,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Send another registration-change intent, this type with all-types set to true, and
// verify that the on-disk state is updated and that no addition Intents are issued.
- getService().onHandleIntent(
- InvalidationIntentProtocol.createRegisterIntent(account, true, null));
+ getService().onHandleIntent(createRegisterIntent(account, true, null));
assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE),
invPrefs.getSavedSyncedTypes());
@@ -550,8 +551,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// and verify that it both updates the account, stops thye existing client, and
// starts a new client.
Account account2 = AccountManagerHelper.createAccountFromName("test2@example.com");
- getService().onHandleIntent(
- InvalidationIntentProtocol.createRegisterIntent(account2, true, null));
+ getService().onHandleIntent(createRegisterIntent(account2, true, null));
assertEquals(account2, invPrefs.getSavedSyncedAccount());
assertEquals(3, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
@@ -614,15 +614,14 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
objectIds.add(ObjectId.newInstance(1, "obj1".getBytes()));
objectIds.add(ObjectId.newInstance(2, "obj2".getBytes()));
Intent registrationIntent =
- InvalidationIntentProtocol.createRegisterIntent(account, new int[] {1, 2},
- new String[] {"obj1", "obj2"});
+ createRegisterIntent(account, new int[] {1, 2}, new String[] {"obj1", "obj2"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, false /* isReady */));
// Register for some types.
types.add(ModelType.BOOKMARK);
types.add(ModelType.SESSION);
- registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false, types);
+ registrationIntent = createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, false /* isReady */));
@@ -632,43 +631,40 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Change object id registration with types registered.
objectIds.add(ObjectId.newInstance(3, "obj3".getBytes()));
- registrationIntent =
- InvalidationIntentProtocol.createRegisterIntent(account, new int[] {1, 2, 3},
- new String[] {"obj1", "obj2", "obj3"});
+ registrationIntent = createRegisterIntent(
+ account, new int[] {1, 2, 3}, new String[] {"obj1", "obj2", "obj3"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Change type registration with object ids registered.
types.remove(ModelType.BOOKMARK);
- registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false, types);
+ registrationIntent = createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Unregister all types.
types.clear();
- registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false, types);
+ registrationIntent = createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Change object id registration with no types registered.
objectIds.remove(ObjectId.newInstance(2, "obj2".getBytes()));
- registrationIntent =
- InvalidationIntentProtocol.createRegisterIntent(account, new int[] {1, 3},
- new String[] {"obj1", "obj3"});
+ registrationIntent = createRegisterIntent(
+ account, new int[] {1, 3}, new String[] {"obj1", "obj3"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Unregister all object ids.
objectIds.clear();
- registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, new int[0],
- new String[0]);
+ registrationIntent = createRegisterIntent(account, new int[0], new String[0]);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Change type registration with no object ids registered.
types.add(ModelType.BOOKMARK);
types.add(ModelType.PASSWORD);
- registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false, types);
+ registrationIntent = createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
}
@@ -681,8 +677,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Send register Intent.
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
- Intent registrationIntent =
- InvalidationIntentProtocol.createRegisterIntent(account, true, null);
+ Intent registrationIntent = createRegisterIntent(account, true, null);
getService().onHandleIntent(registrationIntent);
// Verify client started and state written.
@@ -712,8 +707,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Send register Intent with no desired types.
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
- Intent registrationIntent = InvalidationIntentProtocol.createRegisterIntent(
- account, false, new HashSet<ModelType>());
+ Intent registrationIntent = createRegisterIntent(account, false, new HashSet<ModelType>());
getService().onHandleIntent(registrationIntent);
// Verify client started and state written.
@@ -729,7 +723,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
assertTrue(Arrays.equals(CLIENT_ID, InvalidationService.getClientIdForTest()));
// Choose to register for all types in an already ready client.
- registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, true, null);
+ registrationIntent = createRegisterIntent(account, true, null);
getService().onHandleIntent(registrationIntent);
// Ensure registrations are correct.
@@ -752,8 +746,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
Set<ModelType> desiredRegistrations = CollectionUtil.newHashSet(
ModelType.BOOKMARK, ModelType.SESSION);
- Intent registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false,
- desiredRegistrations);
+ Intent registrationIntent = createRegisterIntent(account, false, desiredRegistrations);
getService().onHandleIntent(registrationIntent);
// Verify state written but client not started.
@@ -782,8 +775,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
ModelType.BOOKMARK, ModelType.SESSION);
Set<ObjectId> desiredObjectIds = ModelType.modelTypesToObjectIds(desiredRegistrations);
- Intent registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false,
- desiredRegistrations);
+ Intent registrationIntent = createRegisterIntent(account, false, desiredRegistrations);
getService().onHandleIntent(registrationIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
assertEquals(1, mStartServiceIntents.size());
@@ -822,6 +814,37 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Bug: https://code.google.com/p/chromium/issues/detail?id=172398
}
+ /** Creates an intent to start the InvalidationService. */
+ private Intent createStartIntent() {
+ Intent intent = new Intent();
+ intent.putExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME, TEST_CLIENT_NAME);
+ return intent;
+ }
+
+ /** Creates an intent to stop the InvalidationService. */
+ private Intent createStopIntent() {
+ Intent intent = new Intent();
+ intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
+ intent.putExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME, TEST_CLIENT_NAME);
+ return intent;
+ }
+
+ /** Creates an intent to register some types with the InvalidationService. */
+ private Intent createRegisterIntent(Account account, boolean allTypes, Set<ModelType> types) {
+ Intent intent = InvalidationIntentProtocol.createRegisterIntent(account, allTypes, types);
+ intent.putExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME, TEST_CLIENT_NAME);
+ return intent;
+ }
+
+ /** Creates an intent to register some types with the InvalidationService. */
+ private Intent createRegisterIntent(
+ Account account, int[] objectSources, String[] objectNames) {
+ Intent intent = InvalidationIntentProtocol.createRegisterIntent(
+ account, objectSources, objectNames);
+ intent.putExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME, TEST_CLIENT_NAME);
+ return intent;
+ }
+
/** Returns whether {@code intent} is an {@link AndroidListener} start intent. */
private boolean isAndroidListenerStartIntent(Intent intent) {
Intent startIntent = AndroidListener.createStartIntent(getContext(),