model_type.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #ifndef COMPONENTS_SYNC_BASE_MODEL_TYPE_H_
  5. #define COMPONENTS_SYNC_BASE_MODEL_TYPE_H_
  6. #include <iosfwd>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "base/containers/enum_set.h"
  11. namespace base {
  12. class ListValue;
  13. class Value;
  14. } // namespace base
  15. namespace sync_pb {
  16. class EntitySpecifics;
  17. }
  18. namespace syncer {
  19. // Enumerate the various item subtypes that are supported by sync.
  20. // Each sync object is expected to have an immutable object type.
  21. // An object's type is inferred from the type of data it holds.
  22. //
  23. // A Java counterpart will be generated for this enum.
  24. // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.sync
  25. //
  26. // |kModelTypeInfoMap| struct entries are in the same order as their definition
  27. // in ModelType enum. When you make changes in ModelType enum, don't forget to
  28. // update the |kModelTypeInfoMap| struct in model_type.cc and also the
  29. // SyncModelType histogram suffix in histograms.xml
  30. enum ModelType {
  31. // Object type unknown. This may be used when:
  32. // a) The client received *valid* data from a data type which this version
  33. // is unaware of (only present in versions newer than this one, or present
  34. // in older versions but removed since).
  35. // b) The client received invalid data from the server due to some error.
  36. // c) A data object was just created, in which case this is a temporary state.
  37. UNSPECIFIED,
  38. // ------------------------------------ Start of "real" model types.
  39. // The model types declared before here are somewhat special, as they
  40. // they do not correspond to any browser data model. The remaining types
  41. // are bona fide model types; all have a related browser data model and
  42. // can be represented in the protocol using a specific Message type in the
  43. // EntitySpecifics protocol buffer.
  44. //
  45. // A bookmark folder or a bookmark URL object.
  46. BOOKMARKS,
  47. FIRST_USER_MODEL_TYPE = BOOKMARKS, // Declared 2nd, for debugger prettiness.
  48. FIRST_REAL_MODEL_TYPE = FIRST_USER_MODEL_TYPE,
  49. // A preference object, a.k.a. "Settings".
  50. PREFERENCES,
  51. // A password object.
  52. PASSWORDS,
  53. // An autofill_profile object, i.e. an address.
  54. AUTOFILL_PROFILE,
  55. // An autofill object, i.e. an autocomplete entry keyed to an HTML form field.
  56. AUTOFILL,
  57. // Credit cards and addresses from the user's account. These are read-only on
  58. // the client.
  59. AUTOFILL_WALLET_DATA,
  60. // Usage counts and last use dates for Wallet cards and addresses. This data
  61. // is both readable and writable.
  62. AUTOFILL_WALLET_METADATA,
  63. // Offers and rewards from the user's account. These are read-only on the
  64. // client side.
  65. AUTOFILL_WALLET_OFFER,
  66. // A theme object.
  67. THEMES,
  68. // A typed_url object, i.e. a URL the user has typed into the Omnibox.
  69. TYPED_URLS,
  70. // An extension object.
  71. EXTENSIONS,
  72. // An object representing a custom search engine.
  73. SEARCH_ENGINES,
  74. // An object representing a browser session, e.g. an open tab. This is used
  75. // for both "History" (together with TYPED_URLS) and "Tabs" (depending on
  76. // PROXY_TABS).
  77. SESSIONS,
  78. // An app object.
  79. APPS,
  80. // An app setting from the extension settings API.
  81. APP_SETTINGS,
  82. // An extension setting from the extension settings API.
  83. EXTENSION_SETTINGS,
  84. // History delete directives, used to propagate history deletions (e.g. based
  85. // on a time range).
  86. HISTORY_DELETE_DIRECTIVES,
  87. // Custom spelling dictionary entries.
  88. DICTIONARY,
  89. // Client-specific metadata, synced before other user types.
  90. DEVICE_INFO,
  91. // These preferences are synced before other user types and are never
  92. // encrypted.
  93. PRIORITY_PREFERENCES,
  94. // Supervised user settings. Cannot be encrypted.
  95. SUPERVISED_USER_SETTINGS,
  96. // App List items, used by the ChromeOS app launcher.
  97. APP_LIST,
  98. // ARC package items, i.e. Android apps on ChromeOS.
  99. ARC_PACKAGE,
  100. // Printer device information. ChromeOS only.
  101. PRINTERS,
  102. // Reading list items. iOS only.
  103. READING_LIST,
  104. // Commit only user events.
  105. USER_EVENTS,
  106. // Commit only user consents.
  107. USER_CONSENTS,
  108. // Tabs sent between devices.
  109. SEND_TAB_TO_SELF,
  110. // Commit only security events.
  111. SECURITY_EVENTS,
  112. // Wi-Fi network configurations + credentials
  113. WIFI_CONFIGURATIONS,
  114. // A web app object.
  115. WEB_APPS,
  116. // OS-specific preferences (a.k.a. "OS settings"). Chrome OS only.
  117. OS_PREFERENCES,
  118. // Synced before other user types. Never encrypted. Chrome OS only.
  119. OS_PRIORITY_PREFERENCES,
  120. // Commit only sharing message object.
  121. SHARING_MESSAGE,
  122. // A workspace desk saved by user. Chrome OS only.
  123. WORKSPACE_DESK,
  124. // WebAuthn credentials. Commented out because this type is currently only
  125. // used by the server and Play Services, not Chrome itself.
  126. // (crbug.com/1223853)
  127. // WEBAUTHN_CREDENTIAL,
  128. // Synced history. An entity roughly corresponds to a navigation.
  129. HISTORY,
  130. // Trusted Authorization Servers for printers. ChromeOS only.
  131. PRINTERS_AUTHORIZATION_SERVERS,
  132. // Proxy types are excluded from the sync protocol, but are still considered
  133. // real user types. By convention, we prefix them with 'PROXY_' to distinguish
  134. // them from normal protocol types.
  135. //
  136. // Tab sync. This is a placeholder type, so that Sessions can be implicitly
  137. // enabled for history sync and tabs sync.
  138. PROXY_TABS,
  139. LAST_USER_MODEL_TYPE = PROXY_TABS,
  140. // ---- Control Types ----
  141. // An object representing a set of Nigori keys.
  142. NIGORI,
  143. LAST_REAL_MODEL_TYPE = NIGORI,
  144. // NEW ENTRIES MUST BE ADDED ABOVE THIS.
  145. LAST_ENTRY = LAST_REAL_MODEL_TYPE,
  146. };
  147. using ModelTypeSet =
  148. base::EnumSet<ModelType, FIRST_REAL_MODEL_TYPE, LAST_REAL_MODEL_TYPE>;
  149. using FullModelTypeSet =
  150. base::EnumSet<ModelType, UNSPECIFIED, LAST_REAL_MODEL_TYPE>;
  151. using ModelTypeNameMap = std::map<ModelType, const char*>;
  152. constexpr int GetNumModelTypes() {
  153. return static_cast<int>(ModelType::LAST_ENTRY) + 1;
  154. }
  155. inline ModelType ModelTypeFromInt(int i) {
  156. DCHECK_GE(i, 0);
  157. DCHECK_LT(i, GetNumModelTypes());
  158. return static_cast<ModelType>(i);
  159. }
  160. // A version of the ModelType enum for use in histograms. ModelType does not
  161. // have stable values (e.g. new ones may be inserted in the middle), so it can't
  162. // be recorded directly.
  163. // Instead of using entries from this enum directly, you'll usually want to get
  164. // them via ModelTypeHistogramValue(model_type).
  165. // These values are persisted to logs. Entries should not be renumbered and
  166. // numeric values should never be reused. When you add a new entry or when you
  167. // deprecate an existing one, also update SyncModelTypes in enums.xml and
  168. // SyncModelType suffix in histograms.xml.
  169. enum class ModelTypeForHistograms {
  170. kUnspecified = 0,
  171. // kTopLevelFolder = 1,
  172. kBookmarks = 2,
  173. kPreferences = 3,
  174. kPasswords = 4,
  175. kAutofillProfile = 5,
  176. kAutofill = 6,
  177. kThemes = 7,
  178. kTypedUrls = 8,
  179. kExtensions = 9,
  180. kSearchEngines = 10,
  181. kSessions = 11,
  182. kApps = 12,
  183. kAppSettings = 13,
  184. kExtensionSettings = 14,
  185. // kDeprecatedAppNotifications = 15,
  186. kHistoryDeleteDirectices = 16,
  187. kNigori = 17,
  188. kDeviceInfo = 18,
  189. // kDeprecatedExperiments = 19,
  190. // kDeprecatedSyncedNotifications = 20,
  191. kPriorityPreferences = 21,
  192. kDictionary = 22,
  193. // kFaviconImages = 23,
  194. // kFaviconTracking = 24,
  195. kProxyTabs = 25,
  196. kSupervisedUserSettings = 26,
  197. // kDeprecatedSupervisedUsers = 27,
  198. // kDeprecatedArticles = 28,
  199. kAppList = 29,
  200. // kDeprecatedSupervisedUserSharedSettings = 30,
  201. // kDeprecatedSyncedNotificationAppInfo = 31,
  202. // kDeprecatedWifiCredentials = 32,
  203. kDeprecatedSupervisedUserAllowlists = 33,
  204. kAutofillWalletData = 34,
  205. kAutofillWalletMetadata = 35,
  206. kArcPackage = 36,
  207. kPrinters = 37,
  208. kReadingList = 38,
  209. kUserEvents = 39,
  210. // kDeprecatedMountainShares = 40,
  211. kUserConsents = 41,
  212. kSendTabToSelf = 42,
  213. kSecurityEvents = 43,
  214. kWifiConfigurations = 44,
  215. kWebApps = 45,
  216. kOsPreferences = 46,
  217. kOsPriorityPreferences = 47,
  218. kSharingMessage = 48,
  219. kAutofillWalletOffer = 49,
  220. kWorkspaceDesk = 50,
  221. kHistory = 51,
  222. kPrintersAuthorizationServers = 52,
  223. kMaxValue = kPrintersAuthorizationServers
  224. };
  225. // Used to mark the type of EntitySpecifics that has no actual data.
  226. void AddDefaultFieldValue(ModelType type, sync_pb::EntitySpecifics* specifics);
  227. // Extract the model type from an EntitySpecifics field. ModelType is a
  228. // local concept: the enum is not in the protocol.
  229. ModelType GetModelTypeFromSpecifics(const sync_pb::EntitySpecifics& specifics);
  230. // Protocol types are those types that have actual protocol buffer
  231. // representations. This distinguishes them from Proxy types, which have no
  232. // protocol representation and are never sent to the server.
  233. constexpr ModelTypeSet ProtocolTypes() {
  234. return ModelTypeSet(
  235. BOOKMARKS, PREFERENCES, PASSWORDS, AUTOFILL_PROFILE, AUTOFILL,
  236. AUTOFILL_WALLET_DATA, AUTOFILL_WALLET_METADATA, AUTOFILL_WALLET_OFFER,
  237. THEMES, TYPED_URLS, EXTENSIONS, SEARCH_ENGINES, SESSIONS, APPS,
  238. APP_SETTINGS, EXTENSION_SETTINGS, HISTORY_DELETE_DIRECTIVES, DICTIONARY,
  239. DEVICE_INFO, PRIORITY_PREFERENCES, SUPERVISED_USER_SETTINGS, APP_LIST,
  240. ARC_PACKAGE, PRINTERS, READING_LIST, USER_EVENTS, NIGORI, USER_CONSENTS,
  241. SEND_TAB_TO_SELF, SECURITY_EVENTS, WEB_APPS, WIFI_CONFIGURATIONS,
  242. OS_PREFERENCES, OS_PRIORITY_PREFERENCES, SHARING_MESSAGE, WORKSPACE_DESK,
  243. HISTORY, PRINTERS_AUTHORIZATION_SERVERS);
  244. }
  245. // These are the normal user-controlled types. This is to distinguish from
  246. // ControlTypes which are always enabled. Note that some of these share a
  247. // preference flag, so not all of them are individually user-selectable.
  248. constexpr ModelTypeSet UserTypes() {
  249. return ModelTypeSet::FromRange(FIRST_USER_MODEL_TYPE, LAST_USER_MODEL_TYPE);
  250. }
  251. // User types, which are not user-controlled.
  252. constexpr ModelTypeSet AlwaysPreferredUserTypes() {
  253. return ModelTypeSet(DEVICE_INFO, USER_CONSENTS, SECURITY_EVENTS,
  254. SEND_TAB_TO_SELF, SUPERVISED_USER_SETTINGS,
  255. SHARING_MESSAGE);
  256. }
  257. // User types which are always encrypted.
  258. constexpr ModelTypeSet AlwaysEncryptedUserTypes() {
  259. // If you add a new model type here that is conceptually different from a
  260. // password, make sure you audit UI code that refers to these types as
  261. // passwords, e.g. consumers of IsEncryptEverythingEnabled().
  262. return ModelTypeSet(PASSWORDS, WIFI_CONFIGURATIONS);
  263. }
  264. // This is the subset of UserTypes() that have priority over other types. These
  265. // types are synced before other user types (both for get_updates and commits).
  266. // This mostly matters during initial sync, since priority types can become
  267. // active before all the data for non-prio types has been downloaded (which may
  268. // be a lot of data).
  269. constexpr ModelTypeSet PriorityUserTypes() {
  270. return ModelTypeSet(
  271. // The "Send to Your Devices" feature needs fast updating of the list of
  272. // your devices and also fast sending of the actual messages.
  273. DEVICE_INFO, SHARING_MESSAGE,
  274. // For supervised users, it is important to quickly deliver changes in
  275. // settings and in allowed sites to the supervised user.
  276. SUPERVISED_USER_SETTINGS,
  277. // These are by definition preferences for which it is important that the
  278. // client picks them up quickly (also because these can get changed
  279. // server-side). For example, such a pref could control whether a
  280. // non-priority type gets enabled (Wallet has such a pref).
  281. PRIORITY_PREFERENCES, OS_PRIORITY_PREFERENCES,
  282. // Speed matters for the user experience when sync gets enabled directly
  283. // in the creation flow for a new profile. If the user has no theme in
  284. // their sync data, the browser offers a theme customization bubble which
  285. // should appear soon after opening the browser.
  286. THEMES);
  287. }
  288. // Returns a list of all control types.
  289. //
  290. // The control types are intended to contain metadata nodes that are essential
  291. // for the normal operation of the syncer. As such, they have the following
  292. // special properties:
  293. // - They are downloaded early during SyncBackend initialization.
  294. // - They are always enabled. Users may not disable these types.
  295. // - Their contents are not encrypted automatically.
  296. // - They support custom update application and conflict resolution logic.
  297. // - All change processing occurs on the sync thread.
  298. constexpr ModelTypeSet ControlTypes() {
  299. return ModelTypeSet(NIGORI);
  300. }
  301. // Returns true if this is a control type.
  302. //
  303. // See comment above for more information on what makes these types special.
  304. constexpr bool IsControlType(ModelType model_type) {
  305. return ControlTypes().Has(model_type);
  306. }
  307. // Types that may commit data, but should never be included in a GetUpdates.
  308. // These are never encrypted.
  309. constexpr ModelTypeSet CommitOnlyTypes() {
  310. return ModelTypeSet(USER_EVENTS, USER_CONSENTS, SECURITY_EVENTS,
  311. SHARING_MESSAGE);
  312. }
  313. // User types that can be encrypted, which is a subset of UserTypes() and a
  314. // superset of AlwaysEncryptedUserTypes();
  315. ModelTypeSet EncryptableUserTypes();
  316. // Determine a model type from the field number of its associated
  317. // EntitySpecifics field. Returns UNSPECIFIED if the field number is
  318. // not recognized.
  319. //
  320. // If you're putting the result in a ModelTypeSet, you should use the
  321. // following pattern:
  322. //
  323. // ModelTypeSet model_types;
  324. // // Say we're looping through a list of items, each of which has a
  325. // // field number.
  326. // for (...) {
  327. // int field_number = ...;
  328. // ModelType model_type =
  329. // GetModelTypeFromSpecificsFieldNumber(field_number);
  330. // if (!IsRealDataType(model_type)) {
  331. // DLOG(WARNING) << "Unknown field number " << field_number;
  332. // continue;
  333. // }
  334. // model_types.Put(model_type);
  335. // }
  336. ModelType GetModelTypeFromSpecificsFieldNumber(int field_number);
  337. // Return the field number of the EntitySpecifics field associated with
  338. // a model type.
  339. int GetSpecificsFieldNumberFromModelType(ModelType model_type);
  340. // TODO(sync): The functions below badly need some cleanup.
  341. // Returns a string with application lifetime that represents the name of
  342. // |model_type|.
  343. const char* ModelTypeToDebugString(ModelType model_type);
  344. // Returns a string with application lifetime that is used as the histogram
  345. // suffix for |model_type|.
  346. const char* ModelTypeToHistogramSuffix(ModelType model_type);
  347. // Some histograms take an integer parameter that represents a model type.
  348. // The mapping from ModelType to integer is defined here. It defines a
  349. // completely different order than the ModelType enum itself. The mapping should
  350. // match the SyncModelTypes mapping from integer to labels defined in enums.xml.
  351. ModelTypeForHistograms ModelTypeHistogramValue(ModelType model_type);
  352. // Returns for every model_type a positive unique integer that is stable over
  353. // time and thus can be used when persisting data.
  354. int ModelTypeToStableIdentifier(ModelType model_type);
  355. // Handles all model types, and not just real ones.
  356. std::unique_ptr<base::Value> ModelTypeToValue(ModelType model_type);
  357. // Returns the comma-separated string representation of |model_types|.
  358. std::string ModelTypeSetToDebugString(ModelTypeSet model_types);
  359. // Necessary for compatibility with EXPECT_EQ and the like.
  360. std::ostream& operator<<(std::ostream& out, ModelTypeSet model_type_set);
  361. // Generates a base::ListValue from |model_types|.
  362. std::unique_ptr<base::ListValue> ModelTypeSetToValue(ModelTypeSet model_types);
  363. // Returns a string corresponding to the syncable tag for this datatype.
  364. std::string ModelTypeToRootTag(ModelType type);
  365. // Returns root_tag for |model_type| in ModelTypeInfo.
  366. // Difference with ModelTypeToRootTag(), this just simply returns root_tag in
  367. // ModelTypeInfo.
  368. const char* GetModelTypeRootTag(ModelType model_type);
  369. // Convert a real model type to a notification type (used for
  370. // subscribing to server-issued notifications). Returns true iff
  371. // |model_type| was a real model type and |notification_type| was
  372. // filled in.
  373. bool RealModelTypeToNotificationType(ModelType model_type,
  374. std::string* notification_type);
  375. // Converts a notification type to a real model type. Returns true
  376. // iff |notification_type| was the notification type of a real model
  377. // type and |model_type| was filled in.
  378. [[nodiscard]] bool NotificationTypeToRealModelType(
  379. const std::string& notification_type,
  380. ModelType* model_type);
  381. // Returns true if |model_type| is a real datatype
  382. bool IsRealDataType(ModelType model_type);
  383. // Returns true if |model_type| is an act-once type. Act once types drop
  384. // entities after applying them. Drops are deletes that are not synced to other
  385. // clients.
  386. // TODO(haitaol): Make entries of act-once data types immutable.
  387. bool IsActOnceDataType(ModelType model_type);
  388. // Returns true if |model_type| requires its root folder to be explicitly
  389. // created on the server during initial sync.
  390. bool IsTypeWithServerGeneratedRoot(ModelType model_type);
  391. // Returns true if root folder for |model_type| is created on the client when
  392. // that type is initially synced.
  393. bool IsTypeWithClientGeneratedRoot(ModelType model_type);
  394. } // namespace syncer
  395. #endif // COMPONENTS_SYNC_BASE_MODEL_TYPE_H_