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

Side by Side Diff: sync/android/java/src/org/chromium/sync/ModelTypeHelper.java

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.sync;
6
7 import com.google.ipc.invalidation.external.client.types.ObjectId;
8 import com.google.protos.ipc.invalidation.Types;
9
10 import org.chromium.base.VisibleForTesting;
11 import org.chromium.base.annotations.JNINamespace;
12
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.Set;
16
17 /**
18 * Helper methods for dealing with ModelTypes.
19 *
20 * This class deals primarily with converting ModelTypes into notification types (string
21 * representations that are used to register for invalidations) and converting n otification
22 * types into the actual ObjectIds used for invalidations.
23 *
24 */
25 @JNINamespace("syncer")
26 public class ModelTypeHelper {
27 /**
28 * Implement this class to override the behavior of
29 * {@link ModelTypeHelper#toNotificationType()} for tests.
30 */
31 public interface TestDelegate {
32 public String toNotificationType(int modelType);
33 }
34
35 private static final String TAG = "ModelTypeHelper";
36
37 private static final Object sLock = new Object();
38
39 private static final int[] NON_INVALIDATION_TYPES_ARRAY = new int[] {
40 ModelType.PROXY_TABS
41 };
42
43 private static TestDelegate sDelegate = null;
44
45 // Convenience sets for checking whether a type can have invalidations. Some ModelTypes
46 // such as PROXY_TABS are not real types and can't be registered. Initializi ng these
47 // once reduces toNotificationType() calls in the isInvalidationType() metho d.
48 private static Set<String> sNonInvalidationTypes = null;
49
50 /**
51 * Initializes the non-invalidation sets. Called lazily the first time they' re needed.
52 */
53 private static void initNonInvalidationTypes() {
54 synchronized (sLock) {
55 if (sNonInvalidationTypes != null) return;
56
57 sNonInvalidationTypes = new HashSet<String>();
58 for (int i = 0; i < NON_INVALIDATION_TYPES_ARRAY.length; i++) {
59 sNonInvalidationTypes.add(toNotificationType(NON_INVALIDATION_TY PES_ARRAY[i]));
60 }
61 }
62 }
63
64 /**
65 * Checks whether a type is allowed to register for invalidations.
66 */
67 private static boolean isInvalidationType(String notificationType) {
68 initNonInvalidationTypes();
69 return !sNonInvalidationTypes.contains(notificationType);
70 }
71
72 /**
73 * Converts a notification type into an ObjectId.
74 *
75 * If the model type is not an invalidation type, this function uses the str ing "NULL".
76 */
77 private static ObjectId toObjectId(String notificationType) {
78 String objectIdString = isInvalidationType(notificationType) ? notificat ionType : "NULL";
79 return ObjectId.newInstance(Types.ObjectSource.CHROME_SYNC, objectIdStri ng.getBytes());
80 }
81
82 @VisibleForTesting
83 public static ObjectId toObjectId(int modelType) {
84 return toObjectId(toNotificationType(modelType));
85 }
86
87 /**
88 * Converts a model type to its notification type representation using JNI.
89 *
90 * This is the value that is stored in the invalidation preferences and used to
91 * register for invalidations.
92 *
93 * @param modelType the model type to convert to a string.
94 * @return the string representation of the model type constant.
95 */
96 public static String toNotificationType(int modelType) {
97 if (sDelegate != null) return sDelegate.toNotificationType(modelType);
98
99 // Because PROXY_TABS isn't an invalidation type, it doesn't have a stri ng from native,
100 // but for backwards compatibility we need to keep its pref value the sa me as the old
101 // ModelType enum name value.
102 if (modelType == ModelType.PROXY_TABS) {
103 return "PROXY_TABS";
104 }
105 return nativeModelTypeToNotificationType(modelType);
106 }
107
108 /**
109 * Converts a set of {@link String} notification types to a set of {@link Ob jectId}.
110 *
111 * This function assumes that all the strings passed in were generated with
112 * ModelTypeHelper.toNotificationType. Any notification types that are nonIn validationTypes
113 * are filtered out.
114 */
115 public static Set<ObjectId> notificationTypesToObjectIds(Collection<String> notificationTypes) {
116 Set<ObjectId> objectIds = new HashSet<ObjectId>();
117 for (String notificationType : notificationTypes) {
118 if (isInvalidationType(notificationType)) {
119 objectIds.add(toObjectId(notificationType));
120 }
121 }
122 return objectIds;
123 }
124
125 @VisibleForTesting
126 public static void setTestDelegate(TestDelegate delegate) {
127 sDelegate = delegate;
128 }
129
130 private static native String nativeModelTypeToNotificationType(int modelType );
131 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698