1 | // Copyright (c) 2013 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.internal_api.pub.base; |
6 | |
7 | import android.util.Log; |
8 | |
9 | import com.google.common.annotations.VisibleForTesting; |
10 | import com.google.ipc.invalidation.external.client.types.ObjectId; |
11 | import com.google.protos.ipc.invalidation.Types; |
12 | |
13 | import java.util.Collection; |
14 | import java.util.EnumSet; |
15 | import java.util.HashSet; |
16 | import java.util.Set; |
17 | |
18 | /** |
19 | * The model types that are synced in Chrome for Android. |
20 | */ |
21 | public enum ModelType { |
22 | /** |
23 | * An autofill object. |
24 | */ |
25 | AUTOFILL("AUTOFILL"), |
26 | /** |
27 | * An autofill profile object. |
28 | */ |
29 | AUTOFILL_PROFILE("AUTOFILL_PROFILE"), |
30 | /** |
31 | * A bookmark folder or a bookmark URL object. |
32 | */ |
33 | BOOKMARK("BOOKMARK"), |
34 | /** |
35 | * Flags to enable experimental features. |
36 | */ |
37 | EXPERIMENTS("EXPERIMENTS"), |
38 | /** |
39 | * An object representing a set of Nigori keys. |
40 | */ |
41 | NIGORI("NIGORI"), |
42 | /** |
43 | * A password entry. |
44 | */ |
45 | PASSWORD("PASSWORD"), |
46 | /** |
47 | * An object representing a browser session or tab. |
48 | */ |
49 | SESSION("SESSION"), |
50 | /** |
51 | * A typed_url folder or a typed_url object. |
52 | */ |
53 | TYPED_URL("TYPED_URL"), |
54 | /** |
55 | * A history delete directive object. |
56 | */ |
57 | HISTORY_DELETE_DIRECTIVE("HISTORY_DELETE_DIRECTIVE"), |
58 | /** |
59 | * A device info object. |
60 | */ |
61 | DEVICE_INFO("DEVICE_INFO"), |
62 | /** |
63 | * A proxy tabs object (placeholder for sessions). |
64 | */ |
65 | PROXY_TABS("NULL", true), |
66 | /** |
67 | * A favicon image object. |
68 | */ |
69 | FAVICON_IMAGE("FAVICON_IMAGE"), |
70 | /** |
71 | * A favicon tracking object. |
72 | */ |
73 | FAVICON_TRACKING("FAVICON_TRACKING"); |
74 | |
75 | /** Special type representing all possible types. */ |
76 | public static final String ALL_TYPES_TYPE = "ALL_TYPES"; |
77 | |
78 | private static final String TAG = "ModelType"; |
79 | |
80 | private final String mModelType; |
81 | |
82 | private final boolean mNonInvalidationType; |
83 | |
84 | ModelType(String modelType, boolean nonInvalidationType) { |
85 | mModelType = modelType; |
86 | mNonInvalidationType = nonInvalidationType; |
87 | } |
88 | |
89 | ModelType(String modelType) { |
90 | this(modelType, false); |
91 | } |
92 | |
93 | /** |
94 | * Returns the {@link ObjectId} representation of this {@link ModelType}. |
95 | * |
96 | * This should be used with caution, since it converts even {@link ModelType} instances with |
97 | * |mNonInvalidationType| set. For automatically stripping such {@link ModelType} entries out, |
98 | * use {@link ModelType#modelTypesToObjectIds(java.util.Set)} instead. |
99 | */ |
100 | @VisibleForTesting |
101 | public ObjectId toObjectId() { |
102 | return ObjectId.newInstance(Types.ObjectSource.Type.CHROME_SYNC.getNumber(), |
103 | mModelType.getBytes()); |
104 | } |
105 | |
106 | public static ModelType fromObjectId(ObjectId objectId) { |
107 | try { |
108 | return valueOf(new String(objectId.getName())); |
109 | } catch (IllegalArgumentException e) { |
110 | return null; |
111 | } |
112 | } |
113 | |
114 | /** |
115 | * Converts string representations of types to sync to {@link ModelType}s. |
116 | * <p> |
117 | * If {@code syncTypes} contains {@link #ALL_TYPES_TYPE}, then the returned |
118 | * set contains all values of the {@code ModelType} enum. |
119 | * <p> |
120 | * Otherwise, the returned set contains the {@code ModelType} values for all elements of |
121 | * {@code syncTypes} for which {@link ModelType#valueOf(String)} successfully returns; other |
122 | * elements are dropped. |
123 | */ |
124 | public static Set<ModelType> syncTypesToModelTypes(Collection<String> syncTypes) { |
125 | if (syncTypes.contains(ALL_TYPES_TYPE)) { |
126 | return EnumSet.allOf(ModelType.class); |
127 | } else { |
128 | Set<ModelType> modelTypes = new HashSet<ModelType>(syncTypes.size()); |
129 | for (String syncType : syncTypes) { |
130 | try { |
131 | modelTypes.add(valueOf(syncType)); |
132 | } catch (IllegalArgumentException exception) { |
133 | // Drop invalid sync types. |
134 | Log.w(TAG, "Could not translate sync type to model type: " + syncType); |
135 | } |
136 | } |
137 | return modelTypes; |
138 | } |
139 | } |
140 | |
141 | /** |
142 | * Converts a set of sync types {@link String} to a set of {@link ObjectId}. |
143 | * |
144 | * This strips out any {@link ModelType} that is not an invalidation type. |
145 | */ |
146 | public static Set<ObjectId> syncTypesToObjectIds(Collection<String> syncTypes) { |
147 | return modelTypesToObjectIds(syncTypesToModelTypes(syncTypes)); |
148 | } |
149 | |
150 | /** |
151 | * Converts a set of {@link ModelType} to a set of {@link ObjectId}. |
152 | * |
153 | * This strips out any {@link ModelType} that is not an invalidation type. |
154 | */ |
155 | public static Set<ObjectId> modelTypesToObjectIds(Set<ModelType> modelTypes) { |
156 | Set<ObjectId> objectIds = new HashSet<ObjectId>(modelTypes.size()); |
157 | for (ModelType modelType : modelTypes) { |
158 | if (!modelType.mNonInvalidationType) { |
159 | objectIds.add(modelType.toObjectId()); |
160 | } |
161 | } |
162 | return objectIds; |
163 | } |
164 | |
165 | /** Converts a set of {@link ModelType} to a set of string names. */ |
166 | public static Set<String> modelTypesToSyncTypes(Set<ModelType> modelTypes) { |
167 | Set<String> objectIds = new HashSet<String>(modelTypes.size()); |
168 | for (ModelType modelType : modelTypes) { |
169 | objectIds.add(modelType.toString()); |
170 | } |
171 | return objectIds; |
172 | } |
173 | } |