| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/chromeos/login/inline_login_handler_chromeos.h
" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "chrome/browser/chromeos/login/signin/oauth2_token_fetcher.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/signin/account_tracker_service_factory.h" | |
| 13 #include "chrome/browser/signin/chrome_signin_client_factory.h" | |
| 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 15 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 16 #include "chrome/browser/signin/signin_promo.h" | |
| 17 #include "chrome/common/url_constants.h" | |
| 18 #include "components/signin/core/browser/account_tracker_service.h" | |
| 19 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 20 #include "components/signin/core/browser/signin_client.h" | |
| 21 #include "components/signin/core/browser/signin_manager.h" | |
| 22 #include "content/public/browser/storage_partition.h" | |
| 23 #include "content/public/browser/web_contents.h" | |
| 24 #include "content/public/browser/web_ui.h" | |
| 25 #include "google_apis/gaia/gaia_urls.h" | |
| 26 #include "net/base/url_util.h" | |
| 27 | |
| 28 namespace chromeos { | |
| 29 | |
| 30 class InlineLoginHandlerChromeOS::InlineLoginUIOAuth2Delegate | |
| 31 : public OAuth2TokenFetcher::Delegate { | |
| 32 public: | |
| 33 explicit InlineLoginUIOAuth2Delegate(content::WebUI* web_ui, | |
| 34 const std::string& account_id) | |
| 35 : web_ui_(web_ui), account_id_(account_id) {} | |
| 36 | |
| 37 ~InlineLoginUIOAuth2Delegate() override {} | |
| 38 | |
| 39 // OAuth2TokenFetcher::Delegate overrides: | |
| 40 void OnOAuth2TokensAvailable( | |
| 41 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) override { | |
| 42 // Closes sign-in dialog before update token service. Token service update | |
| 43 // might trigger a permission dialog and if this dialog does not close, | |
| 44 // a DCHECK would be triggered because attempting to activate a window | |
| 45 // while there is a modal dialog. | |
| 46 web_ui_->CallJavascriptFunctionUnsafe("inline.login.closeDialog"); | |
| 47 | |
| 48 Profile* profile = Profile::FromWebUI(web_ui_); | |
| 49 ProfileOAuth2TokenService* token_service = | |
| 50 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 51 token_service->UpdateCredentials(account_id_, oauth2_tokens.refresh_token); | |
| 52 } | |
| 53 | |
| 54 void OnOAuth2TokensFetchFailed() override { | |
| 55 LOG(ERROR) << "Failed to fetch oauth2 token with inline login."; | |
| 56 web_ui_->CallJavascriptFunctionUnsafe( | |
| 57 "inline.login.handleOAuth2TokenFailure"); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 content::WebUI* web_ui_; | |
| 62 std::string account_id_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(InlineLoginUIOAuth2Delegate); | |
| 65 }; | |
| 66 | |
| 67 InlineLoginHandlerChromeOS::InlineLoginHandlerChromeOS() {} | |
| 68 | |
| 69 InlineLoginHandlerChromeOS::~InlineLoginHandlerChromeOS() {} | |
| 70 | |
| 71 void InlineLoginHandlerChromeOS::CompleteLogin(const base::ListValue* args) { | |
| 72 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 73 | |
| 74 const base::DictionaryValue* dict = NULL; | |
| 75 args->GetDictionary(0, &dict); | |
| 76 | |
| 77 std::string session_index; | |
| 78 dict->GetString("sessionIndex", &session_index); | |
| 79 CHECK(!session_index.empty()) << "Session index is empty."; | |
| 80 | |
| 81 std::string email; | |
| 82 dict->GetString("email", &email); | |
| 83 CHECK(!email.empty()) << "Email is empty."; | |
| 84 | |
| 85 std::string gaia_id; | |
| 86 dict->GetString("gaiaId", &gaia_id); | |
| 87 CHECK(!gaia_id.empty()) << "Gaia ID is empty."; | |
| 88 | |
| 89 AccountTrackerService* account_tracker = | |
| 90 AccountTrackerServiceFactory::GetForProfile(profile); | |
| 91 account_tracker->SeedAccountInfo(gaia_id, email); | |
| 92 | |
| 93 const std::string account_id = | |
| 94 account_tracker->PickAccountIdForAccount(gaia_id, email); | |
| 95 oauth2_delegate_.reset(new InlineLoginUIOAuth2Delegate(web_ui(), account_id)); | |
| 96 net::URLRequestContextGetter* request_context = | |
| 97 content::BrowserContext::GetStoragePartitionForSite( | |
| 98 profile, GURL(chrome::kChromeUIChromeSigninURL)) | |
| 99 ->GetURLRequestContext(); | |
| 100 oauth2_token_fetcher_.reset( | |
| 101 new OAuth2TokenFetcher(oauth2_delegate_.get(), request_context)); | |
| 102 SigninClient* signin_client = | |
| 103 ChromeSigninClientFactory::GetForProfile(profile); | |
| 104 std::string signin_scoped_device_id = | |
| 105 signin_client->GetSigninScopedDeviceId(); | |
| 106 oauth2_token_fetcher_->StartExchangeFromCookies(session_index, | |
| 107 signin_scoped_device_id); | |
| 108 } | |
| 109 | |
| 110 } // namespace chromeos | |
| OLD | NEW |