Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / webui / app_launcher_login_handler.cc
blobb2821c9f25fabdc640b7a6a2b129ded147506dac
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/common/pref_names.h"
31 #include "chrome/common/url_constants.h"
32 #include "chrome/grit/chromium_strings.h"
33 #include "chrome/grit/generated_resources.h"
34 #include "components/signin/core/browser/signin_manager.h"
35 #include "components/web_resource/promo_resource_service.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 = SigninManagerFactory::GetForProfile(profile)
118 ->GetAuthenticatedAccountInfo()
119 .email;
120 if (!username.empty())
121 return;
123 content::WebContents* web_contents = web_ui()->GetWebContents();
124 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
125 if (!browser)
126 return;
128 // The user isn't signed in, show the sign in promo.
129 signin_metrics::Source source =
130 web_contents->GetURL().spec() == chrome::kChromeUIAppsURL ?
131 signin_metrics::SOURCE_APPS_PAGE_LINK :
132 signin_metrics::SOURCE_NTP_LINK;
133 chrome::ShowBrowserSignin(browser, source);
134 RecordInHistogram(NTP_SIGN_IN_PROMO_CLICKED);
137 void AppLauncherLoginHandler::RecordInHistogram(int type) {
138 // Invalid type to record.
139 if (type < NTP_SIGN_IN_PROMO_VIEWED ||
140 type > NTP_SIGN_IN_PROMO_CLICKED) {
141 NOTREACHED();
142 } else {
143 UMA_HISTOGRAM_ENUMERATION("SyncPromo.NTPPromo", type,
144 NTP_SIGN_IN_PROMO_BUCKET_BOUNDARY);
148 void AppLauncherLoginHandler::HandleLoginMessageSeen(
149 const base::ListValue* args) {
150 Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean(
151 prefs::kSignInPromoShowNTPBubble, false);
152 NewTabUI* ntp_ui = NewTabUI::FromWebUIController(web_ui()->GetController());
153 // When instant extended is enabled, there may not be a NewTabUI object.
154 if (ntp_ui)
155 ntp_ui->set_showing_sync_bubble(true);
158 void AppLauncherLoginHandler::HandleShowAdvancedLoginUI(
159 const base::ListValue* args) {
160 Browser* browser =
161 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
162 if (browser)
163 chrome::ShowBrowserSignin(browser, signin_metrics::SOURCE_NTP_LINK);
166 void AppLauncherLoginHandler::UpdateLogin() {
167 std::string username = profile_info_watcher_->GetAuthenticatedUsername();
168 base::string16 header, sub_header;
169 std::string icon_url;
170 Profile* profile = Profile::FromWebUI(web_ui());
171 if (!username.empty()) {
172 ProfileInfoCache& cache =
173 g_browser_process->profile_manager()->GetProfileInfoCache();
174 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
175 if (profile_index != std::string::npos) {
176 // Only show the profile picture and full name for the single profile
177 // case. In the multi-profile case the profile picture is visible in the
178 // title bar and the full name can be ambiguous.
179 if (cache.GetNumberOfProfiles() == 1) {
180 base::string16 name = cache.GetGAIANameOfProfileAtIndex(profile_index);
181 if (!name.empty())
182 header = CreateElementWithClass(name, "span", "profile-name", "");
183 const gfx::Image* image =
184 cache.GetGAIAPictureOfProfileAtIndex(profile_index);
185 if (image)
186 icon_url = webui::GetBitmapDataUrl(GetGAIAPictureForNTP(*image));
188 if (header.empty()) {
189 header = CreateElementWithClass(base::UTF8ToUTF16(username), "span",
190 "profile-name", "");
193 } else {
194 #if !defined(OS_CHROMEOS)
195 // Chromeos does not show this status header.
196 SigninManager* signin = SigninManagerFactory::GetForProfile(
197 profile->GetOriginalProfile());
198 if (!profile->IsLegacySupervised() && signin->IsSigninAllowed()) {
199 base::string16 signed_in_link = l10n_util::GetStringUTF16(
200 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK);
201 signed_in_link =
202 CreateElementWithClass(signed_in_link, "a", "", "action-link");
203 header = l10n_util::GetStringFUTF16(
204 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER,
205 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
206 sub_header = l10n_util::GetStringFUTF16(
207 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link);
208 // Record that the user was shown the promo.
209 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED);
211 #endif
214 base::StringValue header_value(header);
215 base::StringValue sub_header_value(sub_header);
216 base::StringValue icon_url_value(icon_url);
217 base::FundamentalValue is_user_signed_in(!username.empty());
218 web_ui()->CallJavascriptFunction("ntp.updateLogin",
219 header_value, sub_header_value, icon_url_value, is_user_signed_in);
222 // static
223 bool AppLauncherLoginHandler::ShouldShow(Profile* profile) {
224 #if defined(OS_CHROMEOS)
225 // For now we don't care about showing sync status on Chrome OS. The promo
226 // UI and the avatar menu don't exist on that platform.
227 return false;
228 #else
229 SigninManager* signin = SigninManagerFactory::GetForProfile(profile);
230 return !profile->IsOffTheRecord() && signin && signin->IsSigninAllowed();
231 #endif
234 // static
235 void AppLauncherLoginHandler::GetLocalizedValues(
236 Profile* profile, base::DictionaryValue* values) {
237 PrefService* prefs = profile->GetPrefs();
238 bool hide_sync = !prefs->GetBoolean(prefs::kSignInPromoShowNTPBubble);
240 base::string16 message = hide_sync ? base::string16() :
241 l10n_util::GetStringFUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_MESSAGE,
242 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
244 values->SetString("login_status_message", message);
245 values->SetString("login_status_url",
246 hide_sync ? std::string() : chrome::kSyncLearnMoreURL);
247 values->SetString("login_status_advanced",
248 hide_sync ? base::string16() :
249 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED));
250 values->SetString("login_status_dismiss",
251 hide_sync ? base::string16() :
252 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK));