OLD | NEW |
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/full_wallet.h" | 5 #include "components/autofill/content/browser/wallet/full_wallet.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 } // anonymous namespace | 21 } // anonymous namespace |
22 | 22 |
23 namespace autofill { | 23 namespace autofill { |
24 namespace wallet { | 24 namespace wallet { |
25 | 25 |
26 FullWallet::FullWallet(int expiration_month, | 26 FullWallet::FullWallet(int expiration_month, |
27 int expiration_year, | 27 int expiration_year, |
28 const std::string& iin, | 28 const std::string& iin, |
29 const std::string& encrypted_rest, | 29 const std::string& encrypted_rest, |
30 scoped_ptr<Address> billing_address, | 30 scoped_ptr<Address> billing_address, |
31 scoped_ptr<Address> shipping_address, | 31 scoped_ptr<Address> shipping_address) |
32 const std::vector<RequiredAction>& required_actions) | |
33 : expiration_month_(expiration_month), | 32 : expiration_month_(expiration_month), |
34 expiration_year_(expiration_year), | 33 expiration_year_(expiration_year), |
35 iin_(iin), | 34 iin_(iin), |
36 encrypted_rest_(encrypted_rest), | 35 encrypted_rest_(encrypted_rest), |
37 billing_address_(billing_address.Pass()), | 36 billing_address_(billing_address.Pass()), |
38 shipping_address_(shipping_address.Pass()), | 37 shipping_address_(shipping_address.Pass()) { |
39 required_actions_(required_actions) { | 38 DCHECK(billing_address_.get()); |
40 DCHECK(required_actions_.size() > 0 || billing_address_.get()); | |
41 } | 39 } |
42 | 40 |
43 FullWallet::~FullWallet() {} | 41 FullWallet::~FullWallet() {} |
44 | 42 |
45 // static | 43 // static |
46 scoped_ptr<FullWallet> | 44 scoped_ptr<FullWallet> |
47 FullWallet::CreateFullWallet(const base::DictionaryValue& dictionary) { | |
48 const base::ListValue* required_actions_list; | |
49 std::vector<RequiredAction> required_actions; | |
50 if (dictionary.GetList("required_action", &required_actions_list)) { | |
51 for (size_t i = 0; i < required_actions_list->GetSize(); ++i) { | |
52 std::string action_string; | |
53 if (required_actions_list->GetString(i, &action_string)) { | |
54 RequiredAction action = ParseRequiredActionFromString(action_string); | |
55 if (!ActionAppliesToFullWallet(action)) { | |
56 DLOG(ERROR) << "Response from Google wallet with bad required action:" | |
57 " \"" << action_string << "\""; | |
58 return scoped_ptr<FullWallet>(); | |
59 } | |
60 required_actions.push_back(action); | |
61 } | |
62 } | |
63 if (required_actions.size() > 0) { | |
64 return scoped_ptr<FullWallet>(new FullWallet(-1, | |
65 -1, | |
66 std::string(), | |
67 std::string(), | |
68 scoped_ptr<Address>(), | |
69 scoped_ptr<Address>(), | |
70 required_actions)); | |
71 } | |
72 } else { | |
73 DVLOG(1) << "Response from Google wallet missing required actions"; | |
74 } | |
75 | |
76 int expiration_month; | |
77 if (!dictionary.GetInteger("expiration_month", &expiration_month)) { | |
78 DLOG(ERROR) << "Response from Google wallet missing expiration month"; | |
79 return scoped_ptr<FullWallet>(); | |
80 } | |
81 | |
82 int expiration_year; | |
83 if (!dictionary.GetInteger("expiration_year", &expiration_year)) { | |
84 DLOG(ERROR) << "Response from Google wallet missing expiration year"; | |
85 return scoped_ptr<FullWallet>(); | |
86 } | |
87 | |
88 std::string iin; | |
89 if (!dictionary.GetString("iin", &iin)) { | |
90 DLOG(ERROR) << "Response from Google wallet missing iin"; | |
91 return scoped_ptr<FullWallet>(); | |
92 } | |
93 | |
94 std::string encrypted_rest; | |
95 if (!dictionary.GetString("rest", &encrypted_rest)) { | |
96 DLOG(ERROR) << "Response from Google wallet missing rest"; | |
97 return scoped_ptr<FullWallet>(); | |
98 } | |
99 | |
100 const base::DictionaryValue* billing_address_dict; | |
101 if (!dictionary.GetDictionary("billing_address", &billing_address_dict)) { | |
102 DLOG(ERROR) << "Response from Google wallet missing billing address"; | |
103 return scoped_ptr<FullWallet>(); | |
104 } | |
105 | |
106 scoped_ptr<Address> billing_address = | |
107 Address::CreateAddress(*billing_address_dict); | |
108 if (!billing_address.get()) { | |
109 DLOG(ERROR) << "Response from Google wallet has malformed billing address"; | |
110 return scoped_ptr<FullWallet>(); | |
111 } | |
112 | |
113 const base::DictionaryValue* shipping_address_dict; | |
114 scoped_ptr<Address> shipping_address; | |
115 if (dictionary.GetDictionary("shipping_address", &shipping_address_dict)) { | |
116 shipping_address = | |
117 Address::CreateAddressWithID(*shipping_address_dict); | |
118 } else { | |
119 DVLOG(1) << "Response from Google wallet missing shipping address"; | |
120 } | |
121 | |
122 return scoped_ptr<FullWallet>(new FullWallet(expiration_month, | |
123 expiration_year, | |
124 iin, | |
125 encrypted_rest, | |
126 billing_address.Pass(), | |
127 shipping_address.Pass(), | |
128 required_actions)); | |
129 } | |
130 | |
131 // static | |
132 scoped_ptr<FullWallet> | |
133 FullWallet::CreateFullWalletFromClearText( | 45 FullWallet::CreateFullWalletFromClearText( |
134 int expiration_month, | 46 int expiration_month, |
135 int expiration_year, | 47 int expiration_year, |
136 const std::string& pan, | 48 const std::string& pan, |
137 const std::string& cvn, | 49 const std::string& cvn, |
138 scoped_ptr<Address> billing_address, | 50 scoped_ptr<Address> billing_address, |
139 scoped_ptr<Address> shipping_address) { | 51 scoped_ptr<Address> shipping_address) { |
140 DCHECK(billing_address); | 52 DCHECK(billing_address); |
141 DCHECK(!pan.empty()); | 53 DCHECK(!pan.empty()); |
142 DCHECK(!cvn.empty()); | 54 DCHECK(!cvn.empty()); |
143 | 55 |
144 scoped_ptr<FullWallet> wallet(new FullWallet( | 56 scoped_ptr<FullWallet> wallet(new FullWallet( |
145 expiration_month, | 57 expiration_month, expiration_year, |
146 expiration_year, | |
147 std::string(), // no iin -- clear text pan/cvn are set below. | 58 std::string(), // no iin -- clear text pan/cvn are set below. |
148 std::string(), // no encrypted_rest -- clear text pan/cvn are set below. | 59 std::string(), // no encrypted_rest -- clear text pan/cvn are set below. |
149 billing_address.Pass(), | 60 billing_address.Pass(), shipping_address.Pass())); |
150 shipping_address.Pass(), | |
151 std::vector<RequiredAction>())); // no required actions in clear text. | |
152 wallet->pan_ = pan; | 61 wallet->pan_ = pan; |
153 wallet->cvn_ = cvn; | 62 wallet->cvn_ = cvn; |
154 return wallet.Pass(); | 63 return wallet.Pass(); |
155 } | 64 } |
156 | 65 |
157 base::string16 FullWallet::GetInfo(const std::string& app_locale, | 66 base::string16 FullWallet::GetInfo(const std::string& app_locale, |
158 const AutofillType& type) { | 67 const AutofillType& type) { |
159 switch (type.GetStorableType()) { | 68 switch (type.GetStorableType()) { |
160 case CREDIT_CARD_NUMBER: | 69 case CREDIT_CARD_NUMBER: |
161 return base::ASCIIToUTF16(GetPan()); | 70 return base::ASCIIToUTF16(GetPan()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 case CREDIT_CARD: | 120 case CREDIT_CARD: |
212 NOTREACHED(); | 121 NOTREACHED(); |
213 | 122 |
214 default: | 123 default: |
215 return shipping_address_->GetInfo(type, app_locale); | 124 return shipping_address_->GetInfo(type, app_locale); |
216 } | 125 } |
217 } | 126 } |
218 } | 127 } |
219 } | 128 } |
220 | 129 |
221 bool FullWallet::HasRequiredAction(RequiredAction action) const { | |
222 DCHECK(ActionAppliesToFullWallet(action)); | |
223 return std::find(required_actions_.begin(), | |
224 required_actions_.end(), | |
225 action) != required_actions_.end(); | |
226 } | |
227 | |
228 base::string16 FullWallet::TypeAndLastFourDigits() { | 130 base::string16 FullWallet::TypeAndLastFourDigits() { |
229 CreditCard card; | 131 CreditCard card; |
230 card.SetRawInfo(CREDIT_CARD_NUMBER, base::ASCIIToUTF16(GetPan())); | 132 card.SetRawInfo(CREDIT_CARD_NUMBER, base::ASCIIToUTF16(GetPan())); |
231 return card.TypeAndLastFourDigits(); | 133 return card.TypeAndLastFourDigits(); |
232 } | 134 } |
233 | 135 |
234 const std::string& FullWallet::GetPan() { | 136 const std::string& FullWallet::GetPan() { |
235 if (pan_.empty()) | 137 if (pan_.empty()) |
236 DecryptCardInfo(); | 138 DecryptCardInfo(); |
237 return pan_; | 139 return pan_; |
(...skipping 19 matching lines...) Expand all Loading... |
257 return false; | 159 return false; |
258 } | 160 } |
259 | 161 |
260 if (shipping_address_.get() && other.shipping_address_.get()) { | 162 if (shipping_address_.get() && other.shipping_address_.get()) { |
261 if (*shipping_address_.get() != *other.shipping_address_.get()) | 163 if (*shipping_address_.get() != *other.shipping_address_.get()) |
262 return false; | 164 return false; |
263 } else if (shipping_address_.get() || other.shipping_address_.get()) { | 165 } else if (shipping_address_.get() || other.shipping_address_.get()) { |
264 return false; | 166 return false; |
265 } | 167 } |
266 | 168 |
267 if (required_actions_ != other.required_actions_) | |
268 return false; | |
269 | |
270 return true; | 169 return true; |
271 } | 170 } |
272 | 171 |
273 bool FullWallet::operator!=(const FullWallet& other) const { | 172 bool FullWallet::operator!=(const FullWallet& other) const { |
274 return !(*this == other); | 173 return !(*this == other); |
275 } | 174 } |
276 | 175 |
277 void FullWallet::DecryptCardInfo() { | 176 void FullWallet::DecryptCardInfo() { |
278 // |encrypted_rest_| must be of length |kEncryptedRestSize| in order for | 177 // |encrypted_rest_| must be of length |kEncryptedRestSize| in order for |
279 // decryption to succeed and the server will not pad it with zeros. | 178 // decryption to succeed and the server will not pad it with zeros. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 } | 223 } |
325 | 224 |
326 const std::string& FullWallet::GetCvn() { | 225 const std::string& FullWallet::GetCvn() { |
327 if (cvn_.empty()) | 226 if (cvn_.empty()) |
328 DecryptCardInfo(); | 227 DecryptCardInfo(); |
329 return cvn_; | 228 return cvn_; |
330 } | 229 } |
331 | 230 |
332 } // namespace wallet | 231 } // namespace wallet |
333 } // namespace autofill | 232 } // namespace autofill |
OLD | NEW |