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 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(scoped_ptr
<WebDatabaseTable
>(
79 new autofill::AutofillTable(application_locale
)));
80 web_database_
->AddTable(scoped_ptr
<WebDatabaseTable
>(new KeywordTable()));
81 // TODO(mdm): We only really need the LoginsTable on Windows for IE7 password
82 // access, but for now, we still create it on all platforms since it deletes
83 // the old logins table. We can remove this after a while, e.g. in M22 or so.
84 web_database_
->AddTable(scoped_ptr
<WebDatabaseTable
>(new LoginsTable()));
85 web_database_
->AddTable(
86 scoped_ptr
<WebDatabaseTable
>(new TokenServiceTable()));
87 web_database_
->LoadDatabase();
89 autofill_web_data_
= new autofill::AutofillWebDataService(
90 web_database_
, ui_thread
, db_thread
,
91 base::Bind(show_error_callback
, ERROR_LOADING_AUTOFILL
));
92 autofill_web_data_
->Init();
94 keyword_web_data_
= new KeywordWebDataService(
95 web_database_
, ui_thread
,
96 base::Bind(show_error_callback
, ERROR_LOADING_KEYWORD
));
97 keyword_web_data_
->Init();
99 token_web_data_
= new TokenWebData(
100 web_database_
, ui_thread
, db_thread
,
101 base::Bind(show_error_callback
, ERROR_LOADING_TOKEN
));
102 token_web_data_
->Init();
105 password_web_data_
= new PasswordWebDataService(
106 web_database_
, ui_thread
,
107 base::Bind(show_error_callback
, ERROR_LOADING_PASSWORD
));
108 password_web_data_
->Init();
111 autofill_web_data_
->GetAutofillBackend(
112 base::Bind(&InitSyncableServicesOnDBThread
, db_thread
, flare
,
113 autofill_web_data_
, context_path
, application_locale
));
116 WebDataServiceWrapper::~WebDataServiceWrapper() {
119 void WebDataServiceWrapper::Shutdown() {
120 autofill_web_data_
->ShutdownOnUIThread();
121 keyword_web_data_
->ShutdownOnUIThread();
122 token_web_data_
->ShutdownOnUIThread();
125 password_web_data_
->ShutdownOnUIThread();
128 web_database_
->ShutdownDatabase();
131 scoped_refptr
<autofill::AutofillWebDataService
>
132 WebDataServiceWrapper::GetAutofillWebData() {
133 return autofill_web_data_
.get();
136 scoped_refptr
<KeywordWebDataService
>
137 WebDataServiceWrapper::GetKeywordWebData() {
138 return keyword_web_data_
.get();
141 scoped_refptr
<TokenWebData
> WebDataServiceWrapper::GetTokenWebData() {
142 return token_web_data_
.get();
146 scoped_refptr
<PasswordWebDataService
>
147 WebDataServiceWrapper::GetPasswordWebData() {
148 return password_web_data_
.get();