Commit 05788069 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Simplify AwFormDatabaseService interaction with AutofillWebDataService.

The AutofillWebDataService APIs can be consumed on any thread, not just the DB
thread, so it's not necessary to post to the DB thread to call them.

The AwFormDatabaseService itself is only accessed on one thread, and blocks
while it has an ongoing HasFormData() call, so there can't be multiple requests
in-flight at once.  This lets us remove the entire result map.

Bug: None
Change-Id: I7b6cd5272cb569d00fcd4cc6f89f22082100184c
Reviewed-on: https://chromium-review.googlesource.com/578298
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarSelim Gurun <sgurun@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488163}
parent 51a27f03
...@@ -4,9 +4,10 @@ ...@@ -4,9 +4,10 @@
#include "android_webview/browser/aw_form_database_service.h" #include "android_webview/browser/aw_form_database_service.h"
#include "base/bind_helpers.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/synchronization/waitable_event.h" #include "base/task_scheduler/post_task.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "components/autofill/core/browser/webdata/autofill_table.h" #include "components/autofill/core/browser/webdata/autofill_table.h"
#include "components/webdata/common/webdata_constants.h" #include "components/webdata/common/webdata_constants.h"
...@@ -28,7 +29,11 @@ void DatabaseErrorCallback(sql::InitStatus init_status, ...@@ -28,7 +29,11 @@ void DatabaseErrorCallback(sql::InitStatus init_status,
namespace android_webview { namespace android_webview {
AwFormDatabaseService::AwFormDatabaseService(const base::FilePath path) { AwFormDatabaseService::AwFormDatabaseService(const base::FilePath path)
: has_form_data_result_(false),
has_form_data_completion_(
base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
web_database_ = new WebDatabaseService( web_database_ = new WebDatabaseService(
path.Append(kWebDataFilename), path.Append(kWebDataFilename),
...@@ -50,7 +55,6 @@ AwFormDatabaseService::~AwFormDatabaseService() { ...@@ -50,7 +55,6 @@ AwFormDatabaseService::~AwFormDatabaseService() {
void AwFormDatabaseService::Shutdown() { void AwFormDatabaseService::Shutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(result_map_.empty());
// TODO(sgurun) we don't run into this logic right now, // TODO(sgurun) we don't run into this logic right now,
// but if we do, then we need to implement cancellation // but if we do, then we need to implement cancellation
// of pending queries. // of pending queries.
...@@ -64,14 +68,6 @@ AwFormDatabaseService::get_autofill_webdata_service() { ...@@ -64,14 +68,6 @@ AwFormDatabaseService::get_autofill_webdata_service() {
} }
void AwFormDatabaseService::ClearFormData() { void AwFormDatabaseService::ClearFormData() {
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(&AwFormDatabaseService::ClearFormDataImpl,
base::Unretained(this)));
}
void AwFormDatabaseService::ClearFormDataImpl() {
base::Time begin; base::Time begin;
base::Time end = base::Time::Max(); base::Time end = base::Time::Max();
autofill_data_->RemoveFormElementsAddedBetween(begin, end); autofill_data_->RemoveFormElementsAddedBetween(begin, end);
...@@ -79,57 +75,30 @@ void AwFormDatabaseService::ClearFormDataImpl() { ...@@ -79,57 +75,30 @@ void AwFormDatabaseService::ClearFormDataImpl() {
} }
bool AwFormDatabaseService::HasFormData() { bool AwFormDatabaseService::HasFormData() {
WaitableEvent completion(base::WaitableEvent::ResetPolicy::AUTOMATIC, has_form_data_result_ = false;
base::WaitableEvent::InitialState::NOT_SIGNALED); has_form_data_completion_.Reset();
bool result = false; using awds = autofill::AutofillWebDataService;
BrowserThread::PostTask( base::PostTask(
BrowserThread::DB,
FROM_HERE, FROM_HERE,
base::Bind(&AwFormDatabaseService::HasFormDataImpl, base::Bind(base::IgnoreResult(&awds::GetCountOfValuesContainedBetween),
base::Unretained(this), autofill_data_, base::Time(), base::Time::Max(), this));
&completion,
&result));
{ {
base::ThreadRestrictions::ScopedAllowWait wait; base::ThreadRestrictions::ScopedAllowWait wait;
completion.Wait(); has_form_data_completion_.Wait();
} }
return result; return has_form_data_result_;
}
void AwFormDatabaseService::HasFormDataImpl(
WaitableEvent* completion,
bool* result) {
WebDataServiceBase::Handle pending_query_handle =
autofill_data_->GetCountOfValuesContainedBetween(
base::Time(), base::Time::Max(), this);
PendingQuery query;
query.result = result;
query.completion = completion;
result_map_[pending_query_handle] = query;
} }
void AwFormDatabaseService::OnWebDataServiceRequestDone( void AwFormDatabaseService::OnWebDataServiceRequestDone(
WebDataServiceBase::Handle h, WebDataServiceBase::Handle h,
std::unique_ptr<WDTypedResult> result) { std::unique_ptr<WDTypedResult> result) {
DCHECK_CURRENTLY_ON(BrowserThread::DB);
bool has_form_data = false;
if (result) { if (result) {
DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType());
const WDResult<int>* autofill_result = const WDResult<int>* autofill_result =
static_cast<const WDResult<int>*>(result.get()); static_cast<const WDResult<int>*>(result.get());
has_form_data = autofill_result->GetValue() > 0; has_form_data_result_ = autofill_result->GetValue() > 0;
}
QueryMap::const_iterator it = result_map_.find(h);
if (it == result_map_.end()) {
LOG(WARNING) << "Received unexpected callback from web data service";
return;
} }
has_form_data_completion_.Signal();
WaitableEvent* completion = it->second.completion;
*(it->second.result) = has_form_data;
result_map_.erase(h);
completion->Signal();
} }
} // namespace android_webview } // namespace android_webview
...@@ -7,14 +7,11 @@ ...@@ -7,14 +7,11 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/synchronization/waitable_event.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h" #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/webdata/common/web_data_service_consumer.h" #include "components/webdata/common/web_data_service_consumer.h"
#include "components/webdata/common/web_database_service.h" #include "components/webdata/common/web_database_service.h"
namespace base {
class WaitableEvent;
};
namespace android_webview { namespace android_webview {
// Handles the database operations necessary to implement the autocomplete // Handles the database operations necessary to implement the autocomplete
...@@ -45,16 +42,8 @@ class AwFormDatabaseService : public WebDataServiceConsumer { ...@@ -45,16 +42,8 @@ class AwFormDatabaseService : public WebDataServiceConsumer {
std::unique_ptr<WDTypedResult> result) override; std::unique_ptr<WDTypedResult> result) override;
private: private:
struct PendingQuery { bool has_form_data_result_;
bool* result; base::WaitableEvent has_form_data_completion_;
base::WaitableEvent* completion;
};
typedef std::map<WebDataServiceBase::Handle, PendingQuery> QueryMap;
void ClearFormDataImpl();
void HasFormDataImpl(base::WaitableEvent* completion, bool* result);
QueryMap result_map_;
scoped_refptr<autofill::AutofillWebDataService> autofill_data_; scoped_refptr<autofill::AutofillWebDataService> autofill_data_;
scoped_refptr<WebDatabaseService> web_database_; scoped_refptr<WebDatabaseService> web_database_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment