1 // Copyright 2012 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 // Enumerate the various item subtypes that are supported by sync.
6 // Each sync object is expected to have an immutable object type.
7 // An object's type is inferred from the type of data it holds.
8
9 #ifndef SYNC_INTERNAL_API_PUBLIC_BASE_MODEL_TYPE_H_
10 #define SYNC_INTERNAL_API_PUBLIC_BASE_MODEL_TYPE_H_
11
12 #include <set>
13 #include <string>
14
15 #include "base/logging.h"
16 #include "sync/base/sync_export.h"
17 #include "sync/internal_api/public/base/enum_set.h"
18
19 namespace base {
20 class ListValue;
21 class StringValue;
22 class Value;
23 }
24
25 namespace sync_pb {
26 class EntitySpecifics;
27 class SyncEntity;
28 }
29
30 namespace syncer {
31
32 // TODO(akalin): Move the non-exported functions in this file to a
33 // private header.
34
35 enum ModelType {
36 // Object type unknown. Objects may transition through
37 // the unknown state during their initial creation, before
38 // their properties are set. After deletion, object types
39 // are generally preserved.
40 UNSPECIFIED,
41 // A permanent folder whose children may be of mixed
42 // datatypes (e.g. the "Google Chrome" folder).
43 TOP_LEVEL_FOLDER,
44
45 // ------------------------------------ Start of "real" model types.
46 // The model types declared before here are somewhat special, as they
47 // they do not correspond to any browser data model. The remaining types
48 // are bona fide model types; all have a related browser data model and
49 // can be represented in the protocol using a specific Message type in the
50 // EntitySpecifics protocol buffer.
51 //
52 // A bookmark folder or a bookmark URL object.
53 BOOKMARKS,
54 FIRST_USER_MODEL_TYPE = BOOKMARKS, // Declared 2nd, for debugger prettiness.
55 FIRST_REAL_MODEL_TYPE = FIRST_USER_MODEL_TYPE,
56
57 // A preference object.
58 PREFERENCES,
59 // A password object.
60 PASSWORDS,
61 // An AutofillProfile Object
62 AUTOFILL_PROFILE,
63 // An autofill object.
64 AUTOFILL,
65 // A themes object.
66 THEMES,
67 // A typed_url object.
68 TYPED_URLS,
69 // An extension object.
70 EXTENSIONS,
71 // An object representing a custom search engine.
72 SEARCH_ENGINES,
73 // An object representing a browser session.
74 SESSIONS,
75 // An app object.
76 APPS,
77 // An app setting from the extension settings API.
78 APP_SETTINGS,
79 // An extension setting from the extension settings API.
80 EXTENSION_SETTINGS,
81 // App notifications.
82 APP_NOTIFICATIONS,
83 // History delete directives.
84 HISTORY_DELETE_DIRECTIVES,
85 // Synced push notifications.
86 SYNCED_NOTIFICATIONS,
87 // Custom spelling dictionary.
88 DICTIONARY,
89 // Favicon images.
90 FAVICON_IMAGES,
91 // Favicon tracking information.
92 FAVICON_TRACKING,
93 // These preferences are synced before other user types and are never
94 // encrypted.
95 PRIORITY_PREFERENCES,
96 // Managed user settings.
97 MANAGED_USER_SETTINGS,
98 // Managed users. Every managed user is a profile that is configured remotely
99 // by this user and can have restrictions applied. MANAGED_USERS and
100 // MANAGED_USER_SETTINGS can not be encrypted.
101 MANAGED_USERS,
102 // Distilled articles.
103 ARTICLES,
104 // App List items
105 APP_LIST,
106
107 // ---- Proxy types ----
108 // Proxy types are excluded from the sync protocol, but are still considered
109 // real user types. By convention, we prefix them with 'PROXY_' to distinguish
110 // them from normal protocol types.
111
112 // Tab sync. This is a placeholder type, so that Sessions can be implicitly
113 // enabled for history sync and tabs sync.
114 PROXY_TABS,
115
116 FIRST_PROXY_TYPE = PROXY_TABS,
117 LAST_PROXY_TYPE = PROXY_TABS,
118
119 LAST_USER_MODEL_TYPE = PROXY_TABS,
120
121 // ---- Control Types ----
122 // An object representing a set of Nigori keys.
123 NIGORI,
124 FIRST_CONTROL_MODEL_TYPE = NIGORI,
125 // Client-specific metadata.
126 DEVICE_INFO,
127 // Flags to enable experimental features.
128 EXPERIMENTS,
129 LAST_CONTROL_MODEL_TYPE = EXPERIMENTS,
130
131 LAST_REAL_MODEL_TYPE = LAST_CONTROL_MODEL_TYPE,
132
133 // If you are adding a new sync datatype that is exposed to the user via the
134 // sync preferences UI, be sure to update the list in
135 // chrome/browser/sync/user_selectable_sync_type.h so that the UMA histograms
136 // for sync include your new type.
137 // In this case, be sure to also update the UserSelectableTypes() definition
138 // in sync/syncable/model_type.cc.
139
140 MODEL_TYPE_COUNT,
141 };
142
143 typedef EnumSet<ModelType, FIRST_REAL_MODEL_TYPE, LAST_REAL_MODEL_TYPE>
144 ModelTypeSet;
145 typedef EnumSet<ModelType, UNSPECIFIED, LAST_REAL_MODEL_TYPE>
146 FullModelTypeSet;
147
ModelTypeFromInt(int i)148 inline ModelType ModelTypeFromInt(int i) {
149 DCHECK_GE(i, 0);
150 DCHECK_LT(i, MODEL_TYPE_COUNT);
151 return static_cast<ModelType>(i);
152 }
153
154 // Used by tests outside of sync/.
155 SYNC_EXPORT void AddDefaultFieldValue(ModelType datatype,
156 sync_pb::EntitySpecifics* specifics);
157
158 // Extract the model type of a SyncEntity protocol buffer. ModelType is a
159 // local concept: the enum is not in the protocol. The SyncEntity's ModelType
160 // is inferred from the presence of particular datatype field in the
161 // entity specifics.
162 SYNC_EXPORT_PRIVATE ModelType GetModelType(
163 const sync_pb::SyncEntity& sync_entity);
164
165 // Extract the model type from an EntitySpecifics field. Note that there
166 // are some ModelTypes (like TOP_LEVEL_FOLDER) that can't be inferred this way;
167 // prefer using GetModelType where possible.
168 SYNC_EXPORT ModelType GetModelTypeFromSpecifics(
169 const sync_pb::EntitySpecifics& specifics);
170
171 // Protocol types are those types that have actual protocol buffer
172 // representations. This distinguishes them from Proxy types, which have no
173 // protocol representation and are never sent to the server.
174 SYNC_EXPORT ModelTypeSet ProtocolTypes();
175
176 // These are the normal user-controlled types. This is to distinguish from
177 // ControlTypes which are always enabled. Note that some of these share a
178 // preference flag, so not all of them are individually user-selectable.
179 SYNC_EXPORT ModelTypeSet UserTypes();
180
181 // These are the user-selectable data types.
182 SYNC_EXPORT ModelTypeSet UserSelectableTypes();
183 SYNC_EXPORT bool IsUserSelectableType(ModelType model_type);
184
185 // This is the subset of UserTypes() that can be encrypted.
186 SYNC_EXPORT_PRIVATE ModelTypeSet EncryptableUserTypes();
187
188 // This is the subset of UserTypes() that have priority over other types. These
189 // types are synced before other user types and are never encrypted.
190 SYNC_EXPORT ModelTypeSet PriorityUserTypes();
191
192 // Proxy types are placeholder types for handling implicitly enabling real
193 // types. They do not exist at the server, and are simply used for
194 // UI/Configuration logic.
195 SYNC_EXPORT ModelTypeSet ProxyTypes();
196
197 // Returns a list of all control types.
198 //
199 // The control types are intended to contain metadata nodes that are essential
200 // for the normal operation of the syncer. As such, they have the following
201 // special properties:
202 // - They are downloaded early during SyncBackend initialization.
203 // - They are always enabled. Users may not disable these types.
204 // - Their contents are not encrypted automatically.
205 // - They support custom update application and conflict resolution logic.
206 // - All change processing occurs on the sync thread (GROUP_PASSIVE).
207 SYNC_EXPORT ModelTypeSet ControlTypes();
208
209 // Returns true if this is a control type.
210 //
211 // See comment above for more information on what makes these types special.
212 SYNC_EXPORT bool IsControlType(ModelType model_type);
213
214 // Core types are those data types used by sync's core functionality (i.e. not
215 // user data types). These types are always enabled, and include ControlTypes().
216 //
217 // The set of all core types.
218 SYNC_EXPORT ModelTypeSet CoreTypes();
219 // Those core types that have high priority (includes ControlTypes()).
220 SYNC_EXPORT ModelTypeSet PriorityCoreTypes();
221
222 // Determine a model type from the field number of its associated
223 // EntitySpecifics field. Returns UNSPECIFIED if the field number is
224 // not recognized.
225 //
226 // If you're putting the result in a ModelTypeSet, you should use the
227 // following pattern:
228 //
229 // ModelTypeSet model_types;
230 // // Say we're looping through a list of items, each of which has a
231 // // field number.
232 // for (...) {
233 // int field_number = ...;
234 // ModelType model_type =
235 // GetModelTypeFromSpecificsFieldNumber(field_number);
236 // if (!IsRealDataType(model_type)) {
237 // DLOG(WARNING) << "Unknown field number " << field_number;
238 // continue;
239 // }
240 // model_types.Put(model_type);
241 // }
242 SYNC_EXPORT_PRIVATE ModelType GetModelTypeFromSpecificsFieldNumber(
243 int field_number);
244
245 // Return the field number of the EntitySpecifics field associated with
246 // a model type.
247 //
248 // Used by tests outside of sync.
249 SYNC_EXPORT int GetSpecificsFieldNumberFromModelType(
250 ModelType model_type);
251
252 FullModelTypeSet ToFullModelTypeSet(ModelTypeSet in);
253
254 // TODO(sync): The functions below badly need some cleanup.
255
256 // Returns a pointer to a string with application lifetime that represents
257 // the name of |model_type|.
258 SYNC_EXPORT const char* ModelTypeToString(ModelType model_type);
259
260 // Some histograms take an integer parameter that represents a model type.
261 // The mapping from ModelType to integer is defined here. It should match
262 // the mapping from integer to labels defined in histograms.xml.
263 SYNC_EXPORT int ModelTypeToHistogramInt(ModelType model_type);
264
265 // Handles all model types, and not just real ones.
266 //
267 // Caller takes ownership of returned value.
268 SYNC_EXPORT_PRIVATE base::StringValue* ModelTypeToValue(ModelType model_type);
269
270 // Converts a Value into a ModelType - complement to ModelTypeToValue().
271 SYNC_EXPORT_PRIVATE ModelType ModelTypeFromValue(const base::Value& value);
272
273 // Returns the ModelType corresponding to the name |model_type_string|.
274 SYNC_EXPORT ModelType ModelTypeFromString(
275 const std::string& model_type_string);
276
277 SYNC_EXPORT std::string ModelTypeSetToString(ModelTypeSet model_types);
278
279 // Caller takes ownership of returned list.
280 SYNC_EXPORT base::ListValue* ModelTypeSetToValue(ModelTypeSet model_types);
281
282 SYNC_EXPORT ModelTypeSet ModelTypeSetFromValue(const base::ListValue& value);
283
284 // Returns a string corresponding to the syncable tag for this datatype.
285 SYNC_EXPORT std::string ModelTypeToRootTag(ModelType type);
286
287 // Convert a real model type to a notification type (used for
288 // subscribing to server-issued notifications). Returns true iff
289 // |model_type| was a real model type and |notification_type| was
290 // filled in.
291 bool RealModelTypeToNotificationType(ModelType model_type,
292 std::string* notification_type);
293
294 // Converts a notification type to a real model type. Returns true
295 // iff |notification_type| was the notification type of a real model
296 // type and |model_type| was filled in.
297 SYNC_EXPORT bool NotificationTypeToRealModelType(
298 const std::string& notification_type,
299 ModelType* model_type);
300
301 // Returns true if |model_type| is a real datatype
302 SYNC_EXPORT bool IsRealDataType(ModelType model_type);
303
304 // Returns true if |model_type| is an act-once type. Act once types drop
305 // entities after applying them. Drops are deletes that are not synced to other
306 // clients.
307 // TODO(haitaol): Make entries of act-once data types immutable.
308 SYNC_EXPORT bool IsActOnceDataType(ModelType model_type);
309
310 } // namespace syncer
311
312 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_MODEL_TYPE_H_
313