| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browsing_data/autofill_counter.h" | 5 #include "components/browsing_data/core/counters/autofill_counter.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/web_data_service_factory.h" | |
| 14 #include "components/autofill/core/browser/autofill_profile.h" | 12 #include "components/autofill/core/browser/autofill_profile.h" |
| 15 #include "components/autofill/core/browser/credit_card.h" | 13 #include "components/autofill/core/browser/credit_card.h" |
| 16 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 14 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 17 #include "components/browsing_data/core/pref_names.h" | 15 #include "components/browsing_data/core/pref_names.h" |
| 18 | 16 |
| 19 AutofillCounter::AutofillCounter(Profile* profile) | 17 namespace browsing_data { |
| 20 : BrowsingDataCounter(browsing_data::prefs::kDeleteFormData), | 18 |
| 21 profile_(profile), | 19 AutofillCounter::AutofillCounter( |
| 22 web_data_service_(nullptr), | 20 scoped_refptr<autofill::AutofillWebDataService> web_data_service) |
| 21 : web_data_service_(web_data_service), |
| 23 suggestions_query_(0), | 22 suggestions_query_(0), |
| 24 credit_cards_query_(0), | 23 credit_cards_query_(0), |
| 25 addresses_query_(0), | 24 addresses_query_(0), |
| 26 num_suggestions_(0), | 25 num_suggestions_(0), |
| 27 num_credit_cards_(0), | 26 num_credit_cards_(0), |
| 28 num_addresses_(0) {} | 27 num_addresses_(0) {} |
| 29 | 28 |
| 30 AutofillCounter::~AutofillCounter() { | 29 AutofillCounter::~AutofillCounter() { |
| 31 CancelAllRequests(); | 30 CancelAllRequests(); |
| 32 } | 31 } |
| 33 | 32 |
| 34 void AutofillCounter::OnInitialized() { | 33 void AutofillCounter::OnInitialized() { |
| 35 web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile( | |
| 36 profile_, ServiceAccessType::EXPLICIT_ACCESS); | |
| 37 DCHECK(web_data_service_); | 34 DCHECK(web_data_service_); |
| 38 } | 35 } |
| 39 | 36 |
| 37 const char* AutofillCounter::GetPrefName() const { |
| 38 return browsing_data::prefs::kDeleteFormData; |
| 39 } |
| 40 |
| 40 void AutofillCounter::SetPeriodStartForTesting( | 41 void AutofillCounter::SetPeriodStartForTesting( |
| 41 const base::Time& period_start_for_testing) { | 42 const base::Time& period_start_for_testing) { |
| 42 period_start_for_testing_ = period_start_for_testing; | 43 period_start_for_testing_ = period_start_for_testing; |
| 43 } | 44 } |
| 44 | 45 |
| 45 void AutofillCounter::Count() { | 46 void AutofillCounter::Count() { |
| 46 const base::Time start = period_start_for_testing_.is_null() | 47 const base::Time start = period_start_for_testing_.is_null() |
| 47 ? GetPeriodStart() | 48 ? GetPeriodStart() |
| 48 : period_start_for_testing_; | 49 : period_start_for_testing_; |
| 49 | 50 |
| 50 CancelAllRequests(); | 51 CancelAllRequests(); |
| 51 | 52 |
| 52 // Count the autocomplete suggestions (also called form elements in Autofill). | 53 // Count the autocomplete suggestions (also called form elements in Autofill). |
| 53 // Note that |AutofillTable::RemoveFormElementsAddedBetween| only deletes | 54 // Note that |AutofillTable::RemoveFormElementsAddedBetween| only deletes |
| 54 // those whose entire existence (i.e. the interval between creation time | 55 // those whose entire existence (i.e. the interval between creation time |
| 55 // and last modified time) lies within the deletion time range. Otherwise, | 56 // and last modified time) lies within the deletion time range. Otherwise, |
| 56 // it only decreases the count property, but always to a nonzero value, | 57 // it only decreases the count property, but always to a nonzero value, |
| 57 // and the suggestion is retained. Therefore here as well, we must only count | 58 // and the suggestion is retained. Therefore here as well, we must only count |
| 58 // the entries that are entirely contained in [start, base::Time::Max()). | 59 // the entries that are entirely contained in [start, base::Time::Max()). |
| (...skipping 12 matching lines...) Expand all Loading... |
| 71 start, base::Time::Max(), this); | 72 start, base::Time::Max(), this); |
| 72 | 73 |
| 73 // Count the credit cards. | 74 // Count the credit cards. |
| 74 credit_cards_query_ = web_data_service_->GetCreditCards(this); | 75 credit_cards_query_ = web_data_service_->GetCreditCards(this); |
| 75 | 76 |
| 76 // Count the addresses. | 77 // Count the addresses. |
| 77 addresses_query_ = web_data_service_->GetAutofillProfiles(this); | 78 addresses_query_ = web_data_service_->GetAutofillProfiles(this); |
| 78 } | 79 } |
| 79 | 80 |
| 80 void AutofillCounter::OnWebDataServiceRequestDone( | 81 void AutofillCounter::OnWebDataServiceRequestDone( |
| 81 WebDataServiceBase::Handle handle, const WDTypedResult* result) { | 82 WebDataServiceBase::Handle handle, |
| 83 const WDTypedResult* result) { |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
| 83 if (!result) { | 85 if (!result) { |
| 84 CancelAllRequests(); | 86 CancelAllRequests(); |
| 85 return; | 87 return; |
| 86 } | 88 } |
| 87 | 89 |
| 88 const base::Time start = period_start_for_testing_.is_null() | 90 const base::Time start = period_start_for_testing_.is_null() |
| 89 ? GetPeriodStart() | 91 ? GetPeriodStart() |
| 90 : period_start_for_testing_; | 92 : period_start_for_testing_; |
| 91 | 93 |
| 92 if (handle == suggestions_query_) { | 94 if (handle == suggestions_query_) { |
| 93 // Autocomplete suggestions. | 95 // Autocomplete suggestions. |
| 94 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); | 96 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); |
| 95 num_suggestions_ = static_cast<const WDResult<int>*>(result)->GetValue(); | 97 num_suggestions_ = static_cast<const WDResult<int>*>(result)->GetValue(); |
| 96 suggestions_query_ = 0; | 98 suggestions_query_ = 0; |
| 97 | 99 |
| 98 } else if (handle == credit_cards_query_) { | 100 } else if (handle == credit_cards_query_) { |
| 99 // Credit cards. | 101 // Credit cards. |
| 100 DCHECK_EQ(AUTOFILL_CREDITCARDS_RESULT, result->GetType()); | 102 DCHECK_EQ(AUTOFILL_CREDITCARDS_RESULT, result->GetType()); |
| 101 const std::vector<autofill::CreditCard*> credit_cards = | 103 const std::vector<autofill::CreditCard*> credit_cards = |
| 102 static_cast<const WDResult<std::vector<autofill::CreditCard*>>*>( | 104 static_cast<const WDResult<std::vector<autofill::CreditCard*>>*>(result) |
| 103 result)->GetValue(); | 105 ->GetValue(); |
| 104 | 106 |
| 105 // We own the result from this query. Make sure it will be deleted. | 107 // We own the result from this query. Make sure it will be deleted. |
| 106 ScopedVector<const autofill::CreditCard> owned_result; | 108 ScopedVector<const autofill::CreditCard> owned_result; |
| 107 owned_result.assign(credit_cards.begin(), credit_cards.end()); | 109 owned_result.assign(credit_cards.begin(), credit_cards.end()); |
| 108 | 110 |
| 109 num_credit_cards_ = std::count_if( | 111 num_credit_cards_ = std::count_if( |
| 110 credit_cards.begin(), | 112 credit_cards.begin(), |
| 111 credit_cards.end(), | 113 credit_cards.end(), |
| 112 [start](const autofill::CreditCard* card) { | 114 [start](const autofill::CreditCard* card) { |
| 113 return card->modification_date() >= start; | 115 return card->modification_date() >= start; |
| 114 }); | 116 }); |
| 115 credit_cards_query_ = 0; | 117 credit_cards_query_ = 0; |
| 116 | 118 |
| 117 } else if (handle == addresses_query_) { | 119 } else if (handle == addresses_query_) { |
| 118 // Addresses. | 120 // Addresses. |
| 119 DCHECK_EQ(AUTOFILL_PROFILES_RESULT, result->GetType()); | 121 DCHECK_EQ(AUTOFILL_PROFILES_RESULT, result->GetType()); |
| 120 const std::vector<autofill::AutofillProfile*> addresses = | 122 const std::vector<autofill::AutofillProfile*> addresses = |
| 121 static_cast<const WDResult<std::vector<autofill::AutofillProfile*>>*>( | 123 static_cast<const WDResult<std::vector<autofill::AutofillProfile*>>*>( |
| 122 result)->GetValue(); | 124 result) |
| 125 ->GetValue(); |
| 123 | 126 |
| 124 // We own the result from this query. Make sure it will be deleted. | 127 // We own the result from this query. Make sure it will be deleted. |
| 125 ScopedVector<const autofill::AutofillProfile> owned_result; | 128 ScopedVector<const autofill::AutofillProfile> owned_result; |
| 126 owned_result.assign(addresses.begin(), addresses.end()); | 129 owned_result.assign(addresses.begin(), addresses.end()); |
| 127 | 130 |
| 128 num_addresses_ = std::count_if( | 131 num_addresses_ = |
| 129 addresses.begin(), | 132 std::count_if(addresses.begin(), addresses.end(), |
| 130 addresses.end(), | 133 [start](const autofill::AutofillProfile* address) { |
| 131 [start](const autofill::AutofillProfile* address) { | 134 return address->modification_date() >= start; |
| 132 return address->modification_date() >= start; | 135 }); |
| 133 }); | |
| 134 addresses_query_ = 0; | 136 addresses_query_ = 0; |
| 135 | 137 |
| 136 } else { | 138 } else { |
| 137 NOTREACHED() << "No such query: " << handle; | 139 NOTREACHED() << "No such query: " << handle; |
| 138 } | 140 } |
| 139 | 141 |
| 140 // If we still have pending queries, do not report data yet. | 142 // If we still have pending queries, do not report data yet. |
| 141 if (HasPendingQuery()) | 143 if (HasPendingQuery()) |
| 142 return; | 144 return; |
| 143 | 145 |
| 144 std::unique_ptr<Result> reported_result(new AutofillResult( | 146 std::unique_ptr<Result> reported_result(new AutofillResult( |
| 145 this, num_suggestions_, num_credit_cards_, num_addresses_)); | 147 this, num_suggestions_, num_credit_cards_, num_addresses_)); |
| 146 ReportResult(std::move(reported_result)); | 148 ReportResult(std::move(reported_result)); |
| 147 } | 149 } |
| 148 | 150 |
| 149 void AutofillCounter::CancelAllRequests() { | 151 void AutofillCounter::CancelAllRequests() { |
| 150 if (suggestions_query_) | 152 if (suggestions_query_) |
| 151 web_data_service_->CancelRequest(suggestions_query_); | 153 web_data_service_->CancelRequest(suggestions_query_); |
| 152 if (credit_cards_query_) | 154 if (credit_cards_query_) |
| 153 web_data_service_->CancelRequest(credit_cards_query_); | 155 web_data_service_->CancelRequest(credit_cards_query_); |
| 154 if (addresses_query_) | 156 if (addresses_query_) |
| 155 web_data_service_->CancelRequest(addresses_query_); | 157 web_data_service_->CancelRequest(addresses_query_); |
| 156 } | 158 } |
| 157 | 159 |
| 158 // AutofillCounter::AutofillResult --------------------------------------------- | 160 // AutofillCounter::AutofillResult --------------------------------------------- |
| 159 | 161 |
| 160 AutofillCounter::AutofillResult::AutofillResult( | 162 AutofillCounter::AutofillResult::AutofillResult(const AutofillCounter* source, |
| 161 const AutofillCounter* source, | 163 ResultInt num_suggestions, |
| 162 ResultInt num_suggestions, | 164 ResultInt num_credit_cards, |
| 163 ResultInt num_credit_cards, | 165 ResultInt num_addresses) |
| 164 ResultInt num_addresses) | |
| 165 : FinishedResult(source, num_suggestions), | 166 : FinishedResult(source, num_suggestions), |
| 166 num_credit_cards_(num_credit_cards), | 167 num_credit_cards_(num_credit_cards), |
| 167 num_addresses_(num_addresses) { | 168 num_addresses_(num_addresses) {} |
| 168 } | |
| 169 | 169 |
| 170 AutofillCounter::AutofillResult::~AutofillResult() { | 170 AutofillCounter::AutofillResult::~AutofillResult() {} |
| 171 } | 171 |
| 172 } // namespace browsing_data |
| OLD | NEW |