Move LowerCaseEqualsASCII to base namespace
[chromium-blink-merge.git] / components / autofill / content / browser / wallet / wallet_signin_helper.cc
blobfb84fba53b79d80729879f1c0d06d397d85ebfaf
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"
28 namespace autofill {
29 namespace wallet {
31 namespace {
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
37 // Wallet cookies.
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();
51 break;
54 content::BrowserThread::PostTask(content::BrowserThread::UI,
55 FROM_HERE,
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,
75 FROM_HERE,
76 base::Bind(callback, std::string()));
77 return;
80 net::CookieOptions cookie_options;
81 cookie_options.set_include_httponly();
82 cookie_monster->GetAllCookiesForURLWithOptionsAsync(
83 wallet::GetPassiveAuthUrl(0).GetWithEmptyPath(),
84 cookie_options,
85 base::Bind(&GetGoogleCookiesCallback, callback));
88 } // namespace
90 WalletSigninHelper::WalletSigninHelper(
91 WalletSigninHelperDelegate* delegate,
92 net::URLRequestContextGetter* getter)
93 : delegate_(delegate),
94 getter_(getter),
95 weak_ptr_factory_(this) {
96 DCHECK(delegate_);
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());
115 return;
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();
146 OnOtherError();
147 return;
150 std::string data;
151 if (!fetcher->GetResponseAsString(&data)) {
152 DVLOG(1) << "failed to GetResponseAsString";
153 OnOtherError();
154 return;
157 if (!base::LowerCaseEqualsASCII(data, "yes")) {
158 OnServiceError(
159 GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP));
160 return;
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