OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/webdata/web_data_service_factory.h" | 5 #include "chrome/browser/webdata/web_data_service_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "chrome/browser/profiles/profile_dependency_manager.h" | 9 #include "chrome/browser/profiles/profile_dependency_manager.h" |
10 #include "chrome/browser/ui/profile_error_dialog.h" | 10 #include "chrome/browser/ui/profile_error_dialog.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Callback to show error dialog on profile load error. | 29 // Callback to show error dialog on profile load error. |
30 void ProfileErrorCallback(sql::InitStatus status) { | 30 void ProfileErrorCallback(sql::InitStatus status) { |
31 ShowProfileErrorDialog( | 31 ShowProfileErrorDialog( |
32 (status == sql::INIT_FAILURE) ? | 32 (status == sql::INIT_FAILURE) ? |
33 IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR); | 33 IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR); |
34 } | 34 } |
35 | 35 |
36 void InitSyncableServicesOnDBThread(scoped_refptr<WebDataService> web_data) { | 36 void InitSyncableServicesOnDBThread( |
| 37 scoped_refptr<AutofillWebDataService> autofill_web_data) { |
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
38 | 39 |
39 // Currently only Autocomplete and Autofill profiles use the new Sync API, but | 40 // Currently only Autocomplete and Autofill profiles use the new Sync API, but |
40 // all the database data should migrate to this API over time. | 41 // all the database data should migrate to this API over time. |
41 AutocompleteSyncableService::CreateForWebDataService(web_data); | 42 AutocompleteSyncableService::CreateForWebDataService(autofill_web_data); |
42 AutofillProfileSyncableService::CreateForWebDataService(web_data); | 43 AutofillProfileSyncableService::CreateForWebDataService(autofill_web_data); |
43 } | 44 } |
44 | 45 |
45 } // namespace | 46 } // namespace |
46 | 47 |
47 WebDataServiceWrapper::WebDataServiceWrapper() {} | 48 WebDataServiceWrapper::WebDataServiceWrapper() {} |
48 | 49 |
49 WebDataServiceWrapper::WebDataServiceWrapper(Profile* profile) { | 50 WebDataServiceWrapper::WebDataServiceWrapper(Profile* profile) { |
50 base::FilePath path = profile->GetPath(); | 51 base::FilePath path = profile->GetPath(); |
51 path = path.Append(chrome::kWebDataFilename); | 52 path = path.Append(chrome::kWebDataFilename); |
52 web_data_service_ = | |
53 new WebDataService(path, base::Bind(&ProfileErrorCallback)); | |
54 | 53 |
55 // All table objects that participate in managing the database must be added | 54 // TODO (caitkp): Rework the callbacks here. They're ugly. |
56 // here. | 55 |
57 web_data_service_->AddTable( | 56 web_database_ = new WebDatabaseService(path); |
| 57 |
| 58 // All tables objects that participate in managing the database must |
| 59 // be added here. |
| 60 web_database_->AddTable( |
58 scoped_ptr<WebDatabaseTable>(new AutofillTable())); | 61 scoped_ptr<WebDatabaseTable>(new AutofillTable())); |
59 web_data_service_->AddTable( | 62 web_database_->AddTable( |
60 scoped_ptr<WebDatabaseTable>(new KeywordTable())); | 63 scoped_ptr<WebDatabaseTable>(new KeywordTable())); |
61 // TODO(mdm): We only really need the LoginsTable on Windows for IE7 password | 64 // TODO(mdm): We only really need the LoginsTable on Windows for IE7 password |
62 // access, but for now, we still create it on all platforms since it deletes | 65 // access, but for now, we still create it on all platforms since it deletes |
63 // the old logins table. We can remove this after a while, e.g. in M22 or so. | 66 // the old logins table. We can remove this after a while, e.g. in M22 or so. |
64 web_data_service_->AddTable( | 67 web_database_->AddTable( |
65 scoped_ptr<WebDatabaseTable>(new LoginsTable())); | 68 scoped_ptr<WebDatabaseTable>(new LoginsTable())); |
66 web_data_service_->AddTable( | 69 web_database_->AddTable( |
67 scoped_ptr<WebDatabaseTable>(new TokenServiceTable())); | 70 scoped_ptr<WebDatabaseTable>(new TokenServiceTable())); |
68 web_data_service_->AddTable( | 71 web_database_->AddTable( |
69 scoped_ptr<WebDatabaseTable>(new WebAppsTable())); | 72 scoped_ptr<WebDatabaseTable>(new WebAppsTable())); |
70 // TODO(thakis): Add a migration to delete the SQL table used by | 73 // TODO(thakis): Add a migration to delete the SQL table used by |
71 // WebIntentsTable, then remove this. | 74 // WebIntentsTable, then remove this. |
72 web_data_service_->AddTable( | 75 web_database_->AddTable( |
73 scoped_ptr<WebDatabaseTable>(new WebIntentsTable())); | 76 scoped_ptr<WebDatabaseTable>(new WebIntentsTable())); |
74 | 77 |
| 78 web_database_->LoadDatabase(WebDatabaseService::InitCallback()); |
| 79 |
| 80 web_data_service_ = new WebDataService( |
| 81 web_database_, base::Bind(&ProfileErrorCallback)); |
75 web_data_service_->Init(); | 82 web_data_service_->Init(); |
76 | 83 |
| 84 autofill_web_data_ = new AutofillWebDataServiceImpl( |
| 85 web_database_, base::Bind(&ProfileErrorCallback)); |
| 86 autofill_web_data_->Init(); |
| 87 |
77 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 88 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
78 base::Bind(&InitSyncableServicesOnDBThread, | 89 base::Bind(&InitSyncableServicesOnDBThread, |
79 web_data_service_)); | 90 autofill_web_data_)); |
80 } | 91 } |
81 | 92 |
82 WebDataServiceWrapper::~WebDataServiceWrapper() { | 93 WebDataServiceWrapper::~WebDataServiceWrapper() { |
83 } | 94 } |
84 | 95 |
85 void WebDataServiceWrapper::Shutdown() { | 96 void WebDataServiceWrapper::Shutdown() { |
86 web_data_service_->ShutdownOnUIThread(); | 97 web_data_service_->ShutdownOnUIThread(); |
| 98 autofill_web_data_->ShutdownOnUIThread(); |
| 99 web_database_->ShutdownDatabase(); |
87 web_data_service_ = NULL; | 100 web_data_service_ = NULL; |
88 } | 101 } |
89 | 102 |
90 scoped_refptr<WebDataService> WebDataServiceWrapper::GetWebData() { | 103 scoped_refptr<WebDataService> WebDataServiceWrapper::GetWebData() { |
91 return web_data_service_.get(); | 104 return web_data_service_.get(); |
92 } | 105 } |
93 | 106 |
| 107 scoped_refptr<AutofillWebDataService> |
| 108 WebDataServiceWrapper::GetAutofillWebData() { |
| 109 return autofill_web_data_.get(); |
| 110 } |
| 111 |
94 // static | 112 // static |
95 scoped_refptr<AutofillWebDataService> | 113 scoped_refptr<AutofillWebDataService> |
96 AutofillWebDataService::FromBrowserContext(content::BrowserContext* context) { | 114 AutofillWebDataService::FromBrowserContext(content::BrowserContext* context) { |
97 // For this service, the implicit/explicit distinction doesn't | 115 // For this service, the implicit/explicit distinction doesn't |
98 // really matter; it's just used for a DCHECK. So we currently | 116 // really matter; it's just used for a DCHECK. So we currently |
99 // cheat and always say EXPLICIT_ACCESS. | 117 // cheat and always say EXPLICIT_ACCESS. |
100 scoped_refptr<WebDataService> service = WebDataServiceFactory::GetForProfile( | 118 WebDataServiceWrapper* wrapper = |
101 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); | 119 WebDataServiceFactory::GetForProfile( |
102 | 120 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); |
103 if (service.get()) { | 121 if (wrapper) |
104 return scoped_refptr<AutofillWebDataService>( | 122 return wrapper->GetAutofillWebData(); |
105 new AutofillWebDataServiceImpl(service)); | 123 // |wrapper| can be NULL in Incognito mode. |
106 } else { | 124 return scoped_refptr<AutofillWebDataService>(NULL); |
107 return scoped_refptr<AutofillWebDataService>(NULL); | |
108 } | |
109 } | 125 } |
110 | 126 |
111 // static | 127 // static |
112 scoped_refptr<WebDataService> WebDataService::FromBrowserContext( | 128 scoped_refptr<WebDataService> WebDataService::FromBrowserContext( |
113 content::BrowserContext* context) { | 129 content::BrowserContext* context) { |
114 // For this service, the implicit/explicit distinction doesn't | 130 // For this service, the implicit/explicit distinction doesn't |
115 // really matter; it's just used for a DCHECK. So we currently | 131 // really matter; it's just used for a DCHECK. So we currently |
116 // cheat and always say EXPLICIT_ACCESS. | 132 // cheat and always say EXPLICIT_ACCESS. |
117 return WebDataServiceFactory::GetForProfile( | 133 WebDataServiceWrapper* wrapper = |
118 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); | 134 WebDataServiceFactory::GetForProfile( |
| 135 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); |
| 136 if (wrapper) |
| 137 return wrapper->GetWebData(); |
| 138 // |wrapper| can be NULL in Incognito mode. |
| 139 return scoped_refptr<WebDataService>(NULL); |
119 } | 140 } |
120 | 141 |
| 142 |
121 WebDataServiceFactory::WebDataServiceFactory() | 143 WebDataServiceFactory::WebDataServiceFactory() |
122 : ProfileKeyedServiceFactory( | 144 : ProfileKeyedServiceFactory( |
123 "WebDataService", | 145 "WebDataService", |
124 ProfileDependencyManager::GetInstance()) { | 146 ProfileDependencyManager::GetInstance()) { |
125 // WebDataServiceFactory has no dependecies. | 147 // WebDataServiceFactory has no dependecies. |
126 } | 148 } |
127 | 149 |
128 WebDataServiceFactory::~WebDataServiceFactory() {} | 150 WebDataServiceFactory::~WebDataServiceFactory() {} |
129 | 151 |
130 // static | 152 // static |
131 scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfile( | 153 WebDataServiceWrapper* WebDataServiceFactory::GetForProfile( |
132 Profile* profile, Profile::ServiceAccessType access_type) { | 154 Profile* profile, Profile::ServiceAccessType access_type) { |
133 // If |access_type| starts being used for anything other than this | 155 // If |access_type| starts being used for anything other than this |
134 // DCHECK, we need to start taking it as a parameter to | 156 // DCHECK, we need to start taking it as a parameter to |
135 // AutofillWebDataServiceImpl::FromBrowserContext (see above). | 157 // AutofillWebDataServiceImpl::FromBrowserContext (see above). |
136 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); | 158 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); |
137 WebDataServiceWrapper* wrapper = | 159 return static_cast<WebDataServiceWrapper*>( |
138 static_cast<WebDataServiceWrapper*>( | |
139 GetInstance()->GetServiceForProfile(profile, true)); | 160 GetInstance()->GetServiceForProfile(profile, true)); |
140 if (wrapper) | |
141 return wrapper->GetWebData(); | |
142 // |wrapper| can be NULL in Incognito mode. | |
143 return NULL; | |
144 } | 161 } |
145 | 162 |
146 // static | 163 // static |
147 scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfileIfExists( | 164 WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists( |
148 Profile* profile, Profile::ServiceAccessType access_type) { | 165 Profile* profile, Profile::ServiceAccessType access_type) { |
149 // If |access_type| starts being used for anything other than this | 166 // If |access_type| starts being used for anything other than this |
150 // DCHECK, we need to start taking it as a parameter to | 167 // DCHECK, we need to start taking it as a parameter to |
151 // AutofillWebDataServiceImpl::FromBrowserContext (see above). | 168 // AutofillWebDataServiceImpl::FromBrowserContext (see above). |
152 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); | 169 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); |
153 WebDataServiceWrapper* wrapper = | 170 return static_cast<WebDataServiceWrapper*>( |
154 static_cast<WebDataServiceWrapper*>( | 171 GetInstance()->GetServiceForProfile(profile, false)); |
155 GetInstance()->GetServiceForProfile(profile, true)); | |
156 if (wrapper) | |
157 return wrapper->GetWebData(); | |
158 // |wrapper| can be NULL in Incognito mode. | |
159 return NULL; | |
160 } | 172 } |
161 | 173 |
162 // static | 174 // static |
163 WebDataServiceFactory* WebDataServiceFactory::GetInstance() { | 175 WebDataServiceFactory* WebDataServiceFactory::GetInstance() { |
164 return Singleton<WebDataServiceFactory>::get(); | 176 return Singleton<WebDataServiceFactory>::get(); |
165 } | 177 } |
166 | 178 |
167 bool WebDataServiceFactory::ServiceRedirectedInIncognito() const { | 179 bool WebDataServiceFactory::ServiceRedirectedInIncognito() const { |
168 return true; | 180 return true; |
169 } | 181 } |
170 | 182 |
171 ProfileKeyedService* | 183 ProfileKeyedService* |
172 WebDataServiceFactory::BuildServiceInstanceFor(Profile* profile) const { | 184 WebDataServiceFactory::BuildServiceInstanceFor(Profile* profile) const { |
173 return new WebDataServiceWrapper(profile); | 185 return new WebDataServiceWrapper(profile); |
174 } | 186 } |
175 | 187 |
176 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const { | 188 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const { |
177 return true; | 189 return true; |
178 } | 190 } |
OLD | NEW |