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 CloseModalSigninWindow(LoginUIService::ABORT_SIGNIN); |
| 52 SigninManagerFactory::GetForProfile(Profile::FromWebUI(web_ui()))->SignOut( |
| 53 signin_metrics::ABORT_SIGNIN); |
| 54 } |
| 55 |
| 56 void SyncConfirmationHandler::HandleInitialized(const base::ListValue* args) { |
| 57 Browser* browser = GetDesktopBrowser(); |
| 58 Profile* profile = browser->profile(); |
| 59 AccountInfo info = AccountTrackerServiceFactory::GetForProfile(profile)-> |
| 60 GetAccounts()[0]; |
| 61 |
| 62 if (!info.IsValid()) |
| 63 AccountTrackerServiceFactory::GetForProfile(profile)->AddObserver(this); |
| 64 else |
| 65 SetUserImageURL(info.picture_url); |
| 66 } |
| 67 |
| 68 void SyncConfirmationHandler::SetUserImageURL(const std::string& picture_url) { |
| 69 GURL url; |
| 70 if (profiles::GetImageURLWithThumbnailSize(GURL(picture_url), |
| 71 kProfileImageSize, |
| 72 &url)) { |
| 73 base::StringValue picture_url_value(url.spec()); |
| 74 web_ui()->CallJavascriptFunction( |
| 75 "sync.confirmation.setUserImageURL", picture_url_value); |
| 76 } |
| 77 } |
| 78 |
| 79 void SyncConfirmationHandler::OnAccountUpdated(const AccountInfo& info) { |
| 80 DCHECK(info.IsValid()); |
| 81 Profile* profile = Profile::FromWebUI(web_ui()); |
| 82 AccountTrackerServiceFactory::GetForProfile(profile)->RemoveObserver(this); |
| 83 |
| 84 SetUserImageURL(info.picture_url); |
| 85 } |
| 86 |
| 87 Browser* SyncConfirmationHandler::GetDesktopBrowser() { |
| 88 Browser* browser = chrome::FindBrowserWithWebContents( |
| 89 web_ui()->GetWebContents()); |
| 90 if (!browser) { |
| 91 browser = chrome::FindLastActiveWithProfile( |
| 92 Profile::FromWebUI(web_ui()), chrome::GetActiveDesktop()); |
| 93 } |
| 94 DCHECK(browser); |
| 95 return browser; |
| 96 } |
| 97 |
| 98 void SyncConfirmationHandler::CloseModalSigninWindow( |
| 99 LoginUIService::SyncConfirmationUIClosedResults results) { |
| 100 Browser* browser = GetDesktopBrowser(); |
| 101 LoginUIServiceFactory::GetForProfile(browser->profile())-> |
| 102 SyncConfirmationUIClosed(results); |
| 103 browser->window()->CloseModalSigninWindow(); |
| 104 } |
OLD | NEW |