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