user_selectable_type.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright 2019 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. #include "components/sync/base/user_selectable_type.h"
  5. #include <type_traits>
  6. #include "base/notreached.h"
  7. #include "build/chromeos_buildflags.h"
  8. #include "components/sync/base/model_type.h"
  9. #include "components/sync/base/pref_names.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #if BUILDFLAG(IS_CHROMEOS_ASH)
  12. #include "ash/constants/ash_features.h"
  13. #endif
  14. namespace syncer {
  15. namespace {
  16. struct UserSelectableTypeInfo {
  17. const char* const type_name;
  18. const ModelType canonical_model_type;
  19. const ModelTypeSet model_type_group;
  20. };
  21. constexpr char kBookmarksTypeName[] = "bookmarks";
  22. constexpr char kPreferencesTypeName[] = "preferences";
  23. constexpr char kPasswordsTypeName[] = "passwords";
  24. constexpr char kAutofillTypeName[] = "autofill";
  25. constexpr char kThemesTypeName[] = "themes";
  26. constexpr char kTypedUrlsTypeName[] = "typedUrls";
  27. constexpr char kExtensionsTypeName[] = "extensions";
  28. constexpr char kAppsTypeName[] = "apps";
  29. constexpr char kReadingListTypeName[] = "readingList";
  30. constexpr char kTabsTypeName[] = "tabs";
  31. constexpr char kWifiConfigurationsTypeName[] = "wifiConfigurations";
  32. UserSelectableTypeInfo GetUserSelectableTypeInfo(UserSelectableType type) {
  33. static_assert(40 == syncer::GetNumModelTypes(),
  34. "Almost always when adding a new ModelType, you must tie it to "
  35. "a UserSelectableType below (new or existing) so the user can "
  36. "disable syncing of that data. Today you must also update the "
  37. "UI code yourself; crbug.com/1067282 and related bugs will "
  38. "improve that");
  39. // UserSelectableTypeInfo::type_name is used in js code and shouldn't be
  40. // changed without updating js part.
  41. switch (type) {
  42. case UserSelectableType::kBookmarks:
  43. return {kBookmarksTypeName, BOOKMARKS, {BOOKMARKS}};
  44. case UserSelectableType::kPreferences: {
  45. ModelTypeSet model_types = {PREFERENCES, DICTIONARY, PRIORITY_PREFERENCES,
  46. SEARCH_ENGINES};
  47. #if BUILDFLAG(IS_CHROMEOS_ASH)
  48. if (!chromeos::features::IsSyncSettingsCategorizationEnabled()) {
  49. // SyncSettingsCategorization makes Printers a separate OS setting.
  50. model_types.Put(PRINTERS);
  51. model_types.Put(PRINTERS_AUTHORIZATION_SERVERS);
  52. // Workspace desk template is an OS-only feature. When
  53. // SyncSettingsCategorization is disabled, WORKSPACE_DESK should be
  54. // enabled with user preferences. Otherwise, WORKSPACE_DESK should be
  55. // enabled with OS preferences below.
  56. model_types.Put(WORKSPACE_DESK);
  57. }
  58. #endif
  59. return {kPreferencesTypeName, PREFERENCES, model_types};
  60. }
  61. case UserSelectableType::kPasswords:
  62. return {kPasswordsTypeName, PASSWORDS, {PASSWORDS}};
  63. case UserSelectableType::kAutofill:
  64. return {kAutofillTypeName,
  65. AUTOFILL,
  66. {AUTOFILL, AUTOFILL_PROFILE, AUTOFILL_WALLET_DATA,
  67. AUTOFILL_WALLET_METADATA, AUTOFILL_WALLET_OFFER}};
  68. case UserSelectableType::kThemes:
  69. return {kThemesTypeName, THEMES, {THEMES}};
  70. case UserSelectableType::kHistory:
  71. return {kTypedUrlsTypeName,
  72. TYPED_URLS,
  73. {TYPED_URLS, HISTORY, HISTORY_DELETE_DIRECTIVES, SESSIONS,
  74. USER_EVENTS}};
  75. case UserSelectableType::kExtensions:
  76. return {
  77. kExtensionsTypeName, EXTENSIONS, {EXTENSIONS, EXTENSION_SETTINGS}};
  78. case UserSelectableType::kApps: {
  79. #if BUILDFLAG(IS_CHROMEOS_ASH)
  80. // SyncSettingsCategorization moves apps to Chrome OS settings.
  81. if (chromeos::features::IsSyncSettingsCategorizationEnabled()) {
  82. return {kAppsTypeName, UNSPECIFIED};
  83. } else {
  84. return {kAppsTypeName,
  85. APPS,
  86. {APP_LIST, APPS, APP_SETTINGS, ARC_PACKAGE, WEB_APPS}};
  87. }
  88. #else
  89. return {kAppsTypeName, APPS, {APPS, APP_SETTINGS, WEB_APPS}};
  90. #endif
  91. }
  92. case UserSelectableType::kReadingList:
  93. return {kReadingListTypeName, READING_LIST, {READING_LIST}};
  94. case UserSelectableType::kTabs: {
  95. return {kTabsTypeName, PROXY_TABS, {PROXY_TABS, SESSIONS}};
  96. }
  97. case UserSelectableType::kWifiConfigurations: {
  98. #if BUILDFLAG(IS_CHROMEOS_ASH)
  99. // SyncSettingsCategorization moves Wi-Fi configurations to Chrome OS
  100. // settings.
  101. if (chromeos::features::IsSyncSettingsCategorizationEnabled())
  102. return {kWifiConfigurationsTypeName, UNSPECIFIED};
  103. #endif
  104. return {kWifiConfigurationsTypeName,
  105. WIFI_CONFIGURATIONS,
  106. {WIFI_CONFIGURATIONS}};
  107. }
  108. }
  109. NOTREACHED();
  110. return {nullptr, UNSPECIFIED, {}};
  111. }
  112. #if BUILDFLAG(IS_CHROMEOS_ASH)
  113. constexpr char kOsAppsTypeName[] = "osApps";
  114. constexpr char kOsPreferencesTypeName[] = "osPreferences";
  115. constexpr char kOsWifiConfigurationsTypeName[] = "osWifiConfigurations";
  116. UserSelectableTypeInfo GetUserSelectableOsTypeInfo(UserSelectableOsType type) {
  117. // UserSelectableTypeInfo::type_name is used in js code and shouldn't be
  118. // changed without updating js part.
  119. switch (type) {
  120. case UserSelectableOsType::kOsApps:
  121. return {kOsAppsTypeName,
  122. APPS,
  123. {APP_LIST, APPS, APP_SETTINGS, ARC_PACKAGE, WEB_APPS}};
  124. case UserSelectableOsType::kOsPreferences:
  125. return {kOsPreferencesTypeName,
  126. OS_PREFERENCES,
  127. {OS_PREFERENCES, OS_PRIORITY_PREFERENCES, PRINTERS,
  128. PRINTERS_AUTHORIZATION_SERVERS, WORKSPACE_DESK}};
  129. case UserSelectableOsType::kOsWifiConfigurations:
  130. return {kOsWifiConfigurationsTypeName,
  131. WIFI_CONFIGURATIONS,
  132. {WIFI_CONFIGURATIONS}};
  133. }
  134. }
  135. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  136. } // namespace
  137. const char* GetUserSelectableTypeName(UserSelectableType type) {
  138. return GetUserSelectableTypeInfo(type).type_name;
  139. }
  140. absl::optional<UserSelectableType> GetUserSelectableTypeFromString(
  141. const std::string& type) {
  142. if (type == kBookmarksTypeName) {
  143. return UserSelectableType::kBookmarks;
  144. }
  145. if (type == kPreferencesTypeName) {
  146. return UserSelectableType::kPreferences;
  147. }
  148. if (type == kPasswordsTypeName) {
  149. return UserSelectableType::kPasswords;
  150. }
  151. if (type == kAutofillTypeName) {
  152. return UserSelectableType::kAutofill;
  153. }
  154. if (type == kThemesTypeName) {
  155. return UserSelectableType::kThemes;
  156. }
  157. if (type == kTypedUrlsTypeName) {
  158. return UserSelectableType::kHistory;
  159. }
  160. if (type == kExtensionsTypeName) {
  161. return UserSelectableType::kExtensions;
  162. }
  163. if (type == kAppsTypeName) {
  164. return UserSelectableType::kApps;
  165. }
  166. if (type == kReadingListTypeName) {
  167. return UserSelectableType::kReadingList;
  168. }
  169. if (type == kTabsTypeName) {
  170. return UserSelectableType::kTabs;
  171. }
  172. if (type == kWifiConfigurationsTypeName) {
  173. return UserSelectableType::kWifiConfigurations;
  174. }
  175. return absl::nullopt;
  176. }
  177. std::string UserSelectableTypeSetToString(UserSelectableTypeSet types) {
  178. std::string result;
  179. for (UserSelectableType type : types) {
  180. if (!result.empty()) {
  181. result += ", ";
  182. }
  183. result += GetUserSelectableTypeName(type);
  184. }
  185. return result;
  186. }
  187. ModelTypeSet UserSelectableTypeToAllModelTypes(UserSelectableType type) {
  188. return GetUserSelectableTypeInfo(type).model_type_group;
  189. }
  190. ModelType UserSelectableTypeToCanonicalModelType(UserSelectableType type) {
  191. return GetUserSelectableTypeInfo(type).canonical_model_type;
  192. }
  193. #if BUILDFLAG(IS_CHROMEOS_ASH)
  194. const char* GetUserSelectableOsTypeName(UserSelectableOsType type) {
  195. return GetUserSelectableOsTypeInfo(type).type_name;
  196. }
  197. std::string UserSelectableOsTypeSetToString(UserSelectableOsTypeSet types) {
  198. std::string result;
  199. for (UserSelectableOsType type : types) {
  200. if (!result.empty()) {
  201. result += ", ";
  202. }
  203. result += GetUserSelectableOsTypeName(type);
  204. }
  205. return result;
  206. }
  207. absl::optional<UserSelectableOsType> GetUserSelectableOsTypeFromString(
  208. const std::string& type) {
  209. if (type == kOsAppsTypeName) {
  210. return UserSelectableOsType::kOsApps;
  211. }
  212. if (type == kOsPreferencesTypeName) {
  213. return UserSelectableOsType::kOsPreferences;
  214. }
  215. if (type == kOsWifiConfigurationsTypeName) {
  216. return UserSelectableOsType::kOsWifiConfigurations;
  217. }
  218. // Some pref types migrated from browser prefs to OS prefs. Map the browser
  219. // type name to the OS type so that enterprise policy SyncTypesListDisabled
  220. // still applies to the migrated names during SyncSettingsCategorization
  221. // roll-out.
  222. // TODO(https://crbug.com/1059309): Rename "osApps" to "apps" and
  223. // "osWifiConfigurations" to "wifiConfigurations" after
  224. // SyncSettingsCategorization is the default, and remove the mapping for
  225. // "preferences".
  226. if (type == kAppsTypeName) {
  227. return UserSelectableOsType::kOsApps;
  228. }
  229. if (type == kWifiConfigurationsTypeName) {
  230. return UserSelectableOsType::kOsWifiConfigurations;
  231. }
  232. if (type == kPreferencesTypeName) {
  233. return UserSelectableOsType::kOsPreferences;
  234. }
  235. return absl::nullopt;
  236. }
  237. ModelTypeSet UserSelectableOsTypeToAllModelTypes(UserSelectableOsType type) {
  238. return GetUserSelectableOsTypeInfo(type).model_type_group;
  239. }
  240. ModelType UserSelectableOsTypeToCanonicalModelType(UserSelectableOsType type) {
  241. return GetUserSelectableOsTypeInfo(type).canonical_model_type;
  242. }
  243. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  244. } // namespace syncer