Chromium Code Reviews

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

Issue 1701012: Cleanup: Order the methods in WebDataService. Rename a method to match current style. (Closed)
Patch Set: Fix test compile. Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/stl_util-inl.h" 8 #include "base/stl_util-inl.h"
9 #include "base/task.h" 9 #include "base/task.h"
10 #include "base/thread.h" 10 #include "base/thread.h"
(...skipping 22 matching lines...)
33 33
34 WebDataService::WebDataService() 34 WebDataService::WebDataService()
35 : is_running_(false), 35 : is_running_(false),
36 db_(NULL), 36 db_(NULL),
37 failed_init_(false), 37 failed_init_(false),
38 should_commit_(false), 38 should_commit_(false),
39 next_request_handle_(1), 39 next_request_handle_(1),
40 main_loop_(MessageLoop::current()) { 40 main_loop_(MessageLoop::current()) {
41 } 41 }
42 42
43 WebDataService::~WebDataService() {
44 if (is_running_ && db_) {
45 DLOG_ASSERT("WebDataService dtor called without Shutdown");
46 }
47 }
48
49 bool WebDataService::Init(const FilePath& profile_path) { 43 bool WebDataService::Init(const FilePath& profile_path) {
50 FilePath path = profile_path; 44 FilePath path = profile_path;
51 path = path.Append(chrome::kWebDataFilename); 45 path = path.Append(chrome::kWebDataFilename);
52 return InitWithPath(path); 46 return InitWithPath(path);
53 } 47 }
54 48
55 bool WebDataService::InitWithPath(const FilePath& path) {
56 path_ = path;
57 is_running_ = true;
58 ScheduleTask(NewRunnableMethod(this,
59 &WebDataService::InitializeDatabaseIfNecessary));
60 return true;
61 }
62
63 void WebDataService::Shutdown() { 49 void WebDataService::Shutdown() {
64 UnloadDatabase(); 50 UnloadDatabase();
65 } 51 }
66 52
67 bool WebDataService::IsRunning() const { 53 bool WebDataService::IsRunning() const {
68 return is_running_; 54 return is_running_;
69 } 55 }
70 56
71 void WebDataService::UnloadDatabase() { 57 void WebDataService::UnloadDatabase() {
72 ScheduleTask(NewRunnableMethod(this, &WebDataService::ShutdownDatabase)); 58 ScheduleTask(NewRunnableMethod(this, &WebDataService::ShutdownDatabase));
73 } 59 }
74 60
75 void WebDataService::ScheduleCommit() {
76 if (should_commit_ == false) {
77 should_commit_ = true;
78 ScheduleTask(NewRunnableMethod(this, &WebDataService::Commit));
79 }
80 }
81
82 void WebDataService::ScheduleTask(Task* t) {
83 if (is_running_)
84 ChromeThread::PostTask(ChromeThread::DB, FROM_HERE, t);
85 else
86 NOTREACHED() << "Task scheduled after Shutdown()";
87 }
88
89 void WebDataService::RegisterRequest(WebDataRequest* request) {
90 AutoLock l(pending_lock_);
91 pending_requests_[request->GetHandle()] = request;
92 }
93
94 void WebDataService::CancelRequest(Handle h) { 61 void WebDataService::CancelRequest(Handle h) {
95 AutoLock l(pending_lock_); 62 AutoLock l(pending_lock_);
96 RequestMap::iterator i = pending_requests_.find(h); 63 RequestMap::iterator i = pending_requests_.find(h);
97 if (i == pending_requests_.end()) { 64 if (i == pending_requests_.end()) {
98 NOTREACHED() << "Canceling a nonexistant web data service request"; 65 NOTREACHED() << "Canceling a nonexistant web data service request";
99 return; 66 return;
100 } 67 }
101 i->second->Cancel(); 68 i->second->Cancel();
102 } 69 }
103 70
104 void WebDataService::AddFormFieldValues(
105 const std::vector<FormField>& element) {
106 GenericRequest<std::vector<FormField> >* request =
107 new GenericRequest<std::vector<FormField> >(
108 this, GetNextRequestHandle(), NULL, element);
109 RegisterRequest(request);
110 ScheduleTask(NewRunnableMethod(this,
111 &WebDataService::AddFormFieldValuesImpl,
112 request));
113 }
114
115 WebDataService::Handle WebDataService::GetFormValuesForElementName(
116 const string16& name, const string16& prefix, int limit,
117 WebDataServiceConsumer* consumer) {
118 WebDataRequest* request =
119 new WebDataRequest(this, GetNextRequestHandle(), consumer);
120 RegisterRequest(request);
121 ScheduleTask(
122 NewRunnableMethod(this,
123 &WebDataService::GetFormValuesForElementNameImpl,
124 request,
125 name,
126 prefix,
127 limit));
128 return request->GetHandle();
129 }
130
131 void WebDataService::RemoveFormValueForElementName(
132 const string16& name, const string16& value) {
133 GenericRequest2<string16, string16>* request =
134 new GenericRequest2<string16, string16>(this,
135 GetNextRequestHandle(),
136 NULL,
137 name, value);
138 RegisterRequest(request);
139 ScheduleTask(
140 NewRunnableMethod(this,
141 &WebDataService::RemoveFormValueForElementNameImpl,
142 request));
143 }
144
145 void WebDataService::AddAutoFillProfile(const AutoFillProfile& profile) {
146 GenericRequest<AutoFillProfile>* request =
147 new GenericRequest<AutoFillProfile>(
148 this, GetNextRequestHandle(), NULL, profile);
149 RegisterRequest(request);
150 ScheduleTask(NewRunnableMethod(this,
151 &WebDataService::AddAutoFillProfileImpl,
152 request));
153 }
154
155 void WebDataService::UpdateAutoFillProfile(const AutoFillProfile& profile) {
156 GenericRequest<AutoFillProfile>* request =
157 new GenericRequest<AutoFillProfile>(
158 this, GetNextRequestHandle(), NULL, profile);
159 RegisterRequest(request);
160 ScheduleTask(NewRunnableMethod(this,
161 &WebDataService::UpdateAutoFillProfileImpl,
162 request));
163 }
164
165 void WebDataService::RemoveAutoFillProfile(int profile_id) {
166 GenericRequest<int>* request =
167 new GenericRequest<int>(
168 this, GetNextRequestHandle(), NULL, profile_id);
169 RegisterRequest(request);
170 ScheduleTask(NewRunnableMethod(this,
171 &WebDataService::RemoveAutoFillProfileImpl,
172 request));
173 }
174
175 WebDataService::Handle WebDataService::GetAutoFillProfiles(
176 WebDataServiceConsumer* consumer) {
177 WebDataRequest* request =
178 new WebDataRequest(this, GetNextRequestHandle(), consumer);
179 RegisterRequest(request);
180 ScheduleTask(
181 NewRunnableMethod(this,
182 &WebDataService::GetAutoFillProfilesImpl,
183 request));
184 return request->GetHandle();
185 }
186
187 void WebDataService::AddCreditCard(const CreditCard& creditcard) {
188 GenericRequest<CreditCard>* request =
189 new GenericRequest<CreditCard>(
190 this, GetNextRequestHandle(), NULL, creditcard);
191 RegisterRequest(request);
192 ScheduleTask(NewRunnableMethod(this,
193 &WebDataService::AddCreditCardImpl,
194 request));
195 }
196
197 void WebDataService::UpdateCreditCard(const CreditCard& creditcard) {
198 GenericRequest<CreditCard>* request =
199 new GenericRequest<CreditCard>(
200 this, GetNextRequestHandle(), NULL, creditcard);
201 RegisterRequest(request);
202 ScheduleTask(NewRunnableMethod(this,
203 &WebDataService::UpdateCreditCardImpl,
204 request));
205 }
206
207 void WebDataService::RemoveCreditCard(int creditcard_id) {
208 GenericRequest<int>* request =
209 new GenericRequest<int>(
210 this, GetNextRequestHandle(), NULL, creditcard_id);
211 RegisterRequest(request);
212 ScheduleTask(NewRunnableMethod(this,
213 &WebDataService::RemoveCreditCardImpl,
214 request));
215 }
216
217 WebDataService::Handle WebDataService::GetCreditCards(
218 WebDataServiceConsumer* consumer) {
219 WebDataRequest* request =
220 new WebDataRequest(this, GetNextRequestHandle(), consumer);
221 RegisterRequest(request);
222 ScheduleTask(
223 NewRunnableMethod(this,
224 &WebDataService::GetCreditCardsImpl,
225 request));
226 return request->GetHandle();
227 }
228
229 bool WebDataService::IsDatabaseLoaded() { 71 bool WebDataService::IsDatabaseLoaded() {
230 return db_ != NULL; 72 return db_ != NULL;
231 } 73 }
232 74
233 WebDatabase* WebDataService::GetDatabase() { 75 WebDatabase* WebDataService::GetDatabase() {
234 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB)); 76 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB));
235 return db_; 77 return db_;
236 } 78 }
237 79
238 void WebDataService::RequestCompleted(Handle h) {
239 pending_lock_.Acquire();
240 RequestMap::iterator i = pending_requests_.find(h);
241 if (i == pending_requests_.end()) {
242 NOTREACHED() << "Request completed called for an unknown request";
243 pending_lock_.Release();
244 return;
245 }
246
247 // Take ownership of the request object and remove it from the map.
248 scoped_ptr<WebDataRequest> request(i->second);
249 pending_requests_.erase(i);
250 pending_lock_.Release();
251
252 // Notify the consumer if needed.
253 WebDataServiceConsumer* consumer;
254 if (!request->IsCancelled() && (consumer = request->GetConsumer())) {
255 consumer->OnWebDataServiceRequestDone(request->GetHandle(),
256 request->GetResult());
257 } else {
258 // Nobody is taken ownership of the result, either because it is canceled
259 // or there is no consumer. Destroy results that require special handling.
260 WDTypedResult const *result = request->GetResult();
261 if (result) {
262 if (result->GetType() == AUTOFILL_PROFILES_RESULT) {
263 const WDResult<std::vector<AutoFillProfile*> >* r =
264 static_cast<const WDResult<std::vector<AutoFillProfile*> >*>(
265 result);
266 std::vector<AutoFillProfile*> profiles = r->GetValue();
267 STLDeleteElements(&profiles);
268 } else if (result->GetType() == AUTOFILL_CREDITCARDS_RESULT) {
269 const WDResult<std::vector<CreditCard*> >* r =
270 static_cast<const WDResult<std::vector<CreditCard*> >*>(result);
271
272 std::vector<CreditCard*> credit_cards = r->GetValue();
273 STLDeleteElements(&credit_cards);
274 }
275 }
276 }
277 }
278
279 ////////////////////////////////////////////////////////////////////////////// 80 //////////////////////////////////////////////////////////////////////////////
280 // 81 //
281 // Keywords. 82 // Keywords.
282 // 83 //
283 ////////////////////////////////////////////////////////////////////////////// 84 //////////////////////////////////////////////////////////////////////////////
284 85
285 void WebDataService::AddKeyword(const TemplateURL& url) { 86 void WebDataService::AddKeyword(const TemplateURL& url) {
286 GenericRequest<TemplateURL>* request = 87 GenericRequest<TemplateURL>* request =
287 new GenericRequest<TemplateURL>(this, GetNextRequestHandle(), NULL, url); 88 new GenericRequest<TemplateURL>(this, GetNextRequestHandle(), NULL, url);
288 RegisterRequest(request); 89 RegisterRequest(request);
(...skipping 140 matching lines...)
429 delete_end); 230 delete_end);
430 RegisterRequest(request); 231 RegisterRequest(request);
431 ScheduleTask(NewRunnableMethod(this, 232 ScheduleTask(NewRunnableMethod(this,
432 &WebDataService::RemoveLoginsCreatedBetweenImpl, request)); 233 &WebDataService::RemoveLoginsCreatedBetweenImpl, request));
433 } 234 }
434 235
435 void WebDataService::RemoveLoginsCreatedAfter(const Time& delete_begin) { 236 void WebDataService::RemoveLoginsCreatedAfter(const Time& delete_begin) {
436 RemoveLoginsCreatedBetween(delete_begin, Time()); 237 RemoveLoginsCreatedBetween(delete_begin, Time());
437 } 238 }
438 239
439 void WebDataService::RemoveFormElementsAddedBetween(const Time& delete_begin,
440 const Time& delete_end) {
441 GenericRequest2<Time, Time>* request =
442 new GenericRequest2<Time, Time>(this,
443 GetNextRequestHandle(),
444 NULL,
445 delete_begin,
446 delete_end);
447 RegisterRequest(request);
448 ScheduleTask(NewRunnableMethod(this,
449 &WebDataService::RemoveFormElementsAddedBetweenImpl, request));
450 }
451
452 WebDataService::Handle WebDataService::GetLogins( 240 WebDataService::Handle WebDataService::GetLogins(
453 const PasswordForm& form, 241 const PasswordForm& form,
454 WebDataServiceConsumer* consumer) { 242 WebDataServiceConsumer* consumer) {
455 GenericRequest<PasswordForm>* request = 243 GenericRequest<PasswordForm>* request =
456 new GenericRequest<PasswordForm>(this, GetNextRequestHandle(), 244 new GenericRequest<PasswordForm>(this, GetNextRequestHandle(),
457 consumer, form); 245 consumer, form);
458 RegisterRequest(request); 246 RegisterRequest(request);
459 ScheduleTask(NewRunnableMethod(this, &WebDataService::GetLoginsImpl, 247 ScheduleTask(NewRunnableMethod(this, &WebDataService::GetLoginsImpl,
460 request)); 248 request));
461 return request->GetHandle(); 249 return request->GetHandle();
(...skipping 14 matching lines...)
476 WebDataServiceConsumer* consumer) { 264 WebDataServiceConsumer* consumer) {
477 WebDataRequest* request = 265 WebDataRequest* request =
478 new WebDataRequest(this, GetNextRequestHandle(), consumer); 266 new WebDataRequest(this, GetNextRequestHandle(), consumer);
479 RegisterRequest(request); 267 RegisterRequest(request);
480 ScheduleTask(NewRunnableMethod(this, 268 ScheduleTask(NewRunnableMethod(this,
481 &WebDataService::GetBlacklistLoginsImpl, 269 &WebDataService::GetBlacklistLoginsImpl,
482 request)); 270 request));
483 return request->GetHandle(); 271 return request->GetHandle();
484 } 272 }
485 273
274 ////////////////////////////////////////////////////////////////////////////////
275 //
276 // AutoFill.
277 //
278 ////////////////////////////////////////////////////////////////////////////////
279
280 void WebDataService::AddFormFields(
281 const std::vector<FormField>& fields) {
282 GenericRequest<std::vector<FormField> >* request =
283 new GenericRequest<std::vector<FormField> >(
284 this, GetNextRequestHandle(), NULL, fields);
285 RegisterRequest(request);
286 ScheduleTask(NewRunnableMethod(this,
287 &WebDataService::AddFormElementsImpl,
288 request));
289 }
290
291 WebDataService::Handle WebDataService::GetFormValuesForElementName(
292 const string16& name, const string16& prefix, int limit,
293 WebDataServiceConsumer* consumer) {
294 WebDataRequest* request =
295 new WebDataRequest(this, GetNextRequestHandle(), consumer);
296 RegisterRequest(request);
297 ScheduleTask(
298 NewRunnableMethod(this,
299 &WebDataService::GetFormValuesForElementNameImpl,
300 request,
301 name,
302 prefix,
303 limit));
304 return request->GetHandle();
305 }
306
307 void WebDataService::RemoveFormElementsAddedBetween(const Time& delete_begin,
308 const Time& delete_end) {
309 GenericRequest2<Time, Time>* request =
310 new GenericRequest2<Time, Time>(this,
311 GetNextRequestHandle(),
312 NULL,
313 delete_begin,
314 delete_end);
315 RegisterRequest(request);
316 ScheduleTask(NewRunnableMethod(this,
317 &WebDataService::RemoveFormElementsAddedBetweenImpl, request));
318 }
319
320 void WebDataService::RemoveFormValueForElementName(
321 const string16& name, const string16& value) {
322 GenericRequest2<string16, string16>* request =
323 new GenericRequest2<string16, string16>(this,
324 GetNextRequestHandle(),
325 NULL,
326 name, value);
327 RegisterRequest(request);
328 ScheduleTask(
329 NewRunnableMethod(this,
330 &WebDataService::RemoveFormValueForElementNameImpl,
331 request));
332 }
333
334 void WebDataService::AddAutoFillProfile(const AutoFillProfile& profile) {
335 GenericRequest<AutoFillProfile>* request =
336 new GenericRequest<AutoFillProfile>(
337 this, GetNextRequestHandle(), NULL, profile);
338 RegisterRequest(request);
339 ScheduleTask(NewRunnableMethod(this,
340 &WebDataService::AddAutoFillProfileImpl,
341 request));
342 }
343
344 void WebDataService::UpdateAutoFillProfile(const AutoFillProfile& profile) {
345 GenericRequest<AutoFillProfile>* request =
346 new GenericRequest<AutoFillProfile>(
347 this, GetNextRequestHandle(), NULL, profile);
348 RegisterRequest(request);
349 ScheduleTask(NewRunnableMethod(this,
350 &WebDataService::UpdateAutoFillProfileImpl,
351 request));
352 }
353
354 void WebDataService::RemoveAutoFillProfile(int profile_id) {
355 GenericRequest<int>* request =
356 new GenericRequest<int>(
357 this, GetNextRequestHandle(), NULL, profile_id);
358 RegisterRequest(request);
359 ScheduleTask(NewRunnableMethod(this,
360 &WebDataService::RemoveAutoFillProfileImpl,
361 request));
362 }
363
364 WebDataService::Handle WebDataService::GetAutoFillProfiles(
365 WebDataServiceConsumer* consumer) {
366 WebDataRequest* request =
367 new WebDataRequest(this, GetNextRequestHandle(), consumer);
368 RegisterRequest(request);
369 ScheduleTask(
370 NewRunnableMethod(this,
371 &WebDataService::GetAutoFillProfilesImpl,
372 request));
373 return request->GetHandle();
374 }
375
376 void WebDataService::AddCreditCard(const CreditCard& creditcard) {
377 GenericRequest<CreditCard>* request =
378 new GenericRequest<CreditCard>(
379 this, GetNextRequestHandle(), NULL, creditcard);
380 RegisterRequest(request);
381 ScheduleTask(NewRunnableMethod(this,
382 &WebDataService::AddCreditCardImpl,
383 request));
384 }
385
386 void WebDataService::UpdateCreditCard(const CreditCard& creditcard) {
387 GenericRequest<CreditCard>* request =
388 new GenericRequest<CreditCard>(
389 this, GetNextRequestHandle(), NULL, creditcard);
390 RegisterRequest(request);
391 ScheduleTask(NewRunnableMethod(this,
392 &WebDataService::UpdateCreditCardImpl,
393 request));
394 }
395
396 void WebDataService::RemoveCreditCard(int creditcard_id) {
397 GenericRequest<int>* request =
398 new GenericRequest<int>(
399 this, GetNextRequestHandle(), NULL, creditcard_id);
400 RegisterRequest(request);
401 ScheduleTask(NewRunnableMethod(this,
402 &WebDataService::RemoveCreditCardImpl,
403 request));
404 }
405
406 WebDataService::Handle WebDataService::GetCreditCards(
407 WebDataServiceConsumer* consumer) {
408 WebDataRequest* request =
409 new WebDataRequest(this, GetNextRequestHandle(), consumer);
410 RegisterRequest(request);
411 ScheduleTask(
412 NewRunnableMethod(this,
413 &WebDataService::GetCreditCardsImpl,
414 request));
415 return request->GetHandle();
416 }
417
418 WebDataService::~WebDataService() {
419 if (is_running_ && db_) {
420 DLOG_ASSERT("WebDataService dtor called without Shutdown");
421 }
422 }
423
424 bool WebDataService::InitWithPath(const FilePath& path) {
425 path_ = path;
426 is_running_ = true;
427 ScheduleTask(NewRunnableMethod(this,
428 &WebDataService::InitializeDatabaseIfNecessary));
429 return true;
430 }
431
432 void WebDataService::RequestCompleted(Handle h) {
433 pending_lock_.Acquire();
434 RequestMap::iterator i = pending_requests_.find(h);
435 if (i == pending_requests_.end()) {
436 NOTREACHED() << "Request completed called for an unknown request";
437 pending_lock_.Release();
438 return;
439 }
440
441 // Take ownership of the request object and remove it from the map.
442 scoped_ptr<WebDataRequest> request(i->second);
443 pending_requests_.erase(i);
444 pending_lock_.Release();
445
446 // Notify the consumer if needed.
447 WebDataServiceConsumer* consumer;
448 if (!request->IsCancelled() && (consumer = request->GetConsumer())) {
449 consumer->OnWebDataServiceRequestDone(request->GetHandle(),
450 request->GetResult());
451 } else {
452 // Nobody is taken ownership of the result, either because it is canceled
453 // or there is no consumer. Destroy results that require special handling.
454 WDTypedResult const *result = request->GetResult();
455 if (result) {
456 if (result->GetType() == AUTOFILL_PROFILES_RESULT) {
457 const WDResult<std::vector<AutoFillProfile*> >* r =
458 static_cast<const WDResult<std::vector<AutoFillProfile*> >*>(
459 result);
460 std::vector<AutoFillProfile*> profiles = r->GetValue();
461 STLDeleteElements(&profiles);
462 } else if (result->GetType() == AUTOFILL_CREDITCARDS_RESULT) {
463 const WDResult<std::vector<CreditCard*> >* r =
464 static_cast<const WDResult<std::vector<CreditCard*> >*>(result);
465
466 std::vector<CreditCard*> credit_cards = r->GetValue();
467 STLDeleteElements(&credit_cards);
468 }
469 }
470 }
471 }
472
473 void WebDataService::RegisterRequest(WebDataRequest* request) {
474 AutoLock l(pending_lock_);
475 pending_requests_[request->GetHandle()] = request;
476 }
477
478 ////////////////////////////////////////////////////////////////////////////////
479 //
480 // The following methods are executed in Chrome_WebDataThread.
481 //
482 ////////////////////////////////////////////////////////////////////////////////
483
486 void WebDataService::DBInitFailed(sql::InitStatus init_status) { 484 void WebDataService::DBInitFailed(sql::InitStatus init_status) {
487 Source<WebDataService> source(this); 485 Source<WebDataService> source(this);
488 int message_id = (init_status == sql::INIT_FAILURE) ? 486 int message_id = (init_status == sql::INIT_FAILURE) ?
489 IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR; 487 IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR;
490 NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, 488 NotificationService::current()->Notify(NotificationType::PROFILE_ERROR,
491 source, Details<int>(&message_id)); 489 source, Details<int>(&message_id));
492 } 490 }
493 491
494 ////////////////////////////////////////////////////////////////////////////////
495 //
496 // The following methods are executed in Chrome_WebDataThread.
497 //
498 ////////////////////////////////////////////////////////////////////////////////
499
500 void WebDataService::Commit() {
501 if (should_commit_) {
502 should_commit_ = false;
503
504 if (db_) {
505 db_->CommitTransaction();
506 db_->BeginTransaction();
507 }
508 }
509 }
510
511 void WebDataService::InitializeDatabaseIfNecessary() { 492 void WebDataService::InitializeDatabaseIfNecessary() {
512 if (db_ || failed_init_ || path_.empty()) 493 if (db_ || failed_init_ || path_.empty())
513 return; 494 return;
514 495
515 // In the rare case where the db fails to initialize a dialog may get shown 496 // In the rare case where the db fails to initialize a dialog may get shown
516 // the blocks the caller, yet allows other messages through. For this reason 497 // the blocks the caller, yet allows other messages through. For this reason
517 // we only set db_ to the created database if creation is successful. That 498 // we only set db_ to the created database if creation is successful. That
518 // way other methods won't do anything as db_ is still NULL. 499 // way other methods won't do anything as db_ is still NULL.
519 WebDatabase* db = new WebDatabase(); 500 WebDatabase* db = new WebDatabase();
520 sql::InitStatus init_status = db->Init(path_); 501 sql::InitStatus init_status = db->Init(path_);
(...skipping 26 matching lines...)
547 void WebDataService::ShutdownDatabase() { 528 void WebDataService::ShutdownDatabase() {
548 should_commit_ = false; 529 should_commit_ = false;
549 530
550 if (db_) { 531 if (db_) {
551 db_->CommitTransaction(); 532 db_->CommitTransaction();
552 delete db_; 533 delete db_;
553 db_ = NULL; 534 db_ = NULL;
554 } 535 }
555 } 536 }
556 537
538 void WebDataService::Commit() {
539 if (should_commit_) {
540 should_commit_ = false;
541
542 if (db_) {
543 db_->CommitTransaction();
544 db_->BeginTransaction();
545 }
546 }
547 }
548
549 void WebDataService::ScheduleTask(Task* t) {
550 if (is_running_)
551 ChromeThread::PostTask(ChromeThread::DB, FROM_HERE, t);
552 else
553 NOTREACHED() << "Task scheduled after Shutdown()";
554 }
555
556 void WebDataService::ScheduleCommit() {
557 if (should_commit_ == false) {
558 should_commit_ = true;
559 ScheduleTask(NewRunnableMethod(this, &WebDataService::Commit));
560 }
561 }
562
563 int WebDataService::GetNextRequestHandle() {
564 AutoLock l(pending_lock_);
565 return ++next_request_handle_;
566 }
567
568 ////////////////////////////////////////////////////////////////////////////////
557 // 569 //
558 // Keywords. 570 // Keywords implementation.
559 // 571 //
572 ////////////////////////////////////////////////////////////////////////////////
573
560 void WebDataService::AddKeywordImpl(GenericRequest<TemplateURL>* request) { 574 void WebDataService::AddKeywordImpl(GenericRequest<TemplateURL>* request) {
561 InitializeDatabaseIfNecessary(); 575 InitializeDatabaseIfNecessary();
562 if (db_ && !request->IsCancelled()) { 576 if (db_ && !request->IsCancelled()) {
563 db_->AddKeyword(request->GetArgument()); 577 db_->AddKeyword(request->GetArgument());
564 ScheduleCommit(); 578 ScheduleCommit();
565 } 579 }
566 request->RequestComplete(); 580 request->RequestComplete();
567 } 581 }
568 582
569 void WebDataService::RemoveKeywordImpl( 583 void WebDataService::RemoveKeywordImpl(
(...skipping 45 matching lines...)
615 GenericRequest<int>* request) { 629 GenericRequest<int>* request) {
616 InitializeDatabaseIfNecessary(); 630 InitializeDatabaseIfNecessary();
617 if (db_ && !request->IsCancelled()) { 631 if (db_ && !request->IsCancelled()) {
618 if (!db_->SetBuitinKeywordVersion(request->GetArgument())) 632 if (!db_->SetBuitinKeywordVersion(request->GetArgument()))
619 NOTREACHED(); 633 NOTREACHED();
620 ScheduleCommit(); 634 ScheduleCommit();
621 } 635 }
622 request->RequestComplete(); 636 request->RequestComplete();
623 } 637 }
624 638
639 ////////////////////////////////////////////////////////////////////////////////
625 // 640 //
626 // Password manager support. 641 // Web Apps implementation.
627 // 642 //
643 ////////////////////////////////////////////////////////////////////////////////
644
645 void WebDataService::SetWebAppImageImpl(
646 GenericRequest2<GURL, SkBitmap>* request) {
647 InitializeDatabaseIfNecessary();
648 if (db_ && !request->IsCancelled()) {
649 db_->SetWebAppImage(request->GetArgument1(), request->GetArgument2());
650 ScheduleCommit();
651 }
652 request->RequestComplete();
653 }
654
655 void WebDataService::SetWebAppHasAllImagesImpl(
656 GenericRequest2<GURL, bool>* request) {
657 InitializeDatabaseIfNecessary();
658 if (db_ && !request->IsCancelled()) {
659 db_->SetWebAppHasAllImages(request->GetArgument1(),
660 request->GetArgument2());
661 ScheduleCommit();
662 }
663 request->RequestComplete();
664 }
665
666 void WebDataService::RemoveWebAppImpl(GenericRequest<GURL>* request) {
667 InitializeDatabaseIfNecessary();
668 if (db_ && !request->IsCancelled()) {
669 db_->RemoveWebApp(request->GetArgument());
670 ScheduleCommit();
671 }
672 request->RequestComplete();
673 }
674
675 void WebDataService::GetWebAppImagesImpl(GenericRequest<GURL>* request) {
676 InitializeDatabaseIfNecessary();
677 if (db_ && !request->IsCancelled()) {
678 WDAppImagesResult result;
679 result.has_all_images = db_->GetWebAppHasAllImages(request->GetArgument());
680 db_->GetWebAppImages(request->GetArgument(), &result.images);
681 request->SetResult(
682 new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result));
683 }
684 request->RequestComplete();
685 }
686
687 ////////////////////////////////////////////////////////////////////////////////
688 //
689 // Password manager implementation.
690 //
691 ////////////////////////////////////////////////////////////////////////////////
692
628 void WebDataService::AddLoginImpl(GenericRequest<PasswordForm>* request) { 693 void WebDataService::AddLoginImpl(GenericRequest<PasswordForm>* request) {
629 InitializeDatabaseIfNecessary(); 694 InitializeDatabaseIfNecessary();
630 if (db_ && !request->IsCancelled()) { 695 if (db_ && !request->IsCancelled()) {
631 if (db_->AddLogin(request->GetArgument())) 696 if (db_->AddLogin(request->GetArgument()))
632 ScheduleCommit(); 697 ScheduleCommit();
633 } 698 }
634 request->RequestComplete(); 699 request->RequestComplete();
635 } 700 }
636 701
637 void WebDataService::UpdateLoginImpl(GenericRequest<PasswordForm>* request) { 702 void WebDataService::UpdateLoginImpl(GenericRequest<PasswordForm>* request) {
(...skipping 63 matching lines...)
701 all_forms.clear(); 766 all_forms.clear();
702 request->SetResult( 767 request->SetResult(
703 new WDResult<std::vector<PasswordForm*> >(PASSWORD_RESULT, 768 new WDResult<std::vector<PasswordForm*> >(PASSWORD_RESULT,
704 blacklist_forms)); 769 blacklist_forms));
705 } 770 }
706 request->RequestComplete(); 771 request->RequestComplete();
707 } 772 }
708 773
709 //////////////////////////////////////////////////////////////////////////////// 774 ////////////////////////////////////////////////////////////////////////////////
710 // 775 //
711 // Autofill support. 776 // AutoFill implementation.
712 // 777 //
713 //////////////////////////////////////////////////////////////////////////////// 778 ////////////////////////////////////////////////////////////////////////////////
714 779
715 void WebDataService::AddFormFieldValuesImpl( 780 void WebDataService::AddFormElementsImpl(
716 GenericRequest<std::vector<FormField> >* request) { 781 GenericRequest<std::vector<FormField> >* request) {
717 InitializeDatabaseIfNecessary(); 782 InitializeDatabaseIfNecessary();
718 const std::vector<FormField>& form_fields = request->GetArgument(); 783 const std::vector<FormField>& form_fields = request->GetArgument();
719 if (db_ && !request->IsCancelled()) { 784 if (db_ && !request->IsCancelled()) {
720 AutofillChangeList changes; 785 AutofillChangeList changes;
721 if (!db_->AddFormFieldValues(form_fields, &changes)) 786 if (!db_->AddFormFieldValues(form_fields, &changes))
722 NOTREACHED(); 787 NOTREACHED();
723 request->SetResult( 788 request->SetResult(
724 new WDResult<AutofillChangeList>(AUTOFILL_CHANGES, changes)); 789 new WDResult<AutofillChangeList>(AUTOFILL_CHANGES, changes));
725 ScheduleCommit(); 790 ScheduleCommit();
(...skipping 232 matching lines...)
958 db_->GetCreditCards(&creditcards); 1023 db_->GetCreditCards(&creditcards);
959 request->SetResult( 1024 request->SetResult(
960 new WDResult<std::vector<CreditCard*> >(AUTOFILL_CREDITCARDS_RESULT, 1025 new WDResult<std::vector<CreditCard*> >(AUTOFILL_CREDITCARDS_RESULT,
961 creditcards)); 1026 creditcards));
962 } 1027 }
963 request->RequestComplete(); 1028 request->RequestComplete();
964 } 1029 }
965 1030
966 //////////////////////////////////////////////////////////////////////////////// 1031 ////////////////////////////////////////////////////////////////////////////////
967 // 1032 //
968 // Web Apps implementation.
969 //
970 ////////////////////////////////////////////////////////////////////////////////
971
972 void WebDataService::SetWebAppImageImpl(
973 GenericRequest2<GURL, SkBitmap>* request) {
974 InitializeDatabaseIfNecessary();
975 if (db_ && !request->IsCancelled()) {
976 db_->SetWebAppImage(request->GetArgument1(), request->GetArgument2());
977 ScheduleCommit();
978 }
979 request->RequestComplete();
980 }
981
982 void WebDataService::SetWebAppHasAllImagesImpl(
983 GenericRequest2<GURL, bool>* request) {
984 InitializeDatabaseIfNecessary();
985 if (db_ && !request->IsCancelled()) {
986 db_->SetWebAppHasAllImages(request->GetArgument1(),
987 request->GetArgument2());
988 ScheduleCommit();
989 }
990 request->RequestComplete();
991 }
992
993 void WebDataService::RemoveWebAppImpl(GenericRequest<GURL>* request) {
994 InitializeDatabaseIfNecessary();
995 if (db_ && !request->IsCancelled()) {
996 db_->RemoveWebApp(request->GetArgument());
997 ScheduleCommit();
998 }
999 request->RequestComplete();
1000 }
1001
1002 void WebDataService::GetWebAppImagesImpl(GenericRequest<GURL>* request) {
1003 InitializeDatabaseIfNecessary();
1004 if (db_ && !request->IsCancelled()) {
1005 WDAppImagesResult result;
1006 result.has_all_images = db_->GetWebAppHasAllImages(request->GetArgument());
1007 db_->GetWebAppImages(request->GetArgument(), &result.images);
1008 request->SetResult(
1009 new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result));
1010 }
1011 request->RequestComplete();
1012 }
1013
1014 ////////////////////////////////////////////////////////////////////////////////
1015 //
1016 // WebDataRequest implementation. 1033 // WebDataRequest implementation.
1017 // 1034 //
1018 //////////////////////////////////////////////////////////////////////////////// 1035 ////////////////////////////////////////////////////////////////////////////////
1019 1036
1020 WebDataService::WebDataRequest::WebDataRequest(WebDataService* service, 1037 WebDataService::WebDataRequest::WebDataRequest(WebDataService* service,
1021 Handle handle, 1038 Handle handle,
1022 WebDataServiceConsumer* consumer) 1039 WebDataServiceConsumer* consumer)
1023 : service_(service), 1040 : service_(service),
1024 handle_(handle), 1041 handle_(handle),
1025 canceled_(false), 1042 canceled_(false),
(...skipping 31 matching lines...)
1057 return result_; 1074 return result_;
1058 } 1075 }
1059 1076
1060 void WebDataService::WebDataRequest::RequestComplete() { 1077 void WebDataService::WebDataRequest::RequestComplete() {
1061 WebDataService* s = service_; 1078 WebDataService* s = service_;
1062 Task* t = NewRunnableMethod(s, 1079 Task* t = NewRunnableMethod(s,
1063 &WebDataService::RequestCompleted, 1080 &WebDataService::RequestCompleted,
1064 handle_); 1081 handle_);
1065 message_loop_->PostTask(FROM_HERE, t); 1082 message_loop_->PostTask(FROM_HERE, t);
1066 } 1083 }
1067
1068 int WebDataService::GetNextRequestHandle() {
1069 AutoLock l(pending_lock_);
1070 return ++next_request_handle_;
1071 }
OLDNEW
« no previous file with comments | « chrome/browser/webdata/web_data_service.h ('k') | chrome/browser/webdata/web_data_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine