Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / app_launcher_login_handler.cc
blob3d4d3553622e2b89d21031e58fb51a0fc2384f6b
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.
5 #include "chrome/browser/ui/webui/app_launcher_login_handler.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/metrics/histogram.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_info_cache.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/profiles/profile_metrics.h"
20 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/signin/signin_promo.h"
22 #include "chrome/browser/sync/profile_sync_service.h"
23 #include "chrome/browser/sync/profile_sync_service_factory.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_finder.h"
26 #include "chrome/browser/ui/browser_window.h"
27 #include "chrome/browser/ui/chrome_pages.h"
28 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
29 #include "chrome/browser/ui/webui/profile_info_watcher.h"
30 #include "chrome/browser/web_resource/promo_resource_service.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/common/url_constants.h"
33 #include "chrome/grit/chromium_strings.h"
34 #include "chrome/grit/generated_resources.h"
35 #include "components/signin/core/browser/signin_manager.h"
36 #include "content/public/browser/host_zoom_map.h"
37 #include "content/public/browser/web_contents.h"
38 #include "content/public/browser/web_ui.h"
39 #include "content/public/common/page_zoom.h"
40 #include "net/base/escape.h"
41 #include "skia/ext/image_operations.h"
42 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/base/webui/web_ui_util.h"
44 #include "ui/gfx/canvas.h"
45 #include "ui/gfx/image/image.h"
47 using content::OpenURLParams;
48 using content::Referrer;
50 namespace {
52 SkBitmap GetGAIAPictureForNTP(const gfx::Image& image) {
53 // This value must match the width and height value of login-status-icon
54 // in new_tab.css.
55 const int kLength = 27;
56 SkBitmap bmp = skia::ImageOperations::Resize(*image.ToSkBitmap(),
57 skia::ImageOperations::RESIZE_BEST, kLength, kLength);
59 gfx::Canvas canvas(gfx::Size(kLength, kLength), 1.0f, false);
60 canvas.DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bmp), 0, 0);
62 // Draw a gray border on the inside of the icon.
63 SkColor color = SkColorSetARGB(83, 0, 0, 0);
64 canvas.DrawRect(gfx::Rect(0, 0, kLength - 1, kLength - 1), color);
66 return canvas.ExtractImageRep().sk_bitmap();
69 // Puts the |content| into an element with the given CSS class.
70 base::string16 CreateElementWithClass(const base::string16& content,
71 const std::string& tag_name,
72 const std::string& css_class,
73 const std::string& extends_tag) {
74 base::string16 start_tag = base::ASCIIToUTF16("<" + tag_name +
75 " class='" + css_class + "' is='" + extends_tag + "'>");
76 base::string16 end_tag = base::ASCIIToUTF16("</" + tag_name + ">");
77 return start_tag + net::EscapeForHTML(content) + end_tag;
80 } // namespace
82 AppLauncherLoginHandler::AppLauncherLoginHandler() {}
84 AppLauncherLoginHandler::~AppLauncherLoginHandler() {}
86 void AppLauncherLoginHandler::RegisterMessages() {
87 profile_info_watcher_.reset(new ProfileInfoWatcher(
88 Profile::FromWebUI(web_ui()),
89 base::Bind(&AppLauncherLoginHandler::UpdateLogin,
90 base::Unretained(this))));
92 web_ui()->RegisterMessageCallback("initializeSyncLogin",
93 base::Bind(&AppLauncherLoginHandler::HandleInitializeSyncLogin,
94 base::Unretained(this)));
95 web_ui()->RegisterMessageCallback("showSyncLoginUI",
96 base::Bind(&AppLauncherLoginHandler::HandleShowSyncLoginUI,
97 base::Unretained(this)));
98 web_ui()->RegisterMessageCallback("loginMessageSeen",
99 base::Bind(&AppLauncherLoginHandler::HandleLoginMessageSeen,
100 base::Unretained(this)));
101 web_ui()->RegisterMessageCallback("showAdvancedLoginUI",
102 base::Bind(&AppLauncherLoginHandler::HandleShowAdvancedLoginUI,
103 base::Unretained(this)));
106 void AppLauncherLoginHandler::HandleInitializeSyncLogin(
107 const base::ListValue* args) {
108 UpdateLogin();
111 void AppLauncherLoginHandler::HandleShowSyncLoginUI(
112 const base::ListValue* args) {
113 Profile* profile = Profile::FromWebUI(web_ui());
114 if (!signin::ShouldShowPromo(profile))
115 return;
117 std::string username =
118 SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername();
119 if (!username.empty())
120 return;
122 content::WebContents* web_contents = web_ui()->GetWebContents();
123 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
124 if (!browser)
125 return;
127 // The user isn't signed in, show the sign in promo.
128 signin_metrics::Source source =
129 web_contents->GetURL().spec() == chrome::kChromeUIAppsURL ?
130 signin_metrics::SOURCE_APPS_PAGE_LINK :
131 signin_metrics::SOURCE_NTP_LINK;
132 chrome::ShowBrowserSignin(browser, source);
133 RecordInHistogram(NTP_SIGN_IN_PROMO_CLICKED);
136 void AppLauncherLoginHandler::RecordInHistogram(int type) {
137 // Invalid type to record.
138 if (type < NTP_SIGN_IN_PROMO_VIEWED ||
139 type > NTP_SIGN_IN_PROMO_CLICKED) {
140 NOTREACHED();
141 } else {
142 UMA_HISTOGRAM_ENUMERATION("SyncPromo.NTPPromo", type,
143 NTP_SIGN_IN_PROMO_BUCKET_BOUNDARY);
147 void AppLauncherLoginHandler::HandleLoginMessageSeen(
148 const base::ListValue* args) {
149 Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean(
150 prefs::kSignInPromoShowNTPBubble, false);
151 NewTabUI* ntp_ui = NewTabUI::FromWebUIController(web_ui()->GetController());
152 // When instant extended is enabled, there may not be a NewTabUI object.
153 if (ntp_ui)
154 ntp_ui->set_showing_sync_bubble(true);
157 void AppLauncherLoginHandler::HandleShowAdvancedLoginUI(
158 const base::ListValue* args) {
159 Browser* browser =
160 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
161 if (browser)
162 chrome::ShowBrowserSignin(browser, signin_metrics::SOURCE_NTP_LINK);
165 void AppLauncherLoginHandler::UpdateLogin() {
166 std::string username = profile_info_watcher_->GetAuthenticatedUsername();
167 base::string16 header, sub_header;
168 std::string icon_url;
169 Profile* profile = Profile::FromWebUI(web_ui());
170 if (!username.empty()) {
171 ProfileInfoCache& cache =
172 g_browser_process->profile_manager()->GetProfileInfoCache();
173 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
174 if (profile_index != std::string::npos) {
175 // Only show the profile picture and full name for the single profile
176 // case. In the multi-profile case the profile picture is visible in the
177 // title bar and the full name can be ambiguous.
178 if (cache.GetNumberOfProfiles() == 1) {
179 base::string16 name = cache.GetGAIANameOfProfileAtIndex(profile_index);
180 if (!name.empty())
181 header = CreateElementWithClass(name, "span", "profile-name", "");
182 const gfx::Image* image =
183 cache.GetGAIAPictureOfProfileAtIndex(profile_index);
184 if (image)
185 icon_url = webui::GetBitmapDataUrl(GetGAIAPictureForNTP(*image));
187 if (header.empty()) {
188 header = CreateElementWithClass(base::UTF8ToUTF16(username), "span",
189 "profile-name", "");
192 } else {
193 #if !defined(OS_CHROMEOS)
194 // Chromeos does not show this status header.
195 SigninManager* signin = SigninManagerFactory::GetForProfile(
196 profile->GetOriginalProfile());
197 if (!profile->IsLegacySupervised() && signin->IsSigninAllowed()) {
198 base::string16 signed_in_link = l10n_util::GetStringUTF16(
199 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK);
200 signed_in_link =
201 CreateElementWithClass(signed_in_link, "a", "", "action-link");
202 header = l10n_util::GetStringFUTF16(
203 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER,
204 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
205 sub_header = l10n_util::GetStringFUTF16(
206 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link);
207 // Record that the user was shown the promo.
208 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED);
210 #endif
213 base::StringValue header_value(header);
214 base::StringValue sub_header_value(sub_header);
215 base::StringValue icon_url_value(icon_url);
216 base::FundamentalValue is_user_signed_in(!username.empty());
217 web_ui()->CallJavascriptFunction("ntp.updateLogin",
218 header_value, sub_header_value, icon_url_value, is_user_signed_in);
221 // static
222 bool AppLauncherLoginHandler::ShouldShow(Profile* profile) {
223 #if defined(OS_CHROMEOS)
224 // For now we don't care about showing sync status on Chrome OS. The promo
225 // UI and the avatar menu don't exist on that platform.
226 return false;
227 #else
228 SigninManager* signin = SigninManagerFactory::GetForProfile(profile);
229 return !profile->IsOffTheRecord() && signin && signin->IsSigninAllowed();
230 #endif
233 // static
234 void AppLauncherLoginHandler::GetLocalizedValues(
235 Profile* profile, base::DictionaryValue* values) {
236 PrefService* prefs = profile->GetPrefs();
237 bool hide_sync = !prefs->GetBoolean(prefs::kSignInPromoShowNTPBubble);
239 base::string16 message = hide_sync ? base::string16() :
240 l10n_util::GetStringFUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_MESSAGE,
241 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
243 values->SetString("login_status_message", message);
244 values->SetString("login_status_url",
245 hide_sync ? std::string() : chrome::kSyncLearnMoreURL);
246 values->SetString("login_status_advanced",
247 hide_sync ? base::string16() :
248 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED));
249 values->SetString("login_status_dismiss",
250 hide_sync ? base::string16() :
251 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK));