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