5989cc28da6a5512136b1c45465e1c60dd3cf167
[platform/framework/web/crosswalk.git] / src / sync / android / java / src / org / chromium / sync / internal_api / pub / base / ModelType.java
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.
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 org.chromium.base.FieldTrialList;
14 import org.chromium.base.library_loader.LibraryLoader;
15
16 import java.util.Collection;
17 import java.util.EnumSet;
18 import java.util.HashSet;
19 import java.util.Set;
20
21 /**
22  * The model types that are synced in Chrome for Android.
23  */
24 public enum ModelType {
25     /**
26      * An autofill object.
27      */
28     AUTOFILL("AUTOFILL"),
29     /**
30      * An autofill profile object.
31      */
32     AUTOFILL_PROFILE("AUTOFILL_PROFILE"),
33     /**
34      * A bookmark folder or a bookmark URL object.
35      */
36     BOOKMARK("BOOKMARK"),
37     /**
38      * Flags to enable experimental features.
39      */
40     EXPERIMENTS("EXPERIMENTS"),
41     /**
42      * An object representing a set of Nigori keys.
43      */
44     NIGORI("NIGORI"),
45     /**
46      * A password entry.
47      */
48     PASSWORD("PASSWORD"),
49     /**
50      * An object representing a browser session or tab.
51      */
52     SESSION("SESSION"),
53     /**
54      * A typed_url folder or a typed_url object.
55      */
56     TYPED_URL("TYPED_URL"),
57     /**
58      * A history delete directive object.
59      */
60     HISTORY_DELETE_DIRECTIVE("HISTORY_DELETE_DIRECTIVE"),
61     /**
62      * A device info object.
63      */
64     DEVICE_INFO("DEVICE_INFO"),
65     /**
66      * A proxy tabs object (placeholder for sessions).
67      */
68     PROXY_TABS("NULL", true),
69     /**
70      * A favicon image object.
71      */
72     FAVICON_IMAGE("FAVICON_IMAGE"),
73     /**
74      * A favicon tracking object.
75      */
76     FAVICON_TRACKING("FAVICON_TRACKING");
77
78     /** Special type representing all possible types. */
79     public static final String ALL_TYPES_TYPE = "ALL_TYPES";
80
81     private static final String TAG = "ModelType";
82
83     private final String mModelType;
84
85     private final boolean mNonInvalidationType;
86
87     ModelType(String modelType, boolean nonInvalidationType) {
88         mModelType = modelType;
89         mNonInvalidationType = nonInvalidationType;
90     }
91
92     ModelType(String modelType) {
93         this(modelType, false);
94     }
95
96     private boolean isNonInvalidationType() {
97       if ((this == SESSION || this == FAVICON_TRACKING) && LibraryLoader.isInitialized()) {
98         return FieldTrialList
99             .findFullName("AndroidSessionNotifications")
100             .equals("Disabled");
101       }
102       return mNonInvalidationType;
103     }
104
105     /**
106      * Returns the {@link ObjectId} representation of this {@link ModelType}.
107      *
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.
111      */
112     @VisibleForTesting
113     public ObjectId toObjectId() {
114         return ObjectId.newInstance(Types.ObjectSource.Type.CHROME_SYNC.getNumber(),
115                 mModelType.getBytes());
116     }
117
118     public static ModelType fromObjectId(ObjectId objectId) {
119         try {
120             return valueOf(new String(objectId.getName()));
121         } catch (IllegalArgumentException e) {
122             return null;
123         }
124     }
125
126     /**
127      * Converts string representations of types to sync to {@link ModelType}s.
128      * <p>
129      * If {@code syncTypes} contains {@link #ALL_TYPES_TYPE}, then the returned
130      * set contains all values of the {@code ModelType} enum.
131      * <p>
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.
135      */
136     public static Set<ModelType> syncTypesToModelTypes(Collection<String> syncTypes) {
137         if (syncTypes.contains(ALL_TYPES_TYPE)) {
138             return EnumSet.allOf(ModelType.class);
139         } else {
140             Set<ModelType> modelTypes = new HashSet<ModelType>(syncTypes.size());
141             for (String syncType : syncTypes) {
142                 try {
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);
147                 }
148             }
149             return modelTypes;
150         }
151     }
152
153     /**
154      * Converts a set of sync types {@link String} to a set of {@link ObjectId}.
155      *
156      * This strips out any {@link ModelType} that is not an invalidation type.
157      */
158     public static Set<ObjectId> syncTypesToObjectIds(Collection<String> syncTypes) {
159         return modelTypesToObjectIds(syncTypesToModelTypes(syncTypes));
160     }
161
162     /**
163      * Converts a set of {@link ModelType} to a set of {@link ObjectId}.
164      *
165      * This strips out any {@link ModelType} that is not an invalidation type.
166      */
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());
172             }
173         }
174         return objectIds;
175     }
176
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());
182         }
183         return objectIds;
184     }
185 }