1 // Copyright 2014 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 "components/webdata_services/web_data_service_wrapper.h"
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/single_thread_task_runner.h"
11 #include "components/autofill/core/browser/webdata/autocomplete_syncable_service.h"
12 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
13 #include "components/autofill/core/browser/webdata/autofill_table.h"
14 #include "components/autofill/core/browser/webdata/autofill_wallet_metadata_syncable_service.h"
15 #include "components/autofill/core/browser/webdata/autofill_wallet_syncable_service.h"
16 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
17 #include "components/password_manager/core/browser/webdata/logins_table.h"
18 #include "components/search_engines/keyword_table.h"
19 #include "components/search_engines/keyword_web_data_service.h"
20 #include "components/signin/core/browser/webdata/token_service_table.h"
21 #include "components/signin/core/browser/webdata/token_web_data.h"
22 #include "components/webdata/common/web_database_service.h"
23 #include "components/webdata/common/webdata_constants.h"
26 #include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
31 void InitSyncableServicesOnDBThread(
32 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread
,
33 const syncer::SyncableService::StartSyncFlare
& sync_flare
,
34 const scoped_refptr
<autofill::AutofillWebDataService
>& autofill_web_data
,
35 const base::FilePath
& context_path
,
36 const std::string
& app_locale
,
37 autofill::AutofillWebDataBackend
* autofill_backend
) {
38 DCHECK(db_thread
->BelongsToCurrentThread());
40 // Currently only Autocomplete and Autofill profiles use the new Sync API, but
41 // all the database data should migrate to this API over time.
42 autofill::AutocompleteSyncableService::CreateForWebDataServiceAndBackend(
43 autofill_web_data
.get(), autofill_backend
);
44 autofill::AutocompleteSyncableService::FromWebDataService(
45 autofill_web_data
.get())->InjectStartSyncFlare(sync_flare
);
47 autofill::AutofillProfileSyncableService::CreateForWebDataServiceAndBackend(
48 autofill_web_data
.get(), autofill_backend
, app_locale
);
49 autofill::AutofillWalletSyncableService::CreateForWebDataServiceAndBackend(
50 autofill_web_data
.get(), autofill_backend
, app_locale
);
51 autofill::AutofillWalletMetadataSyncableService::
52 CreateForWebDataServiceAndBackend(autofill_web_data
.get(),
53 autofill_backend
, app_locale
);
55 autofill::AutofillProfileSyncableService::FromWebDataService(
56 autofill_web_data
.get())->InjectStartSyncFlare(sync_flare
);
57 autofill::AutofillWalletSyncableService::FromWebDataService(
58 autofill_web_data
.get())->InjectStartSyncFlare(sync_flare
);
63 WebDataServiceWrapper::WebDataServiceWrapper() {
66 WebDataServiceWrapper::WebDataServiceWrapper(
67 const base::FilePath
& context_path
,
68 const std::string
& application_locale
,
69 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_thread
,
70 const scoped_refptr
<base::SingleThreadTaskRunner
>& db_thread
,
71 const syncer::SyncableService::StartSyncFlare
& flare
,
72 const ShowErrorCallback
& show_error_callback
) {
73 base::FilePath path
= context_path
.Append(kWebDataFilename
);
74 web_database_
= new WebDatabaseService(path
, ui_thread
, db_thread
);
76 // All tables objects that participate in managing the database must
78 web_database_
->AddTable(make_scoped_ptr(new autofill::AutofillTable
));
79 web_database_
->AddTable(make_scoped_ptr(new KeywordTable
));
80 // TODO(mdm): We only really need the LoginsTable on Windows for IE7 password
81 // access, but for now, we still create it on all platforms since it deletes
82 // the old logins table. We can remove this after a while, e.g. in M22 or so.
83 web_database_
->AddTable(make_scoped_ptr(new LoginsTable
));
84 web_database_
->AddTable(make_scoped_ptr(new TokenServiceTable
));
85 web_database_
->LoadDatabase();
87 autofill_web_data_
= new autofill::AutofillWebDataService(
88 web_database_
, ui_thread
, db_thread
,
89 base::Bind(show_error_callback
, ERROR_LOADING_AUTOFILL
));
90 autofill_web_data_
->Init();
92 keyword_web_data_
= new KeywordWebDataService(
93 web_database_
, ui_thread
,
94 base::Bind(show_error_callback
, ERROR_LOADING_KEYWORD
));
95 keyword_web_data_
->Init();
97 token_web_data_
= new TokenWebData(
98 web_database_
, ui_thread
, db_thread
,
99 base::Bind(show_error_callback
, ERROR_LOADING_TOKEN
));
100 token_web_data_
->Init();
103 password_web_data_
= new PasswordWebDataService(
104 web_database_
, ui_thread
,
105 base::Bind(show_error_callback
, ERROR_LOADING_PASSWORD
));
106 password_web_data_
->Init();
109 autofill_web_data_
->GetAutofillBackend(
110 base::Bind(&InitSyncableServicesOnDBThread
, db_thread
, flare
,
111 autofill_web_data_
, context_path
, application_locale
));
114 WebDataServiceWrapper::~WebDataServiceWrapper() {
117 void WebDataServiceWrapper::Shutdown() {
118 autofill_web_data_
->ShutdownOnUIThread();
119 keyword_web_data_
->ShutdownOnUIThread();
120 token_web_data_
->ShutdownOnUIThread();
123 password_web_data_
->ShutdownOnUIThread();
126 web_database_
->ShutdownDatabase();
129 scoped_refptr
<autofill::AutofillWebDataService
>
130 WebDataServiceWrapper::GetAutofillWebData() {
131 return autofill_web_data_
.get();
134 scoped_refptr
<KeywordWebDataService
>
135 WebDataServiceWrapper::GetKeywordWebData() {
136 return keyword_web_data_
.get();
139 scoped_refptr
<TokenWebData
> WebDataServiceWrapper::GetTokenWebData() {
140 return token_web_data_
.get();
144 scoped_refptr
<PasswordWebDataService
>
145 WebDataServiceWrapper::GetPasswordWebData() {
146 return password_web_data_
.get();