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