diff options
author | maxbogue <maxbogue@chromium.org> | 2015-08-12 11:05:01 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-12 18:05:28 +0000 |
commit | 47c75040af42bf8d8ddcb8aad88b65aa740d1a73 (patch) | |
tree | cb80bec637d94fc80ad56f4c86e8cd8d9fec387a /sync/android | |
parent | 894419d4fe4b9e017d22259335b770d0b2723506 (diff) | |
download | chromium_src-47c75040af42bf8d8ddcb8aad88b65aa740d1a73.zip chromium_src-47c75040af42bf8d8ddcb8aad88b65aa740d1a73.tar.gz chromium_src-47c75040af42bf8d8ddcb8aad88b65aa740d1a73.tar.bz2 |
[Sync] Convert to using auto-generated ModelType.
See http://crrev.com/1247853007 for where the new ModelType was added.
- ModelType is now just represented by an integer.
- ModelType is now located in org.chromium.sync.
- Many of the constants were singular and are now plural to match
the ones in model_type.h.
- ModelTypeSet is removed and a jintArray is used for JNI.
BUG=509788
TBR=thestig@chromium.org
Review URL: https://codereview.chromium.org/1262413006
Cr-Commit-Position: refs/heads/master@{#343051}
Diffstat (limited to 'sync/android')
4 files changed, 22 insertions, 224 deletions
diff --git a/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java b/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java index 3e053c2..dd86170 100644 --- a/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java +++ b/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java @@ -24,6 +24,14 @@ import java.util.Set; */ @JNINamespace("syncer") public class ModelTypeHelper { + /** + * Implement this class to override the behavior of + * {@link ModelTypeHelper#toNotificationType()} for tests. + */ + public interface TestDelegate { + public String toNotificationType(int modelType); + } + private static final String TAG = "ModelTypeHelper"; private static final Object sLock = new Object(); @@ -32,6 +40,8 @@ public class ModelTypeHelper { ModelType.PROXY_TABS }; + private static TestDelegate sDelegate = null; + // Convenience sets for checking whether a type can have invalidations. Some ModelTypes // such as PROXY_TABS are not real types and can't be registered. Initializing these // once reduces toNotificationType() calls in the isInvalidationType() method. @@ -84,6 +94,8 @@ public class ModelTypeHelper { * @return the string representation of the model type constant. */ public static String toNotificationType(int modelType) { + if (sDelegate != null) return sDelegate.toNotificationType(modelType); + // Because PROXY_TABS isn't an invalidation type, it doesn't have a string from native, // but for backwards compatibility we need to keep its pref value the same as the old // ModelType enum name value. @@ -110,5 +122,10 @@ public class ModelTypeHelper { return objectIds; } + @VisibleForTesting + public static void setTestDelegate(TestDelegate delegate) { + sDelegate = delegate; + } + private static native String nativeModelTypeToNotificationType(int modelType); } diff --git a/sync/android/java/src/org/chromium/sync/internal_api/pub/base/ModelType.java b/sync/android/java/src/org/chromium/sync/internal_api/pub/base/ModelType.java deleted file mode 100644 index dc35ba6..0000000 --- a/sync/android/java/src/org/chromium/sync/internal_api/pub/base/ModelType.java +++ /dev/null @@ -1,203 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.sync.internal_api.pub.base; - -import android.util.Log; - -import com.google.ipc.invalidation.external.client.types.ObjectId; -import com.google.protos.ipc.invalidation.Types; - -import org.chromium.base.VisibleForTesting; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -/** - * The model types that are synced in Chrome for Android. - */ -public enum ModelType { - /** - * An autofill object. - */ - AUTOFILL("AUTOFILL"), - /** - * An autofill profile object. - */ - AUTOFILL_PROFILE("AUTOFILL_PROFILE"), - /** - * A bookmark folder or a bookmark URL object. - */ - BOOKMARK("BOOKMARK"), - /** - * Flags to enable experimental features. - */ - EXPERIMENTS("EXPERIMENTS"), - /** - * An object representing a set of Nigori keys. - */ - NIGORI("NIGORI"), - /** - * A password entry. - */ - PASSWORD("PASSWORD"), - /** - * An object representing a preference. - */ - PREFERENCE("PREFERENCE"), - /** - * An object representing a priority preference. - */ - PRIORITY_PREFERENCE("PRIORITY_PREFERENCE"), - /** - * An object representing a browser session or tab. - */ - SESSION("SESSION"), - /** - * A typed_url folder or a typed_url object. - */ - TYPED_URL("TYPED_URL"), - /** - * A history delete directive object. - */ - HISTORY_DELETE_DIRECTIVE("HISTORY_DELETE_DIRECTIVE"), - /** - * A device info object. - */ - DEVICE_INFO("DEVICE_INFO"), - /** - * A proxy tabs object (placeholder for sessions). - */ - PROXY_TABS("NULL", true), - /** - * A favicon image object. - */ - FAVICON_IMAGE("FAVICON_IMAGE"), - /** - * A favicon tracking object. - */ - FAVICON_TRACKING("FAVICON_TRACKING"), - /** - * A supervised user setting object. The old name "managed user" is used for backwards - * compatibility. - */ - MANAGED_USER_SETTING("MANAGED_USER_SETTING"), - /** - * A supervised user whitelist object. - */ - MANAGED_USER_WHITELIST("MANAGED_USER_WHITELIST"), - /** - * An autofill wallet data object. - */ - AUTOFILL_WALLET("AUTOFILL_WALLET"), - /** - * Usage counts and last use dates for autofill wallet data objects. - */ - AUTOFILL_WALLET_METADATA("AUTOFILL_WALLET_METADATA"); - - private static final String TAG = "ModelType"; - - private final String mModelType; - - private final boolean mNonInvalidationType; - - ModelType(String modelType, boolean nonInvalidationType) { - assert nonInvalidationType || name().equals(modelType); - mModelType = modelType; - mNonInvalidationType = nonInvalidationType; - } - - ModelType(String modelType) { - this(modelType, false); - } - - private boolean isNonInvalidationType() { - return mNonInvalidationType; - } - - /** - * Returns the {@link ObjectId} representation of this {@link ModelType}. - * - * This should be used with caution, since it converts even {@link ModelType} instances with - * |mNonInvalidationType| set. For automatically stripping such {@link ModelType} entries out, - * use {@link ModelType#modelTypesToObjectIds(java.util.Set)} instead. - */ - @VisibleForTesting - public ObjectId toObjectId() { - return ObjectId.newInstance(Types.ObjectSource.CHROME_SYNC, mModelType.getBytes()); - } - - public static ModelType fromObjectId(ObjectId objectId) { - try { - return valueOf(new String(objectId.getName())); - } catch (IllegalArgumentException e) { - return null; - } - } - - /** - * Converts string representations of types to sync to {@link ModelType}s. - * Returns set of {@code ModelType} values for all elements of {@code syncTypes} for which - * {@link ModelType#valueOf(String)} successfully returns; other elements are dropped. - */ - public static Set<ModelType> syncTypesToModelTypes(Collection<String> syncTypes) { - Set<ModelType> modelTypes = new HashSet<ModelType>(syncTypes.size()); - for (String syncType : syncTypes) { - try { - modelTypes.add(valueOf(syncType)); - } catch (IllegalArgumentException exception) { - // Drop invalid sync types. - Log.w(TAG, "Could not translate sync type to model type: " + syncType); - } - } - return modelTypes; - } - - /** - * Converts a set of sync types {@link String} to a set of {@link ObjectId}. - * - * This strips out any {@link ModelType} that is not an invalidation type. - */ - public static Set<ObjectId> syncTypesToObjectIds(Collection<String> syncTypes) { - return modelTypesToObjectIds(syncTypesToModelTypes(syncTypes)); - } - - /** - * Converts a set of {@link ModelType} to a set of {@link ObjectId}. - * - * This strips out any {@link ModelType} that is not an invalidation type. - */ - public static Set<ObjectId> modelTypesToObjectIds(Set<ModelType> modelTypes) { - Set<ModelType> filteredModelTypes = filterOutNonInvalidationTypes(modelTypes); - Set<ObjectId> objectIds = new HashSet<ObjectId>(filteredModelTypes.size()); - for (ModelType modelType : filteredModelTypes) { - objectIds.add(modelType.toObjectId()); - } - return objectIds; - } - - /** Converts a set of {@link ModelType} to a set of string names. */ - @VisibleForTesting - public static Set<String> modelTypesToSyncTypesForTest(Set<ModelType> modelTypes) { - Set<String> objectIds = new HashSet<String>(modelTypes.size()); - for (ModelType modelType : modelTypes) { - objectIds.add(modelType.toString()); - } - return objectIds; - } - - /** Filters out non-invalidation types from a set of {@link ModelType}. */ - @VisibleForTesting - public static Set<ModelType> filterOutNonInvalidationTypes(Set<ModelType> modelTypes) { - Set<ModelType> filteredTypes = new HashSet<ModelType>(modelTypes.size()); - for (ModelType modelType : modelTypes) { - if (!modelType.isNonInvalidationType()) { - filteredTypes.add(modelType); - } - } - return filteredTypes; - } - -} 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 e069204..6f0af94 100644 --- a/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java +++ b/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java @@ -11,7 +11,7 @@ import com.google.ipc.invalidation.external.client.types.ObjectId; import com.google.protos.ipc.invalidation.Types; import org.chromium.base.CollectionUtil; -import org.chromium.sync.internal_api.pub.base.ModelType; +import org.chromium.sync.ModelTypeHelper; import java.util.ArrayList; import java.util.HashSet; @@ -59,12 +59,12 @@ public class InvalidationIntentProtocol { * Create an Intent that will start the invalidation listener service and * register for the specified types. */ - public static Intent createRegisterIntent(Account account, Set<ModelType> types) { + public static Intent createRegisterIntent(Account account, Set<Integer> types) { Intent registerIntent = new Intent(ACTION_REGISTER); String[] selectedTypesArray = new String[types.size()]; int pos = 0; - for (ModelType type : types) { - selectedTypesArray[pos++] = type.name(); + for (Integer type : types) { + selectedTypesArray[pos++] = ModelTypeHelper.toNotificationType(type); } registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES, CollectionUtil.newArrayList(selectedTypesArray)); diff --git a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationPreferencesTest.java b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationPreferencesTest.java index f8e841ed..4bcfd20 100644 --- a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationPreferencesTest.java +++ b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationPreferencesTest.java @@ -14,10 +14,8 @@ import com.google.ipc.invalidation.external.client.types.ObjectId; import org.chromium.base.CollectionUtil; import org.chromium.base.test.util.AdvancedMockContext; import org.chromium.base.test.util.Feature; -import org.chromium.sync.internal_api.pub.base.ModelType; import java.util.Arrays; -import java.util.HashSet; import java.util.Set; /** @@ -36,20 +34,6 @@ public class InvalidationPreferencesTest extends InstrumentationTestCase { @SmallTest @Feature({"Sync"}) - public void testTranslateBasicSyncTypes() throws Exception { - /* - * Test plan: convert three strings to model types, one of which is invalid. Verify that - * the two valid strings are properly converted and that the invalid string is dropped. - */ - HashSet<ModelType> expectedTypes = CollectionUtil.newHashSet( - ModelType.BOOKMARK, ModelType.SESSION); - Set<ModelType> actualTypes = ModelType.syncTypesToModelTypes( - CollectionUtil.newHashSet("BOOKMARK", "SESSION", "0!!!INVALID")); - assertEquals(expectedTypes, actualTypes); - } - - @SmallTest - @Feature({"Sync"}) public void testReadMissingData() { /* * Test plan: read saved state from empty preferences. Verify that null is returned. @@ -97,4 +81,4 @@ public class InvalidationPreferencesTest extends InstrumentationTestCase { assertTrue(Arrays.equals( internalClientState, invPreferences.getInternalNotificationClientState())); } -}
\ No newline at end of file +} |