OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "chrome/browser/autofill/wallet/wallet_data_retriever.h" | |
6 | |
7 #include "base/json/json_reader.h" | |
8 #include "base/json/json_writer.h" | |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/string_number_conversions.h" | |
12 #include "base/string_split.h" | |
13 #include "base/stringprintf.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/autofill/wallet/cart.h" | |
16 #include "chrome/browser/autofill/wallet/full_wallet.h" | |
17 #include "chrome/browser/autofill/wallet/wallet_address.h" | |
18 #include "chrome/browser/autofill/wallet/wallet_items.h" | |
19 #include "chrome/browser/autofill/wallet/wallet_service_url.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "net/http/http_status_code.h" | |
22 #include "net/url_request/url_fetcher.h" | |
23 #include "net/url_request/url_fetcher_delegate.h" | |
24 #include "net/url_request/url_request_context_getter.h" | |
25 | |
26 namespace { | |
27 static const char kEncryptOtpBodyFormat[] = "cvv=%s:%s"; | |
28 static const size_t kMaxBits = 63; | |
29 | |
30 } // end anonymous namespace | |
31 | |
32 namespace wallet { | |
33 | |
34 class WalletDataRetriever::Core | |
35 : public base::RefCountedThreadSafe<WalletDataRetriever::Core>, | |
36 public net::URLFetcherDelegate { | |
37 public: | |
38 explicit Core(net::URLRequestContextGetter* context_getter) | |
39 : context_getter_(context_getter), | |
40 delegate_(NULL), | |
41 request_type_(NO_PENDING_REQUEST) {} | |
42 void AcceptLegalDocuments(const std::vector<std::string> document_ids, | |
43 const std::string& google_transaction_id, | |
44 Delegate* delegate); | |
45 void EncryptOtp(const void* otp, size_t length, Delegate* delegate); | |
Albert Bodenhamer
2012/11/30 01:06:02
Does this really need to be a void*?
ahutter
2012/12/01 04:06:50
It's a void* because that's what crypto/random.h s
| |
46 void GetFullWallet(const std::string& instrument_id, | |
47 const std::string& address_id, | |
48 const std::string& merchant_domain, | |
49 const Cart& cart, | |
50 const std::string& google_transaction_id, | |
51 const std::string& encrypted_otp, | |
52 const std::string& session_material, | |
53 Delegate* delegate); | |
54 void GetWalletItems(Delegate* delegate); | |
55 void SendExtendedAutofillStatus(bool success, | |
56 const std::string& merchant_domain, | |
57 const std::string& reason, | |
58 const std::string& google_transaction_id, | |
59 Delegate* delegate); | |
60 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
61 private: | |
62 friend class base::RefCountedThreadSafe<Core>; | |
63 // TODO(ahutter): Implement this. | |
Albert Bodenhamer
2012/11/30 01:06:02
Ilya is currently working on the risk encoder.
| |
64 std::string GetRiskParams() { return ""; } | |
65 | |
66 enum RequestType { | |
67 NO_PENDING_REQUEST, | |
68 ACCEPT_LEGAL_DOCUMENTS, | |
69 ENCRYPT_OTP, | |
70 GET_FULL_WALLET, | |
71 GET_WALLET_ITEMS, | |
72 SEND_STATUS, | |
73 }; | |
74 | |
75 virtual ~Core() {} | |
76 | |
77 void MakeWalletRequest(const GURL url, | |
78 const std::string& post_body, | |
79 WalletDataRetriever::Delegate* delegate, | |
80 const std::string& content_type = "application/json"); | |
81 | |
82 void HandleResponse(const net::URLFetcher* source, | |
83 bool* should_retry_request); | |
84 | |
85 scoped_refptr<net::URLRequestContextGetter> context_getter_; | |
86 WalletDataRetriever::Delegate* delegate_; | |
87 scoped_ptr<net::URLFetcher> request_; | |
88 RequestType request_type_; | |
89 }; | |
90 | |
91 void WalletDataRetriever::Core::AcceptLegalDocuments( | |
92 const std::vector<std::string> document_ids, | |
93 const std::string& google_transaction_id, | |
94 WalletDataRetriever::Delegate* delegate) { | |
95 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | |
96 request_type_ = ACCEPT_LEGAL_DOCUMENTS; | |
97 GURL url = GetAcceptLegalDocumentsUrl(); | |
98 DictionaryValue request_dict; | |
99 request_dict.SetString("api_key", wallet::kApiKey); | |
100 request_dict.SetString("google_transaction_id", google_transaction_id); | |
101 ListValue* docs_list = new ListValue(); | |
102 for (std::vector<std::string>::const_iterator it = document_ids.begin(); | |
103 it != document_ids.end(); | |
104 ++it) | |
105 docs_list->AppendString(*it); | |
106 request_dict.Set("accepted_legal_document", docs_list); | |
107 | |
108 std::string post_body; | |
109 base::JSONWriter::Write(&request_dict, &post_body); | |
110 MakeWalletRequest(url, post_body, delegate); | |
111 } | |
112 | |
113 void WalletDataRetriever::Core::EncryptOtp( | |
114 const void* otp, | |
115 size_t length, | |
116 WalletDataRetriever::Delegate* delegate) { | |
117 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | |
118 request_type_ = ENCRYPT_OTP; | |
119 GURL url = GetSecureUrl(); | |
120 size_t num_bits = length * 8; | |
121 DCHECK_LT(num_bits, kMaxBits); | |
122 std::string post_body = StringPrintf(kEncryptOtpBodyFormat, | |
123 base::HexEncode(&num_bits, 1).c_str(), | |
124 base::HexEncode(otp, length).c_str()); | |
125 MakeWalletRequest(url, | |
126 post_body, | |
127 delegate, | |
128 "application/x-www-form-urlencoded"); | |
129 } | |
130 | |
131 void WalletDataRetriever::Core::GetFullWallet( | |
132 const std::string& instrument_id, | |
133 const std::string& address_id, | |
134 const std::string& merchant_domain, | |
135 const Cart& cart, | |
136 const std::string& google_transaction_id, | |
137 const std::string& encrypted_otp, | |
138 const std::string& session_material, | |
139 WalletDataRetriever::Delegate* delegate) { | |
140 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | |
141 request_type_ = GET_FULL_WALLET; | |
142 GURL url = GetGetFullWalletUrl(); | |
143 DictionaryValue request_dict; | |
144 request_dict.SetString("api_key", wallet::kApiKey); | |
145 request_dict.SetString("risk_params", GetRiskParams()); | |
146 request_dict.SetString("selected_instrument_id", instrument_id); | |
147 request_dict.SetString("selected_address_id", address_id); | |
148 request_dict.SetString("merchant_domain", merchant_domain); | |
149 request_dict.SetString("google_transaction_id", google_transaction_id); | |
150 scoped_ptr<DictionaryValue> cart_dict = cart.ToDictionary(); | |
151 request_dict.Set("cart", cart_dict.release()); | |
152 request_dict.SetString("encrypted_otp", encrypted_otp); | |
153 request_dict.SetString("session_material", session_material); | |
154 std::string post_body; | |
155 base::JSONWriter::Write(&request_dict, &post_body); | |
156 MakeWalletRequest(url, post_body, delegate); | |
157 } | |
158 | |
159 void WalletDataRetriever::Core::GetWalletItems( | |
160 WalletDataRetriever::Delegate* delegate) { | |
161 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | |
162 request_type_ = GET_WALLET_ITEMS; | |
163 GURL url = GetGetWalletItemsUrl(); | |
164 DictionaryValue request_dict; | |
165 request_dict.SetString("api_key", wallet::kApiKey); | |
166 request_dict.SetString("risk_params", GetRiskParams()); | |
167 std::string post_body; | |
168 base::JSONWriter::Write(&request_dict, &post_body); | |
169 MakeWalletRequest(url, post_body, delegate); | |
170 } | |
171 | |
172 void WalletDataRetriever::Core::SendExtendedAutofillStatus( | |
173 bool success, | |
174 const std::string& merchant_domain, | |
175 const std::string& reason, | |
176 const std::string& google_transaction_id, | |
177 WalletDataRetriever::Delegate* delegate) { | |
178 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | |
179 request_type_ = SEND_STATUS; | |
180 GURL url = GetSendStatusUrl(); | |
181 DictionaryValue request_dict; | |
182 request_dict.SetString("api_key", wallet::kApiKey); | |
183 request_dict.SetBoolean("success", success); | |
184 request_dict.SetString("hostname", merchant_domain); | |
185 if (!success) | |
186 // TODO(ahutter): Probably want to do some checks on reason | |
187 request_dict.SetString("reason", reason); | |
188 request_dict.SetString("google_transaction_id", google_transaction_id); | |
189 std::string post_body; | |
190 base::JSONWriter::Write(&request_dict, &post_body); | |
191 MakeWalletRequest(url, post_body, delegate); | |
192 } | |
193 | |
194 void WalletDataRetriever::Core::MakeWalletRequest( | |
195 GURL url, | |
196 const std::string& post_body, | |
197 WalletDataRetriever::Delegate* delegate, | |
198 const std::string& content_type) { | |
199 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; | |
200 delegate_ = delegate; | |
201 request_.reset(net::URLFetcher::Create( | |
202 0, url, net::URLFetcher::POST, this)); | |
203 request_->SetRequestContext(context_getter_); | |
204 VLOG(1) << "Request body: " << post_body; | |
205 request_->SetUploadData(content_type, post_body); | |
206 request_->SetMaxRetries(0); | |
207 request_->Start(); | |
208 } | |
209 | |
210 | |
211 void WalletDataRetriever::Core::OnURLFetchComplete( | |
212 const net::URLFetcher* source) { | |
213 bool should_retry = false; | |
214 HandleResponse(source, &should_retry); | |
215 if (should_retry) { | |
216 // Explicitly call ReceivedContentWasMalformed() to ensure the current | |
217 // request gets counted as a failure for calculation of the back-off | |
218 // period. If it was already a failure by status code, this call will | |
219 // be ignored. | |
220 request_->ReceivedContentWasMalformed(); | |
221 // We must set our context_getter_ again because | |
222 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL... | |
223 request_->SetRequestContext(context_getter_); | |
224 request_->Start(); | |
225 } | |
226 } | |
227 | |
228 void WalletDataRetriever::Core::HandleResponse( | |
229 const net::URLFetcher* source, | |
230 bool* should_retry_request) { | |
231 // Keep the URLFetcher object in case we need to reuse it. | |
232 scoped_ptr<net::URLFetcher> old_request = request_.Pass(); | |
233 DCHECK_EQ(source, old_request.get()); | |
234 | |
235 // HTTP_BAD_REQUEST means the arguments are invalid. No point retrying. We are | |
236 // done here. | |
237 if (source->GetResponseCode() == net::HTTP_BAD_REQUEST) { | |
238 request_type_ = NO_PENDING_REQUEST; | |
239 delegate_->OnWalletError(); | |
240 return; | |
241 } | |
242 | |
243 scoped_ptr<DictionaryValue> response_dict; | |
244 if (source->GetResponseCode() == net::HTTP_OK) { | |
245 std::string data; | |
246 source->GetResponseAsString(&data); | |
247 VLOG(1) << "Response body: " << data; | |
248 scoped_ptr<Value> message_value(base::JSONReader::Read(data)); | |
249 if (message_value.get() && | |
250 message_value->IsType(Value::TYPE_DICTIONARY)) { | |
251 response_dict.reset( | |
252 static_cast<DictionaryValue*>(message_value.release())); | |
253 } | |
254 } | |
255 | |
256 // TODO(ahutter): this condition sucks. needs to be simplified. | |
257 if (!response_dict.get() && | |
258 (!(request_type_ == SEND_STATUS | |
259 || request_type_ == ACCEPT_LEGAL_DOCUMENTS | |
260 || request_type_ == ENCRYPT_OTP) | |
261 || source->GetResponseCode() != net::HTTP_OK)) { | |
262 if ((source->GetMaxRetries() != -1)) { | |
263 request_type_ = NO_PENDING_REQUEST; | |
264 VLOG(1) << "Hitting " << source->GetOriginalURL(); | |
265 delegate_->OnNetworkError(source->GetResponseCode()); | |
266 } else { | |
267 request_ = old_request.Pass(); | |
268 *should_retry_request = true; | |
269 } | |
270 return; | |
271 } | |
272 | |
273 RequestType type = request_type_; | |
274 request_type_ = NO_PENDING_REQUEST; | |
275 | |
276 switch (type) { | |
277 case ACCEPT_LEGAL_DOCUMENTS: | |
278 delegate_->OnAcceptLegalDocuments(); | |
279 break; | |
280 case ENCRYPT_OTP: { | |
281 std::string data; | |
282 source->GetResponseAsString(&data); | |
283 std::vector<std::string> splits; | |
284 base::SplitString(data, '|', &splits); | |
285 if (splits.size() != 2) { | |
286 delegate_->OnWalletError(); | |
287 } else { | |
288 delegate_->OnEncryptOtp(splits[1], splits[0]); | |
289 } | |
290 break; | |
291 } | |
292 case SEND_STATUS: | |
293 delegate_->OnSendExtendedAutofillStatus(); | |
294 break; | |
295 case GET_FULL_WALLET: { | |
296 scoped_ptr<FullWallet> full_wallet( | |
297 FullWallet::CreateFullWallet(response_dict.get())); | |
298 if (full_wallet.get()) { | |
299 delegate_->OnGetFullWallet(full_wallet.release()); | |
300 } else { | |
301 delegate_->OnWalletError(); | |
302 } | |
303 break; | |
304 } | |
305 case GET_WALLET_ITEMS: { | |
306 scoped_ptr<WalletItems> wallet_items( | |
307 WalletItems::CreateWalletItems(response_dict.get())); | |
308 if (wallet_items.get()) { | |
309 delegate_->OnGetWalletItems(wallet_items.release()); | |
310 } else { | |
311 delegate_->OnWalletError(); | |
312 } | |
313 break; | |
314 } | |
315 default: | |
316 NOTREACHED(); | |
317 } | |
318 } | |
319 | |
320 WalletDataRetriever::WalletDataRetriever( | |
321 net::URLRequestContextGetter* context_getter) { | |
322 core_ = new Core(context_getter); | |
323 } | |
324 | |
325 WalletDataRetriever::~WalletDataRetriever() {} | |
326 | |
327 void WalletDataRetriever::AcceptLegalDocuments( | |
328 const std::vector<std::string> document_ids, | |
329 const std::string& google_transaction_id, | |
330 WalletDataRetriever::Delegate* delegate) { | |
331 core_->AcceptLegalDocuments(document_ids, | |
332 google_transaction_id, | |
333 delegate); | |
334 } | |
335 | |
336 void WalletDataRetriever::EncryptOtp(const void* otp, | |
337 size_t length, | |
338 WalletDataRetriever::Delegate* delegate) { | |
339 core_->EncryptOtp(otp, length, delegate); | |
340 } | |
341 | |
342 void WalletDataRetriever::GetFullWallet( | |
343 const std::string& instrument_id, | |
344 const std::string& address_id, | |
345 const std::string& merchant_domain, | |
346 const Cart& cart, | |
347 const std::string& google_transaction_id, | |
348 const std::string& encrypted_otp, | |
349 const std::string& session_material, | |
350 WalletDataRetriever::Delegate* delegate) { | |
351 core_->GetFullWallet(instrument_id, | |
352 address_id, | |
353 merchant_domain, | |
354 cart, | |
355 google_transaction_id, | |
356 encrypted_otp, | |
357 session_material, | |
358 delegate); | |
359 } | |
360 | |
361 void WalletDataRetriever::GetWalletItems( | |
362 WalletDataRetriever::Delegate* delegate) { | |
363 core_->GetWalletItems(delegate); | |
364 } | |
365 | |
366 void WalletDataRetriever::SendExtendedAutofillStatus( | |
367 bool success, | |
368 const std::string& merchant_domain, | |
369 const std::string& reason, | |
370 const std::string& google_transaction_id, | |
371 WalletDataRetriever::Delegate* delegate) { | |
372 core_->SendExtendedAutofillStatus(success, | |
373 merchant_domain, | |
374 reason, | |
375 google_transaction_id, | |
376 delegate); | |
377 } | |
378 | |
379 } // end wallet namespace | |
OLD | NEW |