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

Side by Side Diff: components/autofill/content/browser/wallet/wallet_client.cc

Issue 17970003: New encryption/escrow endpoints for Wallet (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Deleting old escrow unit tests Created 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/content/browser/wallet/wallet_client.h" 5 #include "components/autofill/content/browser/wallet/wallet_client.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
13 #include "components/autofill/content/browser/wallet/form_field_error.h" 16 #include "components/autofill/content/browser/wallet/form_field_error.h"
14 #include "components/autofill/content/browser/wallet/instrument.h" 17 #include "components/autofill/content/browser/wallet/instrument.h"
15 #include "components/autofill/content/browser/wallet/wallet_address.h" 18 #include "components/autofill/content/browser/wallet/wallet_address.h"
16 #include "components/autofill/content/browser/wallet/wallet_client_delegate.h" 19 #include "components/autofill/content/browser/wallet/wallet_client_delegate.h"
17 #include "components/autofill/content/browser/wallet/wallet_items.h" 20 #include "components/autofill/content/browser/wallet/wallet_items.h"
18 #include "components/autofill/content/browser/wallet/wallet_service_url.h" 21 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
19 #include "components/autofill/core/browser/autofill_metrics.h" 22 #include "components/autofill/core/browser/autofill_metrics.h"
20 #include "crypto/random.h" 23 #include "crypto/random.h"
21 #include "google_apis/google_api_keys.h" 24 #include "google_apis/google_api_keys.h"
25 #include "net/base/escape.h"
22 #include "net/http/http_status_code.h" 26 #include "net/http/http_status_code.h"
23 #include "net/url_request/url_fetcher.h" 27 #include "net/url_request/url_fetcher.h"
24 #include "net/url_request/url_request_context_getter.h" 28 #include "net/url_request/url_request_context_getter.h"
25 29
26 namespace autofill { 30 namespace autofill {
27 namespace wallet { 31 namespace wallet {
28 32
29 namespace { 33 namespace {
30 34
35 const char kFormEncodedMimeType[] = "application/x-www-form-urlencoded";
31 const char kJsonMimeType[] = "application/json"; 36 const char kJsonMimeType[] = "application/json";
37 const char kEscrowSensitiveInformationFormat[] =
38 "request_content_type=application/json&request=%s&cvn=%s&card_number=%s";
39 const char kGetFullWalletRequestFormat[] =
40 "request_content_type=application/json&request=%s&otp=%s:%s";
32 const size_t kOneTimePadLength = 6; 41 const size_t kOneTimePadLength = 6;
33 42
43 // The maximum number of bits in the one time pad that the server is willing to
44 // accept.
45 const size_t kMaxBits = 56;
46
47 // The minimum number of bits in the one time pad that the server is willing to
48 // accept.
49 const size_t kMinBits = 40;
50
34 std::string AutocheckoutStatusToString(AutocheckoutStatus status) { 51 std::string AutocheckoutStatusToString(AutocheckoutStatus status) {
35 switch (status) { 52 switch (status) {
36 case MISSING_FIELDMAPPING: 53 case MISSING_FIELDMAPPING:
37 return "MISSING_FIELDMAPPING"; 54 return "MISSING_FIELDMAPPING";
38 case MISSING_ADVANCE: 55 case MISSING_ADVANCE:
39 return "MISSING_ADVANCE"; 56 return "MISSING_ADVANCE";
40 case MISSING_CLICK_ELEMENT_BEFORE_FORM_FILLING: 57 case MISSING_CLICK_ELEMENT_BEFORE_FORM_FILLING:
41 return "MISSING_CLICK_ELEMENT_BEFORE_FORM_FILLING"; 58 return "MISSING_CLICK_ELEMENT_BEFORE_FORM_FILLING";
42 case MISSING_CLICK_ELEMENT_AFTER_FORM_FILLING: 59 case MISSING_CLICK_ELEMENT_AFTER_FORM_FILLING:
43 return "MISSING_CLICK_ELEMENT_AFTER_FORM_FILLING"; 60 return "MISSING_CLICK_ELEMENT_AFTER_FORM_FILLING";
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 const char kRiskParamsKey[] = "risk_params"; 233 const char kRiskParamsKey[] = "risk_params";
217 const char kSelectedAddressIdKey[] = "selected_address_id"; 234 const char kSelectedAddressIdKey[] = "selected_address_id";
218 const char kSelectedInstrumentIdKey[] = "selected_instrument_id"; 235 const char kSelectedInstrumentIdKey[] = "selected_instrument_id";
219 const char kSessionMaterialKey[] = "session_material"; 236 const char kSessionMaterialKey[] = "session_material";
220 const char kShippingAddressIdKey[] = "shipping_address_id"; 237 const char kShippingAddressIdKey[] = "shipping_address_id";
221 const char kShippingAddressKey[] = "shipping_address"; 238 const char kShippingAddressKey[] = "shipping_address";
222 const char kSuccessKey[] = "success"; 239 const char kSuccessKey[] = "success";
223 const char kUpgradedBillingAddressKey[] = "upgraded_billing_address"; 240 const char kUpgradedBillingAddressKey[] = "upgraded_billing_address";
224 const char kUpgradedInstrumentIdKey[] = "upgraded_instrument_id"; 241 const char kUpgradedInstrumentIdKey[] = "upgraded_instrument_id";
225 242
243 void UpdateInstrument(const Instrument& instrument,
244 base::DictionaryValue* request_dict) {
245 DCHECK(instrument.address() ||
246 (instrument.expiration_month() > 0 &&
247 instrument.expiration_year() > 0));
248
249 request_dict->SetString(kUpgradedInstrumentIdKey,
250 instrument.object_id());
251
252 if (instrument.address()) {
253 request_dict->SetString(kInstrumentPhoneNumberKey,
254 instrument.address()->phone_number());
255 request_dict->Set(
256 kUpgradedBillingAddressKey,
257 instrument.address()->ToDictionaryWithoutID().release());
258 }
259
260 if (instrument.expiration_month() > 0 && instrument.expiration_year() > 0) {
261 DCHECK(!instrument.card_verification_number().empty());
262 request_dict->SetInteger(kInstrumentExpMonthKey,
263 instrument.expiration_month());
264 request_dict->SetInteger(kInstrumentExpYearKey,
265 instrument.expiration_year());
266 }
267
268 if (request_dict->HasKey(kInstrumentKey))
269 request_dict->SetString(kInstrumentType, "CREDIT_CARD");
270 }
271
226 } // namespace 272 } // namespace
227 273
228 WalletClient::FullWalletRequest::FullWalletRequest( 274 WalletClient::FullWalletRequest::FullWalletRequest(
229 const std::string& instrument_id, 275 const std::string& instrument_id,
230 const std::string& address_id, 276 const std::string& address_id,
231 const GURL& source_url, 277 const GURL& source_url,
232 const std::string& google_transaction_id, 278 const std::string& google_transaction_id,
233 const std::vector<RiskCapability> risk_capabilities) 279 const std::vector<RiskCapability> risk_capabilities)
234 : instrument_id(instrument_id), 280 : instrument_id(instrument_id),
235 address_id(address_id), 281 address_id(address_id),
236 source_url(source_url), 282 source_url(source_url),
237 google_transaction_id(google_transaction_id), 283 google_transaction_id(google_transaction_id),
238 risk_capabilities(risk_capabilities) {} 284 risk_capabilities(risk_capabilities) {}
239 285
240 WalletClient::FullWalletRequest::~FullWalletRequest() {} 286 WalletClient::FullWalletRequest::~FullWalletRequest() {}
241 287
242 WalletClient::UpdateInstrumentRequest::UpdateInstrumentRequest(
243 const std::string& instrument_id,
244 const GURL& source_url)
245 : instrument_id(instrument_id),
246 expiration_month(0),
247 expiration_year(0),
248 source_url(source_url) {}
249
250 WalletClient::UpdateInstrumentRequest::~UpdateInstrumentRequest() {}
251
252 WalletClient::WalletClient(net::URLRequestContextGetter* context_getter, 288 WalletClient::WalletClient(net::URLRequestContextGetter* context_getter,
253 WalletClientDelegate* delegate) 289 WalletClientDelegate* delegate)
254 : context_getter_(context_getter), 290 : context_getter_(context_getter),
255 delegate_(delegate), 291 delegate_(delegate),
256 request_type_(NO_PENDING_REQUEST), 292 request_type_(NO_PENDING_REQUEST),
257 one_time_pad_(kOneTimePadLength), 293 one_time_pad_(kOneTimePadLength) {
258 encryption_escrow_client_(context_getter, this) {
259 DCHECK(context_getter_.get()); 294 DCHECK(context_getter_.get());
260 DCHECK(delegate_); 295 DCHECK(delegate_);
261 } 296 }
262 297
263 WalletClient::~WalletClient() {} 298 WalletClient::~WalletClient() {}
264 299
265 void WalletClient::AcceptLegalDocuments( 300 void WalletClient::AcceptLegalDocuments(
266 const std::vector<WalletItems::LegalDocument*>& documents, 301 const std::vector<WalletItems::LegalDocument*>& documents,
267 const std::string& google_transaction_id, 302 const std::string& google_transaction_id,
268 const GURL& source_url) { 303 const GURL& source_url) {
269 if (documents.empty()) 304 if (documents.empty())
270 return; 305 return;
271 306
272 std::vector<std::string> document_ids; 307 std::vector<std::string> document_ids;
273 for (size_t i = 0; i < documents.size(); ++i) { 308 for (size_t i = 0; i < documents.size(); ++i) {
274 document_ids.push_back(documents[i]->id()); 309 document_ids.push_back(documents[i]->id());
275 } 310 }
276 DoAcceptLegalDocuments(document_ids, google_transaction_id, source_url); 311 DoAcceptLegalDocuments(document_ids, google_transaction_id, source_url);
277 } 312 }
278 313
279 void WalletClient::AuthenticateInstrument( 314 void WalletClient::AuthenticateInstrument(
280 const std::string& instrument_id, 315 const std::string& instrument_id,
281 const std::string& card_verification_number, 316 const std::string& card_verification_number) {
282 const std::string& obfuscated_gaia_id) {
283 if (HasRequestInProgress()) { 317 if (HasRequestInProgress()) {
284 pending_requests_.push(base::Bind(&WalletClient::AuthenticateInstrument, 318 pending_requests_.push(base::Bind(&WalletClient::AuthenticateInstrument,
285 base::Unretained(this), 319 base::Unretained(this),
286 instrument_id, 320 instrument_id,
287 card_verification_number, 321 card_verification_number));
288 obfuscated_gaia_id));
289 return; 322 return;
290 } 323 }
291 324
292 DCHECK_EQ(NO_PENDING_REQUEST, request_type_); 325 DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
293 DCHECK(pending_request_body_.empty());
294 request_type_ = AUTHENTICATE_INSTRUMENT; 326 request_type_ = AUTHENTICATE_INSTRUMENT;
295 327
296 pending_request_body_.SetString(kApiKeyKey, google_apis::GetAPIKey()); 328 base::DictionaryValue request_dict;
297 pending_request_body_.SetString(kRiskParamsKey, delegate_->GetRiskData()); 329 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey());
298 pending_request_body_.SetString(kInstrumentIdKey, instrument_id); 330 request_dict.SetString(kRiskParamsKey, delegate_->GetRiskData());
331 request_dict.SetString(kInstrumentIdKey, instrument_id);
299 332
300 encryption_escrow_client_.EscrowCardVerificationNumber( 333 std::string json_payload;
301 card_verification_number, obfuscated_gaia_id); 334 base::JSONWriter::Write(&request_dict, &json_payload);
335
336 std::string escaped_card_verification_number = net::EscapeUrlEncodedData(
337 card_verification_number, true);
338
339 std::string post_body = base::StringPrintf(
340 kEscrowSensitiveInformationFormat,
341 net::EscapeUrlEncodedData(json_payload, true).c_str(),
342 escaped_card_verification_number.c_str(),
343 std::string().c_str());
344
345 MakeWalletRequest(GetAuthenticateInstrumentUrl(),
346 post_body,
347 kFormEncodedMimeType);
302 } 348 }
303 349
304 void WalletClient::GetFullWallet(const FullWalletRequest& full_wallet_request) { 350 void WalletClient::GetFullWallet(const FullWalletRequest& full_wallet_request) {
305 if (HasRequestInProgress()) { 351 if (HasRequestInProgress()) {
306 pending_requests_.push(base::Bind(&WalletClient::GetFullWallet, 352 pending_requests_.push(base::Bind(&WalletClient::GetFullWallet,
307 base::Unretained(this), 353 base::Unretained(this),
308 full_wallet_request)); 354 full_wallet_request));
309 return; 355 return;
310 } 356 }
311 357
312 DCHECK_EQ(NO_PENDING_REQUEST, request_type_); 358 DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
313 DCHECK(pending_request_body_.empty());
314 request_type_ = GET_FULL_WALLET; 359 request_type_ = GET_FULL_WALLET;
315 360
316 pending_request_body_.SetString(kApiKeyKey, google_apis::GetAPIKey()); 361 base::DictionaryValue request_dict;
317 pending_request_body_.SetString(kRiskParamsKey, delegate_->GetRiskData()); 362 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey());
318 pending_request_body_.SetString(kSelectedInstrumentIdKey, 363 request_dict.SetString(kRiskParamsKey, delegate_->GetRiskData());
319 full_wallet_request.instrument_id); 364 request_dict.SetString(kSelectedInstrumentIdKey,
320 pending_request_body_.SetString(kSelectedAddressIdKey, 365 full_wallet_request.instrument_id);
321 full_wallet_request.address_id); 366 request_dict.SetString(kSelectedAddressIdKey, full_wallet_request.address_id);
322 pending_request_body_.SetString( 367 request_dict.SetString(
323 kMerchantDomainKey, 368 kMerchantDomainKey,
324 full_wallet_request.source_url.GetWithEmptyPath().spec()); 369 full_wallet_request.source_url.GetWithEmptyPath().spec());
325 pending_request_body_.SetString(kGoogleTransactionIdKey, 370 request_dict.SetString(kGoogleTransactionIdKey,
326 full_wallet_request.google_transaction_id); 371 full_wallet_request.google_transaction_id);
327 pending_request_body_.SetString( 372 request_dict.SetString(kFeatureKey,
328 kFeatureKey, 373 DialogTypeToFeatureString(delegate_->GetDialogType()));
329 DialogTypeToFeatureString(delegate_->GetDialogType()));
330 374
331 scoped_ptr<base::ListValue> risk_capabilities_list(new base::ListValue()); 375 scoped_ptr<base::ListValue> risk_capabilities_list(new base::ListValue());
332 for (std::vector<RiskCapability>::const_iterator it = 376 for (std::vector<RiskCapability>::const_iterator it =
333 full_wallet_request.risk_capabilities.begin(); 377 full_wallet_request.risk_capabilities.begin();
334 it != full_wallet_request.risk_capabilities.end(); 378 it != full_wallet_request.risk_capabilities.end();
335 ++it) { 379 ++it) {
336 risk_capabilities_list->AppendString(RiskCapabilityToString(*it)); 380 risk_capabilities_list->AppendString(RiskCapabilityToString(*it));
337 } 381 }
338 pending_request_body_.Set(kRiskCapabilitiesKey, 382 request_dict.Set(kRiskCapabilitiesKey, risk_capabilities_list.release());
339 risk_capabilities_list.release()); 383
384 std::string json_payload;
385 base::JSONWriter::Write(&request_dict, &json_payload);
340 386
341 crypto::RandBytes(&(one_time_pad_[0]), one_time_pad_.size()); 387 crypto::RandBytes(&(one_time_pad_[0]), one_time_pad_.size());
342 encryption_escrow_client_.EncryptOneTimePad(one_time_pad_); 388
389 size_t num_bits = one_time_pad_.size() * 8;
390 DCHECK_LE(num_bits, kMaxBits);
391 DCHECK_GE(num_bits, kMinBits);
392
393 std::string post_body = base::StringPrintf(
394 kGetFullWalletRequestFormat,
395 net::EscapeUrlEncodedData(json_payload, true).c_str(),
396 base::HexEncode(&num_bits, 1).c_str(),
397 base::HexEncode(&(one_time_pad_[0]), one_time_pad_.size()).c_str());
398
399 MakeWalletRequest(GetGetFullWalletUrl(), post_body, kFormEncodedMimeType);
343 } 400 }
344 401
345 void WalletClient::GetWalletItems(const GURL& source_url) { 402 void WalletClient::SaveToWallet(scoped_ptr<Instrument> instrument,
403 scoped_ptr<Address> address,
404 const GURL& source_url) {
405 DCHECK(instrument || address);
346 if (HasRequestInProgress()) { 406 if (HasRequestInProgress()) {
347 pending_requests_.push(base::Bind(&WalletClient::GetWalletItems, 407 pending_requests_.push(base::Bind(&WalletClient::SaveToWallet,
348 base::Unretained(this), 408 base::Unretained(this),
409 base::Passed(&instrument),
410 base::Passed(&address),
349 source_url)); 411 source_url));
350 return; 412 return;
351 } 413 }
352 414
353 DCHECK_EQ(NO_PENDING_REQUEST, request_type_); 415 DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
354 request_type_ = GET_WALLET_ITEMS; 416 request_type_ = SAVE_TO_WALLET;
355 417
356 base::DictionaryValue request_dict; 418 base::DictionaryValue request_dict;
357 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey()); 419 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey());
358 request_dict.SetString(kMerchantDomainKey,
359 source_url.GetWithEmptyPath().spec());
360
361 std::string post_body;
362 base::JSONWriter::Write(&request_dict, &post_body);
363
364 MakeWalletRequest(GetGetWalletItemsUrl(), post_body);
365 }
366
367 void WalletClient::SaveAddress(const Address& shipping_address,
368 const GURL& source_url) {
369 if (HasRequestInProgress()) {
370 pending_requests_.push(base::Bind(&WalletClient::SaveAddress,
371 base::Unretained(this),
372 shipping_address,
373 source_url));
374 return;
375 }
376
377 DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
378 request_type_ = SAVE_ADDRESS;
379
380 base::DictionaryValue request_dict;
381 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey());
382 request_dict.SetString(kRiskParamsKey, delegate_->GetRiskData()); 420 request_dict.SetString(kRiskParamsKey, delegate_->GetRiskData());
383 request_dict.SetString(kMerchantDomainKey, 421 request_dict.SetString(kMerchantDomainKey,
384 source_url.GetWithEmptyPath().spec()); 422 source_url.GetWithEmptyPath().spec());
385 423
386 request_dict.Set(kShippingAddressKey, 424 std::string primary_account_number;
387 shipping_address.ToDictionaryWithID().release()); 425 std::string card_verification_number;
426 if (instrument) {
427 primary_account_number = net::EscapeUrlEncodedData(
428 UTF16ToUTF8(instrument->primary_account_number()), true);
429 card_verification_number = net::EscapeUrlEncodedData(
430 UTF16ToUTF8(instrument->card_verification_number()), true);
431 if (instrument->object_id().empty()) {
432 request_dict.Set(kInstrumentKey, instrument->ToDictionary().release());
433 request_dict.SetString(kInstrumentPhoneNumberKey,
434 instrument->address()->phone_number());
435 } else {
436 UpdateInstrument(*instrument, &request_dict);
437 }
438 }
439 if (address) {
440 request_dict.Set(kShippingAddressKey,
441 address->ToDictionaryWithID().release());
442 }
388 443
389 std::string post_body;
390 base::JSONWriter::Write(&request_dict, &post_body);
391 444
392 MakeWalletRequest(GetSaveToWalletUrl(), post_body); 445 std::string json_payload;
446 base::JSONWriter::Write(&request_dict, &json_payload);
447
448 std::string post_body = base::StringPrintf(
449 kEscrowSensitiveInformationFormat,
450 net::EscapeUrlEncodedData(json_payload, true).c_str(),
451 card_verification_number.c_str(),
452 primary_account_number.c_str());
453
454 MakeWalletRequest(GetSaveToWalletUrl(), post_body, kFormEncodedMimeType);
393 } 455 }
394 456
395 void WalletClient::SaveInstrument( 457 void WalletClient::GetWalletItems(const GURL& source_url) {
396 const Instrument& instrument,
397 const std::string& obfuscated_gaia_id,
398 const GURL& source_url) {
399 if (HasRequestInProgress()) { 458 if (HasRequestInProgress()) {
400 pending_requests_.push(base::Bind(&WalletClient::SaveInstrument, 459 pending_requests_.push(base::Bind(&WalletClient::GetWalletItems,
401 base::Unretained(this), 460 base::Unretained(this),
402 instrument,
403 obfuscated_gaia_id,
404 source_url)); 461 source_url));
405 return; 462 return;
406 } 463 }
407 464
408 DCHECK_EQ(NO_PENDING_REQUEST, request_type_); 465 DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
409 DCHECK(pending_request_body_.empty()); 466 request_type_ = GET_WALLET_ITEMS;
410 request_type_ = SAVE_INSTRUMENT;
411 467
412 pending_request_body_.SetString(kApiKeyKey, google_apis::GetAPIKey()); 468 base::DictionaryValue request_dict;
413 pending_request_body_.SetString(kRiskParamsKey, delegate_->GetRiskData()); 469 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey());
414 pending_request_body_.SetString(kMerchantDomainKey, 470 request_dict.SetString(kMerchantDomainKey,
415 source_url.GetWithEmptyPath().spec()); 471 source_url.GetWithEmptyPath().spec());
416 472
417 pending_request_body_.Set(kInstrumentKey, 473 std::string post_body;
418 instrument.ToDictionary().release()); 474 base::JSONWriter::Write(&request_dict, &post_body);
419 pending_request_body_.SetString(kInstrumentPhoneNumberKey,
420 instrument.address().phone_number());
421 475
422 encryption_escrow_client_.EscrowInstrumentInformation(instrument, 476 MakeWalletRequest(GetGetWalletItemsUrl(), post_body, kJsonMimeType);
423 obfuscated_gaia_id);
424 }
425
426 void WalletClient::SaveInstrumentAndAddress(
427 const Instrument& instrument,
428 const Address& address,
429 const std::string& obfuscated_gaia_id,
430 const GURL& source_url) {
431 if (HasRequestInProgress()) {
432 pending_requests_.push(base::Bind(&WalletClient::SaveInstrumentAndAddress,
433 base::Unretained(this),
434 instrument,
435 address,
436 obfuscated_gaia_id,
437 source_url));
438 return;
439 }
440
441 DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
442 DCHECK(pending_request_body_.empty());
443 request_type_ = SAVE_INSTRUMENT_AND_ADDRESS;
444
445 pending_request_body_.SetString(kApiKeyKey, google_apis::GetAPIKey());
446 pending_request_body_.SetString(kRiskParamsKey, delegate_->GetRiskData());
447 pending_request_body_.SetString(kMerchantDomainKey,
448 source_url.GetWithEmptyPath().spec());
449
450 pending_request_body_.Set(kInstrumentKey,
451 instrument.ToDictionary().release());
452 pending_request_body_.SetString(kInstrumentPhoneNumberKey,
453 instrument.address().phone_number());
454
455 pending_request_body_.Set(kShippingAddressKey,
456 address.ToDictionaryWithID().release());
457
458 encryption_escrow_client_.EscrowInstrumentInformation(instrument,
459 obfuscated_gaia_id);
460 } 477 }
461 478
462 void WalletClient::SendAutocheckoutStatus( 479 void WalletClient::SendAutocheckoutStatus(
463 AutocheckoutStatus status, 480 AutocheckoutStatus status,
464 const GURL& source_url, 481 const GURL& source_url,
465 const std::string& google_transaction_id) { 482 const std::string& google_transaction_id) {
466 DVLOG(1) << "Sending Autocheckout Status: " << status 483 DVLOG(1) << "Sending Autocheckout Status: " << status
467 << " for: " << source_url; 484 << " for: " << source_url;
468 if (HasRequestInProgress()) { 485 if (HasRequestInProgress()) {
469 pending_requests_.push(base::Bind(&WalletClient::SendAutocheckoutStatus, 486 pending_requests_.push(base::Bind(&WalletClient::SendAutocheckoutStatus,
(...skipping 13 matching lines...) Expand all
483 request_dict.SetBoolean(kSuccessKey, success); 500 request_dict.SetBoolean(kSuccessKey, success);
484 request_dict.SetString(kMerchantDomainKey, 501 request_dict.SetString(kMerchantDomainKey,
485 source_url.GetWithEmptyPath().spec()); 502 source_url.GetWithEmptyPath().spec());
486 if (!success) 503 if (!success)
487 request_dict.SetString(kReasonKey, AutocheckoutStatusToString(status)); 504 request_dict.SetString(kReasonKey, AutocheckoutStatusToString(status));
488 request_dict.SetString(kGoogleTransactionIdKey, google_transaction_id); 505 request_dict.SetString(kGoogleTransactionIdKey, google_transaction_id);
489 506
490 std::string post_body; 507 std::string post_body;
491 base::JSONWriter::Write(&request_dict, &post_body); 508 base::JSONWriter::Write(&request_dict, &post_body);
492 509
493 MakeWalletRequest(GetSendStatusUrl(), post_body); 510 MakeWalletRequest(GetSendStatusUrl(), post_body, kJsonMimeType);
494 }
495
496 void WalletClient::UpdateAddress(const Address& address,
497 const GURL& source_url) {
498 if (HasRequestInProgress()) {
499 pending_requests_.push(base::Bind(&WalletClient::UpdateAddress,
500 base::Unretained(this),
501 address,
502 source_url));
503 return;
504 }
505
506 DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
507 request_type_ = UPDATE_ADDRESS;
508
509 base::DictionaryValue request_dict;
510 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey());
511 request_dict.SetString(kRiskParamsKey, delegate_->GetRiskData());
512 request_dict.SetString(kMerchantDomainKey,
513 source_url.GetWithEmptyPath().spec());
514
515 request_dict.Set(kShippingAddressKey,
516 address.ToDictionaryWithID().release());
517
518 std::string post_body;
519 base::JSONWriter::Write(&request_dict, &post_body);
520
521 MakeWalletRequest(GetSaveToWalletUrl(), post_body);
522 }
523
524 void WalletClient::UpdateInstrument(
525 const UpdateInstrumentRequest& update_instrument_request,
526 scoped_ptr<Address> billing_address) {
527 if (HasRequestInProgress()) {
528 pending_requests_.push(base::Bind(&WalletClient::UpdateInstrument,
529 base::Unretained(this),
530 update_instrument_request,
531 base::Passed(&billing_address)));
532 return;
533 }
534
535 DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
536 DCHECK(pending_request_body_.empty());
537 DCHECK(update_instrument_request.card_verification_number.empty() ==
538 update_instrument_request.obfuscated_gaia_id.empty());
539 DCHECK(billing_address ||
540 (update_instrument_request.expiration_month > 0 &&
541 update_instrument_request.expiration_year > 0));
542
543 request_type_ = UPDATE_INSTRUMENT;
544
545 base::DictionaryValue* active_request_body;
546 base::DictionaryValue request_dict;
547 if (update_instrument_request.card_verification_number.empty())
548 active_request_body = &request_dict;
549 else
550 active_request_body = &pending_request_body_;
551
552 active_request_body->SetString(kApiKeyKey, google_apis::GetAPIKey());
553 active_request_body->SetString(kRiskParamsKey, delegate_->GetRiskData());
554 active_request_body->SetString(
555 kMerchantDomainKey,
556 update_instrument_request.source_url.GetWithEmptyPath().spec());
557
558 active_request_body->SetString(kUpgradedInstrumentIdKey,
559 update_instrument_request.instrument_id);
560
561 if (billing_address) {
562 active_request_body->SetString(kInstrumentPhoneNumberKey,
563 billing_address->phone_number());
564 active_request_body->Set(
565 kUpgradedBillingAddressKey,
566 billing_address->ToDictionaryWithoutID().release());
567 }
568
569 if (update_instrument_request.expiration_month > 0 &&
570 update_instrument_request.expiration_year > 0) {
571 DCHECK(!update_instrument_request.card_verification_number.empty());
572 active_request_body->SetInteger(
573 kInstrumentExpMonthKey,
574 update_instrument_request.expiration_month);
575 active_request_body->SetInteger(kInstrumentExpYearKey,
576 update_instrument_request.expiration_year);
577 }
578
579 if (active_request_body->HasKey(kInstrumentKey))
580 active_request_body->SetString(kInstrumentType, "CREDIT_CARD");
581
582 if (update_instrument_request.card_verification_number.empty()) {
583 std::string post_body;
584 base::JSONWriter::Write(active_request_body, &post_body);
585 MakeWalletRequest(GetSaveToWalletUrl(), post_body);
586 } else {
587 encryption_escrow_client_.EscrowCardVerificationNumber(
588 update_instrument_request.card_verification_number,
589 update_instrument_request.obfuscated_gaia_id);
590 }
591 } 511 }
592 512
593 bool WalletClient::HasRequestInProgress() const { 513 bool WalletClient::HasRequestInProgress() const {
594 // |SaveInstrument*()| and |UpdateInstrument()| methods don't set |request_| 514 return !!request_;
595 // until sensitive info has been escrowed, so this class is considered to have
596 // a request in progress if |encryption_escrow_client_| is working as well.
597 return request_ || encryption_escrow_client_.HasRequestInProgress();
598 } 515 }
599 516
600 void WalletClient::CancelRequests() { 517 void WalletClient::CancelRequests() {
601 encryption_escrow_client_.CancelRequest();
602 pending_request_body_.Clear();
603 request_.reset(); 518 request_.reset();
604 request_type_ = NO_PENDING_REQUEST; 519 request_type_ = NO_PENDING_REQUEST;
605 while (!pending_requests_.empty()) { 520 while (!pending_requests_.empty()) {
606 pending_requests_.pop(); 521 pending_requests_.pop();
607 } 522 }
608 } 523 }
609 524
610 void WalletClient::DoAcceptLegalDocuments( 525 void WalletClient::DoAcceptLegalDocuments(
611 const std::vector<std::string>& document_ids, 526 const std::vector<std::string>& document_ids,
612 const std::string& google_transaction_id, 527 const std::string& google_transaction_id,
(...skipping 19 matching lines...) Expand all
632 for (std::vector<std::string>::const_iterator it = document_ids.begin(); 547 for (std::vector<std::string>::const_iterator it = document_ids.begin();
633 it != document_ids.end(); ++it) { 548 it != document_ids.end(); ++it) {
634 if (!it->empty()) 549 if (!it->empty())
635 docs_list->AppendString(*it); 550 docs_list->AppendString(*it);
636 } 551 }
637 request_dict.Set(kAcceptedLegalDocumentKey, docs_list.release()); 552 request_dict.Set(kAcceptedLegalDocumentKey, docs_list.release());
638 553
639 std::string post_body; 554 std::string post_body;
640 base::JSONWriter::Write(&request_dict, &post_body); 555 base::JSONWriter::Write(&request_dict, &post_body);
641 556
642 MakeWalletRequest(GetAcceptLegalDocumentsUrl(), post_body); 557 MakeWalletRequest(GetAcceptLegalDocumentsUrl(), post_body, kJsonMimeType);
643 } 558 }
644 559
645 void WalletClient::MakeWalletRequest(const GURL& url, 560 void WalletClient::MakeWalletRequest(const GURL& url,
646 const std::string& post_body) { 561 const std::string& post_body,
562 const std::string& mime_type) {
647 DCHECK(!HasRequestInProgress()); 563 DCHECK(!HasRequestInProgress());
648 564
649 request_.reset(net::URLFetcher::Create( 565 request_.reset(net::URLFetcher::Create(
650 0, url, net::URLFetcher::POST, this)); 566 0, url, net::URLFetcher::POST, this));
651 request_->SetRequestContext(context_getter_.get()); 567 request_->SetRequestContext(context_getter_.get());
652 DVLOG(1) << "Making request to " << url << " with post_body=" << post_body; 568 DVLOG(1) << "Making request to " << url << " with post_body=" << post_body;
653 request_->SetUploadData(kJsonMimeType, post_body); 569 request_->SetUploadData(mime_type, post_body);
654 request_started_timestamp_ = base::Time::Now(); 570 request_started_timestamp_ = base::Time::Now();
655 request_->Start(); 571 request_->Start();
656 572
657 delegate_->GetMetricLogger().LogWalletErrorMetric( 573 delegate_->GetMetricLogger().LogWalletErrorMetric(
658 delegate_->GetDialogType(), 574 delegate_->GetDialogType(),
659 AutofillMetrics::WALLET_ERROR_BASELINE_ISSUED_REQUEST); 575 AutofillMetrics::WALLET_ERROR_BASELINE_ISSUED_REQUEST);
660 delegate_->GetMetricLogger().LogWalletRequiredActionMetric( 576 delegate_->GetMetricLogger().LogWalletRequiredActionMetric(
661 delegate_->GetDialogType(), 577 delegate_->GetDialogType(),
662 AutofillMetrics::WALLET_REQUIRED_ACTION_BASELINE_ISSUED_REQUEST); 578 AutofillMetrics::WALLET_REQUIRED_ACTION_BASELINE_ISSUED_REQUEST);
663 } 579 }
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 WalletItems::CreateWalletItems(*response_dict)); 684 WalletItems::CreateWalletItems(*response_dict));
769 if (wallet_items) { 685 if (wallet_items) {
770 LogRequiredActions(wallet_items->required_actions()); 686 LogRequiredActions(wallet_items->required_actions());
771 delegate_->OnDidGetWalletItems(wallet_items.Pass()); 687 delegate_->OnDidGetWalletItems(wallet_items.Pass());
772 } else { 688 } else {
773 HandleMalformedResponse(); 689 HandleMalformedResponse();
774 } 690 }
775 break; 691 break;
776 } 692 }
777 693
778 case SAVE_ADDRESS: { 694 case SAVE_TO_WALLET: {
779 std::string shipping_address_id;
780 std::vector<RequiredAction> required_actions;
781 GetRequiredActionsForSaveToWallet(*response_dict, &required_actions);
782 std::vector<FormFieldError> form_errors;
783 GetFormFieldErrors(*response_dict, &form_errors);
784 if (response_dict->GetString(kShippingAddressIdKey,
785 &shipping_address_id) ||
786 !required_actions.empty()) {
787 LogRequiredActions(required_actions);
788 delegate_->OnDidSaveAddress(shipping_address_id,
789 required_actions,
790 form_errors);
791 } else {
792 HandleMalformedResponse();
793 }
794 break;
795 }
796
797 case SAVE_INSTRUMENT: {
798 std::string instrument_id;
799 std::vector<RequiredAction> required_actions;
800 GetRequiredActionsForSaveToWallet(*response_dict, &required_actions);
801 std::vector<FormFieldError> form_errors;
802 GetFormFieldErrors(*response_dict, &form_errors);
803 if (response_dict->GetString(kInstrumentIdKey, &instrument_id) ||
804 !required_actions.empty()) {
805 LogRequiredActions(required_actions);
806 delegate_->OnDidSaveInstrument(instrument_id,
807 required_actions,
808 form_errors);
809 } else {
810 HandleMalformedResponse();
811 }
812 break;
813 }
814
815 case SAVE_INSTRUMENT_AND_ADDRESS: {
816 std::string instrument_id; 695 std::string instrument_id;
817 response_dict->GetString(kInstrumentIdKey, &instrument_id); 696 response_dict->GetString(kInstrumentIdKey, &instrument_id);
818 std::string shipping_address_id; 697 std::string shipping_address_id;
819 response_dict->GetString(kShippingAddressIdKey, 698 response_dict->GetString(kShippingAddressIdKey,
820 &shipping_address_id); 699 &shipping_address_id);
821 std::vector<RequiredAction> required_actions; 700 std::vector<RequiredAction> required_actions;
822 GetRequiredActionsForSaveToWallet(*response_dict, &required_actions); 701 GetRequiredActionsForSaveToWallet(*response_dict, &required_actions);
823 std::vector<FormFieldError> form_errors; 702 std::vector<FormFieldError> form_errors;
824 GetFormFieldErrors(*response_dict, &form_errors); 703 GetFormFieldErrors(*response_dict, &form_errors);
825 if ((!instrument_id.empty() && !shipping_address_id.empty()) || 704 if ((!instrument_id.empty() || !shipping_address_id.empty()) ||
Raman Kakilate 2013/07/01 19:37:56 you can get rid of parathesis. Its "||" across. o
ahutter 2013/07/01 21:37:45 Done.
826 !required_actions.empty()) { 705 !required_actions.empty()) {
827 LogRequiredActions(required_actions); 706 LogRequiredActions(required_actions);
828 delegate_->OnDidSaveInstrumentAndAddress(instrument_id, 707 delegate_->OnDidSaveToWallet(instrument_id,
829 shipping_address_id, 708 shipping_address_id,
830 required_actions, 709 required_actions,
831 form_errors); 710 form_errors);
832 } else { 711 } else {
833 HandleMalformedResponse(); 712 HandleMalformedResponse();
834 } 713 }
835 break;
836 }
837
838 case UPDATE_ADDRESS: {
839 std::string address_id;
840 std::vector<RequiredAction> required_actions;
841 GetRequiredActionsForSaveToWallet(*response_dict, &required_actions);
842 std::vector<FormFieldError> form_errors;
843 GetFormFieldErrors(*response_dict, &form_errors);
844 if (response_dict->GetString(kShippingAddressIdKey, &address_id) ||
845 !required_actions.empty()) {
846 LogRequiredActions(required_actions);
847 delegate_->OnDidUpdateAddress(address_id,
848 required_actions,
849 form_errors);
850 } else {
851 HandleMalformedResponse();
852 }
853 break;
854 }
855
856 case UPDATE_INSTRUMENT: {
857 std::string instrument_id;
858 std::vector<RequiredAction> required_actions;
859 GetRequiredActionsForSaveToWallet(*response_dict, &required_actions);
860 std::vector<FormFieldError> form_errors;
861 GetFormFieldErrors(*response_dict, &form_errors);
862 if (response_dict->GetString(kInstrumentIdKey, &instrument_id) ||
863 !required_actions.empty()) {
864 LogRequiredActions(required_actions);
865 delegate_->OnDidUpdateInstrument(instrument_id,
866 required_actions,
867 form_errors);
868 } else {
869 HandleMalformedResponse();
870 }
871 break; 714 break;
872 } 715 }
873 716
874 case NO_PENDING_REQUEST: 717 case NO_PENDING_REQUEST:
875 NOTREACHED(); 718 NOTREACHED();
876 } 719 }
877 720
878 request_.reset(); 721 request_.reset();
879 StartNextPendingRequest(); 722 StartNextPendingRequest();
880 } 723 }
(...skipping 12 matching lines...) Expand all
893 request_->ReceivedContentWasMalformed(); 736 request_->ReceivedContentWasMalformed();
894 HandleWalletError(MALFORMED_RESPONSE); 737 HandleWalletError(MALFORMED_RESPONSE);
895 } 738 }
896 739
897 void WalletClient::HandleWalletError(WalletClient::ErrorType error_type) { 740 void WalletClient::HandleWalletError(WalletClient::ErrorType error_type) {
898 delegate_->OnWalletError(error_type); 741 delegate_->OnWalletError(error_type);
899 delegate_->GetMetricLogger().LogWalletErrorMetric( 742 delegate_->GetMetricLogger().LogWalletErrorMetric(
900 delegate_->GetDialogType(), ErrorTypeToUmaMetric(error_type)); 743 delegate_->GetDialogType(), ErrorTypeToUmaMetric(error_type));
901 } 744 }
902 745
903 void WalletClient::OnDidEncryptOneTimePad(
904 const std::string& encrypted_one_time_pad,
905 const std::string& session_material) {
906 DCHECK_EQ(GET_FULL_WALLET, request_type_);
907 pending_request_body_.SetString(kEncryptedOtpKey, encrypted_one_time_pad);
908 pending_request_body_.SetString(kSessionMaterialKey, session_material);
909
910 std::string post_body;
911 base::JSONWriter::Write(&pending_request_body_, &post_body);
912 pending_request_body_.Clear();
913
914 MakeWalletRequest(GetGetFullWalletUrl(), post_body);
915 }
916
917 void WalletClient::OnDidEscrowInstrumentInformation(
918 const std::string& escrow_handle) {
919 DCHECK(request_type_ == SAVE_INSTRUMENT ||
920 request_type_ == SAVE_INSTRUMENT_AND_ADDRESS);
921
922 pending_request_body_.SetString(kInstrumentEscrowHandleKey, escrow_handle);
923
924 std::string post_body;
925 base::JSONWriter::Write(&pending_request_body_, &post_body);
926 pending_request_body_.Clear();
927
928 MakeWalletRequest(GetSaveToWalletUrl(), post_body);
929 }
930
931 void WalletClient::OnDidEscrowCardVerificationNumber(
932 const std::string& escrow_handle) {
933 DCHECK(request_type_ == AUTHENTICATE_INSTRUMENT ||
934 request_type_ == UPDATE_INSTRUMENT);
935 pending_request_body_.SetString(kInstrumentEscrowHandleKey, escrow_handle);
936
937 std::string post_body;
938 base::JSONWriter::Write(&pending_request_body_, &post_body);
939 pending_request_body_.Clear();
940
941 if (request_type_ == AUTHENTICATE_INSTRUMENT)
942 MakeWalletRequest(GetAuthenticateInstrumentUrl(), post_body);
943 else
944 MakeWalletRequest(GetSaveToWalletUrl(), post_body);
945 }
946
947 void WalletClient::OnDidMakeRequest() {
948 delegate_->GetMetricLogger().LogWalletErrorMetric(
949 delegate_->GetDialogType(),
950 AutofillMetrics::WALLET_ERROR_BASELINE_ISSUED_REQUEST);
951 }
952
953 void WalletClient::OnNetworkError() {
954 HandleWalletError(NETWORK_ERROR);
955 }
956
957 void WalletClient::OnMalformedResponse() {
958 HandleWalletError(MALFORMED_RESPONSE);
959 }
960
961 // Logs an UMA metric for each of the |required_actions|. 746 // Logs an UMA metric for each of the |required_actions|.
962 void WalletClient::LogRequiredActions( 747 void WalletClient::LogRequiredActions(
963 const std::vector<RequiredAction>& required_actions) const { 748 const std::vector<RequiredAction>& required_actions) const {
964 for (size_t i = 0; i < required_actions.size(); ++i) { 749 for (size_t i = 0; i < required_actions.size(); ++i) {
965 delegate_->GetMetricLogger().LogWalletRequiredActionMetric( 750 delegate_->GetMetricLogger().LogWalletRequiredActionMetric(
966 delegate_->GetDialogType(), 751 delegate_->GetDialogType(),
967 RequiredActionToUmaMetric(required_actions[i])); 752 RequiredActionToUmaMetric(required_actions[i]));
968 } 753 }
969 } 754 }
970 755
971 AutofillMetrics::WalletApiCallMetric WalletClient::RequestTypeToUmaMetric( 756 AutofillMetrics::WalletApiCallMetric WalletClient::RequestTypeToUmaMetric(
972 RequestType request_type) const { 757 RequestType request_type) const {
973 switch (request_type) { 758 switch (request_type) {
974 case ACCEPT_LEGAL_DOCUMENTS: 759 case ACCEPT_LEGAL_DOCUMENTS:
975 return AutofillMetrics::ACCEPT_LEGAL_DOCUMENTS; 760 return AutofillMetrics::ACCEPT_LEGAL_DOCUMENTS;
976 case AUTHENTICATE_INSTRUMENT: 761 case AUTHENTICATE_INSTRUMENT:
977 return AutofillMetrics::AUTHENTICATE_INSTRUMENT; 762 return AutofillMetrics::AUTHENTICATE_INSTRUMENT;
978 case GET_FULL_WALLET: 763 case GET_FULL_WALLET:
979 return AutofillMetrics::GET_FULL_WALLET; 764 return AutofillMetrics::GET_FULL_WALLET;
980 case GET_WALLET_ITEMS: 765 case GET_WALLET_ITEMS:
981 return AutofillMetrics::GET_WALLET_ITEMS; 766 return AutofillMetrics::GET_WALLET_ITEMS;
982 case SAVE_ADDRESS: 767 case SAVE_TO_WALLET:
983 return AutofillMetrics::SAVE_ADDRESS; 768 return AutofillMetrics::SAVE_TO_WALLET;
984 case SAVE_INSTRUMENT:
985 return AutofillMetrics::SAVE_INSTRUMENT;
986 case SAVE_INSTRUMENT_AND_ADDRESS:
987 return AutofillMetrics::SAVE_INSTRUMENT_AND_ADDRESS;
988 case SEND_STATUS: 769 case SEND_STATUS:
989 return AutofillMetrics::SEND_STATUS; 770 return AutofillMetrics::SEND_STATUS;
990 case UPDATE_ADDRESS:
991 return AutofillMetrics::UPDATE_ADDRESS;
992 case UPDATE_INSTRUMENT:
993 return AutofillMetrics::UPDATE_INSTRUMENT;
994 case NO_PENDING_REQUEST: 771 case NO_PENDING_REQUEST:
995 NOTREACHED(); 772 NOTREACHED();
996 return AutofillMetrics::UNKNOWN_API_CALL; 773 return AutofillMetrics::UNKNOWN_API_CALL;
997 } 774 }
998 775
999 NOTREACHED(); 776 NOTREACHED();
1000 return AutofillMetrics::UNKNOWN_API_CALL; 777 return AutofillMetrics::UNKNOWN_API_CALL;
1001 } 778 }
1002 779
1003 } // namespace wallet 780 } // namespace wallet
1004 } // namespace autofill 781 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698