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
3 // found in the LICENSE file.
5 #include "components/autofill/content/browser/wallet/wallet_signin_helper.h"
7 #include "base/callback_helpers.h"
8 #include "base/json/json_reader.h"
9 #include "base/logging.h"
10 #include "base/rand_util.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
16 #include "components/autofill/content/browser/wallet/wallet_signin_helper_delegate.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "google_apis/gaia/google_service_auth_error.h"
19 #include "net/base/escape.h"
20 #include "net/cookies/canonical_cookie.h"
21 #include "net/cookies/cookie_monster.h"
22 #include "net/cookies/cookie_options.h"
23 #include "net/cookies/cookie_store.h"
24 #include "net/url_request/url_fetcher.h"
25 #include "net/url_request/url_request_context.h"
26 #include "net/url_request/url_request_context_getter.h"
33 const char kWalletCookieName
[] = "gdtoken";
35 // Callback for retrieving Google Wallet cookies. |callback| is passed the
36 // retrieved cookies and posted back to the UI thread. |cookies| is any Google
38 void GetGoogleCookiesCallback(
39 const base::Callback
<void(const std::string
&)>& callback
,
40 const net::CookieList
& cookies
) {
41 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
43 // Cookies for parent domains will also be returned; we only want cookies with
44 // exact host matches. TODO(estade): really?
45 std::string host
= wallet::GetPassiveAuthUrl(0).host();
46 std::string wallet_cookie
;
47 for (size_t i
= 0; i
< cookies
.size(); ++i
) {
48 if (base::LowerCaseEqualsASCII(cookies
[i
].Name(), kWalletCookieName
) &&
49 base::LowerCaseEqualsASCII(cookies
[i
].Domain(), host
.c_str())) {
50 wallet_cookie
= cookies
[i
].Value();
54 content::BrowserThread::PostTask(content::BrowserThread::UI
,
56 base::Bind(callback
, wallet_cookie
));
59 // Gets Google Wallet cookies. Must be called on the IO thread.
60 // |request_context_getter| is a getter for the current request context.
61 // |callback| is called when retrieving cookies is completed.
62 void GetGoogleCookies(
63 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter
,
64 const base::Callback
<void(const std::string
&)>& callback
) {
65 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
67 net::URLRequestContext
* url_request_context
=
68 request_context_getter
->GetURLRequestContext();
69 net::CookieStore
* cookie_store
= url_request_context
?
70 url_request_context
->cookie_store() : NULL
;
71 net::CookieMonster
* cookie_monster
= cookie_store
?
72 cookie_store
->GetCookieMonster() : NULL
;
73 if (!cookie_monster
) {
74 content::BrowserThread::PostTask(content::BrowserThread::UI
,
76 base::Bind(callback
, std::string()));
80 net::CookieOptions cookie_options
;
81 cookie_options
.set_include_httponly();
82 cookie_monster
->GetAllCookiesForURLWithOptionsAsync(
83 wallet::GetPassiveAuthUrl(0).GetWithEmptyPath(),
85 base::Bind(&GetGoogleCookiesCallback
, callback
));
90 WalletSigninHelper::WalletSigninHelper(
91 WalletSigninHelperDelegate
* delegate
,
92 net::URLRequestContextGetter
* getter
)
93 : delegate_(delegate
),
95 weak_ptr_factory_(this) {
99 WalletSigninHelper::~WalletSigninHelper() {
102 void WalletSigninHelper::StartPassiveSignin(size_t user_index
) {
103 DCHECK(!url_fetcher_
);
105 const GURL
& url
= wallet::GetPassiveAuthUrl(user_index
);
106 url_fetcher_
= net::URLFetcher::Create(0, url
, net::URLFetcher::GET
, this);
107 url_fetcher_
->SetRequestContext(getter_
);
108 url_fetcher_
->Start();
111 void WalletSigninHelper::StartWalletCookieValueFetch() {
112 scoped_refptr
<net::URLRequestContextGetter
> request_context(getter_
);
113 if (!request_context
.get()) {
114 ReturnWalletCookieValue(std::string());
118 base::Callback
<void(const std::string
&)> callback
= base::Bind(
119 &WalletSigninHelper::ReturnWalletCookieValue
,
120 weak_ptr_factory_
.GetWeakPtr());
122 base::Closure task
= base::Bind(&GetGoogleCookies
, request_context
, callback
);
123 content::BrowserThread::PostTask(content::BrowserThread::IO
, FROM_HERE
, task
);
126 void WalletSigninHelper::OnServiceError(const GoogleServiceAuthError
& error
) {
127 delegate_
->OnPassiveSigninFailure(error
);
130 void WalletSigninHelper::OnOtherError() {
131 OnServiceError(GoogleServiceAuthError::AuthErrorNone());
134 void WalletSigninHelper::OnURLFetchComplete(
135 const net::URLFetcher
* fetcher
) {
136 DCHECK_EQ(url_fetcher_
.get(), fetcher
);
137 scoped_ptr
<net::URLFetcher
> url_fetcher(url_fetcher_
.release());
139 if (!fetcher
->GetStatus().is_success() ||
140 fetcher
->GetResponseCode() < 200 ||
141 fetcher
->GetResponseCode() >= 300) {
142 DVLOG(1) << "URLFetchFailure:"
143 << " r=" << fetcher
->GetResponseCode()
144 << " s=" << fetcher
->GetStatus().status()
145 << " e=" << fetcher
->GetStatus().error();
151 if (!fetcher
->GetResponseAsString(&data
)) {
152 DVLOG(1) << "failed to GetResponseAsString";
157 if (!base::LowerCaseEqualsASCII(data
, "yes")) {
159 GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP
));
163 delegate_
->OnPassiveSigninSuccess();
166 void WalletSigninHelper::ReturnWalletCookieValue(
167 const std::string
& cookie_value
) {
168 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
170 delegate_
->OnDidFetchWalletCookieValue(cookie_value
);
173 } // namespace wallet
174 } // namespace autofill