| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/sync_confirmation_handler.h" | 5 #include "chrome/browser/ui/webui/signin/sync_confirmation_handler.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/metrics/user_metrics.h" | 10 #include "base/metrics/user_metrics.h" |
| 11 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 11 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 12 #include "chrome/browser/signin/account_tracker_service_factory.h" | 12 #include "chrome/browser/signin/account_tracker_service_factory.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "chrome/browser/ui/browser_list.h" | |
| 15 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/browser/ui/signin_view_controller_delegate.h" | 15 #include "chrome/browser/ui/signin_view_controller_delegate.h" |
| 17 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | 16 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
| 18 #include "chrome/browser/ui/webui/signin/signin_utils.h" | 17 #include "chrome/browser/ui/webui/signin/signin_utils.h" |
| 19 #include "components/signin/core/browser/account_tracker_service.h" | 18 #include "components/signin/core/browser/account_tracker_service.h" |
| 20 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/browser/web_ui.h" | 20 #include "content/public/browser/web_ui.h" |
| 22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 23 | 22 |
| 24 const int kProfileImageSize = 128; | 23 const int kProfileImageSize = 128; |
| 25 | 24 |
| 26 SyncConfirmationHandler::SyncConfirmationHandler(Browser* browser) | 25 SyncConfirmationHandler::SyncConfirmationHandler() |
| 27 : profile_(browser->profile()), | 26 : did_user_explicitly_interact(false) {} |
| 28 browser_(browser), | |
| 29 did_user_explicitly_interact(false) { | |
| 30 DCHECK(profile_); | |
| 31 DCHECK(browser_); | |
| 32 BrowserList::AddObserver(this); | |
| 33 } | |
| 34 | 27 |
| 35 SyncConfirmationHandler::~SyncConfirmationHandler() { | 28 SyncConfirmationHandler::~SyncConfirmationHandler() { |
| 36 BrowserList::RemoveObserver(this); | 29 Profile* profile = Profile::FromWebUI(web_ui()); |
| 37 AccountTrackerServiceFactory::GetForProfile(profile_)->RemoveObserver(this); | 30 AccountTrackerServiceFactory::GetForProfile(profile)->RemoveObserver(this); |
| 38 | 31 |
| 39 // Abort signin and prevent sync from starting if none of the actions on the | 32 // Abort signin and prevent sync from starting if none of the actions on the |
| 40 // sync confirmation dialog are taken by the user. | 33 // sync confirmation dialog are taken by the user. |
| 41 if (!did_user_explicitly_interact) { | 34 if (!did_user_explicitly_interact) { |
| 42 HandleUndo(nullptr); | 35 HandleUndo(nullptr); |
| 43 base::RecordAction(base::UserMetricsAction("Signin_Abort_Signin")); | 36 base::RecordAction(base::UserMetricsAction("Signin_Abort_Signin")); |
| 44 } | 37 } |
| 45 } | 38 } |
| 46 | 39 |
| 47 void SyncConfirmationHandler::OnBrowserRemoved(Browser* browser) { | |
| 48 if (browser_ == browser) | |
| 49 browser_ = nullptr; | |
| 50 } | |
| 51 | |
| 52 void SyncConfirmationHandler::RegisterMessages() { | 40 void SyncConfirmationHandler::RegisterMessages() { |
| 53 web_ui()->RegisterMessageCallback("confirm", | 41 web_ui()->RegisterMessageCallback("confirm", |
| 54 base::Bind(&SyncConfirmationHandler::HandleConfirm, | 42 base::Bind(&SyncConfirmationHandler::HandleConfirm, |
| 55 base::Unretained(this))); | 43 base::Unretained(this))); |
| 56 web_ui()->RegisterMessageCallback("undo", | 44 web_ui()->RegisterMessageCallback("undo", |
| 57 base::Bind(&SyncConfirmationHandler::HandleUndo, base::Unretained(this))); | 45 base::Bind(&SyncConfirmationHandler::HandleUndo, base::Unretained(this))); |
| 58 web_ui()->RegisterMessageCallback("goToSettings", | 46 web_ui()->RegisterMessageCallback("goToSettings", |
| 59 base::Bind(&SyncConfirmationHandler::HandleGoToSettings, | 47 base::Bind(&SyncConfirmationHandler::HandleGoToSettings, |
| 60 base::Unretained(this))); | 48 base::Unretained(this))); |
| 61 web_ui()->RegisterMessageCallback("initializedWithSize", | 49 web_ui()->RegisterMessageCallback("initializedWithSize", |
| 62 base::Bind(&SyncConfirmationHandler::HandleInitializedWithSize, | 50 base::Bind(&SyncConfirmationHandler::HandleInitializedWithSize, |
| 63 base::Unretained(this))); | 51 base::Unretained(this))); |
| 64 } | 52 } |
| 65 | 53 |
| 66 void SyncConfirmationHandler::HandleConfirm(const base::ListValue* args) { | 54 void SyncConfirmationHandler::HandleConfirm(const base::ListValue* args) { |
| 67 did_user_explicitly_interact = true; | 55 did_user_explicitly_interact = true; |
| 68 CloseModalSigninWindow(LoginUIService::SYNC_WITH_DEFAULT_SETTINGS); | 56 CloseModalSigninWindow(LoginUIService::SYNC_WITH_DEFAULT_SETTINGS); |
| 69 } | 57 } |
| 70 | 58 |
| 71 void SyncConfirmationHandler::HandleGoToSettings(const base::ListValue* args) { | 59 void SyncConfirmationHandler::HandleGoToSettings(const base::ListValue* args) { |
| 72 did_user_explicitly_interact = true; | 60 did_user_explicitly_interact = true; |
| 73 CloseModalSigninWindow(LoginUIService::CONFIGURE_SYNC_FIRST); | 61 CloseModalSigninWindow(LoginUIService::CONFIGURE_SYNC_FIRST); |
| 74 } | 62 } |
| 75 | 63 |
| 76 void SyncConfirmationHandler::HandleUndo(const base::ListValue* args) { | 64 void SyncConfirmationHandler::HandleUndo(const base::ListValue* args) { |
| 77 did_user_explicitly_interact = true; | 65 did_user_explicitly_interact = true; |
| 78 base::RecordAction(base::UserMetricsAction("Signin_Undo_Signin")); | 66 base::RecordAction(base::UserMetricsAction("Signin_Undo_Signin")); |
| 79 LoginUIServiceFactory::GetForProfile(profile_)->SyncConfirmationUIClosed( | 67 Browser* browser = signin::GetDesktopBrowser(web_ui()); |
| 80 LoginUIService::ABORT_SIGNIN); | 68 if (browser) { |
| 81 SigninManagerFactory::GetForProfile(profile_)->SignOut( | 69 LoginUIServiceFactory::GetForProfile(browser->profile())-> |
| 82 signin_metrics::ABORT_SIGNIN, | 70 SyncConfirmationUIClosed(LoginUIService::ABORT_SIGNIN); |
| 83 signin_metrics::SignoutDelete::IGNORE_METRIC); | 71 SigninManagerFactory::GetForProfile(Profile::FromWebUI(web_ui()))->SignOut( |
| 84 | 72 signin_metrics::ABORT_SIGNIN, |
| 85 if (browser_) | 73 signin_metrics::SignoutDelete::IGNORE_METRIC); |
| 86 browser_->CloseModalSigninWindow(); | 74 browser->CloseModalSigninWindow(); |
| 75 } |
| 87 } | 76 } |
| 88 | 77 |
| 89 void SyncConfirmationHandler::SetUserImageURL(const std::string& picture_url) { | 78 void SyncConfirmationHandler::SetUserImageURL(const std::string& picture_url) { |
| 90 std::string picture_url_to_load; | 79 std::string picture_url_to_load; |
| 91 GURL url; | 80 GURL url; |
| 92 if (picture_url != AccountTrackerService::kNoPictureURLFound && | 81 if (picture_url != AccountTrackerService::kNoPictureURLFound && |
| 93 profiles::GetImageURLWithThumbnailSize(GURL(picture_url), | 82 profiles::GetImageURLWithThumbnailSize(GURL(picture_url), |
| 94 kProfileImageSize, &url)) { | 83 kProfileImageSize, &url)) { |
| 95 picture_url_to_load = url.spec(); | 84 picture_url_to_load = url.spec(); |
| 96 } else { | 85 } else { |
| 97 // Use the placeholder avatar icon until the account picture URL is fetched. | 86 // Use the placeholder avatar icon until the account picture URL is fetched. |
| 98 picture_url_to_load = profiles::GetPlaceholderAvatarIconUrl(); | 87 picture_url_to_load = profiles::GetPlaceholderAvatarIconUrl(); |
| 99 } | 88 } |
| 100 base::Value picture_url_value(picture_url_to_load); | 89 base::Value picture_url_value(picture_url_to_load); |
| 101 web_ui()->CallJavascriptFunctionUnsafe("sync.confirmation.setUserImageURL", | 90 web_ui()->CallJavascriptFunctionUnsafe("sync.confirmation.setUserImageURL", |
| 102 picture_url_value); | 91 picture_url_value); |
| 103 } | 92 } |
| 104 | 93 |
| 105 void SyncConfirmationHandler::OnAccountUpdated(const AccountInfo& info) { | 94 void SyncConfirmationHandler::OnAccountUpdated(const AccountInfo& info) { |
| 106 if (!info.IsValid()) | 95 if (!info.IsValid()) |
| 107 return; | 96 return; |
| 108 | 97 |
| 109 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(profile_); | 98 Profile* profile = Profile::FromWebUI(web_ui()); |
| 99 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(profile); |
| 110 if (info.account_id != signin_manager->GetAuthenticatedAccountId()) | 100 if (info.account_id != signin_manager->GetAuthenticatedAccountId()) |
| 111 return; | 101 return; |
| 112 | 102 |
| 113 AccountTrackerServiceFactory::GetForProfile(profile_)->RemoveObserver(this); | 103 AccountTrackerServiceFactory::GetForProfile(profile)->RemoveObserver(this); |
| 114 SetUserImageURL(info.picture_url); | 104 SetUserImageURL(info.picture_url); |
| 115 } | 105 } |
| 116 | 106 |
| 117 void SyncConfirmationHandler::CloseModalSigninWindow( | 107 void SyncConfirmationHandler::CloseModalSigninWindow( |
| 118 LoginUIService::SyncConfirmationUIClosedResult result) { | 108 LoginUIService::SyncConfirmationUIClosedResult result) { |
| 119 LoginUIServiceFactory::GetForProfile(profile_)->SyncConfirmationUIClosed( | 109 Browser* browser = signin::GetDesktopBrowser(web_ui()); |
| 120 result); | 110 if (browser) { |
| 121 if (browser_) | 111 LoginUIServiceFactory::GetForProfile(browser->profile())-> |
| 122 browser_->CloseModalSigninWindow(); | 112 SyncConfirmationUIClosed(result); |
| 113 browser->CloseModalSigninWindow(); |
| 114 } |
| 123 } | 115 } |
| 124 | 116 |
| 125 void SyncConfirmationHandler::HandleInitializedWithSize( | 117 void SyncConfirmationHandler::HandleInitializedWithSize( |
| 126 const base::ListValue* args) { | 118 const base::ListValue* args) { |
| 127 if (!browser_) | 119 Browser* browser = signin::GetDesktopBrowser(web_ui()); |
| 120 if (!browser) |
| 128 return; | 121 return; |
| 129 | 122 |
| 130 std::string account_id = SigninManagerFactory::GetForProfile(profile_) | 123 Profile* profile = browser->profile(); |
| 131 ->GetAuthenticatedAccountId(); | 124 std::string account_id = |
| 125 SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedAccountId(); |
| 132 if (account_id.empty()) { | 126 if (account_id.empty()) { |
| 133 // No account is signed in, so there is nothing to be displayed in the sync | 127 // No account is signed in, so there is nothing to be displayed in the sync |
| 134 // confirmation dialog. | 128 // confirmation dialog. |
| 135 return; | 129 return; |
| 136 } | 130 } |
| 137 AccountTrackerService* account_tracker = | 131 AccountTrackerService* account_tracker = |
| 138 AccountTrackerServiceFactory::GetForProfile(profile_); | 132 AccountTrackerServiceFactory::GetForProfile(profile); |
| 139 AccountInfo account_info = account_tracker->GetAccountInfo(account_id); | 133 AccountInfo account_info = account_tracker->GetAccountInfo(account_id); |
| 140 | 134 |
| 141 if (!account_info.IsValid()) { | 135 if (!account_info.IsValid()) { |
| 142 SetUserImageURL(AccountTrackerService::kNoPictureURLFound); | 136 SetUserImageURL(AccountTrackerService::kNoPictureURLFound); |
| 143 account_tracker->AddObserver(this); | 137 account_tracker->AddObserver(this); |
| 144 } else { | 138 } else { |
| 145 SetUserImageURL(account_info.picture_url); | 139 SetUserImageURL(account_info.picture_url); |
| 146 } | 140 } |
| 147 | 141 |
| 148 signin::SetInitializedModalHeight(browser_, web_ui(), args); | 142 signin::SetInitializedModalHeight(web_ui(), args); |
| 149 | 143 |
| 150 // After the dialog is shown, some platforms might have an element focused. | 144 // After the dialog is shown, some platforms might have an element focused. |
| 151 // To be consistent, clear the focused element on all platforms. | 145 // To be consistent, clear the focused element on all platforms. |
| 152 // TODO(anthonyvd): Figure out why this is needed on Mac and not other | 146 // TODO(anthonyvd): Figure out why this is needed on Mac and not other |
| 153 // platforms and if there's a way to start unfocused while avoiding this | 147 // platforms and if there's a way to start unfocused while avoiding this |
| 154 // workaround. | 148 // workaround. |
| 155 web_ui()->CallJavascriptFunctionUnsafe("sync.confirmation.clearFocus"); | 149 web_ui()->CallJavascriptFunctionUnsafe("sync.confirmation.clearFocus"); |
| 156 } | 150 } |
| OLD | NEW |