OLD | NEW |
| (Empty) |
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/profiles/profile.h" | |
18 #include "chrome/browser/profiles/profile_info_cache.h" | |
19 #include "chrome/browser/profiles/profile_manager.h" | |
20 #include "chrome/browser/profiles/profile_metrics.h" | |
21 #include "chrome/browser/signin/signin_manager_factory.h" | |
22 #include "chrome/browser/signin/signin_promo.h" | |
23 #include "chrome/browser/sync/profile_sync_service.h" | |
24 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
25 #include "chrome/browser/ui/browser.h" | |
26 #include "chrome/browser/ui/browser_finder.h" | |
27 #include "chrome/browser/ui/browser_window.h" | |
28 #include "chrome/browser/ui/chrome_pages.h" | |
29 #include "chrome/browser/ui/webui/ntp/new_tab_ui.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" | |
46 | |
47 using content::OpenURLParams; | |
48 using content::Referrer; | |
49 | |
50 namespace { | |
51 | |
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); | |
58 | |
59 gfx::Canvas canvas(gfx::Size(kLength, kLength), 1.0f, false); | |
60 canvas.DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bmp), 0, 0); | |
61 | |
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); | |
65 | |
66 return canvas.ExtractImageRep().sk_bitmap(); | |
67 } | |
68 | |
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; | |
78 } | |
79 | |
80 } // namespace | |
81 | |
82 NTPLoginHandler::NTPLoginHandler() { | |
83 } | |
84 | |
85 NTPLoginHandler::~NTPLoginHandler() { | |
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 | |
92 void NTPLoginHandler::RegisterMessages() { | |
93 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
94 // The profile_manager might be NULL in testing environments. | |
95 if (profile_manager) | |
96 profile_manager->GetProfileInfoCache().AddObserver(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 | |
104 web_ui()->RegisterMessageCallback("initializeSyncLogin", | |
105 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin, | |
106 base::Unretained(this))); | |
107 web_ui()->RegisterMessageCallback("showSyncLoginUI", | |
108 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI, | |
109 base::Unretained(this))); | |
110 web_ui()->RegisterMessageCallback("loginMessageSeen", | |
111 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen, | |
112 base::Unretained(this))); | |
113 web_ui()->RegisterMessageCallback("showAdvancedLoginUI", | |
114 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI, | |
115 base::Unretained(this))); | |
116 } | |
117 | |
118 void NTPLoginHandler::OnProfileAuthInfoChanged( | |
119 const base::FilePath& profile_path) { | |
120 UpdateLogin(); | |
121 } | |
122 | |
123 void NTPLoginHandler::HandleInitializeSyncLogin(const base::ListValue* args) { | |
124 UpdateLogin(); | |
125 } | |
126 | |
127 void NTPLoginHandler::HandleShowSyncLoginUI(const base::ListValue* args) { | |
128 Profile* profile = Profile::FromWebUI(web_ui()); | |
129 if (!signin::ShouldShowPromo(profile)) | |
130 return; | |
131 | |
132 std::string username = | |
133 SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername(); | |
134 if (!username.empty()) | |
135 return; | |
136 | |
137 content::WebContents* web_contents = web_ui()->GetWebContents(); | |
138 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | |
139 if (!browser) | |
140 return; | |
141 | |
142 // The user isn't signed in, show the sign in promo. | |
143 signin_metrics::Source source = | |
144 web_contents->GetURL().spec() == chrome::kChromeUIAppsURL ? | |
145 signin_metrics::SOURCE_APPS_PAGE_LINK : | |
146 signin_metrics::SOURCE_NTP_LINK; | |
147 chrome::ShowBrowserSignin(browser, source); | |
148 RecordInHistogram(NTP_SIGN_IN_PROMO_CLICKED); | |
149 } | |
150 | |
151 void NTPLoginHandler::RecordInHistogram(int type) { | |
152 // Invalid type to record. | |
153 if (type < NTP_SIGN_IN_PROMO_VIEWED || | |
154 type > NTP_SIGN_IN_PROMO_CLICKED) { | |
155 NOTREACHED(); | |
156 } else { | |
157 UMA_HISTOGRAM_ENUMERATION("SyncPromo.NTPPromo", type, | |
158 NTP_SIGN_IN_PROMO_BUCKET_BOUNDARY); | |
159 } | |
160 } | |
161 | |
162 void NTPLoginHandler::HandleLoginMessageSeen(const base::ListValue* args) { | |
163 Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean( | |
164 prefs::kSignInPromoShowNTPBubble, false); | |
165 NewTabUI* ntp_ui = NewTabUI::FromWebUIController(web_ui()->GetController()); | |
166 // When instant extended is enabled, there may not be a NewTabUI object. | |
167 if (ntp_ui) | |
168 ntp_ui->set_showing_sync_bubble(true); | |
169 } | |
170 | |
171 void NTPLoginHandler::HandleShowAdvancedLoginUI(const base::ListValue* args) { | |
172 Browser* browser = | |
173 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()); | |
174 if (browser) | |
175 chrome::ShowBrowserSignin(browser, signin_metrics::SOURCE_NTP_LINK); | |
176 } | |
177 | |
178 void NTPLoginHandler::UpdateLogin() { | |
179 Profile* profile = Profile::FromWebUI(web_ui()); | |
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; | |
190 std::string icon_url; | |
191 if (!username.empty()) { | |
192 ProfileInfoCache& cache = | |
193 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
194 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); | |
195 if (profile_index != std::string::npos) { | |
196 // 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 | |
198 // title bar and the full name can be ambiguous. | |
199 if (cache.GetNumberOfProfiles() == 1) { | |
200 base::string16 name = cache.GetGAIANameOfProfileAtIndex(profile_index); | |
201 if (!name.empty()) | |
202 header = CreateElementWithClass(name, "span", "profile-name", ""); | |
203 const gfx::Image* image = | |
204 cache.GetGAIAPictureOfProfileAtIndex(profile_index); | |
205 if (image) | |
206 icon_url = webui::GetBitmapDataUrl(GetGAIAPictureForNTP(*image)); | |
207 } | |
208 if (header.empty()) { | |
209 header = CreateElementWithClass(base::UTF8ToUTF16(username), "span", | |
210 "profile-name", ""); | |
211 } | |
212 } | |
213 } else { | |
214 #if !defined(OS_CHROMEOS) | |
215 // Chromeos does not show this status header. | |
216 SigninManager* signin = SigninManagerFactory::GetForProfile( | |
217 profile->GetOriginalProfile()); | |
218 if (!profile->IsLegacySupervised() && signin->IsSigninAllowed()) { | |
219 base::string16 signed_in_link = l10n_util::GetStringUTF16( | |
220 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK); | |
221 signed_in_link = | |
222 CreateElementWithClass(signed_in_link, "a", "", "action-link"); | |
223 header = l10n_util::GetStringFUTF16( | |
224 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER, | |
225 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | |
226 sub_header = l10n_util::GetStringFUTF16( | |
227 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link); | |
228 // Record that the user was shown the promo. | |
229 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED); | |
230 } | |
231 #endif | |
232 } | |
233 | |
234 base::StringValue header_value(header); | |
235 base::StringValue sub_header_value(sub_header); | |
236 base::StringValue icon_url_value(icon_url); | |
237 base::FundamentalValue is_user_signed_in(!username.empty()); | |
238 web_ui()->CallJavascriptFunction("ntp.updateLogin", | |
239 header_value, sub_header_value, icon_url_value, is_user_signed_in); | |
240 } | |
241 | |
242 // static | |
243 bool NTPLoginHandler::ShouldShow(Profile* profile) { | |
244 #if defined(OS_CHROMEOS) | |
245 // 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. | |
247 return false; | |
248 #else | |
249 SigninManager* signin = SigninManagerFactory::GetForProfile(profile); | |
250 return !profile->IsOffTheRecord() && signin && signin->IsSigninAllowed(); | |
251 #endif | |
252 } | |
253 | |
254 // static | |
255 void NTPLoginHandler::GetLocalizedValues(Profile* profile, | |
256 base::DictionaryValue* values) { | |
257 PrefService* prefs = profile->GetPrefs(); | |
258 bool hide_sync = !prefs->GetBoolean(prefs::kSignInPromoShowNTPBubble); | |
259 | |
260 base::string16 message = hide_sync ? base::string16() : | |
261 l10n_util::GetStringFUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_MESSAGE, | |
262 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | |
263 | |
264 values->SetString("login_status_message", message); | |
265 values->SetString("login_status_url", | |
266 hide_sync ? std::string() : chrome::kSyncLearnMoreURL); | |
267 values->SetString("login_status_advanced", | |
268 hide_sync ? base::string16() : | |
269 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); | |
270 values->SetString("login_status_dismiss", | |
271 hide_sync ? base::string16() : | |
272 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); | |
273 } | |
OLD | NEW |