Chromium Code Reviews| Index: sync/android/java/src/org/chromium/sync/ModelTypeHelper.java |
| diff --git a/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java b/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7ae906de34262992031444f4fd87118b5aaa0061 |
| --- /dev/null |
| +++ b/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2015 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; |
| + |
| +import com.google.ipc.invalidation.external.client.types.ObjectId; |
| +import com.google.protos.ipc.invalidation.Types; |
| + |
| +import org.chromium.base.FieldTrialList; |
| +import org.chromium.base.JNINamespace; |
| +import org.chromium.base.VisibleForTesting; |
| +import org.chromium.base.library_loader.LibraryLoader; |
| + |
| +import java.util.Collection; |
| +import java.util.HashSet; |
| +import java.util.Set; |
| + |
| +/** |
| + * Helper methods for dealing with ModelTypes. |
| + */ |
| +@JNINamespace("syncer") |
| +public class ModelTypeHelper { |
| + private static final String TAG = "ModelTypeHelper"; |
| + |
| + private static final int[] NON_INVALIDATION_TYPES_ARRAY = new int[] { |
| + ModelType.PROXY_TABS |
| + }; |
| + |
| + private static Set<String> sNonInvalidationTypes = null; |
|
Nicolas Zea
2015/07/30 22:20:13
Would be good to comment what these fields are for
maxbogue
2015/07/31 15:49:56
Done.
|
| + private static Set<String> sTypesDisabledForExperiment = null; |
|
Nicolas Zea
2015/07/30 22:20:13
nit: they're not being disabled per se, just not b
maxbogue
2015/07/31 15:49:56
Done.
|
| + |
| + private static void initNonInvalidationTypes() { |
|
Nicolas Zea
2015/07/30 22:20:13
In general I prefer being generous with commenting
maxbogue
2015/07/31 15:49:56
Done.
|
| + sNonInvalidationTypes = new HashSet<String>(); |
| + for (int i = 0; i < NON_INVALIDATION_TYPES_ARRAY.length; i++) { |
| + sNonInvalidationTypes.add(toString(NON_INVALIDATION_TYPES_ARRAY[i])); |
| + } |
| + sTypesDisabledForExperiment = new HashSet<String>(); |
| + sTypesDisabledForExperiment.add(toString(ModelType.SESSIONS)); |
| + sTypesDisabledForExperiment.add(toString(ModelType.FAVICON_TRACKING)); |
| + sTypesDisabledForExperiment.add(toString(ModelType.FAVICON_IMAGES)); |
| + } |
| + |
| + private static boolean isInvalidationType(String modelType) { |
| + if (sNonInvalidationTypes == null) { |
| + initNonInvalidationTypes(); |
| + } |
| + if (sTypesDisabledForExperiment.contains(modelType) && LibraryLoader.isInitialized()) { |
| + return !FieldTrialList.findFullName("AndroidSessionNotifications").equals("Disabled"); |
|
Nicolas Zea
2015/07/30 22:20:13
Seems like this is a bit of a layering invalidatio
maxbogue
2015/07/31 00:49:39
I wanted to avoid calling toString on the relevant
Nicolas Zea
2015/08/04 17:04:53
Experiments get polled every couple of hours and c
maxbogue
2015/08/04 20:06:12
Yeah, in that case I don't see a much better way t
|
| + } |
| + return !sNonInvalidationTypes.contains(modelType); |
| + } |
| + |
| + private static ObjectId toObjectId(String modelType) { |
| + return ObjectId.newInstance(Types.ObjectSource.CHROME_SYNC, modelType.getBytes()); |
| + } |
| + |
| + @VisibleForTesting |
| + public static ObjectId toObjectId(int modelType) { |
| + return toObjectId(toString(modelType)); |
| + } |
| + |
| + /** |
| + * Converts a model type to its string representation using JNI. |
| + * This is the value that is stored in the invalidation preferences. |
| + * |
| + * @param modelType the model type to convert to a string. |
| + * @return the string representation of the model type constant. |
| + */ |
| + public static String toString(Integer modelType) { |
|
Nicolas Zea
2015/07/30 22:20:13
Given the other comment about ModelTypeToString be
maxbogue
2015/07/31 00:49:40
NotificationType is a more confusing name to me si
maxbogue
2015/07/31 15:49:56
I would actually prefer to not have the concept of
Nicolas Zea
2015/08/04 17:04:53
What would you suggest in place of toString then,
maxbogue
2015/08/04 20:06:12
Ah, I wasn't thinking the native ModelTypeToString
|
| + // 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. |
| + if (modelType == ModelType.PROXY_TABS) { |
| + return "PROXY_TABS"; |
|
Nicolas Zea
2015/07/30 22:20:13
Didn't PROXY_TABS just previously have the string
maxbogue
2015/07/31 00:49:40
That's what I thought at first, but no... Previous
Nicolas Zea
2015/08/04 17:04:53
I see. What information did the preference store r
maxbogue
2015/08/04 20:06:12
My understanding is that it would store "PROXY_TAB
|
| + } |
| + return nativeModelTypeToString(modelType); |
| + } |
| + |
| + /** |
| + * Converts a set of {@link String} model types to a set of {@link ObjectId}. |
| + * |
| + * This strips out any {@link ModelType} that is not an invalidation type. |
| + */ |
| + public static Set<ObjectId> modelTypeStringsToObjectIds(Collection<String> modelTypes) { |
| + Set<ObjectId> objectIds = new HashSet<ObjectId>(); |
| + for (String modelType : modelTypes) { |
| + if (isInvalidationType(modelType)) { |
| + objectIds.add(toObjectId(modelType)); |
| + } |
| + } |
| + return objectIds; |
| + } |
| + |
| + private static native String nativeModelTypeToString(int modelType); |
| +} |