OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ntp/ntp_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/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/google/google_util.h" | 15 #include "chrome/browser/google/google_util.h" |
15 #include "chrome/browser/prefs/pref_notifier.h" | 16 #include "chrome/browser/prefs/pref_notifier.h" |
16 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
17 #include "chrome/browser/profiles/profile.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" |
18 #include "chrome/browser/profiles/profile_metrics.h" | 21 #include "chrome/browser/profiles/profile_metrics.h" |
19 #include "chrome/browser/sync/profile_sync_service.h" | 22 #include "chrome/browser/sync/profile_sync_service.h" |
20 #include "chrome/browser/sync/sync_setup_flow.h" | 23 #include "chrome/browser/sync/sync_setup_flow.h" |
21 #include "chrome/browser/ui/browser.h" | 24 #include "chrome/browser/ui/browser.h" |
22 #include "chrome/browser/ui/browser_list.h" | 25 #include "chrome/browser/ui/browser_list.h" |
23 #include "chrome/browser/ui/browser_window.h" | 26 #include "chrome/browser/ui/browser_window.h" |
24 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 27 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
25 #include "chrome/browser/ui/webui/sync_promo_ui.h" | 28 #include "chrome/browser/ui/webui/sync_promo_ui.h" |
| 29 #include "chrome/browser/ui/webui/web_ui_util.h" |
26 #include "chrome/browser/web_resource/promo_resource_service.h" | 30 #include "chrome/browser/web_resource/promo_resource_service.h" |
27 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
28 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
29 #include "chrome/common/url_constants.h" | 33 #include "chrome/common/url_constants.h" |
30 #include "content/browser/tab_contents/tab_contents.h" | 34 #include "content/browser/tab_contents/tab_contents.h" |
31 #include "content/public/browser/notification_details.h" | 35 #include "content/public/browser/notification_details.h" |
| 36 #include "content/public/browser/notification_service.h" |
32 #include "grit/chromium_strings.h" | 37 #include "grit/chromium_strings.h" |
33 #include "grit/generated_resources.h" | 38 #include "grit/generated_resources.h" |
| 39 #include "skia/ext/image_operations.h" |
34 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
| 41 #include "ui/gfx/canvas_skia.h" |
| 42 #include "ui/gfx/image/image.h" |
| 43 |
| 44 namespace { |
| 45 |
| 46 SkBitmap GetGAIAPictureForNTP(const gfx::Image& image) { |
| 47 // This value must match the width and height value of login-status-icon |
| 48 // in new_tab.css. |
| 49 const int length = 27; |
| 50 SkBitmap bmp = skia::ImageOperations::Resize( |
| 51 image, skia::ImageOperations::RESIZE_BEST, length, length); |
| 52 |
| 53 gfx::CanvasSkia canvas(length, length, false); |
| 54 canvas.DrawBitmapInt(bmp, 0, 0); |
| 55 |
| 56 // Draw a gray border on the inside of the icon. |
| 57 SkColor color = SkColorSetARGB(83, 0, 0, 0); |
| 58 canvas.DrawRectInt(color, 0, 0, length - 1, length - 1); |
| 59 |
| 60 return canvas.ExtractBitmap(); |
| 61 } |
| 62 |
| 63 } // namespace |
35 | 64 |
36 NTPLoginHandler::NTPLoginHandler() { | 65 NTPLoginHandler::NTPLoginHandler() { |
37 } | 66 } |
38 | 67 |
39 NTPLoginHandler::~NTPLoginHandler() { | 68 NTPLoginHandler::~NTPLoginHandler() { |
40 } | 69 } |
41 | 70 |
42 WebUIMessageHandler* NTPLoginHandler::Attach(WebUI* web_ui) { | 71 WebUIMessageHandler* NTPLoginHandler::Attach(WebUI* web_ui) { |
43 PrefService* pref_service = Profile::FromWebUI(web_ui)->GetPrefs(); | 72 PrefService* pref_service = Profile::FromWebUI(web_ui)->GetPrefs(); |
44 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); | 73 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); |
45 | 74 |
| 75 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 76 content::NotificationService::AllSources()); |
| 77 |
46 return WebUIMessageHandler::Attach(web_ui); | 78 return WebUIMessageHandler::Attach(web_ui); |
47 } | 79 } |
48 | 80 |
49 void NTPLoginHandler::RegisterMessages() { | 81 void NTPLoginHandler::RegisterMessages() { |
50 web_ui_->RegisterMessageCallback("initializeSyncLogin", | 82 web_ui_->RegisterMessageCallback("initializeSyncLogin", |
51 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin, | 83 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin, |
52 base::Unretained(this))); | 84 base::Unretained(this))); |
53 web_ui_->RegisterMessageCallback("showSyncLoginUI", | 85 web_ui_->RegisterMessageCallback("showSyncLoginUI", |
54 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI, | 86 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI, |
55 base::Unretained(this))); | 87 base::Unretained(this))); |
56 web_ui_->RegisterMessageCallback("loginMessageSeen", | 88 web_ui_->RegisterMessageCallback("loginMessageSeen", |
57 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen, | 89 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen, |
58 base::Unretained(this))); | 90 base::Unretained(this))); |
59 web_ui_->RegisterMessageCallback("showAdvancedLoginUI", | 91 web_ui_->RegisterMessageCallback("showAdvancedLoginUI", |
60 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI, | 92 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI, |
61 base::Unretained(this))); | 93 base::Unretained(this))); |
62 } | 94 } |
63 | 95 |
64 void NTPLoginHandler::Observe(int type, | 96 void NTPLoginHandler::Observe(int type, |
65 const content::NotificationSource& source, | 97 const content::NotificationSource& source, |
66 const content::NotificationDetails& details) { | 98 const content::NotificationDetails& details) { |
67 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); | 99 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) { |
68 std::string* name = content::Details<std::string>(details).ptr(); | |
69 if (prefs::kGoogleServicesUsername == *name) | |
70 UpdateLogin(); | 100 UpdateLogin(); |
| 101 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
| 102 std::string* name = content::Details<std::string>(details).ptr(); |
| 103 if (prefs::kGoogleServicesUsername == *name) |
| 104 UpdateLogin(); |
| 105 } else { |
| 106 NOTREACHED(); |
| 107 } |
71 } | 108 } |
72 | 109 |
73 void NTPLoginHandler::HandleInitializeSyncLogin(const ListValue* args) { | 110 void NTPLoginHandler::HandleInitializeSyncLogin(const ListValue* args) { |
74 UpdateLogin(); | 111 UpdateLogin(); |
75 } | 112 } |
76 | 113 |
77 void NTPLoginHandler::HandleShowSyncLoginUI(const ListValue* args) { | 114 void NTPLoginHandler::HandleShowSyncLoginUI(const ListValue* args) { |
78 Profile* profile = Profile::FromWebUI(web_ui_); | 115 Profile* profile = Profile::FromWebUI(web_ui_); |
79 std::string username = profile->GetPrefs()->GetString( | 116 std::string username = profile->GetPrefs()->GetString( |
80 prefs::kGoogleServicesUsername); | 117 prefs::kGoogleServicesUsername); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 void NTPLoginHandler::HandleShowAdvancedLoginUI(const ListValue* args) { | 167 void NTPLoginHandler::HandleShowAdvancedLoginUI(const ListValue* args) { |
131 Profile::FromWebUI(web_ui_)->GetProfileSyncService()->ShowConfigure(false); | 168 Profile::FromWebUI(web_ui_)->GetProfileSyncService()->ShowConfigure(false); |
132 } | 169 } |
133 | 170 |
134 void NTPLoginHandler::UpdateLogin() { | 171 void NTPLoginHandler::UpdateLogin() { |
135 Profile* profile = Profile::FromWebUI(web_ui_); | 172 Profile* profile = Profile::FromWebUI(web_ui_); |
136 std::string username = profile->GetPrefs()->GetString( | 173 std::string username = profile->GetPrefs()->GetString( |
137 prefs::kGoogleServicesUsername); | 174 prefs::kGoogleServicesUsername); |
138 | 175 |
139 string16 header, sub_header; | 176 string16 header, sub_header; |
| 177 std::string icon_url; |
140 if (!username.empty()) { | 178 if (!username.empty()) { |
141 header = UTF8ToUTF16(username); | 179 ProfileInfoCache& cache = |
| 180 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 181 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); |
| 182 if (profile_index != std::string::npos) { |
| 183 // Only show the profile picture and full name for the single profile |
| 184 // case. In the multi-profile case the profile picture is visible in the |
| 185 // title bar and the full name can be ambiguous. |
| 186 if (cache.GetNumberOfProfiles() == 1) { |
| 187 header = cache.GetGAIANameOfProfileAtIndex(profile_index); |
| 188 const gfx::Image* image = |
| 189 cache.GetGAIAPictureOfProfileAtIndex(profile_index); |
| 190 if (image) { |
| 191 fprintf(stderr, "%s - setting image\n", __func__); |
| 192 icon_url = web_ui_util::GetImageDataUrl(GetGAIAPictureForNTP(*image)); |
| 193 } |
| 194 } |
| 195 if (header.empty()) |
| 196 header = UTF8ToUTF16(username); |
| 197 } |
142 } else if (SyncPromoUI::ShouldShowSyncPromo(profile) && | 198 } else if (SyncPromoUI::ShouldShowSyncPromo(profile) && |
143 (SyncPromoUI::UserHasSeenSyncPromoAtStartup(profile) || | 199 (SyncPromoUI::UserHasSeenSyncPromoAtStartup(profile) || |
144 PromoResourceService::CanShowNTPSignInPromo(profile))) { | 200 PromoResourceService::CanShowNTPSignInPromo(profile))) { |
145 string16 signed_in_link = l10n_util::GetStringUTF16( | 201 string16 signed_in_link = l10n_util::GetStringUTF16( |
146 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK); | 202 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK); |
147 signed_in_link = ASCIIToUTF16("<span class='link-span'>") + signed_in_link + | 203 signed_in_link = ASCIIToUTF16("<span class='link-span'>") + signed_in_link + |
148 ASCIIToUTF16("</span>"); | 204 ASCIIToUTF16("</span>"); |
149 header = l10n_util::GetStringFUTF16( | 205 header = l10n_util::GetStringFUTF16( |
150 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER, | 206 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER, |
151 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | 207 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); |
152 sub_header = l10n_util::GetStringFUTF16( | 208 sub_header = l10n_util::GetStringFUTF16( |
153 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link); | 209 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link); |
154 // Record that the user was shown the promo. | 210 // Record that the user was shown the promo. |
155 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED); | 211 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED); |
156 } | 212 } |
157 | 213 |
158 StringValue header_value(header); | 214 StringValue header_value(header); |
159 StringValue sub_header_value(sub_header); | 215 StringValue sub_header_value(sub_header); |
| 216 StringValue icon_url_value(icon_url); |
160 web_ui_->CallJavascriptFunction( | 217 web_ui_->CallJavascriptFunction( |
161 "updateLogin", header_value, sub_header_value); | 218 "updateLogin", header_value, sub_header_value, icon_url_value); |
162 } | 219 } |
163 | 220 |
164 // static | 221 // static |
165 bool NTPLoginHandler::ShouldShow(Profile* profile) { | 222 bool NTPLoginHandler::ShouldShow(Profile* profile) { |
166 #if defined(OS_CHROMEOS) | 223 #if defined(OS_CHROMEOS) |
167 // For now we don't care about showing sync status on Chrome OS. The promo | 224 // For now we don't care about showing sync status on Chrome OS. The promo |
168 // UI and the avatar menu don't exist on that platform. | 225 // UI and the avatar menu don't exist on that platform. |
169 return false; | 226 return false; |
170 #else | 227 #else |
171 if (profile->IsOffTheRecord()) | 228 if (profile->IsOffTheRecord()) |
(...skipping 17 matching lines...) Expand all Loading... |
189 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); | 246 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); |
190 values->SetString("login_status_url", | 247 values->SetString("login_status_url", |
191 google_util::StringAppendGoogleLocaleParam(chrome::kSyncLearnMoreURL)); | 248 google_util::StringAppendGoogleLocaleParam(chrome::kSyncLearnMoreURL)); |
192 values->SetString("login_status_learn_more", | 249 values->SetString("login_status_learn_more", |
193 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); | 250 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); |
194 values->SetString("login_status_advanced", | 251 values->SetString("login_status_advanced", |
195 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); | 252 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); |
196 values->SetString("login_status_dismiss", | 253 values->SetString("login_status_dismiss", |
197 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); | 254 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); |
198 } | 255 } |
OLD | NEW |