OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/signin/signin_dice_internals_handler.h" | 5 #include "chrome/browser/ui/webui/signin/signin_dice_internals_handler.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/signin/account_tracker_service_factory.h" |
| 10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 11 #include "chrome/browser/signin/signin_manager_factory.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_finder.h" |
| 14 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" |
9 | 15 |
10 SigninDiceInternalsHandler::SigninDiceInternalsHandler(Profile* profile) | 16 SigninDiceInternalsHandler::SigninDiceInternalsHandler(Profile* profile) |
11 : profile_(profile) { | 17 : profile_(profile) { |
12 DCHECK(profile_); | 18 DCHECK(profile_); |
13 DCHECK(!profile_->IsOffTheRecord()); | 19 DCHECK(!profile_->IsOffTheRecord()); |
14 } | 20 } |
15 | 21 |
16 SigninDiceInternalsHandler::~SigninDiceInternalsHandler() {} | 22 SigninDiceInternalsHandler::~SigninDiceInternalsHandler() {} |
17 | 23 |
18 void SigninDiceInternalsHandler::RegisterMessages() { | 24 void SigninDiceInternalsHandler::RegisterMessages() { |
19 web_ui()->RegisterMessageCallback( | 25 web_ui()->RegisterMessageCallback( |
20 "enableSync", base::Bind(&SigninDiceInternalsHandler::HandleEnableSync, | 26 "enableSync", base::Bind(&SigninDiceInternalsHandler::HandleEnableSync, |
21 base::Unretained(this))); | 27 base::Unretained(this))); |
22 } | 28 } |
23 | 29 |
24 void SigninDiceInternalsHandler::HandleEnableSync(const base::ListValue* args) { | 30 void SigninDiceInternalsHandler::HandleEnableSync(const base::ListValue* args) { |
25 // TODO(msarda): Implement start syncing. | 31 if (SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()) { |
26 VLOG(1) << "[Dice] Start syncing"; | 32 VLOG(1) << "[Dice] Cannot enable sync as profile is already authenticated"; |
| 33 return; |
| 34 } |
| 35 |
| 36 AccountTrackerService* tracker = |
| 37 AccountTrackerServiceFactory::GetForProfile(profile_); |
| 38 ProfileOAuth2TokenService* token_service = |
| 39 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| 40 std::vector<std::string> account_ids = token_service->GetAccounts(); |
| 41 if (account_ids.empty()) { |
| 42 VLOG(1) << "[Dice] No accounts available in the token service"; |
| 43 return; |
| 44 } |
| 45 |
| 46 Browser* browser = chrome::FindLastActiveWithProfile(profile_); |
| 47 DCHECK(browser); |
| 48 std::string account_id = account_ids[0]; |
| 49 std::string gaia_id = tracker->GetAccountInfo(account_id).gaia; |
| 50 std::string email = tracker->GetAccountInfo(account_id).email; |
| 51 VLOG(1) << "[Dice] Start syncing with account " << email; |
| 52 |
| 53 // OneClickSigninSyncStarter is suicidal (it will kill itself once it finishes |
| 54 // enabling sync). |
| 55 OneClickSigninSyncStarter::Callback callback; |
| 56 new OneClickSigninSyncStarter( |
| 57 profile_, browser, gaia_id, email, "" /* password */, |
| 58 "" /* refresh_token */, OneClickSigninSyncStarter::CURRENT_PROFILE, |
| 59 OneClickSigninSyncStarter::CONFIRM_SYNC_SETTINGS_FIRST, |
| 60 web_ui()->GetWebContents(), |
| 61 OneClickSigninSyncStarter::CONFIRM_AFTER_SIGNIN, |
| 62 GURL("") /* current_url */, GURL("") /* continue_url */, callback); |
27 } | 63 } |
OLD | NEW |