OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/ui/webui/chromeos/login/authenticated_user_email_retrie
ver.h" | 5 #include "chrome/browser/ui/webui/chromeos/login/authenticated_user_email_retrie
ver.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include <vector> |
8 #include "base/callback.h" | 8 |
9 #include "base/location.h" | 9 #include "google_apis/gaia/gaia_auth_util.h" |
10 #include "base/logging.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "google_apis/gaia/gaia_auth_fetcher.h" | |
13 #include "google_apis/gaia/gaia_constants.h" | 10 #include "google_apis/gaia/gaia_constants.h" |
14 #include "google_apis/gaia/gaia_urls.h" | |
15 #include "google_apis/gaia/google_service_auth_error.h" | 11 #include "google_apis/gaia/google_service_auth_error.h" |
16 #include "net/cookies/cookie_monster.h" | 12 #include "net/url_request/url_request_context_getter.h" |
17 #include "net/url_request/url_request_context.h" | |
18 #include "url/gurl.h" | |
19 | 13 |
20 namespace chromeos { | 14 namespace chromeos { |
21 | 15 |
22 namespace { | |
23 | |
24 scoped_refptr<net::CookieStore> GetCookieStoreOnIO( | |
25 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { | |
26 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
27 return url_request_context_getter->GetURLRequestContext()->cookie_store(); | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 AuthenticatedUserEmailRetriever::AuthenticatedUserEmailRetriever( | 16 AuthenticatedUserEmailRetriever::AuthenticatedUserEmailRetriever( |
33 const AuthenticatedUserEmailCallback& callback, | 17 const AuthenticatedUserEmailCallback& callback, |
34 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) | 18 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) |
35 : callback_(callback), | 19 : callback_(callback), |
36 url_request_context_getter_(url_request_context_getter), | 20 gaia_auth_fetcher_(this, |
37 weak_factory_(this) { | 21 GaiaConstants::kChromeSource, |
38 content::BrowserThread::PostTaskAndReplyWithResult( | 22 url_request_context_getter) { |
39 content::BrowserThread::IO, | 23 gaia_auth_fetcher_.StartListAccounts(); |
40 FROM_HERE, | |
41 base::Bind(&GetCookieStoreOnIO, url_request_context_getter), | |
42 base::Bind(&AuthenticatedUserEmailRetriever::ExtractCookies, | |
43 weak_factory_.GetWeakPtr())); | |
44 } | 24 } |
45 | 25 |
46 AuthenticatedUserEmailRetriever::~AuthenticatedUserEmailRetriever() { | 26 AuthenticatedUserEmailRetriever::~AuthenticatedUserEmailRetriever() { |
47 } | 27 } |
48 | 28 |
49 void AuthenticatedUserEmailRetriever::OnGetUserInfoSuccess( | 29 void AuthenticatedUserEmailRetriever::OnListAccountsSuccess( |
50 const UserInfoMap& data) { | 30 const std::string& data) { |
51 UserInfoMap::const_iterator it = data.find("email"); | 31 std::vector<std::string> accounts = gaia::ParseListAccountsData(data); |
52 callback_.Run(it != data.end() ? it->second : ""); | 32 if (accounts.size() != 1) |
| 33 callback_.Run(std::string()); |
| 34 else |
| 35 callback_.Run(accounts.front()); |
53 } | 36 } |
54 | 37 |
55 void AuthenticatedUserEmailRetriever::OnGetUserInfoFailure( | 38 void AuthenticatedUserEmailRetriever::OnListAccountsFailure( |
56 const GoogleServiceAuthError& error) { | 39 const GoogleServiceAuthError& error) { |
57 callback_.Run(std::string()); | 40 callback_.Run(std::string()); |
58 } | 41 } |
59 | 42 |
60 void AuthenticatedUserEmailRetriever::ExtractCookies( | |
61 scoped_refptr<net::CookieStore> cookie_store) { | |
62 if (!cookie_store) { | |
63 callback_.Run(std::string()); | |
64 return; | |
65 } | |
66 cookie_store->GetCookieMonster()->GetAllCookiesForURLAsync( | |
67 GaiaUrls::GetInstance()->gaia_url(), | |
68 base::Bind(&AuthenticatedUserEmailRetriever::ExtractLSIDFromCookies, | |
69 weak_factory_.GetWeakPtr())); | |
70 } | |
71 | |
72 void AuthenticatedUserEmailRetriever::ExtractLSIDFromCookies( | |
73 const net::CookieList& cookies) { | |
74 for (net::CookieList::const_iterator it = cookies.begin(); | |
75 it != cookies.end(); ++it) { | |
76 if (it->Name() == "LSID") { | |
77 gaia_auth_fetcher_.reset(new GaiaAuthFetcher( | |
78 this, | |
79 GaiaConstants::kChromeSource, | |
80 url_request_context_getter_)); | |
81 gaia_auth_fetcher_->StartGetUserInfo(it->Value()); | |
82 return; | |
83 } | |
84 } | |
85 callback_.Run(std::string()); | |
86 } | |
87 | |
88 } // namespace chromeos | 43 } // namespace chromeos |
OLD | NEW |