OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ntp/ntp_login_handler.h" | 5 #include "chrome/browser/ui/webui/app_launcher_login_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/prefs/pref_notifier.h" | 12 #include "base/prefs/pref_notifier.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
MAD
2015/05/20 19:33:02
I don't think we need the pref_* stuff anymore, ri
Dan Beam
2015/05/20 21:53:23
this class uses PrefService still
| |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/profiles/profile_info_cache.h" | 18 #include "chrome/browser/profiles/profile_info_cache.h" |
19 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
MAD
2015/05/20 19:33:02
Do we still need this?
Dan Beam
2015/05/20 21:53:23
Yep, see L172
| |
20 #include "chrome/browser/profiles/profile_metrics.h" | 20 #include "chrome/browser/profiles/profile_metrics.h" |
21 #include "chrome/browser/signin/signin_manager_factory.h" | 21 #include "chrome/browser/signin/signin_manager_factory.h" |
22 #include "chrome/browser/signin/signin_promo.h" | 22 #include "chrome/browser/signin/signin_promo.h" |
23 #include "chrome/browser/sync/profile_sync_service.h" | 23 #include "chrome/browser/sync/profile_sync_service.h" |
24 #include "chrome/browser/sync/profile_sync_service_factory.h" | 24 #include "chrome/browser/sync/profile_sync_service_factory.h" |
25 #include "chrome/browser/ui/browser.h" | 25 #include "chrome/browser/ui/browser.h" |
26 #include "chrome/browser/ui/browser_finder.h" | 26 #include "chrome/browser/ui/browser_finder.h" |
27 #include "chrome/browser/ui/browser_window.h" | 27 #include "chrome/browser/ui/browser_window.h" |
28 #include "chrome/browser/ui/chrome_pages.h" | 28 #include "chrome/browser/ui/chrome_pages.h" |
29 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 29 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
30 #include "chrome/browser/ui/webui/profile_info_watcher.h" | |
30 #include "chrome/browser/web_resource/promo_resource_service.h" | 31 #include "chrome/browser/web_resource/promo_resource_service.h" |
31 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
32 #include "chrome/common/url_constants.h" | 33 #include "chrome/common/url_constants.h" |
33 #include "chrome/grit/chromium_strings.h" | 34 #include "chrome/grit/chromium_strings.h" |
34 #include "chrome/grit/generated_resources.h" | 35 #include "chrome/grit/generated_resources.h" |
35 #include "components/signin/core/browser/signin_manager.h" | 36 #include "components/signin/core/browser/signin_manager.h" |
36 #include "content/public/browser/host_zoom_map.h" | 37 #include "content/public/browser/host_zoom_map.h" |
37 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
38 #include "content/public/browser/web_ui.h" | 39 #include "content/public/browser/web_ui.h" |
39 #include "content/public/common/page_zoom.h" | 40 #include "content/public/common/page_zoom.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 const std::string& css_class, | 73 const std::string& css_class, |
73 const std::string& extends_tag) { | 74 const std::string& extends_tag) { |
74 base::string16 start_tag = base::ASCIIToUTF16("<" + tag_name + | 75 base::string16 start_tag = base::ASCIIToUTF16("<" + tag_name + |
75 " class='" + css_class + "' is='" + extends_tag + "'>"); | 76 " class='" + css_class + "' is='" + extends_tag + "'>"); |
76 base::string16 end_tag = base::ASCIIToUTF16("</" + tag_name + ">"); | 77 base::string16 end_tag = base::ASCIIToUTF16("</" + tag_name + ">"); |
77 return start_tag + net::EscapeForHTML(content) + end_tag; | 78 return start_tag + net::EscapeForHTML(content) + end_tag; |
78 } | 79 } |
79 | 80 |
80 } // namespace | 81 } // namespace |
81 | 82 |
82 NTPLoginHandler::NTPLoginHandler() { | 83 AppLauncherLoginHandler::AppLauncherLoginHandler() {} |
83 } | |
84 | 84 |
85 NTPLoginHandler::~NTPLoginHandler() { | 85 AppLauncherLoginHandler::~AppLauncherLoginHandler() {} |
86 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
87 // The profile_manager might be NULL in testing environments. | |
88 if (profile_manager) | |
89 profile_manager->GetProfileInfoCache().RemoveObserver(this); | |
90 } | |
91 | 86 |
92 void NTPLoginHandler::RegisterMessages() { | 87 void AppLauncherLoginHandler::RegisterMessages() { |
93 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 88 profile_info_watcher_.reset(new ProfileInfoWatcher( |
94 // The profile_manager might be NULL in testing environments. | 89 Profile::FromWebUI(web_ui()), |
95 if (profile_manager) | 90 base::Bind(&AppLauncherLoginHandler::UpdateLogin, |
96 profile_manager->GetProfileInfoCache().AddObserver(this); | 91 base::Unretained(this)))); |
97 | |
98 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
99 signin_allowed_pref_.Init(prefs::kSigninAllowed, | |
100 pref_service, | |
101 base::Bind(&NTPLoginHandler::UpdateLogin, | |
102 base::Unretained(this))); | |
103 | 92 |
104 web_ui()->RegisterMessageCallback("initializeSyncLogin", | 93 web_ui()->RegisterMessageCallback("initializeSyncLogin", |
105 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin, | 94 base::Bind(&AppLauncherLoginHandler::HandleInitializeSyncLogin, |
106 base::Unretained(this))); | 95 base::Unretained(this))); |
107 web_ui()->RegisterMessageCallback("showSyncLoginUI", | 96 web_ui()->RegisterMessageCallback("showSyncLoginUI", |
108 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI, | 97 base::Bind(&AppLauncherLoginHandler::HandleShowSyncLoginUI, |
109 base::Unretained(this))); | 98 base::Unretained(this))); |
110 web_ui()->RegisterMessageCallback("loginMessageSeen", | 99 web_ui()->RegisterMessageCallback("loginMessageSeen", |
111 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen, | 100 base::Bind(&AppLauncherLoginHandler::HandleLoginMessageSeen, |
112 base::Unretained(this))); | 101 base::Unretained(this))); |
113 web_ui()->RegisterMessageCallback("showAdvancedLoginUI", | 102 web_ui()->RegisterMessageCallback("showAdvancedLoginUI", |
114 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI, | 103 base::Bind(&AppLauncherLoginHandler::HandleShowAdvancedLoginUI, |
115 base::Unretained(this))); | 104 base::Unretained(this))); |
116 } | 105 } |
117 | 106 |
118 void NTPLoginHandler::OnProfileAuthInfoChanged( | 107 void AppLauncherLoginHandler::HandleInitializeSyncLogin( |
119 const base::FilePath& profile_path) { | 108 const base::ListValue* args) { |
120 UpdateLogin(); | 109 UpdateLogin(); |
121 } | 110 } |
122 | 111 |
123 void NTPLoginHandler::HandleInitializeSyncLogin(const base::ListValue* args) { | 112 void AppLauncherLoginHandler::HandleShowSyncLoginUI( |
124 UpdateLogin(); | 113 const base::ListValue* args) { |
125 } | |
126 | |
127 void NTPLoginHandler::HandleShowSyncLoginUI(const base::ListValue* args) { | |
128 Profile* profile = Profile::FromWebUI(web_ui()); | 114 Profile* profile = Profile::FromWebUI(web_ui()); |
129 if (!signin::ShouldShowPromo(profile)) | 115 if (!signin::ShouldShowPromo(profile)) |
130 return; | 116 return; |
131 | 117 |
132 std::string username = | 118 std::string username = |
133 SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername(); | 119 SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername(); |
134 if (!username.empty()) | 120 if (!username.empty()) |
135 return; | 121 return; |
136 | 122 |
137 content::WebContents* web_contents = web_ui()->GetWebContents(); | 123 content::WebContents* web_contents = web_ui()->GetWebContents(); |
138 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 124 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
139 if (!browser) | 125 if (!browser) |
140 return; | 126 return; |
141 | 127 |
142 // The user isn't signed in, show the sign in promo. | 128 // The user isn't signed in, show the sign in promo. |
143 signin_metrics::Source source = | 129 signin_metrics::Source source = |
144 web_contents->GetURL().spec() == chrome::kChromeUIAppsURL ? | 130 web_contents->GetURL().spec() == chrome::kChromeUIAppsURL ? |
145 signin_metrics::SOURCE_APPS_PAGE_LINK : | 131 signin_metrics::SOURCE_APPS_PAGE_LINK : |
146 signin_metrics::SOURCE_NTP_LINK; | 132 signin_metrics::SOURCE_NTP_LINK; |
147 chrome::ShowBrowserSignin(browser, source); | 133 chrome::ShowBrowserSignin(browser, source); |
148 RecordInHistogram(NTP_SIGN_IN_PROMO_CLICKED); | 134 RecordInHistogram(NTP_SIGN_IN_PROMO_CLICKED); |
149 } | 135 } |
150 | 136 |
151 void NTPLoginHandler::RecordInHistogram(int type) { | 137 void AppLauncherLoginHandler::RecordInHistogram(int type) { |
152 // Invalid type to record. | 138 // Invalid type to record. |
153 if (type < NTP_SIGN_IN_PROMO_VIEWED || | 139 if (type < NTP_SIGN_IN_PROMO_VIEWED || |
154 type > NTP_SIGN_IN_PROMO_CLICKED) { | 140 type > NTP_SIGN_IN_PROMO_CLICKED) { |
155 NOTREACHED(); | 141 NOTREACHED(); |
156 } else { | 142 } else { |
157 UMA_HISTOGRAM_ENUMERATION("SyncPromo.NTPPromo", type, | 143 UMA_HISTOGRAM_ENUMERATION("SyncPromo.NTPPromo", type, |
158 NTP_SIGN_IN_PROMO_BUCKET_BOUNDARY); | 144 NTP_SIGN_IN_PROMO_BUCKET_BOUNDARY); |
159 } | 145 } |
160 } | 146 } |
161 | 147 |
162 void NTPLoginHandler::HandleLoginMessageSeen(const base::ListValue* args) { | 148 void AppLauncherLoginHandler::HandleLoginMessageSeen( |
149 const base::ListValue* args) { | |
163 Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean( | 150 Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean( |
164 prefs::kSignInPromoShowNTPBubble, false); | 151 prefs::kSignInPromoShowNTPBubble, false); |
165 NewTabUI* ntp_ui = NewTabUI::FromWebUIController(web_ui()->GetController()); | 152 NewTabUI* ntp_ui = NewTabUI::FromWebUIController(web_ui()->GetController()); |
166 // When instant extended is enabled, there may not be a NewTabUI object. | 153 // When instant extended is enabled, there may not be a NewTabUI object. |
167 if (ntp_ui) | 154 if (ntp_ui) |
168 ntp_ui->set_showing_sync_bubble(true); | 155 ntp_ui->set_showing_sync_bubble(true); |
169 } | 156 } |
170 | 157 |
171 void NTPLoginHandler::HandleShowAdvancedLoginUI(const base::ListValue* args) { | 158 void AppLauncherLoginHandler::HandleShowAdvancedLoginUI( |
159 const base::ListValue* args) { | |
172 Browser* browser = | 160 Browser* browser = |
173 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()); | 161 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()); |
174 if (browser) | 162 if (browser) |
175 chrome::ShowBrowserSignin(browser, signin_metrics::SOURCE_NTP_LINK); | 163 chrome::ShowBrowserSignin(browser, signin_metrics::SOURCE_NTP_LINK); |
176 } | 164 } |
177 | 165 |
178 void NTPLoginHandler::UpdateLogin() { | 166 void AppLauncherLoginHandler::UpdateLogin() { |
179 Profile* profile = Profile::FromWebUI(web_ui()); | 167 std::string username = profile_info_watcher_->GetAuthenticatedUsername(); |
MAD
2015/05/20 19:33:01
DCHECK(profile_info_watcher_.get()); ?
Dan Beam
2015/05/20 21:53:23
nah, that's redundant
https://code.google.com/p/ch
| |
180 SigninManagerBase* signin_manager = | |
181 SigninManagerFactory::GetForProfile(profile); | |
182 if (!signin_manager) { | |
183 // Guests on desktop do not have a signin manager. | |
184 return; | |
185 } | |
186 | |
187 std::string username = signin_manager->GetAuthenticatedUsername(); | |
188 | |
189 base::string16 header, sub_header; | 168 base::string16 header, sub_header; |
190 std::string icon_url; | 169 std::string icon_url; |
170 Profile* profile = Profile::FromWebUI(web_ui()); | |
191 if (!username.empty()) { | 171 if (!username.empty()) { |
192 ProfileInfoCache& cache = | 172 ProfileInfoCache& cache = |
193 g_browser_process->profile_manager()->GetProfileInfoCache(); | 173 g_browser_process->profile_manager()->GetProfileInfoCache(); |
194 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); | 174 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); |
195 if (profile_index != std::string::npos) { | 175 if (profile_index != std::string::npos) { |
196 // Only show the profile picture and full name for the single profile | 176 // Only show the profile picture and full name for the single profile |
197 // case. In the multi-profile case the profile picture is visible in the | 177 // case. In the multi-profile case the profile picture is visible in the |
198 // title bar and the full name can be ambiguous. | 178 // title bar and the full name can be ambiguous. |
199 if (cache.GetNumberOfProfiles() == 1) { | 179 if (cache.GetNumberOfProfiles() == 1) { |
200 base::string16 name = cache.GetGAIANameOfProfileAtIndex(profile_index); | 180 base::string16 name = cache.GetGAIANameOfProfileAtIndex(profile_index); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 | 213 |
234 base::StringValue header_value(header); | 214 base::StringValue header_value(header); |
235 base::StringValue sub_header_value(sub_header); | 215 base::StringValue sub_header_value(sub_header); |
236 base::StringValue icon_url_value(icon_url); | 216 base::StringValue icon_url_value(icon_url); |
237 base::FundamentalValue is_user_signed_in(!username.empty()); | 217 base::FundamentalValue is_user_signed_in(!username.empty()); |
238 web_ui()->CallJavascriptFunction("ntp.updateLogin", | 218 web_ui()->CallJavascriptFunction("ntp.updateLogin", |
239 header_value, sub_header_value, icon_url_value, is_user_signed_in); | 219 header_value, sub_header_value, icon_url_value, is_user_signed_in); |
240 } | 220 } |
241 | 221 |
242 // static | 222 // static |
243 bool NTPLoginHandler::ShouldShow(Profile* profile) { | 223 bool AppLauncherLoginHandler::ShouldShow(Profile* profile) { |
244 #if defined(OS_CHROMEOS) | 224 #if defined(OS_CHROMEOS) |
245 // For now we don't care about showing sync status on Chrome OS. The promo | 225 // For now we don't care about showing sync status on Chrome OS. The promo |
246 // UI and the avatar menu don't exist on that platform. | 226 // UI and the avatar menu don't exist on that platform. |
247 return false; | 227 return false; |
248 #else | 228 #else |
249 SigninManager* signin = SigninManagerFactory::GetForProfile(profile); | 229 SigninManager* signin = SigninManagerFactory::GetForProfile(profile); |
250 return !profile->IsOffTheRecord() && signin && signin->IsSigninAllowed(); | 230 return !profile->IsOffTheRecord() && signin && signin->IsSigninAllowed(); |
251 #endif | 231 #endif |
252 } | 232 } |
253 | 233 |
254 // static | 234 // static |
255 void NTPLoginHandler::GetLocalizedValues(Profile* profile, | 235 void AppLauncherLoginHandler::GetLocalizedValues( |
256 base::DictionaryValue* values) { | 236 Profile* profile, base::DictionaryValue* values) { |
257 PrefService* prefs = profile->GetPrefs(); | 237 PrefService* prefs = profile->GetPrefs(); |
258 bool hide_sync = !prefs->GetBoolean(prefs::kSignInPromoShowNTPBubble); | 238 bool hide_sync = !prefs->GetBoolean(prefs::kSignInPromoShowNTPBubble); |
259 | 239 |
260 base::string16 message = hide_sync ? base::string16() : | 240 base::string16 message = hide_sync ? base::string16() : |
261 l10n_util::GetStringFUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_MESSAGE, | 241 l10n_util::GetStringFUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_MESSAGE, |
262 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | 242 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); |
263 | 243 |
264 values->SetString("login_status_message", message); | 244 values->SetString("login_status_message", message); |
265 values->SetString("login_status_url", | 245 values->SetString("login_status_url", |
266 hide_sync ? std::string() : chrome::kSyncLearnMoreURL); | 246 hide_sync ? std::string() : chrome::kSyncLearnMoreURL); |
267 values->SetString("login_status_advanced", | 247 values->SetString("login_status_advanced", |
268 hide_sync ? base::string16() : | 248 hide_sync ? base::string16() : |
269 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); | 249 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); |
270 values->SetString("login_status_dismiss", | 250 values->SetString("login_status_dismiss", |
271 hide_sync ? base::string16() : | 251 hide_sync ? base::string16() : |
272 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); | 252 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); |
273 } | 253 } |
OLD | NEW |