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

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

Powered by Google App Engine
This is Rietveld 408576698