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.
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 java.util.Collection;
14 import java.util.EnumSet;
15 import java.util.HashSet;
19 * The model types that are synced in Chrome for Android.
21 public enum ModelType {
27 * An autofill profile object.
29 AUTOFILL_PROFILE("AUTOFILL_PROFILE"),
31 * A bookmark folder or a bookmark URL object.
35 * Flags to enable experimental features.
37 EXPERIMENTS("EXPERIMENTS"),
39 * An object representing a set of Nigori keys.
47 * An object representing a browser session or tab.
51 * A typed_url folder or a typed_url object.
53 TYPED_URL("TYPED_URL"),
55 * A history delete directive object.
57 HISTORY_DELETE_DIRECTIVE("HISTORY_DELETE_DIRECTIVE"),
59 * A device info object.
61 DEVICE_INFO("DEVICE_INFO"),
63 * A proxy tabs object (placeholder for sessions).
65 PROXY_TABS("NULL", true),
67 * A favicon image object.
69 FAVICON_IMAGE("FAVICON_IMAGE"),
71 * A favicon tracking object.
73 FAVICON_TRACKING("FAVICON_TRACKING");
75 /** Special type representing all possible types. */
76 public static final String ALL_TYPES_TYPE = "ALL_TYPES";
78 private static final String TAG = "ModelType";
80 private final String mModelType;
82 private final boolean mNonInvalidationType;
84 ModelType(String modelType, boolean nonInvalidationType) {
85 mModelType = modelType;
86 mNonInvalidationType = nonInvalidationType;
89 ModelType(String modelType) {
90 this(modelType, false);
94 * Returns the {@link ObjectId} representation of this {@link ModelType}.
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.
101 public ObjectId toObjectId() {
102 return ObjectId.newInstance(Types.ObjectSource.Type.CHROME_SYNC.getNumber(),
103 mModelType.getBytes());
106 public static ModelType fromObjectId(ObjectId objectId) {
108 return valueOf(new String(objectId.getName()));
109 } catch (IllegalArgumentException e) {
115 * Converts string representations of types to sync to {@link ModelType}s.
117 * If {@code syncTypes} contains {@link #ALL_TYPES_TYPE}, then the returned
118 * set contains all values of the {@code ModelType} enum.
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.
124 public static Set<ModelType> syncTypesToModelTypes(Collection<String> syncTypes) {
125 if (syncTypes.contains(ALL_TYPES_TYPE)) {
126 return EnumSet.allOf(ModelType.class);
128 Set<ModelType> modelTypes = new HashSet<ModelType>(syncTypes.size());
129 for (String syncType : syncTypes) {
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);
142 * Converts a set of sync types {@link String} to a set of {@link ObjectId}.
144 * This strips out any {@link ModelType} that is not an invalidation type.
146 public static Set<ObjectId> syncTypesToObjectIds(Collection<String> syncTypes) {
147 return modelTypesToObjectIds(syncTypesToModelTypes(syncTypes));
151 * Converts a set of {@link ModelType} to a set of {@link ObjectId}.
153 * This strips out any {@link ModelType} that is not an invalidation type.
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());
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());