| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "android_webview/browser/aw_form_database_service.h" | 5 #include "android_webview/browser/aw_form_database_service.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/synchronization/waitable_event.h" | |
| 8 #include "components/autofill/core/browser/webdata/autofill_table.h" | 7 #include "components/autofill/core/browser/webdata/autofill_table.h" |
| 9 #include "components/webdata/common/webdata_constants.h" | 8 #include "components/webdata/common/webdata_constants.h" |
| 10 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 11 #include "ui/base/l10n/l10n_util_android.h" | 10 #include "ui/base/l10n/l10n_util_android.h" |
| 12 | 11 |
| 13 using base::WaitableEvent; | |
| 14 using content::BrowserThread; | 12 using content::BrowserThread; |
| 15 | 13 |
| 16 namespace { | 14 namespace { |
| 17 | 15 |
| 18 // Callback to handle database error. It seems chrome uses this to | 16 // Callback to handle database error. It seems chrome uses this to |
| 19 // display an error dialog box only. | 17 // display an error dialog box only. |
| 20 void DatabaseErrorCallback(sql::InitStatus status) { | 18 void DatabaseErrorCallback(sql::InitStatus status) { |
| 21 LOG(WARNING) << "initializing autocomplete database failed"; | 19 LOG(WARNING) << "initializing autocomplete database failed"; |
| 22 } | 20 } |
| 23 | 21 |
| 24 } // namespace | 22 } // namespace |
| 25 | 23 |
| 26 namespace android_webview { | 24 namespace android_webview { |
| 27 | 25 |
| 28 AwFormDatabaseService::AwFormDatabaseService(const base::FilePath path) { | 26 AwFormDatabaseService::AwFormDatabaseService(const base::FilePath path) |
| 27 : pending_query_handle_(0), |
| 28 has_form_data_(false), |
| 29 completion_(false, false) { |
| 29 | 30 |
| 30 web_database_ = new WebDatabaseService(path.Append(kWebDataFilename), | 31 web_database_ = new WebDatabaseService(path.Append(kWebDataFilename), |
| 31 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | 32 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
| 32 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)); | 33 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)); |
| 33 web_database_->AddTable( | 34 web_database_->AddTable( |
| 34 scoped_ptr<WebDatabaseTable>(new autofill::AutofillTable( | 35 scoped_ptr<WebDatabaseTable>(new autofill::AutofillTable( |
| 35 l10n_util::GetDefaultLocale()))); | 36 l10n_util::GetDefaultLocale()))); |
| 36 web_database_->LoadDatabase(); | 37 web_database_->LoadDatabase(); |
| 37 | 38 |
| 38 autofill_data_ = new autofill::AutofillWebDataService( | 39 autofill_data_ = new autofill::AutofillWebDataService( |
| 39 web_database_, base::Bind(&DatabaseErrorCallback)); | 40 web_database_, base::Bind(&DatabaseErrorCallback)); |
| 40 autofill_data_->Init(); | 41 autofill_data_->Init(); |
| 41 } | 42 } |
| 42 | 43 |
| 43 AwFormDatabaseService::~AwFormDatabaseService() { | 44 AwFormDatabaseService::~AwFormDatabaseService() { |
| 45 CancelPendingQuery(); |
| 44 Shutdown(); | 46 Shutdown(); |
| 45 } | 47 } |
| 46 | 48 |
| 47 void AwFormDatabaseService::Shutdown() { | 49 void AwFormDatabaseService::Shutdown() { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 DCHECK(result_map_.empty()); | |
| 50 // TODO(sgurun) we don't run into this logic right now, | |
| 51 // but if we do, then we need to implement cancellation | |
| 52 // of pending queries. | |
| 53 autofill_data_->ShutdownOnUIThread(); | 51 autofill_data_->ShutdownOnUIThread(); |
| 54 web_database_->ShutdownDatabase(); | 52 web_database_->ShutdownDatabase(); |
| 55 } | 53 } |
| 56 | 54 |
| 55 void AwFormDatabaseService::CancelPendingQuery() { |
| 56 if (pending_query_handle_) { |
| 57 if (autofill_data_.get()) |
| 58 autofill_data_->CancelRequest(pending_query_handle_); |
| 59 pending_query_handle_ = 0; |
| 60 } |
| 61 } |
| 62 |
| 57 scoped_refptr<autofill::AutofillWebDataService> | 63 scoped_refptr<autofill::AutofillWebDataService> |
| 58 AwFormDatabaseService::get_autofill_webdata_service() { | 64 AwFormDatabaseService::get_autofill_webdata_service() { |
| 59 return autofill_data_; | 65 return autofill_data_; |
| 60 } | 66 } |
| 61 | 67 |
| 62 void AwFormDatabaseService::ClearFormData() { | 68 void AwFormDatabaseService::ClearFormData() { |
| 63 BrowserThread::PostTask( | |
| 64 BrowserThread::DB, | |
| 65 FROM_HERE, | |
| 66 base::Bind(&AwFormDatabaseService::ClearFormDataImpl, | |
| 67 base::Unretained(this))); | |
| 68 } | |
| 69 | |
| 70 void AwFormDatabaseService::ClearFormDataImpl() { | |
| 71 base::Time begin; | 69 base::Time begin; |
| 72 base::Time end = base::Time::Max(); | 70 base::Time end = base::Time::Max(); |
| 73 autofill_data_->RemoveFormElementsAddedBetween(begin, end); | 71 autofill_data_->RemoveFormElementsAddedBetween(begin, end); |
| 74 autofill_data_->RemoveAutofillDataModifiedBetween(begin, end); | 72 autofill_data_->RemoveAutofillDataModifiedBetween(begin, end); |
| 75 } | 73 } |
| 76 | 74 |
| 77 bool AwFormDatabaseService::HasFormData() { | 75 bool AwFormDatabaseService::HasFormData() { |
| 78 WaitableEvent completion(false, false); | 76 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 79 bool result = false; | |
| 80 BrowserThread::PostTask( | |
| 81 BrowserThread::DB, | |
| 82 FROM_HERE, | |
| 83 base::Bind(&AwFormDatabaseService::HasFormDataImpl, | 77 base::Bind(&AwFormDatabaseService::HasFormDataImpl, |
| 84 base::Unretained(this), | 78 base::Unretained(this))); |
| 85 &completion, | 79 completion_.Wait(); |
| 86 &result)); | 80 return has_form_data_; |
| 87 completion.Wait(); | |
| 88 return result; | |
| 89 } | 81 } |
| 90 | 82 |
| 91 void AwFormDatabaseService::HasFormDataImpl( | 83 void AwFormDatabaseService::HasFormDataImpl() { |
| 92 WaitableEvent* completion, | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 93 bool* result) { | 85 pending_query_handle_ = autofill_data_->HasFormElements(this); |
| 94 WebDataServiceBase::Handle pending_query_handle = | |
| 95 autofill_data_->HasFormElements(this); | |
| 96 PendingQuery query; | |
| 97 query.result = result; | |
| 98 query.completion = completion; | |
| 99 result_map_[pending_query_handle] = query; | |
| 100 } | 86 } |
| 101 | 87 |
| 88 |
| 102 void AwFormDatabaseService::OnWebDataServiceRequestDone( | 89 void AwFormDatabaseService::OnWebDataServiceRequestDone( |
| 103 WebDataServiceBase::Handle h, | 90 WebDataServiceBase::Handle h, |
| 104 const WDTypedResult* result) { | 91 const WDTypedResult* result) { |
| 105 | 92 |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 107 bool has_form_data = false; | 94 DCHECK_EQ(pending_query_handle_, h); |
| 95 pending_query_handle_ = 0; |
| 96 has_form_data_ = false; |
| 97 |
| 108 if (result) { | 98 if (result) { |
| 109 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); | 99 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); |
| 110 const WDResult<bool>* autofill_result = | 100 const WDResult<bool>* autofill_result = |
| 111 static_cast<const WDResult<bool>*>(result); | 101 static_cast<const WDResult<bool>*>(result); |
| 112 has_form_data = autofill_result->GetValue(); | 102 has_form_data_ = autofill_result->GetValue(); |
| 113 } | 103 } |
| 114 QueryMap::const_iterator it = result_map_.find(h); | 104 completion_.Signal(); |
| 115 if (it == result_map_.end()) { | |
| 116 LOG(WARNING) << "Received unexpected callback from web data service"; | |
| 117 return; | |
| 118 } | |
| 119 *(it->second.result) = has_form_data; | |
| 120 it->second.completion->Signal(); | |
| 121 result_map_.erase(h); | |
| 122 } | 105 } |
| 123 | 106 |
| 124 } // namespace android_webview | 107 } // namespace android_webview |
| OLD | NEW |