| 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" |
| 32 #include "grit/chromium_strings.h" | 36 #include "grit/chromium_strings.h" |
| 33 #include "grit/generated_resources.h" | 37 #include "grit/generated_resources.h" |
| 34 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
| 39 #include "content/public/browser/notification_service.h" |
| 35 | 40 |
| 36 NTPLoginHandler::NTPLoginHandler() { | 41 NTPLoginHandler::NTPLoginHandler() { |
| 37 } | 42 } |
| 38 | 43 |
| 39 NTPLoginHandler::~NTPLoginHandler() { | 44 NTPLoginHandler::~NTPLoginHandler() { |
| 40 } | 45 } |
| 41 | 46 |
| 42 WebUIMessageHandler* NTPLoginHandler::Attach(WebUI* web_ui) { | 47 WebUIMessageHandler* NTPLoginHandler::Attach(WebUI* web_ui) { |
| 43 PrefService* pref_service = Profile::FromWebUI(web_ui)->GetPrefs(); | 48 PrefService* pref_service = Profile::FromWebUI(web_ui)->GetPrefs(); |
| 44 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); | 49 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); |
| 45 | 50 |
| 51 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 52 content::NotificationService::AllSources()); |
| 53 |
| 46 return WebUIMessageHandler::Attach(web_ui); | 54 return WebUIMessageHandler::Attach(web_ui); |
| 47 } | 55 } |
| 48 | 56 |
| 49 void NTPLoginHandler::RegisterMessages() { | 57 void NTPLoginHandler::RegisterMessages() { |
| 50 web_ui_->RegisterMessageCallback("initializeSyncLogin", | 58 web_ui_->RegisterMessageCallback("initializeSyncLogin", |
| 51 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin, | 59 base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin, |
| 52 base::Unretained(this))); | 60 base::Unretained(this))); |
| 53 web_ui_->RegisterMessageCallback("showSyncLoginUI", | 61 web_ui_->RegisterMessageCallback("showSyncLoginUI", |
| 54 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI, | 62 base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI, |
| 55 base::Unretained(this))); | 63 base::Unretained(this))); |
| 56 web_ui_->RegisterMessageCallback("loginMessageSeen", | 64 web_ui_->RegisterMessageCallback("loginMessageSeen", |
| 57 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen, | 65 base::Bind(&NTPLoginHandler::HandleLoginMessageSeen, |
| 58 base::Unretained(this))); | 66 base::Unretained(this))); |
| 59 web_ui_->RegisterMessageCallback("showAdvancedLoginUI", | 67 web_ui_->RegisterMessageCallback("showAdvancedLoginUI", |
| 60 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI, | 68 base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI, |
| 61 base::Unretained(this))); | 69 base::Unretained(this))); |
| 62 } | 70 } |
| 63 | 71 |
| 64 void NTPLoginHandler::Observe(int type, | 72 void NTPLoginHandler::Observe(int type, |
| 65 const content::NotificationSource& source, | 73 const content::NotificationSource& source, |
| 66 const content::NotificationDetails& details) { | 74 const content::NotificationDetails& details) { |
| 67 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); | 75 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) { |
| 68 std::string* name = content::Details<std::string>(details).ptr(); | 76 UpdateLogin(); |
| 69 if (prefs::kGoogleServicesUsername == *name) | 77 } else { |
| 70 UpdateLogin(); | 78 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); |
| 79 std::string* name = content::Details<std::string>(details).ptr(); |
| 80 if (prefs::kGoogleServicesUsername == *name) |
| 81 UpdateLogin(); |
| 82 } |
| 71 } | 83 } |
| 72 | 84 |
| 73 void NTPLoginHandler::HandleInitializeSyncLogin(const ListValue* args) { | 85 void NTPLoginHandler::HandleInitializeSyncLogin(const ListValue* args) { |
| 74 UpdateLogin(); | 86 UpdateLogin(); |
| 75 } | 87 } |
| 76 | 88 |
| 77 void NTPLoginHandler::HandleShowSyncLoginUI(const ListValue* args) { | 89 void NTPLoginHandler::HandleShowSyncLoginUI(const ListValue* args) { |
| 78 Profile* profile = Profile::FromWebUI(web_ui_); | 90 Profile* profile = Profile::FromWebUI(web_ui_); |
| 79 std::string username = profile->GetPrefs()->GetString( | 91 std::string username = profile->GetPrefs()->GetString( |
| 80 prefs::kGoogleServicesUsername); | 92 prefs::kGoogleServicesUsername); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 void NTPLoginHandler::HandleShowAdvancedLoginUI(const ListValue* args) { | 142 void NTPLoginHandler::HandleShowAdvancedLoginUI(const ListValue* args) { |
| 131 Profile::FromWebUI(web_ui_)->GetProfileSyncService()->ShowConfigure(false); | 143 Profile::FromWebUI(web_ui_)->GetProfileSyncService()->ShowConfigure(false); |
| 132 } | 144 } |
| 133 | 145 |
| 134 void NTPLoginHandler::UpdateLogin() { | 146 void NTPLoginHandler::UpdateLogin() { |
| 135 Profile* profile = Profile::FromWebUI(web_ui_); | 147 Profile* profile = Profile::FromWebUI(web_ui_); |
| 136 std::string username = profile->GetPrefs()->GetString( | 148 std::string username = profile->GetPrefs()->GetString( |
| 137 prefs::kGoogleServicesUsername); | 149 prefs::kGoogleServicesUsername); |
| 138 | 150 |
| 139 string16 header, sub_header; | 151 string16 header, sub_header; |
| 152 std::string icon_url; |
| 140 if (!username.empty()) { | 153 if (!username.empty()) { |
| 141 header = UTF8ToUTF16(username); | 154 string16 full_name; |
| 155 ProfileInfoCache& cache = |
| 156 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 157 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); |
| 158 if (profile_index != std::string::npos) { |
| 159 full_name = cache.GetGAIANameOfProfileAtIndex(profile_index); |
| 160 SkBitmap bmp = cache.GetGAIAPictureOfProfileAtIndex(profile_index); |
| 161 if (!bmp.isNull()) |
| 162 icon_url = web_ui_util::GetImageDataUrl(bmp); |
| 163 } |
| 164 if (full_name.empty()) { |
| 165 header = UTF8ToUTF16(username); |
| 166 } else { |
| 167 header = full_name; |
| 168 } |
| 142 } else if (SyncPromoUI::ShouldShowSyncPromo(profile) && | 169 } else if (SyncPromoUI::ShouldShowSyncPromo(profile) && |
| 143 (SyncPromoUI::UserHasSeenSyncPromoAtStartup(profile) || | 170 (SyncPromoUI::UserHasSeenSyncPromoAtStartup(profile) || |
| 144 PromoResourceService::CanShowNTPSignInPromo(profile))) { | 171 PromoResourceService::CanShowNTPSignInPromo(profile))) { |
| 145 string16 signed_in_link = l10n_util::GetStringUTF16( | 172 string16 signed_in_link = l10n_util::GetStringUTF16( |
| 146 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK); | 173 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK); |
| 147 signed_in_link = ASCIIToUTF16("<span class='link-span'>") + signed_in_link + | 174 signed_in_link = ASCIIToUTF16("<span class='link-span'>") + signed_in_link + |
| 148 ASCIIToUTF16("</span>"); | 175 ASCIIToUTF16("</span>"); |
| 149 header = l10n_util::GetStringFUTF16( | 176 header = l10n_util::GetStringFUTF16( |
| 150 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER, | 177 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER, |
| 151 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | 178 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); |
| 152 sub_header = l10n_util::GetStringFUTF16( | 179 sub_header = l10n_util::GetStringFUTF16( |
| 153 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link); | 180 IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link); |
| 154 // Record that the user was shown the promo. | 181 // Record that the user was shown the promo. |
| 155 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED); | 182 RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED); |
| 156 } | 183 } |
| 157 | 184 |
| 158 StringValue header_value(header); | 185 StringValue header_value(header); |
| 159 StringValue sub_header_value(sub_header); | 186 StringValue sub_header_value(sub_header); |
| 187 StringValue icon_url_value(icon_url); |
| 160 web_ui_->CallJavascriptFunction( | 188 web_ui_->CallJavascriptFunction( |
| 161 "updateLogin", header_value, sub_header_value); | 189 "updateLogin", header_value, sub_header_value, icon_url_value); |
| 162 } | 190 } |
| 163 | 191 |
| 164 // static | 192 // static |
| 165 bool NTPLoginHandler::ShouldShow(Profile* profile) { | 193 bool NTPLoginHandler::ShouldShow(Profile* profile) { |
| 166 #if defined(OS_CHROMEOS) | 194 #if defined(OS_CHROMEOS) |
| 167 // For now we don't care about showing sync status on Chrome OS. The promo | 195 // 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. | 196 // UI and the avatar menu don't exist on that platform. |
| 169 return false; | 197 return false; |
| 170 #else | 198 #else |
| 171 if (profile->IsOffTheRecord()) | 199 if (profile->IsOffTheRecord()) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 189 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); | 217 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); |
| 190 values->SetString("login_status_url", | 218 values->SetString("login_status_url", |
| 191 google_util::StringAppendGoogleLocaleParam(chrome::kSyncLearnMoreURL)); | 219 google_util::StringAppendGoogleLocaleParam(chrome::kSyncLearnMoreURL)); |
| 192 values->SetString("login_status_learn_more", | 220 values->SetString("login_status_learn_more", |
| 193 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); | 221 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); |
| 194 values->SetString("login_status_advanced", | 222 values->SetString("login_status_advanced", |
| 195 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); | 223 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); |
| 196 values->SetString("login_status_dismiss", | 224 values->SetString("login_status_dismiss", |
| 197 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); | 225 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK)); |
| 198 } | 226 } |
| OLD | NEW |