• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Synced Notification app info.
88   SYNCED_NOTIFICATION_APP_INFO,
89   // Custom spelling dictionary.
90   DICTIONARY,
91   // Favicon images.
92   FAVICON_IMAGES,
93   // Favicon tracking information.
94   FAVICON_TRACKING,
95   // Client-specific metadata, synced before other user types.
96   DEVICE_INFO,
97   // These preferences are synced before other user types and are never
98   // encrypted.
99   PRIORITY_PREFERENCES,
100   // Supervised user settings.
101   SUPERVISED_USER_SETTINGS,
102   // Supervised users. Every supervised user is a profile that is configured
103   // remotely by this user and can have restrictions applied. SUPERVISED_USERS
104   // and SUPERVISED_USER_SETTINGS can not be encrypted.
105   SUPERVISED_USERS,
106   // Supervised user shared settings. Shared settings can be modified both by
107   // the manager and the supervised user.
108   SUPERVISED_USER_SHARED_SETTINGS,
109   // Distilled articles.
110   ARTICLES,
111   // App List items
112   APP_LIST,
113 
114   // ---- Proxy types ----
115   // Proxy types are excluded from the sync protocol, but are still considered
116   // real user types. By convention, we prefix them with 'PROXY_' to distinguish
117   // them from normal protocol types.
118 
119   // Tab sync. This is a placeholder type, so that Sessions can be implicitly
120   // enabled for history sync and tabs sync.
121   PROXY_TABS,
122   FIRST_PROXY_TYPE = PROXY_TABS,
123   LAST_PROXY_TYPE = PROXY_TABS,
124   LAST_USER_MODEL_TYPE = PROXY_TABS,
125 
126   // ---- Control Types ----
127   // An object representing a set of Nigori keys.
128   NIGORI,
129   FIRST_CONTROL_MODEL_TYPE = NIGORI,
130   // Flags to enable experimental features.
131   EXPERIMENTS,
132   LAST_CONTROL_MODEL_TYPE = EXPERIMENTS,
133   LAST_REAL_MODEL_TYPE = LAST_CONTROL_MODEL_TYPE,
134 
135   // If you are adding a new sync datatype that is exposed to the user via the
136   // sync preferences UI, be sure to update the list in
137   // components/sync_driver/user_selectable_sync_type.h so that the UMA
138   // histograms for sync include your new type.  In this case, be sure to also
139   // update the UserSelectableTypes() definition in
140   // sync/syncable/model_type.cc.
141   MODEL_TYPE_COUNT,
142 };
143 
144 typedef EnumSet<ModelType, FIRST_REAL_MODEL_TYPE, LAST_REAL_MODEL_TYPE>
145     ModelTypeSet;
146 typedef EnumSet<ModelType, UNSPECIFIED, LAST_REAL_MODEL_TYPE>
147     FullModelTypeSet;
148 
ModelTypeFromInt(int i)149 inline ModelType ModelTypeFromInt(int i) {
150   DCHECK_GE(i, 0);
151   DCHECK_LT(i, MODEL_TYPE_COUNT);
152   return static_cast<ModelType>(i);
153 }
154 
155 // Used by tests outside of sync/.
156 SYNC_EXPORT void AddDefaultFieldValue(ModelType datatype,
157                                       sync_pb::EntitySpecifics* specifics);
158 
159 // Extract the model type of a SyncEntity protocol buffer.  ModelType is a
160 // local concept: the enum is not in the protocol.  The SyncEntity's ModelType
161 // is inferred from the presence of particular datatype field in the
162 // entity specifics.
163 SYNC_EXPORT_PRIVATE ModelType GetModelType(
164     const sync_pb::SyncEntity& sync_entity);
165 
166 // Extract the model type from an EntitySpecifics field.  Note that there
167 // are some ModelTypes (like TOP_LEVEL_FOLDER) that can't be inferred this way;
168 // prefer using GetModelType where possible.
169 SYNC_EXPORT ModelType GetModelTypeFromSpecifics(
170     const sync_pb::EntitySpecifics& specifics);
171 
172 // Protocol types are those types that have actual protocol buffer
173 // representations. This distinguishes them from Proxy types, which have no
174 // protocol representation and are never sent to the server.
175 SYNC_EXPORT ModelTypeSet ProtocolTypes();
176 
177 // These are the normal user-controlled types. This is to distinguish from
178 // ControlTypes which are always enabled.  Note that some of these share a
179 // preference flag, so not all of them are individually user-selectable.
180 SYNC_EXPORT ModelTypeSet UserTypes();
181 
182 // These are the user-selectable data types.
183 SYNC_EXPORT ModelTypeSet UserSelectableTypes();
184 SYNC_EXPORT bool IsUserSelectableType(ModelType model_type);
185 
186 // This is the subset of UserTypes() that can be encrypted.
187 SYNC_EXPORT_PRIVATE ModelTypeSet EncryptableUserTypes();
188 
189 // This is the subset of UserTypes() that have priority over other types.  These
190 // types are synced before other user types and are never encrypted.
191 SYNC_EXPORT ModelTypeSet PriorityUserTypes();
192 
193 // Proxy types are placeholder types for handling implicitly enabling real
194 // types. They do not exist at the server, and are simply used for
195 // UI/Configuration logic.
196 SYNC_EXPORT ModelTypeSet ProxyTypes();
197 
198 // Returns a list of all control types.
199 //
200 // The control types are intended to contain metadata nodes that are essential
201 // for the normal operation of the syncer.  As such, they have the following
202 // special properties:
203 // - They are downloaded early during SyncBackend initialization.
204 // - They are always enabled.  Users may not disable these types.
205 // - Their contents are not encrypted automatically.
206 // - They support custom update application and conflict resolution logic.
207 // - All change processing occurs on the sync thread (GROUP_PASSIVE).
208 SYNC_EXPORT ModelTypeSet ControlTypes();
209 
210 // Returns true if this is a control type.
211 //
212 // See comment above for more information on what makes these types special.
213 SYNC_EXPORT bool IsControlType(ModelType model_type);
214 
215 // Core types are those data types used by sync's core functionality (i.e. not
216 // user data types). These types are always enabled, and include ControlTypes().
217 //
218 // The set of all core types.
219 SYNC_EXPORT ModelTypeSet CoreTypes();
220 // Those core types that have high priority (includes ControlTypes()).
221 SYNC_EXPORT ModelTypeSet PriorityCoreTypes();
222 
223 // Determine a model type from the field number of its associated
224 // EntitySpecifics field.  Returns UNSPECIFIED if the field number is
225 // not recognized.
226 //
227 // If you're putting the result in a ModelTypeSet, you should use the
228 // following pattern:
229 //
230 //   ModelTypeSet model_types;
231 //   // Say we're looping through a list of items, each of which has a
232 //   // field number.
233 //   for (...) {
234 //     int field_number = ...;
235 //     ModelType model_type =
236 //         GetModelTypeFromSpecificsFieldNumber(field_number);
237 //     if (!IsRealDataType(model_type)) {
238 //       DLOG(WARNING) << "Unknown field number " << field_number;
239 //       continue;
240 //     }
241 //     model_types.Put(model_type);
242 //   }
243 SYNC_EXPORT_PRIVATE ModelType GetModelTypeFromSpecificsFieldNumber(
244     int field_number);
245 
246 // Return the field number of the EntitySpecifics field associated with
247 // a model type.
248 //
249 // Used by tests outside of sync.
250 SYNC_EXPORT int GetSpecificsFieldNumberFromModelType(
251     ModelType model_type);
252 
253 FullModelTypeSet ToFullModelTypeSet(ModelTypeSet in);
254 
255 // TODO(sync): The functions below badly need some cleanup.
256 
257 // Returns a pointer to a string with application lifetime that represents
258 // the name of |model_type|.
259 SYNC_EXPORT const char* ModelTypeToString(ModelType model_type);
260 
261 // Some histograms take an integer parameter that represents a model type.
262 // The mapping from ModelType to integer is defined here.  It should match
263 // the mapping from integer to labels defined in histograms.xml.
264 SYNC_EXPORT int ModelTypeToHistogramInt(ModelType model_type);
265 
266 // Handles all model types, and not just real ones.
267 //
268 // Caller takes ownership of returned value.
269 SYNC_EXPORT_PRIVATE base::StringValue* ModelTypeToValue(ModelType model_type);
270 
271 // Converts a Value into a ModelType - complement to ModelTypeToValue().
272 SYNC_EXPORT_PRIVATE ModelType ModelTypeFromValue(const base::Value& value);
273 
274 // Returns the ModelType corresponding to the name |model_type_string|.
275 SYNC_EXPORT ModelType ModelTypeFromString(
276     const std::string& model_type_string);
277 
278 // Returns the comma-separated string representation of |model_types|.
279 SYNC_EXPORT std::string ModelTypeSetToString(ModelTypeSet model_types);
280 
281 // Returns the set of comma-separated model types from |model_type_string|.
282 SYNC_EXPORT ModelTypeSet ModelTypeSetFromString(
283     const std::string& model_type_string);
284 
285 // Caller takes ownership of returned list.
286 SYNC_EXPORT base::ListValue* ModelTypeSetToValue(ModelTypeSet model_types);
287 
288 SYNC_EXPORT ModelTypeSet ModelTypeSetFromValue(const base::ListValue& value);
289 
290 // Returns a string corresponding to the syncable tag for this datatype.
291 SYNC_EXPORT std::string ModelTypeToRootTag(ModelType type);
292 
293 // Convert a real model type to a notification type (used for
294 // subscribing to server-issued notifications).  Returns true iff
295 // |model_type| was a real model type and |notification_type| was
296 // filled in.
297 SYNC_EXPORT bool RealModelTypeToNotificationType(
298     ModelType model_type,
299     std::string* notification_type);
300 
301 // Converts a notification type to a real model type.  Returns true
302 // iff |notification_type| was the notification type of a real model
303 // type and |model_type| was filled in.
304 SYNC_EXPORT bool NotificationTypeToRealModelType(
305     const std::string& notification_type,
306     ModelType* model_type);
307 
308 // Returns true if |model_type| is a real datatype
309 SYNC_EXPORT bool IsRealDataType(ModelType model_type);
310 
311 // Returns true if |model_type| is a proxy type
312 SYNC_EXPORT bool IsProxyType(ModelType model_type);
313 
314 // Returns true if |model_type| is an act-once type. Act once types drop
315 // entities after applying them. Drops are deletes that are not synced to other
316 // clients.
317 // TODO(haitaol): Make entries of act-once data types immutable.
318 SYNC_EXPORT bool IsActOnceDataType(ModelType model_type);
319 
320 // Returns set of model types that should be backed up before first sync.
321 SYNC_EXPORT ModelTypeSet BackupTypes();
322 
323 }  // namespace syncer
324 
325 #endif  // SYNC_INTERNAL_API_PUBLIC_BASE_MODEL_TYPE_H_
326