Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / android_webview / browser / aw_form_database_service.cc
blobc8b98bbcf3c0cc87ad752f7af5fd694bf3e6b6ef
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.
5 #include "android_webview/browser/aw_form_database_service.h"
7 #include "base/logging.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "components/autofill/core/browser/webdata/autofill_table.h"
10 #include "components/webdata/common/webdata_constants.h"
11 #include "content/public/browser/browser_thread.h"
13 using base::WaitableEvent;
14 using content::BrowserThread;
16 namespace {
18 // Callback to handle database error. It seems chrome uses this to
19 // display an error dialog box only.
20 void DatabaseErrorCallback(sql::InitStatus status) {
21 LOG(WARNING) << "initializing autocomplete database failed";
24 } // namespace
26 namespace android_webview {
28 AwFormDatabaseService::AwFormDatabaseService(const base::FilePath path) {
29 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
30 web_database_ = new WebDatabaseService(path.Append(kWebDataFilename),
31 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
32 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB));
33 web_database_->AddTable(make_scoped_ptr(new autofill::AutofillTable));
34 web_database_->LoadDatabase();
36 autofill_data_ = new autofill::AutofillWebDataService(
37 web_database_,
38 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
39 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
40 base::Bind(&DatabaseErrorCallback));
41 autofill_data_->Init();
44 AwFormDatabaseService::~AwFormDatabaseService() {
45 Shutdown();
48 void AwFormDatabaseService::Shutdown() {
49 DCHECK_CURRENTLY_ON(BrowserThread::UI);
50 DCHECK(result_map_.empty());
51 // TODO(sgurun) we don't run into this logic right now,
52 // but if we do, then we need to implement cancellation
53 // of pending queries.
54 autofill_data_->ShutdownOnUIThread();
55 web_database_->ShutdownDatabase();
58 scoped_refptr<autofill::AutofillWebDataService>
59 AwFormDatabaseService::get_autofill_webdata_service() {
60 return autofill_data_;
63 void AwFormDatabaseService::ClearFormData() {
64 BrowserThread::PostTask(
65 BrowserThread::DB,
66 FROM_HERE,
67 base::Bind(&AwFormDatabaseService::ClearFormDataImpl,
68 base::Unretained(this)));
71 void AwFormDatabaseService::ClearFormDataImpl() {
72 base::Time begin;
73 base::Time end = base::Time::Max();
74 autofill_data_->RemoveFormElementsAddedBetween(begin, end);
75 autofill_data_->RemoveAutofillDataModifiedBetween(begin, end);
78 bool AwFormDatabaseService::HasFormData() {
79 WaitableEvent completion(false, false);
80 bool result = false;
81 BrowserThread::PostTask(
82 BrowserThread::DB,
83 FROM_HERE,
84 base::Bind(&AwFormDatabaseService::HasFormDataImpl,
85 base::Unretained(this),
86 &completion,
87 &result));
88 completion.Wait();
89 return result;
92 void AwFormDatabaseService::HasFormDataImpl(
93 WaitableEvent* completion,
94 bool* result) {
95 WebDataServiceBase::Handle pending_query_handle =
96 autofill_data_->HasFormElements(this);
97 PendingQuery query;
98 query.result = result;
99 query.completion = completion;
100 result_map_[pending_query_handle] = query;
103 void AwFormDatabaseService::OnWebDataServiceRequestDone(
104 WebDataServiceBase::Handle h,
105 const WDTypedResult* result) {
107 DCHECK_CURRENTLY_ON(BrowserThread::DB);
108 bool has_form_data = false;
109 if (result) {
110 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType());
111 const WDResult<bool>* autofill_result =
112 static_cast<const WDResult<bool>*>(result);
113 has_form_data = autofill_result->GetValue();
115 QueryMap::const_iterator it = result_map_.find(h);
116 if (it == result_map_.end()) {
117 LOG(WARNING) << "Received unexpected callback from web data service";
118 return;
120 *(it->second.result) = has_form_data;
121 it->second.completion->Signal();
122 result_map_.erase(h);
125 } // namespace android_webview