Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: chrome/browser/ui/webui/signin/sync_confirmation_handler.cc

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

Powered by Google App Engine
This is Rietveld 408576698