Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/signin/sync_confirmation_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | |
| 9 #include "chrome/browser/signin/account_tracker_service_factory.h" | |
| 10 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 11 #include "chrome/browser/ui/browser_finder.h" | |
| 12 #include "chrome/browser/ui/browser_window.h" | |
| 13 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
| 14 #include "components/signin/core/browser/account_tracker_service.h" | |
| 15 #include "content/public/browser/web_contents.h" | |
| 16 #include "content/public/browser/web_ui.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 const int kProfileImageSize = 128; | |
| 20 | |
| 21 SyncConfirmationHandler::SyncConfirmationHandler() {} | |
| 22 | |
| 23 SyncConfirmationHandler::~SyncConfirmationHandler() { | |
| 24 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 25 AccountTrackerServiceFactory::GetForProfile(profile)->RemoveObserver(this); | |
| 26 } | |
| 27 | |
| 28 void SyncConfirmationHandler::RegisterMessages() { | |
| 29 web_ui()->RegisterMessageCallback("confirm", | |
| 30 base::Bind(&SyncConfirmationHandler::HandleConfirm, | |
| 31 base::Unretained(this))); | |
| 32 web_ui()->RegisterMessageCallback("undo", | |
| 33 base::Bind(&SyncConfirmationHandler::HandleUndo, base::Unretained(this))); | |
| 34 web_ui()->RegisterMessageCallback("initialized", | |
| 35 base::Bind(&SyncConfirmationHandler::HandleInitialized, | |
| 36 base::Unretained(this))); | |
| 37 web_ui()->RegisterMessageCallback("goToSettings", | |
| 38 base::Bind(&SyncConfirmationHandler::HandleGoToSettings, | |
| 39 base::Unretained(this))); | |
| 40 } | |
| 41 | |
| 42 void SyncConfirmationHandler::HandleConfirm(const base::ListValue* args) { | |
| 43 CloseModalSigninWindow(LoginUIService::SYNC_WITH_DEFAULT_SETTINGS); | |
| 44 } | |
| 45 | |
| 46 void SyncConfirmationHandler::HandleGoToSettings(const base::ListValue* args) { | |
| 47 CloseModalSigninWindow(LoginUIService::CONFIGURE_SYNC_FIRST); | |
| 48 } | |
| 49 | |
| 50 void SyncConfirmationHandler::HandleUndo(const base::ListValue* args) { | |
| 51 Browser* browser = GetDesktopBrowser(); | |
| 52 LoginUIServiceFactory::GetForProfile(browser->profile())-> | |
| 53 SyncConfirmationUIClosed(LoginUIService::ABORT_SIGNIN); | |
| 54 SigninManagerFactory::GetForProfile(Profile::FromWebUI(web_ui()))->SignOut( | |
| 55 signin_metrics::ABORT_SIGNIN); | |
| 56 browser->window()->CloseModalSigninWindow(); | |
| 57 } | |
| 58 | |
| 59 void SyncConfirmationHandler::HandleInitialized(const base::ListValue* args) { | |
| 60 Browser* browser = GetDesktopBrowser(); | |
| 61 Profile* profile = browser->profile(); | |
| 62 std::vector<AccountInfo> accounts = | |
| 63 AccountTrackerServiceFactory::GetForProfile(profile)->GetAccounts(); | |
| 64 | |
| 65 if (accounts.size() == 0) | |
|
Dan Beam
2016/01/26 19:54:20
nit: if (accounts.empty())
anthonyvd
2016/01/26 22:00:39
Done.
| |
| 66 return; | |
| 67 | |
| 68 AccountInfo primary_account_info = accounts[0]; | |
| 69 | |
| 70 if (!primary_account_info.IsValid()) | |
| 71 AccountTrackerServiceFactory::GetForProfile(profile)->AddObserver(this); | |
| 72 else | |
| 73 SetUserImageURL(primary_account_info.picture_url); | |
| 74 } | |
| 75 | |
| 76 void SyncConfirmationHandler::SetUserImageURL(const std::string& picture_url) { | |
| 77 GURL url; | |
| 78 if (profiles::GetImageURLWithThumbnailSize(GURL(picture_url), | |
| 79 kProfileImageSize, | |
| 80 &url)) { | |
| 81 base::StringValue picture_url_value(url.spec()); | |
| 82 web_ui()->CallJavascriptFunction( | |
| 83 "sync.confirmation.setUserImageURL", picture_url_value); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 void SyncConfirmationHandler::OnAccountUpdated(const AccountInfo& info) { | |
| 88 DCHECK(info.IsValid()); | |
| 89 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 90 AccountTrackerServiceFactory::GetForProfile(profile)->RemoveObserver(this); | |
| 91 | |
| 92 SetUserImageURL(info.picture_url); | |
| 93 } | |
| 94 | |
| 95 Browser* SyncConfirmationHandler::GetDesktopBrowser() { | |
| 96 Browser* browser = chrome::FindBrowserWithWebContents( | |
| 97 web_ui()->GetWebContents()); | |
| 98 if (!browser) { | |
| 99 browser = chrome::FindLastActiveWithProfile( | |
| 100 Profile::FromWebUI(web_ui()), chrome::GetActiveDesktop()); | |
| 101 } | |
| 102 DCHECK(browser); | |
| 103 return browser; | |
| 104 } | |
| 105 | |
| 106 void SyncConfirmationHandler::CloseModalSigninWindow( | |
| 107 LoginUIService::SyncConfirmationUIClosedResults results) { | |
| 108 Browser* browser = GetDesktopBrowser(); | |
| 109 LoginUIServiceFactory::GetForProfile(browser->profile())-> | |
| 110 SyncConfirmationUIClosed(results); | |
| 111 browser->window()->CloseModalSigninWindow(); | |
| 112 } | |
| OLD | NEW |