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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" 6 7 #include <string> 8 9 #include "base/bind.h" 10 #include "base/bind_helpers.h" 11 #include "base/metrics/histogram.h" 12 #include "base/prefs/pref_notifier.h" 13 #include "base/prefs/pref_service.h" 14 #include "base/strings/utf_string_conversions.h" 15 #include "base/values.h" 16 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/chrome_notification_types.h" 18 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile_info_cache.h" 20 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_metrics.h" 22 #include "chrome/browser/signin/signin_manager_factory.h" 23 #include "chrome/browser/signin/signin_promo.h" 24 #include "chrome/browser/sync/profile_sync_service.h" 25 #include "chrome/browser/sync/profile_sync_service_factory.h" 26 #include "chrome/browser/ui/browser.h" 27 #include "chrome/browser/ui/browser_finder.h" 28 #include "chrome/browser/ui/browser_window.h" 29 #include "chrome/browser/ui/chrome_pages.h" 30 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 31 #include "chrome/browser/web_resource/promo_resource_service.h" 32 #include "chrome/common/pref_names.h" 33 #include "chrome/common/url_constants.h" 34 #include "components/signin/core/browser/signin_manager.h" 35 #include "content/public/browser/host_zoom_map.h" 36 #include "content/public/browser/notification_details.h" 37 #include "content/public/browser/notification_service.h" 38 #include "content/public/browser/web_contents.h" 39 #include "content/public/browser/web_ui.h" 40 #include "content/public/common/page_zoom.h" 41 #include "grit/chromium_strings.h" 42 #include "grit/generated_resources.h" 43 #include "net/base/escape.h" 44 #include "skia/ext/image_operations.h" 45 #include "ui/base/l10n/l10n_util.h" 46 #include "ui/base/webui/web_ui_util.h" 47 #include "ui/gfx/canvas.h" 48 #include "ui/gfx/image/image.h" 49 50 using content::OpenURLParams; 51 using content::Referrer; 52 53 namespace { 54 55 SkBitmap GetGAIAPictureForNTP(const gfx::Image& image) { 56 // This value must match the width and height value of login-status-icon 57 // in new_tab.css. 58 const int kLength = 27; 59 SkBitmap bmp = skia::ImageOperations::Resize(*image.ToSkBitmap(), 60 skia::ImageOperations::RESIZE_BEST, kLength, kLength); 61 62 gfx::Canvas canvas(gfx::Size(kLength, kLength), 1.0f, false); 63 canvas.DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bmp), 0, 0); 64 65 // Draw a gray border on the inside of the icon. 66 SkColor color = SkColorSetARGB(83, 0, 0, 0); 67 canvas.DrawRect(gfx::Rect(0, 0, kLength - 1, kLength - 1), color); 68 69 return canvas.ExtractImageRep().sk_bitmap(); 70 } 71 72 // Puts the |content| into a span with the given CSS class. 73 base::string16 CreateSpanWithClass(const base::string16& content, 74 const std::string& css_class) { 75 return base::ASCIIToUTF16("<span class='" + css_class + "'>") + 76 net::EscapeForHTML(content) + base::ASCIIToUTF16("</span>"); 77 } 78 79 } // namespace 80 81 NTPLoginHandler::NTPLoginHandler() { 82 } 83 84 NTPLoginHandler::~NTPLoginHandler() { 85 } 86 87 void NTPLoginHandler::RegisterMessages() { 88 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); 89 username_pref_.Init(prefs::kGoogleServicesUsername, 90 pref_service, 91 base::Bind(&NTPLoginHandler::UpdateLogin, 92 base::Unretained(this))); 93 signin_allowed_pref_.Init(prefs::kSigninAllowed, 94 pref_service, 95 base::Bind(&NTPLoginHandler::UpdateLogin, 96 base::Unretained(this))); 97 98 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, 99 content::NotificationService::AllSources()); 100 101 web_ui()->RegisterMessageCallback("initializeSyncLogin", 102 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin, 103 base::Unretained(this))); 104 web_ui()->RegisterMessageCallback("showSyncLoginUI", 105 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI, 106 base::Unretained(this))); 107 web_ui()->RegisterMessageCallback("loginMessageSeen", 108 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen, 109 base::Unretained(this))); 110 web_ui()->RegisterMessageCallback("showAdvancedLoginUI", 111 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI, 112 base::Unretained(this))); 113 } 114 115 void NTPLoginHandler::Observe(int type, 116 const content::NotificationSource& source, 117 const content::NotificationDetails& details) { 118 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, type); 119 UpdateLogin(); 120 } 121 122 void NTPLoginHandler::HandleInitializeSyncLogin(const base::ListValue* args) { 123 UpdateLogin(); 124 } 125 126 void NTPLoginHandler::HandleShowSyncLoginUI(const base::ListValue* args) { 127 Profile* profile = Profile::FromWebUI(web_ui()); 128 std::string username = profile->GetPrefs()->GetString( 129 prefs::kGoogleServicesUsername); 130 content::WebContents* web_contents = web_ui()->GetWebContents(); 131 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 132 if (!browser) 133 return; 134 135 if (username.empty()) { 136 // The user isn't signed in, show the sign in promo. 137 if (signin::ShouldShowPromo(profile)) { 138 signin::Source source = 139 (web_contents->GetURL().spec() == chrome::kChromeUIAppsURL) ? 140 signin::SOURCE_APPS_PAGE_LINK : 141 signin::SOURCE_NTP_LINK; 142 chrome::ShowBrowserSignin(browser, source); 143 RecordInHistogram(NTP_SIGN_IN_PROMO_CLICKED); 144 } 145 } else if (args->GetSize() == 4) { 146 // The user is signed in, show the profiles menu. 147 double x = 0; 148 double y = 0; 149 double width = 0; 150 double height = 0; 151 bool success = args->GetDouble(0, &x); 152 DCHECK(success); 153 success = args->GetDouble(1, &y); 154 DCHECK(success); 155 success = args->GetDouble(2, &width); 156 DCHECK(success); 157 success = args->GetDouble(3, &height); 158 DCHECK(success); 159 160 double zoom = content::ZoomLevelToZoomFactor( 161 content::HostZoomMap::GetZoomLevel(web_contents)); 162 gfx::Rect rect(x * zoom, y * zoom, width * zoom, height * zoom); 163 164 browser->window()->ShowAvatarBubble(web_ui()->GetWebContents(), rect); 165 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::NTP_AVATAR_BUBBLE); 166 } 167 } 168 169 void NTPLoginHandler::RecordInHistogram(int type) { 170 // Invalid type to record. 171 if (type < NTP_SIGN_IN_PROMO_VIEWED || 172 type > NTP_SIGN_IN_PROMO_CLICKED) { 173 NOTREACHED(); 174 } else { 175 UMA_HISTOGRAM_ENUMERATION("SyncPromo.NTPPromo", type, 176 NTP_SIGN_IN_PROMO_BUCKET_BOUNDARY); 177 } 178 } 179 180 void NTPLoginHandler::HandleLoginMessageSeen(const base::ListValue* args) { 181 Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean( 182 prefs::kSignInPromoShowNTPBubble, false); 183 NewTabUI* ntp_ui = NewTabUI::FromWebUIController(web_ui()->GetController()); 184 // When instant extended is enabled, there may not be a NewTabUI object. 185 if (ntp_ui) 186 ntp_ui->set_showing_sync_bubble(true); 187 } 188 189 void NTPLoginHandler::HandleShowAdvancedLoginUI(const base::ListValue* args) { 190 Browser* browser = 191 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()); 192 if (browser) 193 chrome::ShowBrowserSignin(browser, signin::SOURCE_NTP_LINK); 194 } 195 196 void NTPLoginHandler::UpdateLogin() { 197 Profile* profile = Profile::FromWebUI(web_ui()); 198 std::string username = profile->GetPrefs()->GetString( 199 prefs::kGoogleServicesUsername); 200 201 base::string16 header, sub_header; 202 std::string icon_url; 203 if (!username.empty()) { 204 ProfileInfoCache& cache = 205 g_browser_process->profile_manager()->GetProfileInfoCache(); 206 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 207 if (profile_index != std::string::npos) { 208 // Only show the profile picture and full name for the single profile 209 // case. In the multi-profile case the profile picture is visible in the 210 // title bar and the full name can be ambiguous. 211 if (cache.GetNumberOfProfiles() == 1) { 212 base::string16 name = cache.GetGAIANameOfProfileAtIndex(profile_index); 213 if (!name.empty()) 214 header = CreateSpanWithClass(name, "profile-name"); 215 const gfx::Image* image = 216 cache.GetGAIAPictureOfProfileAtIndex(profile_index); 217 if (image) 218 icon_url = webui::GetBitmapDataUrl(GetGAIAPictureForNTP(*image)); 219 } 220 if (header.empty()) { 221 header = CreateSpanWithClass(base::UTF8ToUTF16(username), 222 "profile-name"); 223 } 224 } 225 } else { 226 #if !defined(OS_CHROMEOS) 227 // Chromeos does not show this status header. 228 SigninManager* signin = SigninManagerFactory::GetForProfile( 229 profile->GetOriginalProfile()); 230 if (!profile->IsSupervised() && signin->IsSigninAllowed()) { 231 base::string16 signed_in_link = l10n_util::GetStringUTF16( 232 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK); 233 signed_in_link = CreateSpanWithClass(signed_in_link, "link-span"); 234 header = l10n_util::GetStringFUTF16( 235 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER, 236 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); 237 sub_header = l10n_util::GetStringFUTF16( 238 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link); 239 // Record that the user was shown the promo. 240 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED); 241 } 242 #endif 243 } 244 245 base::StringValue header_value(header); 246 base::StringValue sub_header_value(sub_header); 247 base::StringValue icon_url_value(icon_url); 248 base::FundamentalValue is_user_signed_in(!username.empty()); 249 web_ui()->CallJavascriptFunction("ntp.updateLogin", 250 header_value, sub_header_value, icon_url_value, is_user_signed_in); 251 } 252 253 // static 254 bool NTPLoginHandler::ShouldShow(Profile* profile) { 255 #if defined(OS_CHROMEOS) 256 // For now we don't care about showing sync status on Chrome OS. The promo 257 // UI and the avatar menu don't exist on that platform. 258 return false; 259 #else 260 SigninManager* signin = SigninManagerFactory::GetForProfile(profile); 261 return !profile->IsOffTheRecord() && signin && signin->IsSigninAllowed(); 262 #endif 263 } 264 265 // static 266 void NTPLoginHandler::GetLocalizedValues(Profile* profile, 267 base::DictionaryValue* values) { 268 PrefService* prefs = profile->GetPrefs(); 269 bool hide_sync = !prefs->GetBoolean(prefs::kSignInPromoShowNTPBubble); 270 271 base::string16 message = hide_sync ? base::string16() : 272 l10n_util::GetStringFUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_MESSAGE, 273 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); 274 275 values->SetString("login_status_message", message); 276 values->SetString("login_status_url", 277 hide_sync ? std::string() : chrome::kSyncLearnMoreURL); 278 values->SetString("login_status_advanced", 279 hide_sync ? base::string16() : 280 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); 281 values->SetString("login_status_dismiss", 282 hide_sync ? base::string16() : 283 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); 284 } 285