Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: chrome/browser/webdata/web_data_service.cc

Issue 12871006: Second try at splitting WebDataService (minus ownership changes) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Missed a couple Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.h" 5 #include "chrome/browser/webdata/web_data_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_manager.h" 13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/search_engines/template_url.h" 14 #include "chrome/browser/search_engines/template_url.h"
15 #include "chrome/browser/ui/profile_error_dialog.h" 15 #include "chrome/browser/ui/profile_error_dialog.h"
16 #include "chrome/browser/webdata/autocomplete_syncable_service.h" 16 #include "chrome/browser/webdata/autocomplete_syncable_service.h"
17 #include "chrome/browser/webdata/autofill_change.h" 17 #include "chrome/browser/webdata/autofill_change.h"
18 #include "chrome/browser/webdata/autofill_entry.h" 18 #include "chrome/browser/webdata/autofill_entry.h"
19 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" 19 #include "chrome/browser/webdata/autofill_profile_syncable_service.h"
20 #include "chrome/browser/webdata/autofill_table.h" 20 #include "chrome/browser/webdata/autofill_table.h"
21 #include "chrome/browser/webdata/keyword_table.h" 21 #include "chrome/browser/webdata/keyword_table.h"
22 #include "chrome/browser/webdata/logins_table.h" 22 #include "chrome/browser/webdata/logins_table.h"
23 #include "chrome/browser/webdata/token_service_table.h" 23 #include "chrome/browser/webdata/token_service_table.h"
24 #include "chrome/browser/webdata/web_apps_table.h" 24 #include "chrome/browser/webdata/web_apps_table.h"
25 #include "chrome/browser/webdata/web_database.h" 25 #include "chrome/browser/webdata/web_database_service.h"
26 #include "chrome/browser/webdata/web_intents_table.h" 26 #include "chrome/browser/webdata/web_intents_table.h"
27 #include "chrome/common/chrome_constants.h" 27 #include "chrome/common/chrome_constants.h"
28 #include "chrome/common/chrome_notification_types.h" 28 #include "chrome/common/chrome_notification_types.h"
29 #include "components/autofill/browser/autofill_country.h" 29 #include "components/autofill/browser/autofill_country.h"
30 #include "components/autofill/browser/autofill_profile.h" 30 #include "components/autofill/browser/autofill_profile.h"
31 #include "components/autofill/browser/credit_card.h" 31 #include "components/autofill/browser/credit_card.h"
32 #include "components/autofill/common/form_field_data.h" 32 #include "components/autofill/common/form_field_data.h"
33 #ifdef DEBUG 33 #ifdef DEBUG
34 #include "content/public/browser/browser_thread.h" 34 #include "content/public/browser/browser_thread.h"
35 #endif 35 #endif
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 WDAppImagesResult::~WDAppImagesResult() {} 71 WDAppImagesResult::~WDAppImagesResult() {}
72 72
73 WDKeywordsResult::WDKeywordsResult() 73 WDKeywordsResult::WDKeywordsResult()
74 : default_search_provider_id(0), 74 : default_search_provider_id(0),
75 builtin_keyword_version(0) { 75 builtin_keyword_version(0) {
76 } 76 }
77 77
78 WDKeywordsResult::~WDKeywordsResult() {} 78 WDKeywordsResult::~WDKeywordsResult() {}
79 79
80 WebDataService::WebDataService() 80 WebDataService::WebDataService()
81 : is_running_(false), 81 : db_loaded_(false),
82 db_(NULL),
83 request_manager_(new WebDataRequestManager()),
84 app_locale_(AutofillCountry::ApplicationLocale()),
85 autocomplete_syncable_service_(NULL), 82 autocomplete_syncable_service_(NULL),
86 autofill_profile_syncable_service_(NULL), 83 autofill_profile_syncable_service_(NULL) {
87 failed_init_(false),
88 should_commit_(false),
89 main_loop_(MessageLoop::current()) {
90 // WebDataService requires DB thread if instantiated. 84 // WebDataService requires DB thread if instantiated.
91 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL) 85 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL)
92 // if you do not want to instantiate WebDataService in your test. 86 // if you do not want to instantiate WebDataService in your test.
93 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); 87 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB));
94 } 88 }
95 89
96 // static 90 // static
97 void WebDataService::NotifyOfMultipleAutofillChanges( 91 void WebDataService::NotifyOfMultipleAutofillChanges(
98 WebDataService* web_data_service) { 92 WebDataService* web_data_service) {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
100 94
101 if (!web_data_service) 95 if (!web_data_service)
102 return; 96 return;
103 97
104 BrowserThread::PostTask( 98 BrowserThread::PostTask(
105 BrowserThread::UI, FROM_HERE, 99 BrowserThread::UI, FROM_HERE,
106 Bind(&NotifyOfMultipleAutofillChangesTask, 100 Bind(&NotifyOfMultipleAutofillChangesTask,
107 make_scoped_refptr(web_data_service))); 101 make_scoped_refptr(web_data_service)));
108 } 102 }
109 103
110 void WebDataService::ShutdownOnUIThread() { 104 void WebDataService::ShutdownOnUIThread() {
111 ScheduleTask(FROM_HERE, 105 db_loaded_ = false;
112 Bind(&WebDataService::ShutdownSyncableServices, this)); 106 ShutdownDatabase();
113 UnloadDatabase(); 107 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
108 Bind(&WebDataService::ShutdownSyncableServices, this));
114 } 109 }
115 110
116 bool WebDataService::Init(const base::FilePath& profile_path) { 111 void WebDataService::Init(const base::FilePath& path) {
117 base::FilePath path = profile_path; 112 wdbs_.reset(new WebDatabaseService(path));
118 path = path.Append(chrome::kWebDataFilename); 113 wdbs_->LoadDatabase(
dhollowa 2013/03/15 23:50:31 nit: Fits on one line.
Cait (Slow) 2013/03/16 21:34:22 Done.
119 return InitWithPath(path); 114 Bind(&WebDataService::DatabaseInitOnDB, this));
120 }
121 115
122 bool WebDataService::IsRunning() const { 116 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
123 return is_running_; 117 Bind(&WebDataService::InitializeSyncableServices, this));
124 } 118 }
125 119
126 void WebDataService::UnloadDatabase() { 120 void WebDataService::UnloadDatabase() {
127 ScheduleTask(FROM_HERE, Bind(&WebDataService::ShutdownDatabase, this)); 121 if (wdbs_.get())
dhollowa 2013/03/15 23:50:31 if (wdbs_)
Cait (Slow) 2013/03/16 21:34:22 Done.
122 wdbs_->UnloadDatabase();
123 }
124
125 void WebDataService::ShutdownDatabase() {
126 if (wdbs_.get())
dhollowa 2013/03/15 23:50:31 if (wdbs_)
Cait (Slow) 2013/03/16 21:34:22 Done.
127 wdbs_->ShutdownDatabase();
128 } 128 }
129 129
130 void WebDataService::CancelRequest(Handle h) { 130 void WebDataService::CancelRequest(Handle h) {
131 request_manager_->CancelRequest(h); 131 if (wdbs_.get())
dhollowa 2013/03/15 23:50:31 if (wdbs_)
Cait (Slow) 2013/03/16 21:34:22 Done.
132 wdbs_->CancelRequest(h);
132 } 133 }
133 134
134 content::NotificationSource WebDataService::GetNotificationSource() { 135 content::NotificationSource WebDataService::GetNotificationSource() {
135 return content::Source<WebDataService>(this); 136 return content::Source<WebDataService>(this);
136 } 137 }
137 138
138 bool WebDataService::IsDatabaseLoaded() { 139 bool WebDataService::IsDatabaseLoaded() {
139 return db_ != NULL; 140 return db_loaded_;
140 } 141 }
141 142
142 WebDatabase* WebDataService::GetDatabase() { 143 WebDatabase* WebDataService::GetDatabase() {
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 144 if (!wdbs_.get())
dhollowa 2013/03/15 23:50:31 if (!wdbs_)
Cait (Slow) 2013/03/16 21:34:22 Done.
144 return db_; 145 return NULL;
146 return wdbs_->GetDatabaseOnDB();
145 } 147 }
146 148
147 ////////////////////////////////////////////////////////////////////////////// 149 //////////////////////////////////////////////////////////////////////////////
148 // 150 //
149 // Keywords. 151 // Keywords.
150 // 152 //
151 ////////////////////////////////////////////////////////////////////////////// 153 //////////////////////////////////////////////////////////////////////////////
152 154
153 void WebDataService::AddKeyword(const TemplateURLData& data) { 155 void WebDataService::AddKeyword(const TemplateURLData& data) {
154 ScheduleDBTask( 156 wdbs_->ScheduleDBTask(
155 FROM_HERE, Bind(&WebDataService::AddKeywordImpl, this, data)); 157 FROM_HERE, Bind(&WebDataService::AddKeywordImpl, this, data));
156 } 158 }
157 159
158 void WebDataService::RemoveKeyword(TemplateURLID id) { 160 void WebDataService::RemoveKeyword(TemplateURLID id) {
159 ScheduleDBTask( 161 wdbs_->ScheduleDBTask(
160 FROM_HERE, Bind(&WebDataService::RemoveKeywordImpl, this, id)); 162 FROM_HERE, Bind(&WebDataService::RemoveKeywordImpl, this, id));
161 } 163 }
162 164
163 void WebDataService::UpdateKeyword(const TemplateURLData& data) { 165 void WebDataService::UpdateKeyword(const TemplateURLData& data) {
164 ScheduleDBTask( 166 wdbs_->ScheduleDBTask(
165 FROM_HERE, Bind(&WebDataService::UpdateKeywordImpl, this, data)); 167 FROM_HERE, Bind(&WebDataService::UpdateKeywordImpl, this, data));
166 } 168 }
167 169
168 WebDataService::Handle WebDataService::GetKeywords( 170 WebDataService::Handle WebDataService::GetKeywords(
169 WebDataServiceConsumer* consumer) { 171 WebDataServiceConsumer* consumer) {
170 return ScheduleDBTaskWithResult(FROM_HERE, 172 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
171 Bind(&WebDataService::GetKeywordsImpl, this), consumer); 173 Bind(&WebDataService::GetKeywordsImpl, this), consumer);
172 } 174 }
173 175
174 void WebDataService::SetDefaultSearchProvider(const TemplateURL* url) { 176 void WebDataService::SetDefaultSearchProvider(const TemplateURL* url) {
175 ScheduleDBTask(FROM_HERE, 177 wdbs_->ScheduleDBTask(FROM_HERE,
176 Bind(&WebDataService::SetDefaultSearchProviderImpl, this, 178 Bind(&WebDataService::SetDefaultSearchProviderImpl, this,
177 url ? url->id() : 0)); 179 url ? url->id() : 0));
178 } 180 }
179 181
180 void WebDataService::SetBuiltinKeywordVersion(int version) { 182 void WebDataService::SetBuiltinKeywordVersion(int version) {
181 ScheduleDBTask(FROM_HERE, 183 wdbs_->ScheduleDBTask(FROM_HERE,
182 Bind(&WebDataService::SetBuiltinKeywordVersionImpl, this, version)); 184 Bind(&WebDataService::SetBuiltinKeywordVersionImpl, this, version));
183 } 185 }
184 186
185 ////////////////////////////////////////////////////////////////////////////// 187 //////////////////////////////////////////////////////////////////////////////
186 // 188 //
187 // Web Apps 189 // Web Apps
188 // 190 //
189 ////////////////////////////////////////////////////////////////////////////// 191 //////////////////////////////////////////////////////////////////////////////
190 192
191 void WebDataService::SetWebAppImage(const GURL& app_url, 193 void WebDataService::SetWebAppImage(const GURL& app_url,
192 const SkBitmap& image) { 194 const SkBitmap& image) {
193 ScheduleDBTask(FROM_HERE, 195 wdbs_->ScheduleDBTask(FROM_HERE,
194 Bind(&WebDataService::SetWebAppImageImpl, this, app_url, image)); 196 Bind(&WebDataService::SetWebAppImageImpl, this, app_url, image));
195 } 197 }
196 198
197 void WebDataService::SetWebAppHasAllImages(const GURL& app_url, 199 void WebDataService::SetWebAppHasAllImages(const GURL& app_url,
198 bool has_all_images) { 200 bool has_all_images) {
199 ScheduleDBTask(FROM_HERE, 201 wdbs_->ScheduleDBTask(FROM_HERE,
200 Bind(&WebDataService::SetWebAppHasAllImagesImpl, this, app_url, 202 Bind(&WebDataService::SetWebAppHasAllImagesImpl, this, app_url,
201 has_all_images)); 203 has_all_images));
202 } 204 }
203 205
204 void WebDataService::RemoveWebApp(const GURL& app_url) { 206 void WebDataService::RemoveWebApp(const GURL& app_url) {
205 ScheduleDBTask(FROM_HERE, 207 wdbs_->ScheduleDBTask(FROM_HERE,
206 Bind(&WebDataService::RemoveWebAppImpl, this, app_url)); 208 Bind(&WebDataService::RemoveWebAppImpl, this, app_url));
207 } 209 }
208 210
209 WebDataService::Handle WebDataService::GetWebAppImages( 211 WebDataService::Handle WebDataService::GetWebAppImages(
210 const GURL& app_url, 212 const GURL& app_url, WebDataServiceConsumer* consumer) {
211 WebDataServiceConsumer* consumer) { 213 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
212 return ScheduleDBTaskWithResult(FROM_HERE,
213 Bind(&WebDataService::GetWebAppImagesImpl, this, app_url), consumer); 214 Bind(&WebDataService::GetWebAppImagesImpl, this, app_url), consumer);
214 } 215 }
215 216
216 //////////////////////////////////////////////////////////////////////////////// 217 ////////////////////////////////////////////////////////////////////////////////
217 // 218 //
218 // Token Service 219 // Token Service
219 // 220 //
220 //////////////////////////////////////////////////////////////////////////////// 221 ////////////////////////////////////////////////////////////////////////////////
221 222
222 void WebDataService::SetTokenForService(const std::string& service, 223 void WebDataService::SetTokenForService(const std::string& service,
223 const std::string& token) { 224 const std::string& token) {
224 ScheduleDBTask(FROM_HERE, 225 wdbs_->ScheduleDBTask(FROM_HERE,
225 Bind(&WebDataService::SetTokenForServiceImpl, this, service, token)); 226 Bind(&WebDataService::SetTokenForServiceImpl, this, service, token));
226 } 227 }
227 228
228 void WebDataService::RemoveAllTokens() { 229 void WebDataService::RemoveAllTokens() {
229 ScheduleDBTask(FROM_HERE, Bind(&WebDataService::RemoveAllTokensImpl, this)); 230 wdbs_->ScheduleDBTask(FROM_HERE,
231 Bind(&WebDataService::RemoveAllTokensImpl, this));
230 } 232 }
231 233
232 // Null on failure. Success is WDResult<std::string> 234 // Null on failure. Success is WDResult<std::string>
233 WebDataService::Handle WebDataService::GetAllTokens( 235 WebDataService::Handle WebDataService::GetAllTokens(
234 WebDataServiceConsumer* consumer) { 236 WebDataServiceConsumer* consumer) {
235 return ScheduleDBTaskWithResult(FROM_HERE, 237 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
236 Bind(&WebDataService::GetAllTokensImpl, this), consumer); 238 Bind(&WebDataService::GetAllTokensImpl, this), consumer);
237 } 239 }
238 240
239 //////////////////////////////////////////////////////////////////////////////// 241 ////////////////////////////////////////////////////////////////////////////////
240 // 242 //
241 // Autofill. 243 // Autofill.
242 // 244 //
243 //////////////////////////////////////////////////////////////////////////////// 245 ////////////////////////////////////////////////////////////////////////////////
244 246
245 void WebDataService::AddFormFields( 247 void WebDataService::AddFormFields(
246 const std::vector<FormFieldData>& fields) { 248 const std::vector<FormFieldData>& fields) {
247 ScheduleDBTask(FROM_HERE, 249 wdbs_->ScheduleDBTask(FROM_HERE,
248 Bind(&WebDataService::AddFormElementsImpl, this, fields)); 250 Bind(&WebDataService::AddFormElementsImpl, this, fields));
249 } 251 }
250 252
251 WebDataService::Handle WebDataService::GetFormValuesForElementName( 253 WebDataService::Handle WebDataService::GetFormValuesForElementName(
252 const string16& name, const string16& prefix, int limit, 254 const string16& name, const string16& prefix, int limit,
253 WebDataServiceConsumer* consumer) { 255 WebDataServiceConsumer* consumer) {
254 return ScheduleDBTaskWithResult(FROM_HERE, 256 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
255 Bind(&WebDataService::GetFormValuesForElementNameImpl, 257 Bind(&WebDataService::GetFormValuesForElementNameImpl,
256 this, name, prefix, limit), 258 this, name, prefix, limit), consumer);
257 consumer);
258 } 259 }
259 260
260 void WebDataService::RemoveFormElementsAddedBetween(const Time& delete_begin, 261 void WebDataService::RemoveFormElementsAddedBetween(const Time& delete_begin,
261 const Time& delete_end) { 262 const Time& delete_end) {
262 ScheduleDBTask(FROM_HERE, 263 wdbs_->ScheduleDBTask(FROM_HERE,
263 Bind(&WebDataService::RemoveFormElementsAddedBetweenImpl, 264 Bind(&WebDataService::RemoveFormElementsAddedBetweenImpl,
264 this, delete_begin, delete_end)); 265 this, delete_begin, delete_end));
265 } 266 }
266 267
267 void WebDataService::RemoveExpiredFormElements() { 268 void WebDataService::RemoveExpiredFormElements() {
268 ScheduleDBTask(FROM_HERE, 269 wdbs_->ScheduleDBTask(FROM_HERE,
269 Bind(&WebDataService::RemoveExpiredFormElementsImpl, this)); 270 Bind(&WebDataService::RemoveExpiredFormElementsImpl, this));
270 } 271 }
271 272
272 void WebDataService::RemoveFormValueForElementName( 273 void WebDataService::RemoveFormValueForElementName(
273 const string16& name, const string16& value) { 274 const string16& name, const string16& value) {
274 ScheduleDBTask(FROM_HERE, 275 wdbs_->ScheduleDBTask(FROM_HERE,
275 Bind(&WebDataService::RemoveFormValueForElementNameImpl, 276 Bind(&WebDataService::RemoveFormValueForElementNameImpl,
276 this, name, value)); 277 this, name, value));
277 } 278 }
278 279
279 void WebDataService::AddAutofillProfile(const AutofillProfile& profile) { 280 void WebDataService::AddAutofillProfile(const AutofillProfile& profile) {
280 ScheduleDBTask(FROM_HERE, 281 wdbs_->ScheduleDBTask(FROM_HERE,
281 Bind(&WebDataService::AddAutofillProfileImpl, this, profile)); 282 Bind(&WebDataService::AddAutofillProfileImpl, this, profile));
282 } 283 }
283 284
284 void WebDataService::UpdateAutofillProfile(const AutofillProfile& profile) { 285 void WebDataService::UpdateAutofillProfile(const AutofillProfile& profile) {
285 ScheduleDBTask(FROM_HERE, 286 wdbs_->ScheduleDBTask(FROM_HERE,
286 Bind(&WebDataService::UpdateAutofillProfileImpl, this, profile)); 287 Bind(&WebDataService::UpdateAutofillProfileImpl, this, profile));
287 } 288 }
288 289
289 void WebDataService::RemoveAutofillProfile(const std::string& guid) { 290 void WebDataService::RemoveAutofillProfile(const std::string& guid) {
290 ScheduleDBTask(FROM_HERE, 291 wdbs_->ScheduleDBTask(FROM_HERE,
291 Bind(&WebDataService::RemoveAutofillProfileImpl, this, guid)); 292 Bind(&WebDataService::RemoveAutofillProfileImpl, this, guid));
292 } 293 }
293 294
294 WebDataService::Handle WebDataService::GetAutofillProfiles( 295 WebDataService::Handle WebDataService::GetAutofillProfiles(
295 WebDataServiceConsumer* consumer) { 296 WebDataServiceConsumer* consumer) {
296 return ScheduleDBTaskWithResult(FROM_HERE, 297 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
297 Bind(&WebDataService::GetAutofillProfilesImpl, this), consumer); 298 Bind(&WebDataService::GetAutofillProfilesImpl, this), consumer);
298 } 299 }
299 300
300 void WebDataService::AddCreditCard(const CreditCard& credit_card) { 301 void WebDataService::AddCreditCard(const CreditCard& credit_card) {
301 ScheduleDBTask(FROM_HERE, 302 wdbs_->ScheduleDBTask(FROM_HERE,
302 Bind(&WebDataService::AddCreditCardImpl, this, credit_card)); 303 Bind(&WebDataService::AddCreditCardImpl, this, credit_card));
303 } 304 }
304 305
305 void WebDataService::UpdateCreditCard(const CreditCard& credit_card) { 306 void WebDataService::UpdateCreditCard(const CreditCard& credit_card) {
306 ScheduleDBTask(FROM_HERE, 307 wdbs_->ScheduleDBTask(FROM_HERE,
307 Bind(&WebDataService::UpdateCreditCardImpl, this, credit_card)); 308 Bind(&WebDataService::UpdateCreditCardImpl, this, credit_card));
308 } 309 }
309 310
310 void WebDataService::RemoveCreditCard(const std::string& guid) { 311 void WebDataService::RemoveCreditCard(const std::string& guid) {
311 ScheduleDBTask(FROM_HERE, 312 wdbs_->ScheduleDBTask(FROM_HERE,
312 Bind(&WebDataService::RemoveCreditCardImpl, this, guid)); 313 Bind(&WebDataService::RemoveCreditCardImpl, this, guid));
313 } 314 }
314 315
315 WebDataService::Handle WebDataService::GetCreditCards( 316 WebDataService::Handle WebDataService::GetCreditCards(
316 WebDataServiceConsumer* consumer) { 317 WebDataServiceConsumer* consumer) {
317 return ScheduleDBTaskWithResult(FROM_HERE, 318 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
318 Bind(&WebDataService::GetCreditCardsImpl, this), consumer); 319 Bind(&WebDataService::GetCreditCardsImpl, this), consumer);
319 } 320 }
320 321
321 void WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetween( 322 void WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetween(
322 const Time& delete_begin, 323 const Time& delete_begin,
323 const Time& delete_end) { 324 const Time& delete_end) {
324 ScheduleDBTask(FROM_HERE, Bind( 325 wdbs_->ScheduleDBTask(FROM_HERE, Bind(
325 &WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl, 326 &WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl,
326 this, delete_begin, delete_end)); 327 this, delete_begin, delete_end));
327 } 328 }
328 329
329 WebDataService::~WebDataService() { 330 WebDataService::~WebDataService() {
330 if (is_running_ && db_) { 331 wdbs_.reset();
331 NOTREACHED() << "WebDataService dtor called without Shutdown"; 332 DCHECK(!autocomplete_syncable_service_);
332 } 333 DCHECK(!autofill_profile_syncable_service_);
333 }
334
335 bool WebDataService::InitWithPath(const base::FilePath& path) {
336 path_ = path;
337 is_running_ = true;
338
339 ScheduleTask(FROM_HERE,
340 Bind(&WebDataService::InitializeDatabaseIfNecessary, this));
341 ScheduleTask(FROM_HERE,
342 Bind(&WebDataService::InitializeSyncableServices, this));
343 return true;
344 } 334 }
345 335
346 //////////////////////////////////////////////////////////////////////////////// 336 ////////////////////////////////////////////////////////////////////////////////
347 // 337 //
348 // The following methods are executed in Chrome_WebDataThread. 338 // The following methods are executed on the DB thread.
349 // 339 //
350 //////////////////////////////////////////////////////////////////////////////// 340 ////////////////////////////////////////////////////////////////////////////////
351 341
352 void WebDataService::DBInitFailed(sql::InitStatus init_status) { 342 void WebDataService::DBInitFailed(sql::InitStatus sql_status) {
353 ShowProfileErrorDialog( 343 ShowProfileErrorDialog(
354 (init_status == sql::INIT_FAILURE) ? 344 (sql_status == sql::INIT_FAILURE) ?
355 IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR); 345 IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR);
356 } 346 }
357 347
358 void WebDataService::InitializeDatabaseIfNecessary() { 348 void WebDataService::NotifyDatabaseLoadedOnUIThread() {
359 if (db_ || failed_init_ || path_.empty()) 349 db_loaded_ = true;
360 return; 350 // Notify that the database has been initialized.
351 content::NotificationService::current()->Notify(
352 chrome::NOTIFICATION_WEB_DATABASE_LOADED,
353 content::Source<WebDataService>(this),
354 content::NotificationService::NoDetails());
355 }
361 356
362 // In the rare case where the db fails to initialize a dialog may get shown 357 void WebDataService::DatabaseInitOnDB(sql::InitStatus status) {
363 // that blocks the caller, yet allows other messages through. For this reason 358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
364 // we only set db_ to the created database if creation is successful. That 359 if (status == sql::INIT_OK) {
365 // way other methods won't do anything as db_ is still NULL. 360 BrowserThread::PostTask(
366 WebDatabase* db = new WebDatabase(); 361 BrowserThread::UI, FROM_HERE,
367 sql::InitStatus init_status = db->Init(path_, app_locale_); 362 base::Bind(&WebDataService::NotifyDatabaseLoadedOnUIThread, this));
368 if (init_status != sql::INIT_OK) { 363 } else {
369 LOG(ERROR) << "Cannot initialize the web database: " << init_status; 364 BrowserThread::PostTask(
370 failed_init_ = true; 365 BrowserThread::UI, FROM_HERE,
371 delete db; 366 base::Bind(&WebDataService::DBInitFailed, this, status));
372 if (main_loop_) {
373 main_loop_->PostTask(
374 FROM_HERE,
375 base::Bind(&WebDataService::DBInitFailed, this, init_status));
376 }
377 return;
378 } 367 }
379
380 BrowserThread::PostTask(
381 BrowserThread::UI, FROM_HERE,
382 base::Bind(&WebDataService::NotifyDatabaseLoadedOnUIThread, this));
383
384 db_ = db;
385 db_->BeginTransaction();
386 } 368 }
387 369
388 void WebDataService::InitializeSyncableServices() { 370 void WebDataService::InitializeSyncableServices() {
389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
390 DCHECK(!autocomplete_syncable_service_); 372 DCHECK(!autocomplete_syncable_service_);
391 DCHECK(!autofill_profile_syncable_service_); 373 DCHECK(!autofill_profile_syncable_service_);
392 374
393 autocomplete_syncable_service_ = new AutocompleteSyncableService(this); 375 autocomplete_syncable_service_ = new AutocompleteSyncableService(this);
394 autofill_profile_syncable_service_ = new AutofillProfileSyncableService(this); 376 autofill_profile_syncable_service_ = new AutofillProfileSyncableService(this);
395 } 377 }
396 378
397 void WebDataService::NotifyDatabaseLoadedOnUIThread() {
398 // Notify that the database has been initialized.
399 content::NotificationService::current()->Notify(
400 chrome::NOTIFICATION_WEB_DATABASE_LOADED,
401 content::Source<WebDataService>(this),
402 content::NotificationService::NoDetails());
403 }
404
405 void WebDataService::ShutdownDatabase() {
406 should_commit_ = false;
407
408 if (db_) {
409 db_->CommitTransaction();
410 delete db_;
411 db_ = NULL;
412 }
413 }
414 379
dhollowa 2013/03/15 23:50:31 nit: Extra line.
Cait (Slow) 2013/03/16 21:34:22 Done.
415 void WebDataService::ShutdownSyncableServices() { 380 void WebDataService::ShutdownSyncableServices() {
416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
417 382
418 delete autocomplete_syncable_service_; 383 delete autocomplete_syncable_service_;
419 autocomplete_syncable_service_ = NULL; 384 autocomplete_syncable_service_ = NULL;
420 delete autofill_profile_syncable_service_; 385 delete autofill_profile_syncable_service_;
421 autofill_profile_syncable_service_ = NULL; 386 autofill_profile_syncable_service_ = NULL;
422 } 387 }
423 388
424 void WebDataService::Commit() {
425 if (should_commit_) {
426 should_commit_ = false;
427
428 if (db_) {
429 db_->CommitTransaction();
430 db_->BeginTransaction();
431 }
432 }
433 }
434
435 void WebDataService::ScheduleTask(const tracked_objects::Location& from_here,
436 const base::Closure& task) {
437 if (is_running_)
438 BrowserThread::PostTask(BrowserThread::DB, from_here, task);
439 else
440 NOTREACHED() << "Task scheduled after Shutdown()";
441 }
442
443 void WebDataService::ScheduleDBTask(
444 const tracked_objects::Location& from_here,
445 const base::Closure& task) {
446 scoped_ptr<WebDataRequest> request(
447 new WebDataRequest(NULL, request_manager_.get()));
448 if (is_running_) {
449 BrowserThread::PostTask(BrowserThread::DB, from_here,
450 base::Bind(&WebDataService::DBTaskWrapper, this, task,
451 base::Passed(&request)));
452 } else {
453 NOTREACHED() << "Task scheduled after Shutdown()";
454 }
455 }
456
457 WebDataService::Handle WebDataService::ScheduleDBTaskWithResult(
458 const tracked_objects::Location& from_here,
459 const ResultTask& task,
460 WebDataServiceConsumer* consumer) {
461 DCHECK(consumer);
462 scoped_ptr<WebDataRequest> request(
463 new WebDataRequest(consumer, request_manager_.get()));
464 WebDataService::Handle handle = request->GetHandle();
465 if (is_running_) {
466 BrowserThread::PostTask(BrowserThread::DB, from_here,
467 base::Bind(&WebDataService::DBResultTaskWrapper, this, task,
468 base::Passed(&request)));
469 } else {
470 NOTREACHED() << "Task scheduled after Shutdown()";
471 }
472 return handle;
473 }
474
475 void WebDataService::DBTaskWrapper(const base::Closure& task,
476 scoped_ptr<WebDataRequest> request) {
477 InitializeDatabaseIfNecessary();
478 if (db_ && !request->IsCancelled()) {
479 task.Run();
480 }
481 request_manager_->RequestCompleted(request.Pass());
482 }
483
484 void WebDataService::DBResultTaskWrapper(const ResultTask& task,
485 scoped_ptr<WebDataRequest> request) {
486 InitializeDatabaseIfNecessary();
487 if (db_ && !request->IsCancelled()) {
488 request->SetResult(task.Run());
489 }
490 request_manager_->RequestCompleted(request.Pass());
491 }
492
493 void WebDataService::ScheduleCommit() {
494 if (should_commit_ == false) {
495 should_commit_ = true;
496 ScheduleTask(FROM_HERE, Bind(&WebDataService::Commit, this));
497 }
498 }
499
500 //////////////////////////////////////////////////////////////////////////////// 389 ////////////////////////////////////////////////////////////////////////////////
501 // 390 //
502 // Keywords implementation. 391 // Keywords implementation.
503 // 392 //
504 //////////////////////////////////////////////////////////////////////////////// 393 ////////////////////////////////////////////////////////////////////////////////
505 394
506 void WebDataService::AddKeywordImpl(const TemplateURLData& data) { 395 WebDatabase::State WebDataService::AddKeywordImpl(
507 db_->GetKeywordTable()->AddKeyword(data); 396 const TemplateURLData& data, WebDatabase* db) {
508 ScheduleCommit(); 397 db->GetKeywordTable()->AddKeyword(data);
398 return WebDatabase::COMMIT_NEEDED;
509 } 399 }
510 400
511 void WebDataService::RemoveKeywordImpl(TemplateURLID id) { 401 WebDatabase::State WebDataService::RemoveKeywordImpl(
402 TemplateURLID id, WebDatabase* db) {
512 DCHECK(id); 403 DCHECK(id);
513 db_->GetKeywordTable()->RemoveKeyword(id); 404 db->GetKeywordTable()->RemoveKeyword(id);
514 ScheduleCommit(); 405 return WebDatabase::COMMIT_NEEDED;
515 } 406 }
516 407
517 void WebDataService::UpdateKeywordImpl(const TemplateURLData& data) { 408 WebDatabase::State WebDataService::UpdateKeywordImpl(
518 if (!db_->GetKeywordTable()->UpdateKeyword(data)) { 409 const TemplateURLData& data, WebDatabase* db) {
410 if (!db->GetKeywordTable()->UpdateKeyword(data)) {
519 NOTREACHED(); 411 NOTREACHED();
520 return; 412 return WebDatabase::COMMIT_NOT_NEEDED;
521 } 413 }
522 ScheduleCommit(); 414 return WebDatabase::COMMIT_NEEDED;
523 } 415 }
524 416
525 scoped_ptr<WDTypedResult> WebDataService::GetKeywordsImpl() { 417 scoped_ptr<WDTypedResult> WebDataService::GetKeywordsImpl(WebDatabase* db) {
526 WDKeywordsResult result; 418 WDKeywordsResult result;
527 db_->GetKeywordTable()->GetKeywords(&result.keywords); 419 db->GetKeywordTable()->GetKeywords(&result.keywords);
528 result.default_search_provider_id = 420 result.default_search_provider_id =
529 db_->GetKeywordTable()->GetDefaultSearchProviderID(); 421 db->GetKeywordTable()->GetDefaultSearchProviderID();
530 result.builtin_keyword_version = 422 result.builtin_keyword_version =
531 db_->GetKeywordTable()->GetBuiltinKeywordVersion(); 423 db->GetKeywordTable()->GetBuiltinKeywordVersion();
532 return scoped_ptr<WDTypedResult>( 424 return scoped_ptr<WDTypedResult>(
533 new WDResult<WDKeywordsResult>(KEYWORDS_RESULT, result)); 425 new WDResult<WDKeywordsResult>(KEYWORDS_RESULT, result));
534 } 426 }
535 427
536 void WebDataService::SetDefaultSearchProviderImpl(TemplateURLID id) { 428 WebDatabase::State WebDataService::SetDefaultSearchProviderImpl(
537 if (!db_->GetKeywordTable()->SetDefaultSearchProviderID(id)) { 429 TemplateURLID id, WebDatabase* db) {
430 if (!db->GetKeywordTable()->SetDefaultSearchProviderID(id)) {
538 NOTREACHED(); 431 NOTREACHED();
539 return; 432 return WebDatabase::COMMIT_NOT_NEEDED;
540 } 433 }
541 ScheduleCommit(); 434 return WebDatabase::COMMIT_NEEDED;
542 } 435 }
543 436
544 void WebDataService::SetBuiltinKeywordVersionImpl(int version) { 437 WebDatabase::State WebDataService::SetBuiltinKeywordVersionImpl(
545 if (!db_->GetKeywordTable()->SetBuiltinKeywordVersion(version)) { 438 int version, WebDatabase* db) {
439 if (!db->GetKeywordTable()->SetBuiltinKeywordVersion(version)) {
546 NOTREACHED(); 440 NOTREACHED();
547 return; 441 return WebDatabase::COMMIT_NOT_NEEDED;
548 } 442 }
549 ScheduleCommit(); 443 return WebDatabase::COMMIT_NEEDED;
550 } 444 }
551 445
552 //////////////////////////////////////////////////////////////////////////////// 446 ////////////////////////////////////////////////////////////////////////////////
553 // 447 //
554 // Web Apps implementation. 448 // Web Apps implementation.
555 // 449 //
556 //////////////////////////////////////////////////////////////////////////////// 450 ////////////////////////////////////////////////////////////////////////////////
557 451
558 void WebDataService::SetWebAppImageImpl( 452 WebDatabase::State WebDataService::SetWebAppImageImpl(
559 const GURL& app_url, const SkBitmap& image) { 453 const GURL& app_url, const SkBitmap& image, WebDatabase* db) {
560 db_->GetWebAppsTable()->SetWebAppImage(app_url, image); 454 db->GetWebAppsTable()->SetWebAppImage(app_url, image);
561 ScheduleCommit(); 455 return WebDatabase::COMMIT_NEEDED;
562 } 456 }
563 457
564 void WebDataService::SetWebAppHasAllImagesImpl( 458 WebDatabase::State WebDataService::SetWebAppHasAllImagesImpl(
565 const GURL& app_url, bool has_all_images) { 459 const GURL& app_url, bool has_all_images, WebDatabase* db) {
566 db_->GetWebAppsTable()->SetWebAppHasAllImages(app_url, has_all_images); 460 db->GetWebAppsTable()->
567 ScheduleCommit(); 461 SetWebAppHasAllImages(app_url, has_all_images);
462 return WebDatabase::COMMIT_NEEDED;
568 } 463 }
569 464
570 void WebDataService::RemoveWebAppImpl(const GURL& app_url) { 465 WebDatabase::State WebDataService::RemoveWebAppImpl(
571 db_->GetWebAppsTable()->RemoveWebApp(app_url); 466 const GURL& app_url, WebDatabase* db) {
572 ScheduleCommit(); 467 db->GetWebAppsTable()->RemoveWebApp(app_url);
468 return WebDatabase::COMMIT_NEEDED;
573 } 469 }
574 470
575 scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl( 471 scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl(
576 const GURL& app_url) { 472 const GURL& app_url, WebDatabase* db) {
577 WDAppImagesResult result; 473 WDAppImagesResult result;
578 result.has_all_images = 474 result.has_all_images = db->GetWebAppsTable()->GetWebAppHasAllImages(app_url);
579 db_->GetWebAppsTable()->GetWebAppHasAllImages(app_url); 475 db->GetWebAppsTable()->GetWebAppImages(app_url, &result.images);
580 db_->GetWebAppsTable()->GetWebAppImages(app_url, &result.images);
581 return scoped_ptr<WDTypedResult>( 476 return scoped_ptr<WDTypedResult>(
582 new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result)); 477 new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result));
583 } 478 }
584 479
585 //////////////////////////////////////////////////////////////////////////////// 480 ////////////////////////////////////////////////////////////////////////////////
586 // 481 //
587 // Token Service implementation. 482 // Token Service implementation.
588 // 483 //
589 //////////////////////////////////////////////////////////////////////////////// 484 ////////////////////////////////////////////////////////////////////////////////
590 485
591 void WebDataService::RemoveAllTokensImpl() { 486 WebDatabase::State WebDataService::RemoveAllTokensImpl(WebDatabase* db) {
592 if (db_->GetTokenServiceTable()->RemoveAllTokens()) { 487 if (db->GetTokenServiceTable()->RemoveAllTokens()) {
593 ScheduleCommit(); 488 return WebDatabase::COMMIT_NEEDED;
594 } 489 }
490 return WebDatabase::COMMIT_NOT_NEEDED;
595 } 491 }
596 492
597 void WebDataService::SetTokenForServiceImpl(const std::string& service, 493 WebDatabase::State WebDataService::SetTokenForServiceImpl(
598 const std::string& token) { 494 const std::string& service, const std::string& token, WebDatabase* db) {
599 if (db_->GetTokenServiceTable()->SetTokenForService(service, token)) { 495 if (db->GetTokenServiceTable()->SetTokenForService(service, token)) {
600 ScheduleCommit(); 496 return WebDatabase::COMMIT_NEEDED;
601 } 497 }
498 return WebDatabase::COMMIT_NOT_NEEDED;
602 } 499 }
603 500
604 scoped_ptr<WDTypedResult> WebDataService::GetAllTokensImpl() { 501 scoped_ptr<WDTypedResult> WebDataService::GetAllTokensImpl(WebDatabase* db) {
605 std::map<std::string, std::string> map; 502 std::map<std::string, std::string> map;
606 db_->GetTokenServiceTable()->GetAllTokens(&map); 503 db->GetTokenServiceTable()->GetAllTokens(&map);
607 return scoped_ptr<WDTypedResult>( 504 return scoped_ptr<WDTypedResult>(
608 new WDResult<std::map<std::string, std::string> >(TOKEN_RESULT, map)); 505 new WDResult<std::map<std::string, std::string> >(TOKEN_RESULT, map));
609 } 506 }
610 507
611 //////////////////////////////////////////////////////////////////////////////// 508 ////////////////////////////////////////////////////////////////////////////////
612 // 509 //
613 // Autofill implementation. 510 // Autofill implementation.
614 // 511 //
615 //////////////////////////////////////////////////////////////////////////////// 512 ////////////////////////////////////////////////////////////////////////////////
616 513
617 void WebDataService::AddFormElementsImpl( 514 WebDatabase::State WebDataService::AddFormElementsImpl(
618 const std::vector<FormFieldData>& fields) { 515 const std::vector<FormFieldData>& fields, WebDatabase* db) {
619 AutofillChangeList changes; 516 AutofillChangeList changes;
620 if (!db_->GetAutofillTable()->AddFormFieldValues(fields, &changes)) { 517 if (!db->GetAutofillTable()->AddFormFieldValues(fields, &changes)) {
621 NOTREACHED(); 518 NOTREACHED();
622 return; 519 return WebDatabase::COMMIT_NOT_NEEDED;
623 } 520 }
624 ScheduleCommit();
625 521
626 // Post the notifications including the list of affected keys. 522 // Post the notifications including the list of affected keys.
627 // This is sent here so that work resulting from this notification will be 523 // This is sent here so that work resulting from this notification will be
628 // done on the DB thread, and not the UI thread. 524 // done on the DB thread, and not the UI thread.
629 content::NotificationService::current()->Notify( 525 content::NotificationService::current()->Notify(
630 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, 526 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED,
631 content::Source<WebDataService>(this), 527 content::Source<WebDataService>(this),
632 content::Details<AutofillChangeList>(&changes)); 528 content::Details<AutofillChangeList>(&changes));
529
530 return WebDatabase::COMMIT_NEEDED;
633 } 531 }
634 532
635 scoped_ptr<WDTypedResult> WebDataService::GetFormValuesForElementNameImpl( 533 scoped_ptr<WDTypedResult> WebDataService::GetFormValuesForElementNameImpl(
636 const string16& name, const string16& prefix, int limit) { 534 const string16& name, const string16& prefix, int limit, WebDatabase* db) {
637 std::vector<string16> values; 535 std::vector<string16> values;
638 db_->GetAutofillTable()->GetFormValuesForElementName( 536 db->GetAutofillTable()->GetFormValuesForElementName(
639 name, prefix, &values, limit); 537 name, prefix, &values, limit);
640 return scoped_ptr<WDTypedResult>( 538 return scoped_ptr<WDTypedResult>(
641 new WDResult<std::vector<string16> >(AUTOFILL_VALUE_RESULT, values)); 539 new WDResult<std::vector<string16> >(AUTOFILL_VALUE_RESULT, values));
642 } 540 }
643 541
644 void WebDataService::RemoveFormElementsAddedBetweenImpl( 542 WebDatabase::State WebDataService::RemoveFormElementsAddedBetweenImpl(
645 const base::Time& delete_begin, const base::Time& delete_end) { 543 const base::Time& delete_begin, const base::Time& delete_end,
544 WebDatabase* db) {
646 AutofillChangeList changes; 545 AutofillChangeList changes;
647 if (db_->GetAutofillTable()->RemoveFormElementsAddedBetween( 546
547 if (db->GetAutofillTable()->RemoveFormElementsAddedBetween(
648 delete_begin, delete_end, &changes)) { 548 delete_begin, delete_end, &changes)) {
649 if (!changes.empty()) { 549 if (!changes.empty()) {
650 // Post the notifications including the list of affected keys. 550 // Post the notifications including the list of affected keys.
651 // This is sent here so that work resulting from this notification 551 // This is sent here so that work resulting from this notification
652 // will be done on the DB thread, and not the UI thread. 552 // will be done on the DB thread, and not the UI thread.
653 content::NotificationService::current()->Notify( 553 content::NotificationService::current()->Notify(
654 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, 554 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED,
655 content::Source<WebDataService>(this), 555 content::Source<WebDataService>(this),
656 content::Details<AutofillChangeList>(&changes)); 556 content::Details<AutofillChangeList>(&changes));
657 } 557 }
658 ScheduleCommit(); 558 return WebDatabase::COMMIT_NEEDED;
659 } 559 }
560 return WebDatabase::COMMIT_NOT_NEEDED;
660 } 561 }
661 562
662 void WebDataService::RemoveExpiredFormElementsImpl() { 563 WebDatabase::State WebDataService::RemoveExpiredFormElementsImpl(
564 WebDatabase* db) {
663 AutofillChangeList changes; 565 AutofillChangeList changes;
664 566
665 if (db_->GetAutofillTable()->RemoveExpiredFormElements(&changes)) { 567 if (db->GetAutofillTable()->RemoveExpiredFormElements(&changes)) {
666 if (!changes.empty()) { 568 if (!changes.empty()) {
667 // Post the notifications including the list of affected keys. 569 // Post the notifications including the list of affected keys.
668 // This is sent here so that work resulting from this notification 570 // This is sent here so that work resulting from this notification
669 // will be done on the DB thread, and not the UI thread. 571 // will be done on the DB thread, and not the UI thread.
670 content::NotificationService::current()->Notify( 572 content::NotificationService::current()->Notify(
671 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, 573 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED,
672 content::Source<WebDataService>(this), 574 content::Source<WebDataService>(this),
673 content::Details<AutofillChangeList>(&changes)); 575 content::Details<AutofillChangeList>(&changes));
674 } 576 }
675 ScheduleCommit(); 577 return WebDatabase::COMMIT_NEEDED;
676 } 578 }
579 return WebDatabase::COMMIT_NOT_NEEDED;
677 } 580 }
678 581
679 void WebDataService::RemoveFormValueForElementNameImpl( 582 WebDatabase::State WebDataService::RemoveFormValueForElementNameImpl(
680 const string16& name, const string16& value) { 583 const string16& name, const string16& value, WebDatabase* db) {
681 584
682 if (db_->GetAutofillTable()->RemoveFormElement(name, value)) { 585 if (db->GetAutofillTable()->RemoveFormElement(name, value)) {
683 AutofillChangeList changes; 586 AutofillChangeList changes;
684 changes.push_back(AutofillChange(AutofillChange::REMOVE, 587 changes.push_back(AutofillChange(AutofillChange::REMOVE,
685 AutofillKey(name, value))); 588 AutofillKey(name, value)));
686 ScheduleCommit();
687 589
688 // Post the notifications including the list of affected keys. 590 // Post the notifications including the list of affected keys.
689 content::NotificationService::current()->Notify( 591 content::NotificationService::current()->Notify(
690 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, 592 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED,
691 content::Source<WebDataService>(this), 593 content::Source<WebDataService>(this),
692 content::Details<AutofillChangeList>(&changes)); 594 content::Details<AutofillChangeList>(&changes));
595
596 return WebDatabase::COMMIT_NEEDED;
693 } 597 }
598 return WebDatabase::COMMIT_NOT_NEEDED;
694 } 599 }
695 600
696 void WebDataService::AddAutofillProfileImpl(const AutofillProfile& profile) { 601 WebDatabase::State WebDataService::AddAutofillProfileImpl(
697 if (!db_->GetAutofillTable()->AddAutofillProfile(profile)) { 602 const AutofillProfile& profile, WebDatabase* db) {
603 if (!db->GetAutofillTable()->AddAutofillProfile(profile)) {
698 NOTREACHED(); 604 NOTREACHED();
699 return; 605 return WebDatabase::COMMIT_NOT_NEEDED;
700 } 606 }
701 ScheduleCommit();
702 607
703 // Send GUID-based notification. 608 // Send GUID-based notification.
704 AutofillProfileChange change(AutofillProfileChange::ADD, 609 AutofillProfileChange change(AutofillProfileChange::ADD,
705 profile.guid(), &profile); 610 profile.guid(), &profile);
706 content::NotificationService::current()->Notify( 611 content::NotificationService::current()->Notify(
707 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, 612 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED,
708 content::Source<WebDataService>(this), 613 content::Source<WebDataService>(this),
709 content::Details<AutofillProfileChange>(&change)); 614 content::Details<AutofillProfileChange>(&change));
615
616 return WebDatabase::COMMIT_NEEDED;
710 } 617 }
711 618
712 void WebDataService::UpdateAutofillProfileImpl(const AutofillProfile& profile) { 619 WebDatabase::State WebDataService::UpdateAutofillProfileImpl(
620 const AutofillProfile& profile, WebDatabase* db) {
713 // Only perform the update if the profile exists. It is currently 621 // Only perform the update if the profile exists. It is currently
714 // valid to try to update a missing profile. We simply drop the write and 622 // valid to try to update a missing profile. We simply drop the write and
715 // the caller will detect this on the next refresh. 623 // the caller will detect this on the next refresh.
716 AutofillProfile* original_profile = NULL; 624 AutofillProfile* original_profile = NULL;
717 if (!db_->GetAutofillTable()->GetAutofillProfile(profile.guid(), 625 if (!db->GetAutofillTable()->GetAutofillProfile(profile.guid(),
718 &original_profile)) { 626 &original_profile)) {
719 return; 627 return WebDatabase::COMMIT_NOT_NEEDED;
720 } 628 }
721 scoped_ptr<AutofillProfile> scoped_profile(original_profile); 629 scoped_ptr<AutofillProfile> scoped_profile(original_profile);
722 630
723 if (!db_->GetAutofillTable()->UpdateAutofillProfileMulti(profile)) { 631 if (!db->GetAutofillTable()->UpdateAutofillProfileMulti(profile)) {
724 NOTREACHED(); 632 NOTREACHED();
725 return; 633 return WebDatabase::COMMIT_NEEDED;
726 } 634 }
727 ScheduleCommit();
728 635
729 // Send GUID-based notification. 636 // Send GUID-based notification.
730 AutofillProfileChange change(AutofillProfileChange::UPDATE, 637 AutofillProfileChange change(AutofillProfileChange::UPDATE,
731 profile.guid(), &profile); 638 profile.guid(), &profile);
732 content::NotificationService::current()->Notify( 639 content::NotificationService::current()->Notify(
733 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, 640 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED,
734 content::Source<WebDataService>(this), 641 content::Source<WebDataService>(this),
735 content::Details<AutofillProfileChange>(&change)); 642 content::Details<AutofillProfileChange>(&change));
643
644 return WebDatabase::COMMIT_NEEDED;
736 } 645 }
737 646
738 void WebDataService::RemoveAutofillProfileImpl(const std::string& guid) { 647 WebDatabase::State WebDataService::RemoveAutofillProfileImpl(
648 const std::string& guid, WebDatabase* db) {
739 AutofillProfile* profile = NULL; 649 AutofillProfile* profile = NULL;
740 if (!db_->GetAutofillTable()->GetAutofillProfile(guid, &profile)) { 650 if (!db->GetAutofillTable()->GetAutofillProfile(guid, &profile)) {
741 NOTREACHED(); 651 NOTREACHED();
742 return; 652 return WebDatabase::COMMIT_NOT_NEEDED;
743 } 653 }
744 scoped_ptr<AutofillProfile> scoped_profile(profile); 654 scoped_ptr<AutofillProfile> scoped_profile(profile);
745 655
746 if (!db_->GetAutofillTable()->RemoveAutofillProfile(guid)) { 656 if (!db->GetAutofillTable()->RemoveAutofillProfile(guid)) {
747 NOTREACHED(); 657 NOTREACHED();
748 return; 658 return WebDatabase::COMMIT_NOT_NEEDED;
749 } 659 }
750 ScheduleCommit();
751 660
752 // Send GUID-based notification. 661 // Send GUID-based notification.
753 AutofillProfileChange change(AutofillProfileChange::REMOVE, guid, NULL); 662 AutofillProfileChange change(AutofillProfileChange::REMOVE, guid, NULL);
754 content::NotificationService::current()->Notify( 663 content::NotificationService::current()->Notify(
755 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, 664 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED,
756 content::Source<WebDataService>(this), 665 content::Source<WebDataService>(this),
757 content::Details<AutofillProfileChange>(&change)); 666 content::Details<AutofillProfileChange>(&change));
667
668 return WebDatabase::COMMIT_NEEDED;
758 } 669 }
759 670
760 scoped_ptr<WDTypedResult> WebDataService::GetAutofillProfilesImpl() { 671 scoped_ptr<WDTypedResult> WebDataService::GetAutofillProfilesImpl(
672 WebDatabase* db) {
761 std::vector<AutofillProfile*> profiles; 673 std::vector<AutofillProfile*> profiles;
762 db_->GetAutofillTable()->GetAutofillProfiles(&profiles); 674 db->GetAutofillTable()->GetAutofillProfiles(&profiles);
763 return scoped_ptr<WDTypedResult>( 675 return scoped_ptr<WDTypedResult>(
764 new WDDestroyableResult<std::vector<AutofillProfile*> >( 676 new WDDestroyableResult<std::vector<AutofillProfile*> >(
765 AUTOFILL_PROFILES_RESULT, 677 AUTOFILL_PROFILES_RESULT,
766 profiles, 678 profiles,
767 base::Bind(&WebDataService::DestroyAutofillProfileResult, 679 base::Bind(&WebDataService::DestroyAutofillProfileResult,
768 base::Unretained(this)))); 680 base::Unretained(this))));
769 } 681 }
770 682
771 void WebDataService::AddCreditCardImpl(const CreditCard& credit_card) { 683 WebDatabase::State WebDataService::AddCreditCardImpl(
772 if (!db_->GetAutofillTable()->AddCreditCard(credit_card)) { 684 const CreditCard& credit_card, WebDatabase* db) {
685 if (!db->GetAutofillTable()->AddCreditCard(credit_card)) {
773 NOTREACHED(); 686 NOTREACHED();
774 return; 687 return WebDatabase::COMMIT_NOT_NEEDED;
775 } 688 }
776 ScheduleCommit(); 689
690 return WebDatabase::COMMIT_NEEDED;
777 } 691 }
778 692
779 void WebDataService::UpdateCreditCardImpl(const CreditCard& credit_card) { 693 WebDatabase::State WebDataService::UpdateCreditCardImpl(
694 const CreditCard& credit_card, WebDatabase* db) {
780 // It is currently valid to try to update a missing profile. We simply drop 695 // It is currently valid to try to update a missing profile. We simply drop
781 // the write and the caller will detect this on the next refresh. 696 // the write and the caller will detect this on the next refresh.
782 CreditCard* original_credit_card = NULL; 697 CreditCard* original_credit_card = NULL;
783 if (!db_->GetAutofillTable()->GetCreditCard(credit_card.guid(), 698 if (!db->GetAutofillTable()->GetCreditCard(credit_card.guid(),
784 &original_credit_card)) { 699 &original_credit_card)) {
785 return; 700 return WebDatabase::COMMIT_NOT_NEEDED;
786 } 701 }
787 scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); 702 scoped_ptr<CreditCard> scoped_credit_card(original_credit_card);
788 703
789 if (!db_->GetAutofillTable()->UpdateCreditCard(credit_card)) { 704 if (!db->GetAutofillTable()->UpdateCreditCard(credit_card)) {
790 NOTREACHED(); 705 NOTREACHED();
791 return; 706 return WebDatabase::COMMIT_NOT_NEEDED;
792 } 707 }
793 ScheduleCommit(); 708 return WebDatabase::COMMIT_NEEDED;
794 } 709 }
795 710
796 void WebDataService::RemoveCreditCardImpl(const std::string& guid) { 711 WebDatabase::State WebDataService::RemoveCreditCardImpl(
797 if (!db_->GetAutofillTable()->RemoveCreditCard(guid)) { 712 const std::string& guid, WebDatabase* db) {
713 if (!db->GetAutofillTable()->RemoveCreditCard(guid)) {
798 NOTREACHED(); 714 NOTREACHED();
799 return; 715 return WebDatabase::COMMIT_NOT_NEEDED;
800 } 716 }
801 ScheduleCommit(); 717 return WebDatabase::COMMIT_NEEDED;
802 } 718 }
803 719
804 scoped_ptr<WDTypedResult> WebDataService::GetCreditCardsImpl() { 720 scoped_ptr<WDTypedResult> WebDataService::GetCreditCardsImpl(WebDatabase* db) {
805 std::vector<CreditCard*> credit_cards; 721 std::vector<CreditCard*> credit_cards;
806 db_->GetAutofillTable()->GetCreditCards(&credit_cards); 722 db->GetAutofillTable()->GetCreditCards(&credit_cards);
807 return scoped_ptr<WDTypedResult>( 723 return scoped_ptr<WDTypedResult>(
808 new WDDestroyableResult<std::vector<CreditCard*> >( 724 new WDDestroyableResult<std::vector<CreditCard*> >(
809 AUTOFILL_CREDITCARDS_RESULT, 725 AUTOFILL_CREDITCARDS_RESULT,
810 credit_cards, 726 credit_cards,
811 base::Bind(&WebDataService::DestroyAutofillCreditCardResult, 727 base::Bind(&WebDataService::DestroyAutofillCreditCardResult,
812 base::Unretained(this)))); 728 base::Unretained(this))));
813 } 729 }
814 730
815 void WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl( 731 WebDatabase::State
816 const base::Time& delete_begin, const base::Time& delete_end) { 732 WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl(
733 const base::Time& delete_begin, const base::Time& delete_end,
734 WebDatabase* db) {
817 std::vector<std::string> profile_guids; 735 std::vector<std::string> profile_guids;
818 std::vector<std::string> credit_card_guids; 736 std::vector<std::string> credit_card_guids;
819 if (db_->GetAutofillTable()-> 737 if (db->GetAutofillTable()->
820 RemoveAutofillProfilesAndCreditCardsModifiedBetween( 738 RemoveAutofillProfilesAndCreditCardsModifiedBetween(
821 delete_begin, 739 delete_begin,
822 delete_end, 740 delete_end,
823 &profile_guids, 741 &profile_guids,
824 &credit_card_guids)) { 742 &credit_card_guids)) {
825 for (std::vector<std::string>::iterator iter = profile_guids.begin(); 743 for (std::vector<std::string>::iterator iter = profile_guids.begin();
826 iter != profile_guids.end(); ++iter) { 744 iter != profile_guids.end(); ++iter) {
827 AutofillProfileChange change(AutofillProfileChange::REMOVE, *iter, 745 AutofillProfileChange change(AutofillProfileChange::REMOVE, *iter,
828 NULL); 746 NULL);
829 content::NotificationService::current()->Notify( 747 content::NotificationService::current()->Notify(
830 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, 748 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED,
831 content::Source<WebDataService>(this), 749 content::Source<WebDataService>(this),
832 content::Details<AutofillProfileChange>(&change)); 750 content::Details<AutofillProfileChange>(&change));
833 } 751 }
834 // Note: It is the caller's responsibility to post notifications for any 752 // Note: It is the caller's responsibility to post notifications for any
835 // changes, e.g. by calling the Refresh() method of PersonalDataManager. 753 // changes, e.g. by calling the Refresh() method of PersonalDataManager.
836 ScheduleCommit(); 754 return WebDatabase::COMMIT_NEEDED;
837 } 755 }
756 return WebDatabase::COMMIT_NOT_NEEDED;
838 } 757 }
839 758
840 AutofillProfileSyncableService* 759 AutofillProfileSyncableService*
841 WebDataService::GetAutofillProfileSyncableService() const { 760 WebDataService::GetAutofillProfileSyncableService() const {
842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 761 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
843 DCHECK(autofill_profile_syncable_service_); // Make sure we're initialized. 762 DCHECK(autofill_profile_syncable_service_); // Make sure we're initialized.
844 763
845 return autofill_profile_syncable_service_; 764 return autofill_profile_syncable_service_;
846 } 765 }
847 766
(...skipping 15 matching lines...) Expand all
863 782
864 void WebDataService::DestroyAutofillCreditCardResult( 783 void WebDataService::DestroyAutofillCreditCardResult(
865 const WDTypedResult* result) { 784 const WDTypedResult* result) {
866 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); 785 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT);
867 const WDResult<std::vector<CreditCard*> >* r = 786 const WDResult<std::vector<CreditCard*> >* r =
868 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); 787 static_cast<const WDResult<std::vector<CreditCard*> >*>(result);
869 788
870 std::vector<CreditCard*> credit_cards = r->GetValue(); 789 std::vector<CreditCard*> credit_cards = r->GetValue();
871 STLDeleteElements(&credit_cards); 790 STLDeleteElements(&credit_cards);
872 } 791 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698