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(content::BrowserThread::CurrentlyOn(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 (LowerCaseEqualsASCII(cookies
[i
].Name(), kWalletCookieName
) &&
49 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(content::BrowserThread::CurrentlyOn(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_
.reset(net::URLFetcher::Create(
107 0, url
, net::URLFetcher::GET
, this));
108 url_fetcher_
->SetRequestContext(getter_
);
109 url_fetcher_
->Start();
112 void WalletSigninHelper::StartWalletCookieValueFetch() {
113 scoped_refptr
<net::URLRequestContextGetter
> request_context(getter_
);
114 if (!request_context
.get()) {
115 ReturnWalletCookieValue(std::string());
119 base::Callback
<void(const std::string
&)> callback
= base::Bind(
120 &WalletSigninHelper::ReturnWalletCookieValue
,
121 weak_ptr_factory_
.GetWeakPtr());
123 base::Closure task
= base::Bind(&GetGoogleCookies
, request_context
, callback
);
124 content::BrowserThread::PostTask(content::BrowserThread::IO
, FROM_HERE
, task
);
127 void WalletSigninHelper::OnServiceError(const GoogleServiceAuthError
& error
) {
128 delegate_
->OnPassiveSigninFailure(error
);
131 void WalletSigninHelper::OnOtherError() {
132 OnServiceError(GoogleServiceAuthError::AuthErrorNone());
135 void WalletSigninHelper::OnURLFetchComplete(
136 const net::URLFetcher
* fetcher
) {
137 DCHECK_EQ(url_fetcher_
.get(), fetcher
);
138 scoped_ptr
<net::URLFetcher
> url_fetcher(url_fetcher_
.release());
140 if (!fetcher
->GetStatus().is_success() ||
141 fetcher
->GetResponseCode() < 200 ||
142 fetcher
->GetResponseCode() >= 300) {
143 DVLOG(1) << "URLFetchFailure:"
144 << " r=" << fetcher
->GetResponseCode()
145 << " s=" << fetcher
->GetStatus().status()
146 << " e=" << fetcher
->GetStatus().error();
152 if (!fetcher
->GetResponseAsString(&data
)) {
153 DVLOG(1) << "failed to GetResponseAsString";
158 if (!LowerCaseEqualsASCII(data
, "yes")) {
160 GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP
));
164 delegate_
->OnPassiveSigninSuccess();
167 void WalletSigninHelper::ReturnWalletCookieValue(
168 const std::string
& cookie_value
) {
169 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
171 delegate_
->OnDidFetchWalletCookieValue(cookie_value
);
174 } // namespace wallet
175 } // namespace autofill