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

Side by Side Diff: components/password_manager/core/browser/affiliation_fetcher.cc

Issue 767163005: Add AffiliationFetcher to fetch authoritative affiliation information regarding facets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed nits from gcasto@ and rdsmith@. Created 6 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/password_manager/core/browser/affiliation_fetcher.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/values.h"
10 #include "components/password_manager/core/browser/affiliation_api.pb.h"
11 #include "components/password_manager/core/browser/affiliation_utils.h"
12 #include "google_apis/google_api_keys.h"
13 #include "net/base/load_flags.h"
14 #include "net/base/url_util.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "net/url_request/url_request_context_getter.h"
18 #include "url/gurl.h"
19
20 namespace password_manager {
21
22 AffiliationFetcher::AffiliationFetcher(
23 net::URLRequestContextGetter* request_context_getter,
24 const std::vector<FacetURI>& facet_uris,
25 AffiliationFetcherDelegate* delegate)
26 : request_context_getter_(request_context_getter),
27 requested_facet_uris_(facet_uris),
28 delegate_(delegate) {
29 for (const FacetURI& uri : requested_facet_uris_)
30 DCHECK(uri.is_valid());
Ryan Sleevi 2014/12/12 23:02:19 BUG/DANGER: Whenever using debugging macros and co
engedy 2014/12/15 13:54:51 Done.
31 }
32
33 AffiliationFetcher::~AffiliationFetcher() {
34 }
35
36 // static
37 AffiliationFetcher* AffiliationFetcher::Create(
38 net::URLRequestContextGetter* context_getter,
39 const std::vector<FacetURI>& facet_uris,
40 AffiliationFetcherDelegate* delegate) {
41 return new AffiliationFetcher(context_getter, facet_uris, delegate);
42 }
43
44 void AffiliationFetcher::StartRequest() {
45 DCHECK(!fetcher_);
46
47 fetcher_.reset(
48 net::URLFetcher::Create(BuildQueryURL(), net::URLFetcher::POST, this));
49 fetcher_->SetRequestContext(request_context_getter_.get());
50 fetcher_->SetUploadData("application/x-protobuf", PreparePayload());
51 fetcher_->SetLoadFlags(
52 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES |
53 net::LOAD_DO_NOT_SEND_AUTH_DATA | net::LOAD_BYPASS_CACHE |
54 net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_PROMPT_FOR_LOGIN);
55 fetcher_->SetAutomaticallyRetryOn5xx(false);
56 fetcher_->SetAutomaticallyRetryOnNetworkChanges(0);
57 fetcher_->Start();
58 }
59
60 GURL AffiliationFetcher::BuildQueryURL() const {
61 return net::AppendQueryParameter(
62 GURL("https://www.googleapis.com/affiliation/v1/affiliation:lookup"),
63 "key", google_apis::GetAPIKey());
64 }
65
66 std::string AffiliationFetcher::PreparePayload() const {
67 affiliation_pb::LookupAffiliationRequest lookup_request;
68 for (const FacetURI& uri : requested_facet_uris_)
69 lookup_request.add_facet(uri.canonical_spec());
70
71 std::string serialized_request;
72 bool success = lookup_request.SerializeToString(&serialized_request);
73 DCHECK(success);
74 return serialized_request;
75 }
76
77 bool AffiliationFetcher::ParseResponse(
Ryan Sleevi 2014/12/12 23:02:19 DOCUMENTATION: I think it'd be helpful for future
engedy 2014/12/15 13:54:51 Done.
78 AffiliationFetcherDelegate::Result* result) const {
79 std::string serialized_response;
80 if (!fetcher_->GetResponseAsString(&serialized_response))
81 NOTREACHED();
82
83 affiliation_pb::LookupAffiliationResponse response;
84 if (!response.ParseFromString(serialized_response))
85 return false;
86
87 result->reserve(requested_facet_uris_.size());
88
89 std::map<FacetURI, size_t> facet_uri_to_class_index;
90 for (int i = 0; i < response.affiliation_size(); ++i) {
91 const affiliation_pb::Affiliation& equivalence_class(
92 response.affiliation(i));
93
94 AffiliatedFacets affiliated_uris;
95 for (int j = 0; j < equivalence_class.facet_size(); ++j) {
96 const std::string uri_spec(equivalence_class.facet(j));
Ryan Sleevi 2014/12/12 23:02:19 Should this be "const std::string&" ? I didn't di
engedy 2014/12/15 13:54:51 Argh, of course. Done.
97 FacetURI uri = FacetURI::FromPotentiallyInvalidSpec(uri_spec);
98 // Ignore potential future kinds of facet URIs (e.g. for new platforms).
99 if (!uri.is_valid())
100 continue;
101 affiliated_uris.push_back(uri);
102 }
103
104 // Be lenient and ignore empty (after filtering) equivalence classes.
105 if (affiliated_uris.empty())
106 continue;
107
108 // Ignore equivalence classes that are duplicates of earlier ones. However,
109 // fail in the case of a partial overlap, which violates the invariant that
110 // affiliations must form an equivalence relation.
111 for (const FacetURI& uri : affiliated_uris) {
112 if (!facet_uri_to_class_index.count(uri))
113 facet_uri_to_class_index[uri] = result->size();
114 if (facet_uri_to_class_index[uri] !=
115 facet_uri_to_class_index[affiliated_uris[0]]) {
116 return false;
117 }
118 }
119
120 // Filter out duplicate equivalence classes in the response.
121 if (facet_uri_to_class_index[affiliated_uris[0]] == result->size())
122 result->push_back(affiliated_uris);
123 }
124
125 // The API contract of this class is to return an equivalence class for all
126 // requested facets; however, the server will not return anything for facets
127 // that are not affiliated with any other facet. Synthesize an equivalence
128 // class (of size one) for each of the missing facets.
129 for (const FacetURI& uri : requested_facet_uris_) {
130 if (!facet_uri_to_class_index.count(uri))
131 result->push_back(AffiliatedFacets(1, uri));
132 }
133
134 return true;
135 }
136
137 void AffiliationFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
138 DCHECK_EQ(source, fetcher_.get());
139
140 scoped_ptr<AffiliationFetcherDelegate::Result> result(
141 new AffiliationFetcherDelegate::Result);
142 if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS &&
143 fetcher_->GetResponseCode() == net::HTTP_OK) {
144 if (ParseResponse(result.get()))
145 delegate_->OnFetchSucceeded(result.Pass());
146 else
147 delegate_->OnMalformedResponse();
148 } else {
149 delegate_->OnFetchFailed();
150 }
151 }
152
153 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698