aw_form_database_service.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "android_webview/browser/aw_form_database_service.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/logging.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "base/task/thread_pool.h"
  10. #include "base/threading/thread_restrictions.h"
  11. #include "base/threading/thread_task_runner_handle.h"
  12. #include "base/time/time.h"
  13. #include "components/autofill/core/browser/webdata/autofill_table.h"
  14. #include "components/webdata/common/webdata_constants.h"
  15. using base::WaitableEvent;
  16. namespace {
  17. // Callback to handle database error. It seems chrome uses this to
  18. // display an error dialog box only.
  19. void DatabaseErrorCallback(sql::InitStatus init_status,
  20. const std::string& diagnostics) {
  21. LOG(WARNING) << "initializing autocomplete database failed";
  22. }
  23. } // namespace
  24. namespace android_webview {
  25. AwFormDatabaseService::AwFormDatabaseService(const base::FilePath path)
  26. : has_form_data_result_(false),
  27. has_form_data_completion_(
  28. base::WaitableEvent::ResetPolicy::AUTOMATIC,
  29. base::WaitableEvent::InitialState::NOT_SIGNALED) {
  30. auto ui_task_runner = base::ThreadTaskRunnerHandle::Get();
  31. // TODO(pkasting): http://crbug.com/740773 This should likely be sequenced,
  32. // not single-threaded; it's also possible these objects can each use their
  33. // own sequences instead of sharing this one.
  34. auto db_task_runner = base::ThreadPool::CreateSingleThreadTaskRunner(
  35. {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
  36. base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
  37. web_database_ = new WebDatabaseService(path.Append(kWebDataFilename),
  38. ui_task_runner, db_task_runner);
  39. web_database_->AddTable(base::WrapUnique(new autofill::AutofillTable));
  40. web_database_->LoadDatabase();
  41. autofill_data_ = new autofill::AutofillWebDataService(
  42. web_database_, ui_task_runner, db_task_runner);
  43. autofill_data_->Init(base::BindOnce(&DatabaseErrorCallback));
  44. }
  45. AwFormDatabaseService::~AwFormDatabaseService() {
  46. Shutdown();
  47. }
  48. void AwFormDatabaseService::Shutdown() {
  49. // TODO(sgurun) we don't run into this logic right now, but if we do, then we
  50. // need to implement cancellation of pending queries.
  51. autofill_data_->ShutdownOnUISequence();
  52. web_database_->ShutdownDatabase();
  53. }
  54. scoped_refptr<autofill::AutofillWebDataService>
  55. AwFormDatabaseService::get_autofill_webdata_service() {
  56. return autofill_data_;
  57. }
  58. void AwFormDatabaseService::ClearFormData() {
  59. base::Time begin;
  60. base::Time end = base::Time::Max();
  61. autofill_data_->RemoveFormElementsAddedBetween(begin, end);
  62. autofill_data_->RemoveAutofillDataModifiedBetween(begin, end);
  63. }
  64. bool AwFormDatabaseService::HasFormData() {
  65. has_form_data_result_ = false;
  66. has_form_data_completion_.Reset();
  67. using awds = autofill::AutofillWebDataService;
  68. base::ThreadPool::PostTask(
  69. FROM_HERE,
  70. base::BindOnce(
  71. base::IgnoreResult(&awds::GetCountOfValuesContainedBetween),
  72. autofill_data_, base::Time(), base::Time::Max(), this));
  73. {
  74. base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_wait;
  75. has_form_data_completion_.Wait();
  76. }
  77. return has_form_data_result_;
  78. }
  79. void AwFormDatabaseService::OnWebDataServiceRequestDone(
  80. WebDataServiceBase::Handle h,
  81. std::unique_ptr<WDTypedResult> result) {
  82. if (result) {
  83. DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType());
  84. const WDResult<int>* autofill_result =
  85. static_cast<const WDResult<int>*>(result.get());
  86. has_form_data_result_ = autofill_result->GetValue() > 0;
  87. }
  88. has_form_data_completion_.Signal();
  89. }
  90. } // namespace android_webview