| 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 "components/browsing_data/core/counters/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" | |
| 12 #include "components/autofill/core/browser/autofill_profile.h" | 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 13 #include "components/autofill/core/browser/credit_card.h" | 12 #include "components/autofill/core/browser/credit_card.h" |
| 14 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 13 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 15 #include "components/browsing_data/core/pref_names.h" | 14 #include "components/browsing_data/core/pref_names.h" |
| 16 | 15 |
| 17 namespace browsing_data { | 16 namespace browsing_data { |
| 18 | 17 |
| 19 AutofillCounter::AutofillCounter( | 18 AutofillCounter::AutofillCounter( |
| 20 scoped_refptr<autofill::AutofillWebDataService> web_data_service) | 19 scoped_refptr<autofill::AutofillWebDataService> web_data_service) |
| 21 : web_data_service_(web_data_service), | 20 : web_data_service_(web_data_service), |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 72 |
| 74 // Count the credit cards. | 73 // Count the credit cards. |
| 75 credit_cards_query_ = web_data_service_->GetCreditCards(this); | 74 credit_cards_query_ = web_data_service_->GetCreditCards(this); |
| 76 | 75 |
| 77 // Count the addresses. | 76 // Count the addresses. |
| 78 addresses_query_ = web_data_service_->GetAutofillProfiles(this); | 77 addresses_query_ = web_data_service_->GetAutofillProfiles(this); |
| 79 } | 78 } |
| 80 | 79 |
| 81 void AutofillCounter::OnWebDataServiceRequestDone( | 80 void AutofillCounter::OnWebDataServiceRequestDone( |
| 82 WebDataServiceBase::Handle handle, | 81 WebDataServiceBase::Handle handle, |
| 83 const WDTypedResult* result) { | 82 std::unique_ptr<WDTypedResult> result) { |
| 84 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 85 if (!result) { | 84 if (!result) { |
| 86 CancelAllRequests(); | 85 CancelAllRequests(); |
| 87 return; | 86 return; |
| 88 } | 87 } |
| 89 | 88 |
| 90 const base::Time start = period_start_for_testing_.is_null() | 89 const base::Time start = period_start_for_testing_.is_null() |
| 91 ? GetPeriodStart() | 90 ? GetPeriodStart() |
| 92 : period_start_for_testing_; | 91 : period_start_for_testing_; |
| 93 | 92 |
| 94 if (handle == suggestions_query_) { | 93 if (handle == suggestions_query_) { |
| 95 // Autocomplete suggestions. | 94 // Autocomplete suggestions. |
| 96 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); | 95 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); |
| 97 num_suggestions_ = static_cast<const WDResult<int>*>(result)->GetValue(); | 96 num_suggestions_ = |
| 97 static_cast<const WDResult<int>*>(result.get())->GetValue(); |
| 98 suggestions_query_ = 0; | 98 suggestions_query_ = 0; |
| 99 | 99 |
| 100 } else if (handle == credit_cards_query_) { | 100 } else if (handle == credit_cards_query_) { |
| 101 // Credit cards. | 101 // Credit cards. |
| 102 DCHECK_EQ(AUTOFILL_CREDITCARDS_RESULT, result->GetType()); | 102 DCHECK_EQ(AUTOFILL_CREDITCARDS_RESULT, result->GetType()); |
| 103 const std::vector<autofill::CreditCard*> credit_cards = | 103 std::vector<std::unique_ptr<autofill::CreditCard>> credit_cards = std::move( |
| 104 static_cast<const WDResult<std::vector<autofill::CreditCard*>>*>(result) | 104 static_cast< |
| 105 ->GetValue(); | 105 WDResult<std::vector<std::unique_ptr<autofill::CreditCard>>>*>( |
| 106 | 106 result.get()) |
| 107 // We own the result from this query. Make sure it will be deleted. | 107 ->GetValue()); |
| 108 ScopedVector<const autofill::CreditCard> owned_result; | |
| 109 owned_result.assign(credit_cards.begin(), credit_cards.end()); | |
| 110 | 108 |
| 111 num_credit_cards_ = std::count_if( | 109 num_credit_cards_ = std::count_if( |
| 112 credit_cards.begin(), | 110 credit_cards.begin(), credit_cards.end(), |
| 113 credit_cards.end(), | 111 [start](const std::unique_ptr<autofill::CreditCard>& card) { |
| 114 [start](const autofill::CreditCard* card) { | |
| 115 return card->modification_date() >= start; | 112 return card->modification_date() >= start; |
| 116 }); | 113 }); |
| 117 credit_cards_query_ = 0; | 114 credit_cards_query_ = 0; |
| 118 | 115 |
| 119 } else if (handle == addresses_query_) { | 116 } else if (handle == addresses_query_) { |
| 120 // Addresses. | 117 // Addresses. |
| 121 DCHECK_EQ(AUTOFILL_PROFILES_RESULT, result->GetType()); | 118 DCHECK_EQ(AUTOFILL_PROFILES_RESULT, result->GetType()); |
| 122 const std::vector<autofill::AutofillProfile*> addresses = | 119 std::vector<std::unique_ptr<autofill::AutofillProfile>> addresses = |
| 123 static_cast<const WDResult<std::vector<autofill::AutofillProfile*>>*>( | 120 std::move( |
| 124 result) | 121 static_cast<WDResult< |
| 125 ->GetValue(); | 122 std::vector<std::unique_ptr<autofill::AutofillProfile>>>*>( |
| 123 result.get()) |
| 124 ->GetValue()); |
| 126 | 125 |
| 127 // We own the result from this query. Make sure it will be deleted. | 126 num_addresses_ = std::count_if( |
| 128 ScopedVector<const autofill::AutofillProfile> owned_result; | 127 addresses.begin(), addresses.end(), |
| 129 owned_result.assign(addresses.begin(), addresses.end()); | 128 [start](const std::unique_ptr<autofill::AutofillProfile>& address) { |
| 130 | 129 return address->modification_date() >= start; |
| 131 num_addresses_ = | 130 }); |
| 132 std::count_if(addresses.begin(), addresses.end(), | |
| 133 [start](const autofill::AutofillProfile* address) { | |
| 134 return address->modification_date() >= start; | |
| 135 }); | |
| 136 addresses_query_ = 0; | 131 addresses_query_ = 0; |
| 137 | 132 |
| 138 } else { | 133 } else { |
| 139 NOTREACHED() << "No such query: " << handle; | 134 NOTREACHED() << "No such query: " << handle; |
| 140 } | 135 } |
| 141 | 136 |
| 142 // If we still have pending queries, do not report data yet. | 137 // If we still have pending queries, do not report data yet. |
| 143 if (HasPendingQuery()) | 138 if (HasPendingQuery()) |
| 144 return; | 139 return; |
| 145 | 140 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 163 ResultInt num_suggestions, | 158 ResultInt num_suggestions, |
| 164 ResultInt num_credit_cards, | 159 ResultInt num_credit_cards, |
| 165 ResultInt num_addresses) | 160 ResultInt num_addresses) |
| 166 : FinishedResult(source, num_suggestions), | 161 : FinishedResult(source, num_suggestions), |
| 167 num_credit_cards_(num_credit_cards), | 162 num_credit_cards_(num_credit_cards), |
| 168 num_addresses_(num_addresses) {} | 163 num_addresses_(num_addresses) {} |
| 169 | 164 |
| 170 AutofillCounter::AutofillResult::~AutofillResult() {} | 165 AutofillCounter::AutofillResult::~AutofillResult() {} |
| 171 | 166 |
| 172 } // namespace browsing_data | 167 } // namespace browsing_data |
| OLD | NEW |