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