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

Unified Diff: chrome/browser/ui/webui/ntp/ntp_login_handler.cc

Issue 1143963002: Split NTPLoginHandler across chrome://apps and chrome://history. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: lower Created 5 years, 7 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/ntp/ntp_login_handler.cc
diff --git a/chrome/browser/ui/webui/ntp/ntp_login_handler.cc b/chrome/browser/ui/webui/ntp/ntp_login_handler.cc
deleted file mode 100644
index f9cfa021a6792ac56cf3e7f80e9f10f9b90999ec..0000000000000000000000000000000000000000
--- a/chrome/browser/ui/webui/ntp/ntp_login_handler.cc
+++ /dev/null
@@ -1,273 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/ui/webui/ntp/ntp_login_handler.h"
-
-#include <string>
-
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/metrics/histogram.h"
-#include "base/prefs/pref_notifier.h"
-#include "base/prefs/pref_service.h"
-#include "base/strings/utf_string_conversions.h"
-#include "base/values.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/profiles/profile.h"
-#include "chrome/browser/profiles/profile_info_cache.h"
-#include "chrome/browser/profiles/profile_manager.h"
-#include "chrome/browser/profiles/profile_metrics.h"
-#include "chrome/browser/signin/signin_manager_factory.h"
-#include "chrome/browser/signin/signin_promo.h"
-#include "chrome/browser/sync/profile_sync_service.h"
-#include "chrome/browser/sync/profile_sync_service_factory.h"
-#include "chrome/browser/ui/browser.h"
-#include "chrome/browser/ui/browser_finder.h"
-#include "chrome/browser/ui/browser_window.h"
-#include "chrome/browser/ui/chrome_pages.h"
-#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
-#include "chrome/browser/web_resource/promo_resource_service.h"
-#include "chrome/common/pref_names.h"
-#include "chrome/common/url_constants.h"
-#include "chrome/grit/chromium_strings.h"
-#include "chrome/grit/generated_resources.h"
-#include "components/signin/core/browser/signin_manager.h"
-#include "content/public/browser/host_zoom_map.h"
-#include "content/public/browser/web_contents.h"
-#include "content/public/browser/web_ui.h"
-#include "content/public/common/page_zoom.h"
-#include "net/base/escape.h"
-#include "skia/ext/image_operations.h"
-#include "ui/base/l10n/l10n_util.h"
-#include "ui/base/webui/web_ui_util.h"
-#include "ui/gfx/canvas.h"
-#include "ui/gfx/image/image.h"
-
-using content::OpenURLParams;
-using content::Referrer;
-
-namespace {
-
-SkBitmap GetGAIAPictureForNTP(const gfx::Image& image) {
- // This value must match the width and height value of login-status-icon
- // in new_tab.css.
- const int kLength = 27;
- SkBitmap bmp = skia::ImageOperations::Resize(*image.ToSkBitmap(),
- skia::ImageOperations::RESIZE_BEST, kLength, kLength);
-
- gfx::Canvas canvas(gfx::Size(kLength, kLength), 1.0f, false);
- canvas.DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bmp), 0, 0);
-
- // Draw a gray border on the inside of the icon.
- SkColor color = SkColorSetARGB(83, 0, 0, 0);
- canvas.DrawRect(gfx::Rect(0, 0, kLength - 1, kLength - 1), color);
-
- return canvas.ExtractImageRep().sk_bitmap();
-}
-
-// Puts the |content| into an element with the given CSS class.
-base::string16 CreateElementWithClass(const base::string16& content,
- const std::string& tag_name,
- const std::string& css_class,
- const std::string& extends_tag) {
- base::string16 start_tag = base::ASCIIToUTF16("<" + tag_name +
- " class='" + css_class + "' is='" + extends_tag + "'>");
- base::string16 end_tag = base::ASCIIToUTF16("</" + tag_name + ">");
- return start_tag + net::EscapeForHTML(content) + end_tag;
-}
-
-} // namespace
-
-NTPLoginHandler::NTPLoginHandler() {
-}
-
-NTPLoginHandler::~NTPLoginHandler() {
- ProfileManager* profile_manager = g_browser_process->profile_manager();
- // The profile_manager might be NULL in testing environments.
- if (profile_manager)
- profile_manager->GetProfileInfoCache().RemoveObserver(this);
-}
-
-void NTPLoginHandler::RegisterMessages() {
- ProfileManager* profile_manager = g_browser_process->profile_manager();
- // The profile_manager might be NULL in testing environments.
- if (profile_manager)
- profile_manager->GetProfileInfoCache().AddObserver(this);
-
- PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
- signin_allowed_pref_.Init(prefs::kSigninAllowed,
- pref_service,
- base::Bind(&NTPLoginHandler::UpdateLogin,
- base::Unretained(this)));
-
- web_ui()->RegisterMessageCallback("initializeSyncLogin",
- base::Bind(&NTPLoginHandler::HandleInitializeSyncLogin,
- base::Unretained(this)));
- web_ui()->RegisterMessageCallback("showSyncLoginUI",
- base::Bind(&NTPLoginHandler::HandleShowSyncLoginUI,
- base::Unretained(this)));
- web_ui()->RegisterMessageCallback("loginMessageSeen",
- base::Bind(&NTPLoginHandler::HandleLoginMessageSeen,
- base::Unretained(this)));
- web_ui()->RegisterMessageCallback("showAdvancedLoginUI",
- base::Bind(&NTPLoginHandler::HandleShowAdvancedLoginUI,
- base::Unretained(this)));
-}
-
-void NTPLoginHandler::OnProfileAuthInfoChanged(
- const base::FilePath& profile_path) {
- UpdateLogin();
-}
-
-void NTPLoginHandler::HandleInitializeSyncLogin(const base::ListValue* args) {
- UpdateLogin();
-}
-
-void NTPLoginHandler::HandleShowSyncLoginUI(const base::ListValue* args) {
- Profile* profile = Profile::FromWebUI(web_ui());
- if (!signin::ShouldShowPromo(profile))
- return;
-
- std::string username =
- SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername();
- if (!username.empty())
- return;
-
- content::WebContents* web_contents = web_ui()->GetWebContents();
- Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
- if (!browser)
- return;
-
- // The user isn't signed in, show the sign in promo.
- signin_metrics::Source source =
- web_contents->GetURL().spec() == chrome::kChromeUIAppsURL ?
- signin_metrics::SOURCE_APPS_PAGE_LINK :
- signin_metrics::SOURCE_NTP_LINK;
- chrome::ShowBrowserSignin(browser, source);
- RecordInHistogram(NTP_SIGN_IN_PROMO_CLICKED);
-}
-
-void NTPLoginHandler::RecordInHistogram(int type) {
- // Invalid type to record.
- if (type < NTP_SIGN_IN_PROMO_VIEWED ||
- type > NTP_SIGN_IN_PROMO_CLICKED) {
- NOTREACHED();
- } else {
- UMA_HISTOGRAM_ENUMERATION("SyncPromo.NTPPromo", type,
- NTP_SIGN_IN_PROMO_BUCKET_BOUNDARY);
- }
-}
-
-void NTPLoginHandler::HandleLoginMessageSeen(const base::ListValue* args) {
- Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean(
- prefs::kSignInPromoShowNTPBubble, false);
- NewTabUI* ntp_ui = NewTabUI::FromWebUIController(web_ui()->GetController());
- // When instant extended is enabled, there may not be a NewTabUI object.
- if (ntp_ui)
- ntp_ui->set_showing_sync_bubble(true);
-}
-
-void NTPLoginHandler::HandleShowAdvancedLoginUI(const base::ListValue* args) {
- Browser* browser =
- chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
- if (browser)
- chrome::ShowBrowserSignin(browser, signin_metrics::SOURCE_NTP_LINK);
-}
-
-void NTPLoginHandler::UpdateLogin() {
- Profile* profile = Profile::FromWebUI(web_ui());
- SigninManagerBase* signin_manager =
- SigninManagerFactory::GetForProfile(profile);
- if (!signin_manager) {
- // Guests on desktop do not have a signin manager.
- return;
- }
-
- std::string username = signin_manager->GetAuthenticatedUsername();
-
- base::string16 header, sub_header;
- std::string icon_url;
- if (!username.empty()) {
- ProfileInfoCache& cache =
- g_browser_process->profile_manager()->GetProfileInfoCache();
- size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
- if (profile_index != std::string::npos) {
- // Only show the profile picture and full name for the single profile
- // case. In the multi-profile case the profile picture is visible in the
- // title bar and the full name can be ambiguous.
- if (cache.GetNumberOfProfiles() == 1) {
- base::string16 name = cache.GetGAIANameOfProfileAtIndex(profile_index);
- if (!name.empty())
- header = CreateElementWithClass(name, "span", "profile-name", "");
- const gfx::Image* image =
- cache.GetGAIAPictureOfProfileAtIndex(profile_index);
- if (image)
- icon_url = webui::GetBitmapDataUrl(GetGAIAPictureForNTP(*image));
- }
- if (header.empty()) {
- header = CreateElementWithClass(base::UTF8ToUTF16(username), "span",
- "profile-name", "");
- }
- }
- } else {
-#if !defined(OS_CHROMEOS)
- // Chromeos does not show this status header.
- SigninManager* signin = SigninManagerFactory::GetForProfile(
- profile->GetOriginalProfile());
- if (!profile->IsLegacySupervised() && signin->IsSigninAllowed()) {
- base::string16 signed_in_link = l10n_util::GetStringUTF16(
- IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_LINK);
- signed_in_link =
- CreateElementWithClass(signed_in_link, "a", "", "action-link");
- header = l10n_util::GetStringFUTF16(
- IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_HEADER,
- l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
- sub_header = l10n_util::GetStringFUTF16(
- IDS_SYNC_PROMO_NOT_SIGNED_IN_STATUS_SUB_HEADER, signed_in_link);
- // Record that the user was shown the promo.
- RecordInHistogram(NTP_SIGN_IN_PROMO_VIEWED);
- }
-#endif
- }
-
- base::StringValue header_value(header);
- base::StringValue sub_header_value(sub_header);
- base::StringValue icon_url_value(icon_url);
- base::FundamentalValue is_user_signed_in(!username.empty());
- web_ui()->CallJavascriptFunction("ntp.updateLogin",
- header_value, sub_header_value, icon_url_value, is_user_signed_in);
-}
-
-// static
-bool NTPLoginHandler::ShouldShow(Profile* profile) {
-#if defined(OS_CHROMEOS)
- // For now we don't care about showing sync status on Chrome OS. The promo
- // UI and the avatar menu don't exist on that platform.
- return false;
-#else
- SigninManager* signin = SigninManagerFactory::GetForProfile(profile);
- return !profile->IsOffTheRecord() && signin && signin->IsSigninAllowed();
-#endif
-}
-
-// static
-void NTPLoginHandler::GetLocalizedValues(Profile* profile,
- base::DictionaryValue* values) {
- PrefService* prefs = profile->GetPrefs();
- bool hide_sync = !prefs->GetBoolean(prefs::kSignInPromoShowNTPBubble);
-
- base::string16 message = hide_sync ? base::string16() :
- l10n_util::GetStringFUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_MESSAGE,
- l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
-
- values->SetString("login_status_message", message);
- values->SetString("login_status_url",
- hide_sync ? std::string() : chrome::kSyncLearnMoreURL);
- values->SetString("login_status_advanced",
- hide_sync ? base::string16() :
- l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED));
- values->SetString("login_status_dismiss",
- hide_sync ? base::string16() :
- l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_OK));
-}

Powered by Google App Engine
This is Rietveld 408576698