Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(730)

Unified Diff: sync/android/java/src/org/chromium/sync/ModelTypeHelper.java

Issue 1247853007: [Sync] Add auto-generated ModelType in Java. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments, rebase, fix bots. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..37daf3f2f8147e68c1cc12d0a46edcb36d5987a9
--- /dev/null
+++ b/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java
@@ -0,0 +1,111 @@
+// 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.VisibleForTesting;
+import org.chromium.base.annotations.JNINamespace;
+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
+ };
+
+ // 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 toString() calls in the isInvalidationType() method.
+ private static Set<String> sNonInvalidationTypes = null;
+ private static Set<String> sNonInvalidationTypesExperiment = null;
+
+ /**
+ * Initializes the non-invalidation sets. Called lazily the first time they're needed.
+ */
+ private static void initNonInvalidationTypes() {
nyquist 2015/08/03 20:07:10 This doesn't seem thread safe. Could you at least
maxbogue 2015/08/04 19:43:54 Decided to make it actually thread safe; PTAL.
+ sNonInvalidationTypes = new HashSet<String>();
+ for (int i = 0; i < NON_INVALIDATION_TYPES_ARRAY.length; i++) {
+ sNonInvalidationTypes.add(toString(NON_INVALIDATION_TYPES_ARRAY[i]));
+ }
+ sNonInvalidationTypesExperiment = new HashSet<String>();
+ sNonInvalidationTypesExperiment.add(toString(ModelType.SESSIONS));
+ sNonInvalidationTypesExperiment.add(toString(ModelType.FAVICON_TRACKING));
+ sNonInvalidationTypesExperiment.add(toString(ModelType.FAVICON_IMAGES));
+ }
+
+ /**
+ * Checks whether a type is allowed to register for invalidations.
+ */
+ private static boolean isInvalidationType(String modelType) {
+ if (sNonInvalidationTypes == null) {
nyquist 2015/08/03 20:07:10 Same about threading here.
maxbogue 2015/08/04 19:43:54 Done.
+ initNonInvalidationTypes();
+ }
+ if (sNonInvalidationTypesExperiment.contains(modelType) && LibraryLoader.isInitialized()) {
+ return !FieldTrialList.findFullName("AndroidSessionNotifications").equals("Disabled");
Nicolas Zea 2015/08/04 17:04:53 To be clear, the experiment being "on" in this cas
maxbogue 2015/08/04 20:06:12 Yep.
+ }
+ return !sNonInvalidationTypes.contains(modelType);
+ }
+
+ /**
+ * Converts a model type string into an ObjectId.
+ *
+ * If the model type is not an invalidation type, this function uses the string "NULL".
+ */
+ private static ObjectId toObjectId(String modelType) {
+ String objectIdString = isInvalidationType(modelType) ? modelType : "NULL";
+ return ObjectId.newInstance(Types.ObjectSource.CHROME_SYNC, objectIdString.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) {
nyquist 2015/08/03 20:07:10 Nit: Could this be 'int'?
maxbogue 2015/08/04 19:43:54 Yes, thanks!
+ // 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";
+ }
+ return nativeModelTypeToNotificationType(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 nativeModelTypeToNotificationType(int modelType);
+}

Powered by Google App Engine
This is Rietveld 408576698