Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: chrome/browser/ui/webui/ntp/ntp_login_handler.cc

Issue 8680033: Show GAIA profile info in NTP (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix build Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
34 #include "ui/base/l10n/l10n_util.h" 39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/gfx/image/image.h"
35 41
36 NTPLoginHandler::NTPLoginHandler() { 42 NTPLoginHandler::NTPLoginHandler() {
37 } 43 }
38 44
39 NTPLoginHandler::~NTPLoginHandler() { 45 NTPLoginHandler::~NTPLoginHandler() {
40 } 46 }
41 47
42 WebUIMessageHandler* NTPLoginHandler::Attach(WebUI* web_ui) { 48 WebUIMessageHandler* NTPLoginHandler::Attach(WebUI* web_ui) {
43 PrefService* pref_service = Profile::FromWebUI(web_ui)->GetPrefs(); 49 PrefService* pref_service = Profile::FromWebUI(web_ui)->GetPrefs();
44 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); 50 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this);
45 51
52 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
53 content::NotificationService::AllSources());
54
46 return WebUIMessageHandler::Attach(web_ui); 55 return WebUIMessageHandler::Attach(web_ui);
47 } 56 }
48 57
49 void NTPLoginHandler::RegisterMessages() { 58 void NTPLoginHandler::RegisterMessages() {
50 web_ui_->RegisterMessageCallback("initializeSyncLogin", 59 web_ui_->RegisterMessageCallback("initializeSyncLogin",
51 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin, 60 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin,
52 base::Unretained(this))); 61 base::Unretained(this)));
53 web_ui_->RegisterMessageCallback("showSyncLoginUI", 62 web_ui_->RegisterMessageCallback("showSyncLoginUI",
54 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI, 63 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI,
55 base::Unretained(this))); 64 base::Unretained(this)));
56 web_ui_->RegisterMessageCallback("loginMessageSeen", 65 web_ui_->RegisterMessageCallback("loginMessageSeen",
57 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen, 66 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen,
58 base::Unretained(this))); 67 base::Unretained(this)));
59 web_ui_->RegisterMessageCallback("showAdvancedLoginUI", 68 web_ui_->RegisterMessageCallback("showAdvancedLoginUI",
60 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI, 69 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI,
61 base::Unretained(this))); 70 base::Unretained(this)));
62 } 71 }
63 72
64 void NTPLoginHandler::Observe(int type, 73 void NTPLoginHandler::Observe(int type,
65 const content::NotificationSource& source, 74 const content::NotificationSource& source,
66 const content::NotificationDetails& details) { 75 const content::NotificationDetails& details) {
67 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); 76 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(); 77 UpdateLogin();
78 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) {
79 std::string* name = content::Details<std::string>(details).ptr();
80 if (prefs::kGoogleServicesUsername == *name)
81 UpdateLogin();
82 } else {
83 NOTREACHED();
84 }
71 } 85 }
72 86
73 void NTPLoginHandler::HandleInitializeSyncLogin(const ListValue* args) { 87 void NTPLoginHandler::HandleInitializeSyncLogin(const ListValue* args) {
74 UpdateLogin(); 88 UpdateLogin();
75 } 89 }
76 90
77 void NTPLoginHandler::HandleShowSyncLoginUI(const ListValue* args) { 91 void NTPLoginHandler::HandleShowSyncLoginUI(const ListValue* args) {
78 Profile* profile = Profile::FromWebUI(web_ui_); 92 Profile* profile = Profile::FromWebUI(web_ui_);
79 std::string username = profile->GetPrefs()->GetString( 93 std::string username = profile->GetPrefs()->GetString(
80 prefs::kGoogleServicesUsername); 94 prefs::kGoogleServicesUsername);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 void NTPLoginHandler::HandleShowAdvancedLoginUI(const ListValue* args) { 144 void NTPLoginHandler::HandleShowAdvancedLoginUI(const ListValue* args) {
131 Profile::FromWebUI(web_ui_)->GetProfileSyncService()->ShowConfigure(false); 145 Profile::FromWebUI(web_ui_)->GetProfileSyncService()->ShowConfigure(false);
132 } 146 }
133 147
134 void NTPLoginHandler::UpdateLogin() { 148 void NTPLoginHandler::UpdateLogin() {
135 Profile* profile = Profile::FromWebUI(web_ui_); 149 Profile* profile = Profile::FromWebUI(web_ui_);
136 std::string username = profile->GetPrefs()->GetString( 150 std::string username = profile->GetPrefs()->GetString(
137 prefs::kGoogleServicesUsername); 151 prefs::kGoogleServicesUsername);
138 152
139 string16 header, sub_header; 153 string16 header, sub_header;
154 std::string icon_url;
140 if (!username.empty()) { 155 if (!username.empty()) {
141 header = UTF8ToUTF16(username); 156 ProfileInfoCache& cache =
157 g_browser_process->profile_manager()->GetProfileInfoCache();
158 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
159 if (profile_index != std::string::npos) {
160 // Only show the profile picture and full name for the single profile
161 // case. In the multi-profile case the profile picture is visible in the
162 // title bar and the full name can be ambiguous.
163 if (cache.GetNumberOfProfiles() == 1) {
164 header = cache.GetGAIANameOfProfileAtIndex(profile_index);
165 const gfx::Image* image =
166 cache.GetGAIAPictureOfProfileAtIndex(profile_index);
167 if (image)
168 icon_url = web_ui_util::GetImageDataUrl(*image);
169 }
170 if (header.empty())
171 header = UTF8ToUTF16(username);
172 }
142 } else if (SyncPromoUI::ShouldShowSyncPromo(profile) && 173 } else if (SyncPromoUI::ShouldShowSyncPromo(profile) &&
143 (SyncPromoUI::UserHasSeenSyncPromoAtStartup(profile) || 174 (SyncPromoUI::UserHasSeenSyncPromoAtStartup(profile) ||
144 PromoResourceService::CanShowNTPSignInPromo(profile))) { 175 PromoResourceService::CanShowNTPSignInPromo(profile))) {
145 string16 signed_in_link = l10n_util::GetStringUTF16( 176 string16 signed_in_link = l10n_util::GetStringUTF16(
146 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK); 177 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK);
147 signed_in_link = ASCIIToUTF16("<span class='link-span'>") + signed_in_link + 178 signed_in_link = ASCIIToUTF16("<span class='link-span'>") + signed_in_link +
148 ASCIIToUTF16("</span>"); 179 ASCIIToUTF16("</span>");
149 header = l10n_util::GetStringFUTF16( 180 header = l10n_util::GetStringFUTF16(
150 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER, 181 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER,
151 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); 182 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
152 sub_header = l10n_util::GetStringFUTF16( 183 sub_header = l10n_util::GetStringFUTF16(
153 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link); 184 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link);
154 // Record that the user was shown the promo. 185 // Record that the user was shown the promo.
155 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED); 186 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED);
156 } 187 }
157 188
158 StringValue header_value(header); 189 StringValue header_value(header);
159 StringValue sub_header_value(sub_header); 190 StringValue sub_header_value(sub_header);
191 StringValue icon_url_value(icon_url);
160 web_ui_->CallJavascriptFunction( 192 web_ui_->CallJavascriptFunction(
161 "updateLogin", header_value, sub_header_value); 193 "updateLogin", header_value, sub_header_value, icon_url_value);
162 } 194 }
163 195
164 // static 196 // static
165 bool NTPLoginHandler::ShouldShow(Profile* profile) { 197 bool NTPLoginHandler::ShouldShow(Profile* profile) {
166 #if defined(OS_CHROMEOS) 198 #if defined(OS_CHROMEOS)
167 // For now we don't care about showing sync status on Chrome OS. The promo 199 // 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. 200 // UI and the avatar menu don't exist on that platform.
169 return false; 201 return false;
170 #else 202 #else
171 if (profile->IsOffTheRecord()) 203 if (profile->IsOffTheRecord())
(...skipping 17 matching lines...) Expand all
189 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); 221 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)));
190 values->SetString("login_status_url", 222 values->SetString("login_status_url",
191 google_util::StringAppendGoogleLocaleParam(chrome::kSyncLearnMoreURL)); 223 google_util::StringAppendGoogleLocaleParam(chrome::kSyncLearnMoreURL));
192 values->SetString("login_status_learn_more", 224 values->SetString("login_status_learn_more",
193 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); 225 l10n_util::GetStringUTF16(IDS_LEARN_MORE));
194 values->SetString("login_status_advanced", 226 values->SetString("login_status_advanced",
195 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); 227 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED));
196 values->SetString("login_status_dismiss", 228 values->SetString("login_status_dismiss",
197 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); 229 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK));
198 } 230 }
OLDNEW
« chrome/browser/resources/ntp4/new_tab.html ('K') | « chrome/browser/ui/webui/ntp/ntp_login_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698