OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/inline_login_handler_chromeos.h
" | 5 #include "chrome/browser/ui/webui/chromeos/login/inline_login_handler_chromeos.h
" |
6 | 6 |
7 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" | 7 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
10 #include "chrome/browser/signin/signin_manager_factory.h" | 10 #include "chrome/browser/signin/signin_manager_factory.h" |
| 11 #include "chrome/common/url_constants.h" |
11 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 12 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
12 #include "components/signin/core/browser/signin_manager.h" | 13 #include "components/signin/core/browser/signin_manager.h" |
| 14 #include "content/public/browser/storage_partition.h" |
| 15 #include "content/public/browser/web_contents.h" |
13 #include "content/public/browser/web_ui.h" | 16 #include "content/public/browser/web_ui.h" |
| 17 #include "google_apis/gaia/gaia_urls.h" |
| 18 #include "net/base/url_util.h" |
14 | 19 |
15 namespace chromeos { | 20 namespace chromeos { |
16 | 21 |
17 class InlineLoginHandlerChromeOS::InlineLoginUIOAuth2Delegate | 22 class InlineLoginHandlerChromeOS::InlineLoginUIOAuth2Delegate |
18 : public OAuth2TokenFetcher::Delegate { | 23 : public OAuth2TokenFetcher::Delegate { |
19 public: | 24 public: |
20 explicit InlineLoginUIOAuth2Delegate(content::WebUI* web_ui) | 25 explicit InlineLoginUIOAuth2Delegate(content::WebUI* web_ui, |
21 : web_ui_(web_ui) {} | 26 const std::string& account_id) |
| 27 : web_ui_(web_ui), account_id_(account_id) {} |
| 28 |
22 virtual ~InlineLoginUIOAuth2Delegate() {} | 29 virtual ~InlineLoginUIOAuth2Delegate() {} |
23 | 30 |
24 // OAuth2TokenFetcher::Delegate overrides: | 31 // OAuth2TokenFetcher::Delegate overrides: |
25 virtual void OnOAuth2TokensAvailable( | 32 virtual void OnOAuth2TokensAvailable( |
26 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE { | 33 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE { |
27 // Closes sign-in dialog before update token service. Token service update | 34 // Closes sign-in dialog before update token service. Token service update |
28 // might trigger a permission dialog and if this dialog does not close, | 35 // might trigger a permission dialog and if this dialog does not close, |
29 // a DCHECK would be triggered because attempting to activate a window | 36 // a DCHECK would be triggered because attempting to activate a window |
30 // while there is a modal dialog. | 37 // while there is a modal dialog. |
31 web_ui_->CallJavascriptFunction("inline.login.closeDialog"); | 38 web_ui_->CallJavascriptFunction("inline.login.closeDialog"); |
32 | 39 |
33 Profile* profile = Profile::FromWebUI(web_ui_); | 40 Profile* profile = Profile::FromWebUI(web_ui_); |
34 ProfileOAuth2TokenService* token_service = | 41 ProfileOAuth2TokenService* token_service = |
35 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 42 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
36 SigninManagerBase* signin_manager = | 43 token_service->UpdateCredentials(account_id_, oauth2_tokens.refresh_token); |
37 SigninManagerFactory::GetForProfile(profile); | |
38 token_service->UpdateCredentials( | |
39 signin_manager->GetAuthenticatedAccountId(), | |
40 oauth2_tokens.refresh_token); | |
41 } | 44 } |
42 | 45 |
43 virtual void OnOAuth2TokensFetchFailed() OVERRIDE { | 46 virtual void OnOAuth2TokensFetchFailed() OVERRIDE { |
44 LOG(ERROR) << "Failed to fetch oauth2 token with inline login."; | 47 LOG(ERROR) << "Failed to fetch oauth2 token with inline login."; |
45 web_ui_->CallJavascriptFunction("inline.login.handleOAuth2TokenFailure"); | 48 web_ui_->CallJavascriptFunction("inline.login.handleOAuth2TokenFailure"); |
46 } | 49 } |
47 | 50 |
48 private: | 51 private: |
49 content::WebUI* web_ui_; | 52 content::WebUI* web_ui_; |
| 53 std::string account_id_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(InlineLoginUIOAuth2Delegate); |
50 }; | 56 }; |
51 | 57 |
52 InlineLoginHandlerChromeOS::InlineLoginHandlerChromeOS() {} | 58 InlineLoginHandlerChromeOS::InlineLoginHandlerChromeOS() {} |
53 | 59 |
54 InlineLoginHandlerChromeOS::~InlineLoginHandlerChromeOS() {} | 60 InlineLoginHandlerChromeOS::~InlineLoginHandlerChromeOS() {} |
55 | 61 |
| 62 void InlineLoginHandlerChromeOS::SetExtraInitParams( |
| 63 base::DictionaryValue& params) { |
| 64 const GURL& current_url = web_ui()->GetWebContents()->GetURL(); |
| 65 params.SetInteger("authMode", InlineLoginHandler::kDesktopAuthMode); |
| 66 |
| 67 std::string frame_url; |
| 68 net::GetValueForKeyInQuery(current_url, "frameUrl", &frame_url); |
| 69 if (!frame_url.empty()) |
| 70 params.SetString("frameUrl", frame_url); |
| 71 |
| 72 std::string is_constrained; |
| 73 net::GetValueForKeyInQuery(current_url, "constrained", &is_constrained); |
| 74 if (!is_constrained.empty()) |
| 75 params.SetString("constrained", is_constrained); |
| 76 |
| 77 params.SetString( |
| 78 "gaiaPath", |
| 79 GaiaUrls::GetInstance()->embedded_signin_url().path().substr(1)); |
| 80 params.SetString( |
| 81 "continueUrl", |
| 82 "https://www.google.com/intl/en-US/chrome/blank.html?source=10"); |
| 83 } |
| 84 |
56 void InlineLoginHandlerChromeOS::CompleteLogin(const base::ListValue* args) { | 85 void InlineLoginHandlerChromeOS::CompleteLogin(const base::ListValue* args) { |
57 Profile* profile = Profile::FromWebUI(web_ui()); | 86 Profile* profile = Profile::FromWebUI(web_ui()); |
58 | 87 |
59 oauth2_delegate_.reset(new InlineLoginUIOAuth2Delegate(web_ui())); | 88 const base::DictionaryValue* dict = NULL; |
60 oauth2_token_fetcher_.reset(new OAuth2TokenFetcher( | 89 args->GetDictionary(0, &dict); |
61 oauth2_delegate_.get(), profile->GetRequestContext())); | 90 |
62 oauth2_token_fetcher_->StartExchangeFromCookies(); | 91 std::string session_index; |
| 92 dict->GetString("sessionIndex", &session_index); |
| 93 CHECK(!session_index.empty()) << "Session index is empty."; |
| 94 |
| 95 std::string account_id; |
| 96 dict->GetString("email", &account_id); |
| 97 CHECK(!account_id.empty()) << "Account ID is empty."; |
| 98 |
| 99 oauth2_delegate_.reset(new InlineLoginUIOAuth2Delegate(web_ui(), account_id)); |
| 100 net::URLRequestContextGetter* request_context = |
| 101 content::BrowserContext::GetStoragePartitionForSite( |
| 102 profile, GURL(chrome::kChromeUIChromeSigninURL)) |
| 103 ->GetURLRequestContext(); |
| 104 oauth2_token_fetcher_.reset( |
| 105 new OAuth2TokenFetcher(oauth2_delegate_.get(), request_context)); |
| 106 oauth2_token_fetcher_->StartExchangeFromCookies(session_index); |
63 } | 107 } |
64 | 108 |
65 } // namespace chromeos | 109 } // namespace chromeos |
OLD | NEW |