Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(935)

Side by Side Diff: components/browsing_data/core/counters/autofill_counter.cc

Issue 2153863002: Move counters for passwords, history and autofill to components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@separate_build_targets_in_components_bd
Patch Set: Fixed dependencies Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 : pref_name_(browsing_data::prefs::kDeleteFormData),
22 web_data_service_(web_data_service),
23 suggestions_query_(0), 23 suggestions_query_(0),
24 credit_cards_query_(0), 24 credit_cards_query_(0),
25 addresses_query_(0), 25 addresses_query_(0),
26 num_suggestions_(0), 26 num_suggestions_(0),
27 num_credit_cards_(0), 27 num_credit_cards_(0),
28 num_addresses_(0) {} 28 num_addresses_(0) {}
29 29
30 AutofillCounter::~AutofillCounter() { 30 AutofillCounter::~AutofillCounter() {
31 CancelAllRequests(); 31 CancelAllRequests();
32 } 32 }
33 33
34 void AutofillCounter::OnInitialized() { 34 void AutofillCounter::OnInitialized() {
35 web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile(
36 profile_, ServiceAccessType::EXPLICIT_ACCESS);
37 DCHECK(web_data_service_); 35 DCHECK(web_data_service_);
38 } 36 }
39 37
38 const std::string& AutofillCounter::GetPrefName() const {
39 return pref_name_;
msramek 2016/07/20 13:41:16 |pref_name_| is now only used in this method, so w
ioanap 2016/07/20 17:50:36 True. Changed it to return the pref name directly
40 }
41
40 void AutofillCounter::SetPeriodStartForTesting( 42 void AutofillCounter::SetPeriodStartForTesting(
41 const base::Time& period_start_for_testing) { 43 const base::Time& period_start_for_testing) {
42 period_start_for_testing_ = period_start_for_testing; 44 period_start_for_testing_ = period_start_for_testing;
43 } 45 }
44 46
45 void AutofillCounter::Count() { 47 void AutofillCounter::Count() {
46 const base::Time start = period_start_for_testing_.is_null() 48 const base::Time start = period_start_for_testing_.is_null()
47 ? GetPeriodStart() 49 ? GetPeriodStart()
48 : period_start_for_testing_; 50 : period_start_for_testing_;
49 51
50 CancelAllRequests(); 52 CancelAllRequests();
51 53
52 // Count the autocomplete suggestions (also called form elements in Autofill). 54 // Count the autocomplete suggestions (also called form elements in Autofill).
53 // Note that |AutofillTable::RemoveFormElementsAddedBetween| only deletes 55 // Note that |AutofillTable::RemoveFormElementsAddedBetween| only deletes
54 // those whose entire existence (i.e. the interval between creation time 56 // those whose entire existence (i.e. the interval between creation time
55 // and last modified time) lies within the deletion time range. Otherwise, 57 // and last modified time) lies within the deletion time range. Otherwise,
56 // it only decreases the count property, but always to a nonzero value, 58 // 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 59 // 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()). 60 // the entries that are entirely contained in [start, base::Time::Max()).
(...skipping 12 matching lines...) Expand all
71 start, base::Time::Max(), this); 73 start, base::Time::Max(), this);
72 74
73 // Count the credit cards. 75 // Count the credit cards.
74 credit_cards_query_ = web_data_service_->GetCreditCards(this); 76 credit_cards_query_ = web_data_service_->GetCreditCards(this);
75 77
76 // Count the addresses. 78 // Count the addresses.
77 addresses_query_ = web_data_service_->GetAutofillProfiles(this); 79 addresses_query_ = web_data_service_->GetAutofillProfiles(this);
78 } 80 }
79 81
80 void AutofillCounter::OnWebDataServiceRequestDone( 82 void AutofillCounter::OnWebDataServiceRequestDone(
81 WebDataServiceBase::Handle handle, const WDTypedResult* result) { 83 WebDataServiceBase::Handle handle,
84 const WDTypedResult* result) {
82 DCHECK(thread_checker_.CalledOnValidThread()); 85 DCHECK(thread_checker_.CalledOnValidThread());
83 if (!result) { 86 if (!result) {
84 CancelAllRequests(); 87 CancelAllRequests();
85 return; 88 return;
86 } 89 }
87 90
88 const base::Time start = period_start_for_testing_.is_null() 91 const base::Time start = period_start_for_testing_.is_null()
89 ? GetPeriodStart() 92 ? GetPeriodStart()
90 : period_start_for_testing_; 93 : period_start_for_testing_;
91 94
92 if (handle == suggestions_query_) { 95 if (handle == suggestions_query_) {
93 // Autocomplete suggestions. 96 // Autocomplete suggestions.
94 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType()); 97 DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType());
95 num_suggestions_ = static_cast<const WDResult<int>*>(result)->GetValue(); 98 num_suggestions_ = static_cast<const WDResult<int>*>(result)->GetValue();
96 suggestions_query_ = 0; 99 suggestions_query_ = 0;
97 100
98 } else if (handle == credit_cards_query_) { 101 } else if (handle == credit_cards_query_) {
99 // Credit cards. 102 // Credit cards.
100 DCHECK_EQ(AUTOFILL_CREDITCARDS_RESULT, result->GetType()); 103 DCHECK_EQ(AUTOFILL_CREDITCARDS_RESULT, result->GetType());
101 const std::vector<autofill::CreditCard*> credit_cards = 104 const std::vector<autofill::CreditCard*> credit_cards =
102 static_cast<const WDResult<std::vector<autofill::CreditCard*>>*>( 105 static_cast<const WDResult<std::vector<autofill::CreditCard*>>*>(result)
103 result)->GetValue(); 106 ->GetValue();
104 107
105 // We own the result from this query. Make sure it will be deleted. 108 // We own the result from this query. Make sure it will be deleted.
106 ScopedVector<const autofill::CreditCard> owned_result; 109 ScopedVector<const autofill::CreditCard> owned_result;
107 owned_result.assign(credit_cards.begin(), credit_cards.end()); 110 owned_result.assign(credit_cards.begin(), credit_cards.end());
108 111
109 num_credit_cards_ = std::count_if( 112 num_credit_cards_ =
110 credit_cards.begin(), 113 std::count_if(credit_cards.begin(), credit_cards.end(),
msramek 2016/07/20 13:41:16 Is this git cl format? *grumble* *grumble* it's le
ioanap 2016/07/20 17:50:36 Oups, I didn't see this one. Yes, it's because of
111 credit_cards.end(), 114 [start](const autofill::CreditCard* card) {
112 [start](const autofill::CreditCard* card) { 115 return card->modification_date() >= start;
113 return card->modification_date() >= start; 116 });
114 });
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698