OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/password_manager/core/browser/affiliation_fetcher.h" | 5 #include "components/password_manager/core/browser/affiliation_fetcher.h" |
6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/metrics/sparse_histogram.h" |
7 #include "components/password_manager/core/browser/affiliation_api.pb.h" | 9 #include "components/password_manager/core/browser/affiliation_api.pb.h" |
8 #include "components/password_manager/core/browser/affiliation_utils.h" | 10 #include "components/password_manager/core/browser/affiliation_utils.h" |
9 #include "components/password_manager/core/browser/test_affiliation_fetcher_fact
ory.h" | 11 #include "components/password_manager/core/browser/test_affiliation_fetcher_fact
ory.h" |
10 #include "google_apis/google_api_keys.h" | 12 #include "google_apis/google_api_keys.h" |
11 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
12 #include "net/base/url_util.h" | 14 #include "net/base/url_util.h" |
13 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
14 #include "net/url_request/url_fetcher.h" | 16 #include "net/url_request/url_fetcher.h" |
15 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
17 | 19 |
18 namespace password_manager { | 20 namespace password_manager { |
19 | 21 |
| 22 namespace { |
| 23 |
| 24 // Enumeration listing the possible outcomes of fetching affiliation information |
| 25 // from the Affiliation API. This is used in UMA histograms, so do not change |
| 26 // existing values, only add new values at the end. |
| 27 enum AffiliationFetchResult { |
| 28 AFFILIATION_FETCH_RESULT_SUCCESS, |
| 29 AFFILIATION_FETCH_RESULT_FAILURE, |
| 30 AFFILIATION_FETCH_RESULT_MALFORMED, |
| 31 AFFILIATION_FETCH_RESULT_MAX |
| 32 }; |
| 33 |
| 34 // Records the given fetch |result| into the respective UMA histogram, as well |
| 35 // as the response and error codes of |fetcher| if it is non-null. |
| 36 void ReportStatistics(AffiliationFetchResult result, |
| 37 const net::URLFetcher* fetcher) { |
| 38 UMA_HISTOGRAM_ENUMERATION("PasswordManager.AffiliationFetcher.FetchResult", |
| 39 result, AFFILIATION_FETCH_RESULT_MAX); |
| 40 if (fetcher) { |
| 41 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 42 "PasswordManager.AffiliationFetcher.FetchHttpResponseCode", |
| 43 fetcher->GetResponseCode()); |
| 44 // Network error codes are negative. See: src/net/base/net_error_list.h. |
| 45 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 46 "PasswordManager.AffiliationFetcher.FetchErrorCode", |
| 47 -fetcher->GetStatus().error()); |
| 48 } |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
20 static TestAffiliationFetcherFactory* g_testing_factory = nullptr; | 53 static TestAffiliationFetcherFactory* g_testing_factory = nullptr; |
21 | 54 |
22 AffiliationFetcher::AffiliationFetcher( | 55 AffiliationFetcher::AffiliationFetcher( |
23 net::URLRequestContextGetter* request_context_getter, | 56 net::URLRequestContextGetter* request_context_getter, |
24 const std::vector<FacetURI>& facet_uris, | 57 const std::vector<FacetURI>& facet_uris, |
25 AffiliationFetcherDelegate* delegate) | 58 AffiliationFetcherDelegate* delegate) |
26 : request_context_getter_(request_context_getter), | 59 : request_context_getter_(request_context_getter), |
27 requested_facet_uris_(facet_uris), | 60 requested_facet_uris_(facet_uris), |
28 delegate_(delegate) { | 61 delegate_(delegate) { |
29 for (const FacetURI& uri : requested_facet_uris_) { | 62 for (const FacetURI& uri : requested_facet_uris_) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 if (!facet_uri_to_class_index.count(uri)) | 187 if (!facet_uri_to_class_index.count(uri)) |
155 result->push_back(AffiliatedFacets(1, uri)); | 188 result->push_back(AffiliatedFacets(1, uri)); |
156 } | 189 } |
157 | 190 |
158 return true; | 191 return true; |
159 } | 192 } |
160 | 193 |
161 void AffiliationFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 194 void AffiliationFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
162 DCHECK_EQ(source, fetcher_.get()); | 195 DCHECK_EQ(source, fetcher_.get()); |
163 | 196 |
164 scoped_ptr<AffiliationFetcherDelegate::Result> result( | 197 // Note that invoking the |delegate_| may destroy |this| synchronously, so the |
| 198 // invocation must happen last. |
| 199 scoped_ptr<AffiliationFetcherDelegate::Result> result_data( |
165 new AffiliationFetcherDelegate::Result); | 200 new AffiliationFetcherDelegate::Result); |
166 if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS && | 201 if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS && |
167 fetcher_->GetResponseCode() == net::HTTP_OK) { | 202 fetcher_->GetResponseCode() == net::HTTP_OK) { |
168 if (ParseResponse(result.get())) | 203 if (ParseResponse(result_data.get())) { |
169 delegate_->OnFetchSucceeded(result.Pass()); | 204 ReportStatistics(AFFILIATION_FETCH_RESULT_SUCCESS, nullptr); |
170 else | 205 delegate_->OnFetchSucceeded(result_data.Pass()); |
| 206 } else { |
| 207 ReportStatistics(AFFILIATION_FETCH_RESULT_MALFORMED, nullptr); |
171 delegate_->OnMalformedResponse(); | 208 delegate_->OnMalformedResponse(); |
| 209 } |
172 } else { | 210 } else { |
| 211 ReportStatistics(AFFILIATION_FETCH_RESULT_FAILURE, fetcher_.get()); |
173 delegate_->OnFetchFailed(); | 212 delegate_->OnFetchFailed(); |
174 } | 213 } |
175 } | 214 } |
176 | 215 |
177 } // namespace password_manager | 216 } // namespace password_manager |
OLD | NEW |