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/autofill/core/browser/payments/payments_client.h" | 5 #include "components/autofill/core/browser/payments/payments_client.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | |
10 | 9 |
11 #include "base/command_line.h" | 10 #include "base/command_line.h" |
12 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
13 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
14 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
15 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
19 #include "base/values.h" | 18 #include "base/values.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 103 |
105 void AppendStringIfNotEmpty(const AutofillProfile& profile, | 104 void AppendStringIfNotEmpty(const AutofillProfile& profile, |
106 const ServerFieldType& type, | 105 const ServerFieldType& type, |
107 const std::string& app_locale, | 106 const std::string& app_locale, |
108 base::ListValue* list) { | 107 base::ListValue* list) { |
109 const base::string16 value = profile.GetInfo(AutofillType(type), app_locale); | 108 const base::string16 value = profile.GetInfo(AutofillType(type), app_locale); |
110 if (!value.empty()) | 109 if (!value.empty()) |
111 list->AppendString(value); | 110 list->AppendString(value); |
112 } | 111 } |
113 | 112 |
114 // Returns a dictionary with the structure expected by Payments RPCs, containing | |
115 // each of the fields in |profile|, formatted according to |app_locale|. If | |
116 // |include_non_location_data| is false, the name and phone number in |profile| | |
117 // are not included. | |
118 std::unique_ptr<base::DictionaryValue> BuildAddressDictionary( | 113 std::unique_ptr<base::DictionaryValue> BuildAddressDictionary( |
119 const AutofillProfile& profile, | 114 const AutofillProfile& profile, |
120 const std::string& app_locale, | 115 const std::string& app_locale) { |
121 bool include_non_location_data) { | |
122 std::unique_ptr<base::DictionaryValue> postal_address( | 116 std::unique_ptr<base::DictionaryValue> postal_address( |
123 new base::DictionaryValue()); | 117 new base::DictionaryValue()); |
124 | 118 |
125 if (include_non_location_data) { | 119 SetStringIfNotEmpty(profile, NAME_FULL, app_locale, "recipient_name", |
126 SetStringIfNotEmpty(profile, NAME_FULL, app_locale, | 120 postal_address.get()); |
127 PaymentsClient::kRecipientName, postal_address.get()); | |
128 } | |
129 | 121 |
130 std::unique_ptr<base::ListValue> address_lines(new base::ListValue()); | 122 std::unique_ptr<base::ListValue> address_lines(new base::ListValue()); |
131 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE1, app_locale, | 123 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE1, app_locale, |
132 address_lines.get()); | 124 address_lines.get()); |
133 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE2, app_locale, | 125 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE2, app_locale, |
134 address_lines.get()); | 126 address_lines.get()); |
135 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE3, app_locale, | 127 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE3, app_locale, |
136 address_lines.get()); | 128 address_lines.get()); |
137 if (!address_lines->empty()) | 129 if (!address_lines->empty()) |
138 postal_address->Set("address_line", std::move(address_lines)); | 130 postal_address->Set("address_line", std::move(address_lines)); |
139 | 131 |
140 SetStringIfNotEmpty(profile, ADDRESS_HOME_CITY, app_locale, "locality_name", | 132 SetStringIfNotEmpty(profile, ADDRESS_HOME_CITY, app_locale, "locality_name", |
141 postal_address.get()); | 133 postal_address.get()); |
142 SetStringIfNotEmpty(profile, ADDRESS_HOME_STATE, app_locale, | 134 SetStringIfNotEmpty(profile, ADDRESS_HOME_STATE, app_locale, |
143 "administrative_area_name", postal_address.get()); | 135 "administrative_area_name", postal_address.get()); |
144 SetStringIfNotEmpty(profile, ADDRESS_HOME_ZIP, app_locale, | 136 SetStringIfNotEmpty(profile, ADDRESS_HOME_ZIP, app_locale, |
145 "postal_code_number", postal_address.get()); | 137 "postal_code_number", postal_address.get()); |
146 | 138 |
147 // Use GetRawInfo to get a country code instead of the country name: | 139 // Use GetRawInfo to get a country code instead of the country name: |
148 const base::string16 country_code = profile.GetRawInfo(ADDRESS_HOME_COUNTRY); | 140 const base::string16 country_code = profile.GetRawInfo(ADDRESS_HOME_COUNTRY); |
149 if (!country_code.empty()) | 141 if (!country_code.empty()) |
150 postal_address->SetString("country_name_code", country_code); | 142 postal_address->SetString("country_name_code", country_code); |
151 | 143 |
152 std::unique_ptr<base::DictionaryValue> address(new base::DictionaryValue()); | 144 std::unique_ptr<base::DictionaryValue> address(new base::DictionaryValue()); |
153 address->Set("postal_address", std::move(postal_address)); | 145 address->Set("postal_address", std::move(postal_address)); |
154 | 146 SetStringIfNotEmpty(profile, PHONE_HOME_WHOLE_NUMBER, app_locale, |
155 if (include_non_location_data) { | 147 "phone_number", address.get()); |
156 SetStringIfNotEmpty(profile, PHONE_HOME_WHOLE_NUMBER, app_locale, | |
157 PaymentsClient::kPhoneNumber, address.get()); | |
158 } | |
159 | 148 |
160 return address; | 149 return address; |
161 } | 150 } |
162 | 151 |
163 class UnmaskCardRequest : public PaymentsRequest { | 152 class UnmaskCardRequest : public PaymentsRequest { |
164 public: | 153 public: |
165 UnmaskCardRequest(const PaymentsClient::UnmaskRequestDetails& request_details) | 154 UnmaskCardRequest(const PaymentsClient::UnmaskRequestDetails& request_details) |
166 : request_details_(request_details) { | 155 : request_details_(request_details) { |
167 DCHECK( | 156 DCHECK( |
168 CreditCard::MASKED_SERVER_CARD == request_details.card.record_type() || | 157 CreditCard::MASKED_SERVER_CARD == request_details.card.record_type() || |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 delegate->OnDidGetRealPan(result, real_pan_); | 202 delegate->OnDidGetRealPan(result, real_pan_); |
214 } | 203 } |
215 | 204 |
216 private: | 205 private: |
217 PaymentsClient::UnmaskRequestDetails request_details_; | 206 PaymentsClient::UnmaskRequestDetails request_details_; |
218 std::string real_pan_; | 207 std::string real_pan_; |
219 }; | 208 }; |
220 | 209 |
221 class GetUploadDetailsRequest : public PaymentsRequest { | 210 class GetUploadDetailsRequest : public PaymentsRequest { |
222 public: | 211 public: |
223 GetUploadDetailsRequest(const std::vector<AutofillProfile>& addresses, | 212 GetUploadDetailsRequest(const std::string& app_locale) |
224 const std::string& app_locale) | 213 : app_locale_(app_locale) {} |
225 : addresses_(addresses), app_locale_(app_locale) {} | |
226 ~GetUploadDetailsRequest() override {} | 214 ~GetUploadDetailsRequest() override {} |
227 | 215 |
228 std::string GetRequestUrlPath() override { | 216 std::string GetRequestUrlPath() override { |
229 return kGetUploadDetailsRequestPath; | 217 return kGetUploadDetailsRequestPath; |
230 } | 218 } |
231 | 219 |
232 std::string GetRequestContentType() override { return "application/json"; } | 220 std::string GetRequestContentType() override { return "application/json"; } |
233 | 221 |
234 std::string GetRequestContent() override { | 222 std::string GetRequestContent() override { |
235 base::DictionaryValue request_dict; | 223 base::DictionaryValue request_dict; |
236 std::unique_ptr<base::DictionaryValue> context(new base::DictionaryValue()); | 224 std::unique_ptr<base::DictionaryValue> context(new base::DictionaryValue()); |
237 context->SetString("language_code", app_locale_); | 225 context->SetString("language_code", app_locale_); |
238 request_dict.Set("context", std::move(context)); | 226 request_dict.Set("context", std::move(context)); |
239 | 227 |
240 std::unique_ptr<base::ListValue> addresses(new base::ListValue()); | |
241 for (const AutofillProfile& profile : addresses_) { | |
242 // These addresses are used by Payments to (1) accurately determine the | |
243 // user's country in order to show the correct legal documents and (2) to | |
244 // verify that the addresses are valid for their purposes so that we don't | |
245 // offer save in a case where it would definitely fail (e.g. P.O. boxes). | |
246 // The final parameter directs BuildAddressDictionary to omit names and | |
247 // phone numbers, which aren't useful for these purposes. | |
248 addresses->Append(BuildAddressDictionary(profile, app_locale_, false)); | |
249 } | |
250 request_dict.Set("address", std::move(addresses)); | |
251 | |
252 std::string request_content; | 228 std::string request_content; |
253 base::JSONWriter::Write(request_dict, &request_content); | 229 base::JSONWriter::Write(request_dict, &request_content); |
254 VLOG(3) << "getdetailsforsavecard request body: " << request_content; | 230 VLOG(3) << "getdetailsforsavecard request body: " << request_content; |
255 return request_content; | 231 return request_content; |
256 } | 232 } |
257 | 233 |
258 void ParseResponse(std::unique_ptr<base::DictionaryValue> response) override { | 234 void ParseResponse(std::unique_ptr<base::DictionaryValue> response) override { |
259 response->GetString("context_token", &context_token_); | 235 response->GetString("context_token", &context_token_); |
260 base::DictionaryValue* unowned_legal_message; | 236 base::DictionaryValue* unowned_legal_message; |
261 if (response->GetDictionary("legal_message", &unowned_legal_message)) | 237 if (response->GetDictionary("legal_message", &unowned_legal_message)) |
262 legal_message_ = unowned_legal_message->CreateDeepCopy(); | 238 legal_message_ = unowned_legal_message->CreateDeepCopy(); |
263 } | 239 } |
264 | 240 |
265 bool IsResponseComplete() override { | 241 bool IsResponseComplete() override { |
266 return !context_token_.empty() && legal_message_; | 242 return !context_token_.empty() && legal_message_; |
267 } | 243 } |
268 | 244 |
269 void RespondToDelegate(PaymentsClientDelegate* delegate, | 245 void RespondToDelegate(PaymentsClientDelegate* delegate, |
270 AutofillClient::PaymentsRpcResult result) override { | 246 AutofillClient::PaymentsRpcResult result) override { |
271 delegate->OnDidGetUploadDetails(result, context_token_, | 247 delegate->OnDidGetUploadDetails(result, context_token_, |
272 std::move(legal_message_)); | 248 std::move(legal_message_)); |
273 } | 249 } |
274 | 250 |
275 private: | 251 private: |
276 std::vector<AutofillProfile> addresses_; | |
277 std::string app_locale_; | 252 std::string app_locale_; |
278 base::string16 context_token_; | 253 base::string16 context_token_; |
279 std::unique_ptr<base::DictionaryValue> legal_message_; | 254 std::unique_ptr<base::DictionaryValue> legal_message_; |
280 }; | 255 }; |
281 | 256 |
282 class UploadCardRequest : public PaymentsRequest { | 257 class UploadCardRequest : public PaymentsRequest { |
283 public: | 258 public: |
284 UploadCardRequest(const PaymentsClient::UploadRequestDetails& request_details) | 259 UploadCardRequest(const PaymentsClient::UploadRequestDetails& request_details) |
285 : request_details_(request_details) {} | 260 : request_details_(request_details) {} |
286 ~UploadCardRequest() override {} | 261 ~UploadCardRequest() override {} |
(...skipping 14 matching lines...) Expand all Loading... |
301 const std::string& app_locale = request_details_.app_locale; | 276 const std::string& app_locale = request_details_.app_locale; |
302 std::unique_ptr<base::DictionaryValue> context(new base::DictionaryValue()); | 277 std::unique_ptr<base::DictionaryValue> context(new base::DictionaryValue()); |
303 context->SetString("language_code", app_locale); | 278 context->SetString("language_code", app_locale); |
304 request_dict.Set("context", std::move(context)); | 279 request_dict.Set("context", std::move(context)); |
305 | 280 |
306 SetStringIfNotEmpty(request_details_.card, CREDIT_CARD_NAME_FULL, | 281 SetStringIfNotEmpty(request_details_.card, CREDIT_CARD_NAME_FULL, |
307 app_locale, "cardholder_name", &request_dict); | 282 app_locale, "cardholder_name", &request_dict); |
308 | 283 |
309 std::unique_ptr<base::ListValue> addresses(new base::ListValue()); | 284 std::unique_ptr<base::ListValue> addresses(new base::ListValue()); |
310 for (const AutofillProfile& profile : request_details_.profiles) { | 285 for (const AutofillProfile& profile : request_details_.profiles) { |
311 addresses->Append(BuildAddressDictionary(profile, app_locale, true)); | 286 addresses->Append(BuildAddressDictionary(profile, app_locale)); |
312 } | 287 } |
313 request_dict.Set("address", std::move(addresses)); | 288 request_dict.Set("address", std::move(addresses)); |
314 | 289 |
315 request_dict.SetString("context_token", request_details_.context_token); | 290 request_dict.SetString("context_token", request_details_.context_token); |
316 | 291 |
317 int value = 0; | 292 int value = 0; |
318 const base::string16 exp_month = request_details_.card.GetInfo( | 293 const base::string16 exp_month = request_details_.card.GetInfo( |
319 AutofillType(CREDIT_CARD_EXP_MONTH), app_locale); | 294 AutofillType(CREDIT_CARD_EXP_MONTH), app_locale); |
320 const base::string16 exp_year = request_details_.card.GetInfo( | 295 const base::string16 exp_year = request_details_.card.GetInfo( |
321 AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), app_locale); | 296 AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), app_locale); |
(...skipping 26 matching lines...) Expand all Loading... |
348 AutofillClient::PaymentsRpcResult result) override { | 323 AutofillClient::PaymentsRpcResult result) override { |
349 delegate->OnDidUploadCard(result); | 324 delegate->OnDidUploadCard(result); |
350 } | 325 } |
351 | 326 |
352 private: | 327 private: |
353 PaymentsClient::UploadRequestDetails request_details_; | 328 PaymentsClient::UploadRequestDetails request_details_; |
354 }; | 329 }; |
355 | 330 |
356 } // namespace | 331 } // namespace |
357 | 332 |
358 const std::string PaymentsClient::kRecipientName = "recipient_name"; | |
359 const std::string PaymentsClient::kPhoneNumber = "phone_number"; | |
360 | |
361 PaymentsClient::UnmaskRequestDetails::UnmaskRequestDetails() {} | 333 PaymentsClient::UnmaskRequestDetails::UnmaskRequestDetails() {} |
362 PaymentsClient::UnmaskRequestDetails::~UnmaskRequestDetails() {} | 334 PaymentsClient::UnmaskRequestDetails::~UnmaskRequestDetails() {} |
363 | 335 |
364 PaymentsClient::UploadRequestDetails::UploadRequestDetails() {} | 336 PaymentsClient::UploadRequestDetails::UploadRequestDetails() {} |
365 PaymentsClient::UploadRequestDetails::UploadRequestDetails( | 337 PaymentsClient::UploadRequestDetails::UploadRequestDetails( |
366 const UploadRequestDetails& other) = default; | 338 const UploadRequestDetails& other) = default; |
367 PaymentsClient::UploadRequestDetails::~UploadRequestDetails() {} | 339 PaymentsClient::UploadRequestDetails::~UploadRequestDetails() {} |
368 | 340 |
369 PaymentsClient::PaymentsClient(net::URLRequestContextGetter* context_getter, | 341 PaymentsClient::PaymentsClient(net::URLRequestContextGetter* context_getter, |
370 PaymentsClientDelegate* delegate) | 342 PaymentsClientDelegate* delegate) |
(...skipping 10 matching lines...) Expand all Loading... |
381 void PaymentsClient::Prepare() { | 353 void PaymentsClient::Prepare() { |
382 if (access_token_.empty()) | 354 if (access_token_.empty()) |
383 StartTokenFetch(false); | 355 StartTokenFetch(false); |
384 } | 356 } |
385 | 357 |
386 void PaymentsClient::UnmaskCard( | 358 void PaymentsClient::UnmaskCard( |
387 const PaymentsClient::UnmaskRequestDetails& request_details) { | 359 const PaymentsClient::UnmaskRequestDetails& request_details) { |
388 IssueRequest(base::MakeUnique<UnmaskCardRequest>(request_details), true); | 360 IssueRequest(base::MakeUnique<UnmaskCardRequest>(request_details), true); |
389 } | 361 } |
390 | 362 |
391 void PaymentsClient::GetUploadDetails( | 363 void PaymentsClient::GetUploadDetails(const std::string& app_locale) { |
392 const std::vector<AutofillProfile>& addresses, | 364 IssueRequest(base::MakeUnique<GetUploadDetailsRequest>(app_locale), false); |
393 const std::string& app_locale) { | |
394 IssueRequest(base::MakeUnique<GetUploadDetailsRequest>(addresses, app_locale), | |
395 false); | |
396 } | 365 } |
397 | 366 |
398 void PaymentsClient::UploadCard( | 367 void PaymentsClient::UploadCard( |
399 const PaymentsClient::UploadRequestDetails& request_details) { | 368 const PaymentsClient::UploadRequestDetails& request_details) { |
400 IssueRequest(base::MakeUnique<UploadCardRequest>(request_details), true); | 369 IssueRequest(base::MakeUnique<UploadCardRequest>(request_details), true); |
401 } | 370 } |
402 | 371 |
403 void PaymentsClient::IssueRequest(std::unique_ptr<PaymentsRequest> request, | 372 void PaymentsClient::IssueRequest(std::unique_ptr<PaymentsRequest> request, |
404 bool authenticate) { | 373 bool authenticate) { |
405 request_ = std::move(request); | 374 request_ = std::move(request); |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 | 520 |
552 void PaymentsClient::SetOAuth2TokenAndStartRequest() { | 521 void PaymentsClient::SetOAuth2TokenAndStartRequest() { |
553 url_fetcher_->AddExtraRequestHeader(net::HttpRequestHeaders::kAuthorization + | 522 url_fetcher_->AddExtraRequestHeader(net::HttpRequestHeaders::kAuthorization + |
554 std::string(": Bearer ") + access_token_); | 523 std::string(": Bearer ") + access_token_); |
555 | 524 |
556 url_fetcher_->Start(); | 525 url_fetcher_->Start(); |
557 } | 526 } |
558 | 527 |
559 } // namespace payments | 528 } // namespace payments |
560 } // namespace autofill | 529 } // namespace autofill |
OLD | NEW |