1 // Copyright 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.
5 package org.chromium.sync.internal_api.pub.base;
7 import android.util.Log;
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;
13 import org.chromium.base.FieldTrialList;
14 import org.chromium.base.library_loader.LibraryLoader;
16 import java.util.Collection;
17 import java.util.EnumSet;
18 import java.util.HashSet;
22 * The model types that are synced in Chrome for Android.
24 public enum ModelType {
30 * An autofill profile object.
32 AUTOFILL_PROFILE("AUTOFILL_PROFILE"),
34 * A bookmark folder or a bookmark URL object.
38 * Flags to enable experimental features.
40 EXPERIMENTS("EXPERIMENTS"),
42 * An object representing a set of Nigori keys.
50 * An object representing a browser session or tab.
54 * A typed_url folder or a typed_url object.
56 TYPED_URL("TYPED_URL"),
58 * A history delete directive object.
60 HISTORY_DELETE_DIRECTIVE("HISTORY_DELETE_DIRECTIVE"),
62 * A device info object.
64 DEVICE_INFO("DEVICE_INFO"),
66 * A proxy tabs object (placeholder for sessions).
68 PROXY_TABS("NULL", true),
70 * A favicon image object.
72 FAVICON_IMAGE("FAVICON_IMAGE"),
74 * A favicon tracking object.
76 FAVICON_TRACKING("FAVICON_TRACKING");
78 /** Special type representing all possible types. */
79 public static final String ALL_TYPES_TYPE = "ALL_TYPES";
81 private static final String TAG = "ModelType";
83 private final String mModelType;
85 private final boolean mNonInvalidationType;
87 ModelType(String modelType, boolean nonInvalidationType) {
88 mModelType = modelType;
89 mNonInvalidationType = nonInvalidationType;
92 ModelType(String modelType) {
93 this(modelType, false);
96 private boolean isNonInvalidationType() {
97 if ((this == SESSION || this == FAVICON_TRACKING) && LibraryLoader.isInitialized()) {
99 .findFullName("AndroidSessionNotifications")
102 return mNonInvalidationType;
106 * Returns the {@link ObjectId} representation of this {@link ModelType}.
108 * This should be used with caution, since it converts even {@link ModelType} instances with
109 * |mNonInvalidationType| set. For automatically stripping such {@link ModelType} entries out,
110 * use {@link ModelType#modelTypesToObjectIds(java.util.Set)} instead.
113 public ObjectId toObjectId() {
114 return ObjectId.newInstance(Types.ObjectSource.Type.CHROME_SYNC.getNumber(),
115 mModelType.getBytes());
118 public static ModelType fromObjectId(ObjectId objectId) {
120 return valueOf(new String(objectId.getName()));
121 } catch (IllegalArgumentException e) {
127 * Converts string representations of types to sync to {@link ModelType}s.
129 * If {@code syncTypes} contains {@link #ALL_TYPES_TYPE}, then the returned
130 * set contains all values of the {@code ModelType} enum.
132 * Otherwise, the returned set contains the {@code ModelType} values for all elements of
133 * {@code syncTypes} for which {@link ModelType#valueOf(String)} successfully returns; other
134 * elements are dropped.
136 public static Set<ModelType> syncTypesToModelTypes(Collection<String> syncTypes) {
137 if (syncTypes.contains(ALL_TYPES_TYPE)) {
138 return EnumSet.allOf(ModelType.class);
140 Set<ModelType> modelTypes = new HashSet<ModelType>(syncTypes.size());
141 for (String syncType : syncTypes) {
143 modelTypes.add(valueOf(syncType));
144 } catch (IllegalArgumentException exception) {
145 // Drop invalid sync types.
146 Log.w(TAG, "Could not translate sync type to model type: " + syncType);
154 * Converts a set of sync types {@link String} to a set of {@link ObjectId}.
156 * This strips out any {@link ModelType} that is not an invalidation type.
158 public static Set<ObjectId> syncTypesToObjectIds(Collection<String> syncTypes) {
159 return modelTypesToObjectIds(syncTypesToModelTypes(syncTypes));
163 * Converts a set of {@link ModelType} to a set of {@link ObjectId}.
165 * This strips out any {@link ModelType} that is not an invalidation type.
167 public static Set<ObjectId> modelTypesToObjectIds(Set<ModelType> modelTypes) {
168 Set<ObjectId> objectIds = new HashSet<ObjectId>(modelTypes.size());
169 for (ModelType modelType : modelTypes) {
170 if (!modelType.isNonInvalidationType()) {
171 objectIds.add(modelType.toObjectId());
177 /** Converts a set of {@link ModelType} to a set of string names. */
178 public static Set<String> modelTypesToSyncTypes(Set<ModelType> modelTypes) {
179 Set<String> objectIds = new HashSet<String>(modelTypes.size());
180 for (ModelType modelType : modelTypes) {
181 objectIds.add(modelType.toString());