Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / sync / chrome_sync_client.cc
blobe697e4ce0ea06b9a3f89c8af3495a9993c9a3a4b
1 // Copyright 2015 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.
5 #include "chrome/browser/sync/chrome_sync_client.h"
7 #include "chrome/browser/autofill/personal_data_manager_factory.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
10 #include "chrome/browser/history/history_service_factory.h"
11 #include "chrome/browser/password_manager/password_store_factory.h"
12 #include "chrome/browser/prefs/pref_service_syncable_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
16 #include "chrome/browser/sync/profile_sync_service.h"
17 #include "chrome/browser/sync/profile_sync_service_factory.h"
18 #include "chrome/browser/themes/theme_service.h"
19 #include "chrome/browser/themes/theme_service_factory.h"
20 #include "chrome/browser/themes/theme_syncable_service.h"
21 #include "chrome/browser/web_data_service_factory.h"
22 #include "components/autofill/core/browser/webdata/autocomplete_syncable_service.h"
23 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
24 #include "components/autofill/core/browser/webdata/autofill_wallet_metadata_syncable_service.h"
25 #include "components/autofill/core/browser/webdata/autofill_wallet_syncable_service.h"
26 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
27 #include "components/dom_distiller/core/dom_distiller_service.h"
28 #include "components/history/core/browser/history_service.h"
29 #include "components/password_manager/core/browser/password_store.h"
30 #include "components/syncable_prefs/pref_service_syncable.h"
31 #include "content/public/browser/browser_thread.h"
33 #if defined(ENABLE_APP_LIST)
34 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
35 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
36 #include "ui/app_list/app_list_switches.h"
37 #endif
39 #if defined(ENABLE_EXTENSIONS)
40 #include "chrome/browser/extensions/api/storage/settings_sync_util.h"
41 #include "chrome/browser/extensions/extension_sync_service.h"
42 #include "chrome/browser/sync/glue/extension_data_type_controller.h"
43 #include "chrome/browser/sync/glue/extension_setting_data_type_controller.h"
44 #endif
46 #if defined(ENABLE_SUPERVISED_USERS)
47 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service.h"
48 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service_factory.h"
49 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h"
50 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service_factory.h"
51 #include "chrome/browser/supervised_user/supervised_user_service.h"
52 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
53 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
54 #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
55 #include "chrome/browser/supervised_user/supervised_user_sync_data_type_controller.h"
56 #include "chrome/browser/supervised_user/supervised_user_whitelist_service.h"
57 #endif
59 #if defined(ENABLE_SPELLCHECK)
60 #include "chrome/browser/spellchecker/spellcheck_factory.h"
61 #include "chrome/browser/spellchecker/spellcheck_service.h"
62 #endif
64 #if defined(OS_CHROMEOS)
65 #include "components/wifi_sync/wifi_credential_syncable_service.h"
66 #include "components/wifi_sync/wifi_credential_syncable_service_factory.h"
67 #endif
69 namespace browser_sync {
71 ChromeSyncClient::ChromeSyncClient(
72 Profile* profile,
73 sync_driver::SyncApiComponentFactory* component_factory)
74 : profile_(profile),
75 component_factory_(component_factory) {
77 ChromeSyncClient::~ChromeSyncClient() {
80 void ChromeSyncClient::Initialize(sync_driver::SyncService* sync_service) {
81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
82 sync_service_ = sync_service;
83 web_data_service_ = GetWebDataService();
84 password_store_ = GetPasswordStore();
87 sync_driver::SyncService* ChromeSyncClient::GetSyncService() {
88 // TODO(zea): bring back this DCHECK after Typed URLs are converted to
89 // SyncableService.
90 // DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
91 return sync_service_;
94 PrefService* ChromeSyncClient::GetPrefService() {
95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
96 return profile_->GetPrefs();
99 bookmarks::BookmarkModel* ChromeSyncClient::GetBookmarkModel() {
100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
101 return BookmarkModelFactory::GetForProfile(profile_);
104 history::HistoryService* ChromeSyncClient::GetHistoryService() {
105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
106 return HistoryServiceFactory::GetForProfile(
107 profile_, ServiceAccessType::EXPLICIT_ACCESS);
110 autofill::PersonalDataManager* ChromeSyncClient::GetPersonalDataManager() {
111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
112 return autofill::PersonalDataManagerFactory::GetForProfile(profile_);
115 scoped_refptr<password_manager::PasswordStore>
116 ChromeSyncClient::GetPasswordStore() {
117 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
118 return PasswordStoreFactory::GetForProfile(
119 profile_, ServiceAccessType::EXPLICIT_ACCESS);
122 scoped_refptr<autofill::AutofillWebDataService>
123 ChromeSyncClient::GetWebDataService() {
124 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
125 return WebDataServiceFactory::GetAutofillWebDataForProfile(
126 profile_, ServiceAccessType::EXPLICIT_ACCESS);
129 base::WeakPtr<syncer::SyncableService>
130 ChromeSyncClient::GetSyncableServiceForType(syncer::ModelType type) {
131 if (!profile_) { // For tests.
132 return base::WeakPtr<syncer::SyncableService>();
134 switch (type) {
135 case syncer::DEVICE_INFO:
136 return ProfileSyncServiceFactory::GetForProfile(profile_)
137 ->GetDeviceInfoSyncableService()
138 ->AsWeakPtr();
139 case syncer::PREFERENCES:
140 return PrefServiceSyncableFromProfile(profile_)
141 ->GetSyncableService(syncer::PREFERENCES)
142 ->AsWeakPtr();
143 case syncer::PRIORITY_PREFERENCES:
144 return PrefServiceSyncableFromProfile(profile_)
145 ->GetSyncableService(syncer::PRIORITY_PREFERENCES)
146 ->AsWeakPtr();
147 case syncer::AUTOFILL:
148 case syncer::AUTOFILL_PROFILE:
149 case syncer::AUTOFILL_WALLET_DATA:
150 case syncer::AUTOFILL_WALLET_METADATA: {
151 if (!web_data_service_)
152 return base::WeakPtr<syncer::SyncableService>();
153 if (type == syncer::AUTOFILL) {
154 return autofill::AutocompleteSyncableService::FromWebDataService(
155 web_data_service_.get())->AsWeakPtr();
156 } else if (type == syncer::AUTOFILL_PROFILE) {
157 return autofill::AutofillProfileSyncableService::FromWebDataService(
158 web_data_service_.get())->AsWeakPtr();
159 } else if (type == syncer::AUTOFILL_WALLET_METADATA) {
160 return autofill::AutofillWalletMetadataSyncableService::
161 FromWebDataService(web_data_service_.get())->AsWeakPtr();
163 return autofill::AutofillWalletSyncableService::FromWebDataService(
164 web_data_service_.get())->AsWeakPtr();
166 case syncer::SEARCH_ENGINES:
167 return TemplateURLServiceFactory::GetForProfile(profile_)->AsWeakPtr();
168 #if defined(ENABLE_EXTENSIONS)
169 case syncer::APPS:
170 case syncer::EXTENSIONS:
171 return ExtensionSyncService::Get(profile_)->AsWeakPtr();
172 case syncer::APP_SETTINGS:
173 case syncer::EXTENSION_SETTINGS:
174 return extensions::settings_sync_util::GetSyncableService(profile_, type)
175 ->AsWeakPtr();
176 #endif
177 #if defined(ENABLE_APP_LIST)
178 case syncer::APP_LIST:
179 return app_list::AppListSyncableServiceFactory::GetForProfile(profile_)->
180 AsWeakPtr();
181 #endif
182 #if defined(ENABLE_THEMES)
183 case syncer::THEMES:
184 return ThemeServiceFactory::GetForProfile(profile_)->
185 GetThemeSyncableService()->AsWeakPtr();
186 #endif
187 case syncer::HISTORY_DELETE_DIRECTIVES: {
188 history::HistoryService* history = GetHistoryService();
189 return history ? history->AsWeakPtr()
190 : base::WeakPtr<history::HistoryService>();
192 #if defined(ENABLE_SPELLCHECK)
193 case syncer::DICTIONARY:
194 return SpellcheckServiceFactory::GetForContext(profile_)->
195 GetCustomDictionary()->AsWeakPtr();
196 #endif
197 case syncer::FAVICON_IMAGES:
198 case syncer::FAVICON_TRACKING: {
199 browser_sync::FaviconCache* favicons =
200 ProfileSyncServiceFactory::GetForProfile(profile_)->
201 GetFaviconCache();
202 return favicons ? favicons->AsWeakPtr()
203 : base::WeakPtr<syncer::SyncableService>();
205 #if defined(ENABLE_SUPERVISED_USERS)
206 case syncer::SUPERVISED_USER_SETTINGS:
207 return SupervisedUserSettingsServiceFactory::GetForProfile(profile_)->
208 AsWeakPtr();
209 #if !defined(OS_ANDROID) && !defined(OS_IOS)
210 case syncer::SUPERVISED_USERS:
211 return SupervisedUserSyncServiceFactory::GetForProfile(profile_)->
212 AsWeakPtr();
213 case syncer::SUPERVISED_USER_SHARED_SETTINGS:
214 return SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
215 profile_)->AsWeakPtr();
216 #endif
217 case syncer::SUPERVISED_USER_WHITELISTS:
218 return SupervisedUserServiceFactory::GetForProfile(profile_)
219 ->GetWhitelistService()
220 ->AsWeakPtr();
221 #endif
222 case syncer::ARTICLES: {
223 dom_distiller::DomDistillerService* service =
224 dom_distiller::DomDistillerServiceFactory::GetForBrowserContext(
225 profile_);
226 if (service)
227 return service->GetSyncableService()->AsWeakPtr();
228 return base::WeakPtr<syncer::SyncableService>();
230 case syncer::SESSIONS: {
231 return ProfileSyncServiceFactory::GetForProfile(profile_)->
232 GetSessionsSyncableService()->AsWeakPtr();
234 case syncer::PASSWORDS: {
235 return password_store_.get()
236 ? password_store_->GetPasswordSyncableService()
237 : base::WeakPtr<syncer::SyncableService>();
239 #if defined(OS_CHROMEOS)
240 case syncer::WIFI_CREDENTIALS:
241 return wifi_sync::WifiCredentialSyncableServiceFactory::
242 GetForBrowserContext(profile_)->AsWeakPtr();
243 #endif
244 default:
245 // The following datatypes still need to be transitioned to the
246 // syncer::SyncableService API:
247 // Bookmarks
248 // Typed URLs
249 NOTREACHED();
250 return base::WeakPtr<syncer::SyncableService>();
254 sync_driver::SyncApiComponentFactory*
255 ChromeSyncClient::GetSyncApiComponentFactory() {
256 return component_factory_;
259 } // namespace browser_sync