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.cc" | |
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 namespace { | |
20 | |
21 const int kProfileImageSize = 128; | |
22 | |
23 Browser* GetDesktopBrowser(content::WebUI* web_ui) { | |
24 Browser* browser = chrome::FindBrowserWithWebContents( | |
25 web_ui->GetWebContents()); | |
26 if (!browser) { | |
27 browser = chrome::FindLastActiveWithProfile( | |
28 Profile::FromWebUI(web_ui), chrome::GetActiveDesktop()); | |
29 } | |
30 DCHECK(browser); | |
31 return browser; | |
32 } | |
33 | |
34 void CloseModalSigninWindow(content::WebUI* web_ui, | |
Dan Beam
2016/01/13 23:45:22
nit: instead of putting this in an anonymous names
anthonyvd
2016/01/18 21:40:08
Done.
| |
35 LoginUIService::SyncConfirmationUIClosedResults results) { | |
36 Browser* browser = GetDesktopBrowser(web_ui); | |
37 LoginUIServiceFactory::GetForProfile(browser->profile())-> | |
38 SyncConfirmationUIClosed(results); | |
39 browser->window()->CloseModalSigninWindow(); | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 SyncConfirmationHandler::SyncConfirmationHandler() {} | |
45 | |
46 SyncConfirmationHandler::~SyncConfirmationHandler() { | |
47 Profile* profile = Profile::FromWebUI(web_ui()); | |
48 AccountTrackerServiceFactory::GetForProfile(profile)->RemoveObserver(this); | |
Dan Beam
2016/01/13 23:45:22
is it safe to RemoveObserver() if it was never add
anthonyvd
2016/01/18 21:40:08
It looks like it is. ObserverList only performs th
| |
49 } | |
50 | |
51 void SyncConfirmationHandler::RegisterMessages() { | |
52 web_ui()->RegisterMessageCallback("confirm", | |
53 base::Bind(&SyncConfirmationHandler::HandleConfirm, | |
54 base::Unretained(this))); | |
55 web_ui()->RegisterMessageCallback("undo", | |
56 base::Bind(&SyncConfirmationHandler::HandleUndo, base::Unretained(this))); | |
57 web_ui()->RegisterMessageCallback("initialized", | |
58 base::Bind(&SyncConfirmationHandler::HandleInitialized, | |
59 base::Unretained(this))); | |
60 web_ui()->RegisterMessageCallback("goToSettings", | |
61 base::Bind(&SyncConfirmationHandler::HandleGoToSettings, | |
62 base::Unretained(this))); | |
63 } | |
64 | |
65 void SyncConfirmationHandler::HandleConfirm(const base::ListValue* args) { | |
66 CloseModalSigninWindow(web_ui(), LoginUIService::SYNC_WITH_DEFAULT_SETTINGS); | |
67 } | |
68 | |
69 void SyncConfirmationHandler::HandleGoToSettings(const base::ListValue* args) { | |
70 CloseModalSigninWindow(web_ui(), LoginUIService::CONFIGURE_SYNC_FIRST); | |
71 } | |
72 | |
73 void SyncConfirmationHandler::HandleUndo(const base::ListValue* args) { | |
74 CloseModalSigninWindow(web_ui(), LoginUIService::ABORT_SIGNIN); | |
75 SigninManagerFactory::GetForProfile(Profile::FromWebUI(web_ui()))->SignOut( | |
76 signin_metrics::ABORT_SIGNIN); | |
77 } | |
78 | |
79 void SyncConfirmationHandler::HandleInitialized(const base::ListValue* args) { | |
80 Browser* browser = GetDesktopBrowser(web_ui()); | |
81 Profile* profile = browser->profile(); | |
82 AccountInfo info = AccountTrackerServiceFactory::GetForProfile(profile)-> | |
83 GetAccounts()[0]; | |
84 | |
85 if (!info.IsValid()) | |
86 AccountTrackerServiceFactory::GetForProfile(profile)->AddObserver(this); | |
87 else | |
88 SetUserImageURL(info.picture_url); | |
89 } | |
90 | |
91 void SyncConfirmationHandler::SetUserImageURL(const std::string& picture_url) { | |
92 GURL url; | |
93 if (profiles::GetImageURLWithThumbnailSize(GURL(picture_url), | |
94 kProfileImageSize, | |
95 &url)) { | |
96 base::StringValue picture_url_value(url.spec()); | |
97 web_ui()->CallJavascriptFunction( | |
98 "sync.confirmation.setUserImageURL", picture_url_value); | |
99 } | |
100 } | |
101 | |
102 void SyncConfirmationHandler::OnAccountUpdated(const AccountInfo& info) { | |
103 DCHECK(info.IsValid()); | |
104 Profile* profile = Profile::FromWebUI(web_ui()); | |
105 AccountTrackerServiceFactory::GetForProfile(profile)->RemoveObserver(this); | |
106 | |
107 SetUserImageURL(info.picture_url); | |
108 } | |
OLD | NEW |