Home | History | Annotate | Download | only in signin
      1 // Copyright 2013 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/signin/signin_promo.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "base/strings/string_util.h"
     11 #include "base/strings/stringprintf.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "chrome/browser/browser_process.h"
     14 #include "chrome/browser/first_run/first_run.h"
     15 #include "chrome/browser/google/google_util.h"
     16 #include "chrome/browser/profiles/profile.h"
     17 #include "chrome/browser/profiles/profile_info_cache.h"
     18 #include "chrome/browser/profiles/profile_manager.h"
     19 #include "chrome/browser/signin/signin_manager.h"
     20 #include "chrome/browser/signin/signin_manager_factory.h"
     21 #include "chrome/browser/sync/profile_sync_service.h"
     22 #include "chrome/browser/sync/profile_sync_service_factory.h"
     23 #include "chrome/browser/ui/webui/options/core_options_handler.h"
     24 #include "chrome/browser/ui/webui/theme_source.h"
     25 #include "chrome/common/chrome_switches.h"
     26 #include "chrome/common/net/url_util.h"
     27 #include "chrome/common/pref_names.h"
     28 #include "chrome/common/url_constants.h"
     29 #include "components/user_prefs/pref_registry_syncable.h"
     30 #include "content/public/browser/url_data_source.h"
     31 #include "content/public/browser/web_contents.h"
     32 #include "content/public/browser/web_ui.h"
     33 #include "content/public/browser/web_ui_data_source.h"
     34 #include "google_apis/gaia/gaia_urls.h"
     35 #include "grit/browser_resources.h"
     36 #include "grit/generated_resources.h"
     37 #include "grit/theme_resources.h"
     38 #include "net/base/escape.h"
     39 #include "net/base/network_change_notifier.h"
     40 #include "net/base/url_util.h"
     41 #include "ui/base/l10n/l10n_util.h"
     42 
     43 using content::WebContents;
     44 
     45 namespace {
     46 
     47 const char kSignInPromoQueryKeyAutoClose[] = "auto_close";
     48 const char kSignInPromoQueryKeyContinue[] = "continue";
     49 const char kSignInPromoQueryKeySource[] = "source";
     50 const char kSignInPromoQueryKeyConstrained[] = "constrained";
     51 
     52 // Gaia cannot support about:blank as a continue URL, so using a hosted blank
     53 // page instead.
     54 const char kSignInLandingUrlPrefix[] =
     55     "https://www.google.com/intl/%s/chrome/blank.html";
     56 
     57 // The maximum number of times we want to show the sign in promo at startup.
     58 const int kSignInPromoShowAtStartupMaximum = 10;
     59 
     60 // Forces the web based signin flow when set.
     61 bool g_force_web_based_signin_flow = false;
     62 
     63 // Checks we want to show the sign in promo for the given brand.
     64 bool AllowPromoAtStartupForCurrentBrand() {
     65   std::string brand;
     66   google_util::GetBrand(&brand);
     67 
     68   if (brand.empty())
     69     return true;
     70 
     71   if (google_util::IsInternetCafeBrandCode(brand))
     72     return false;
     73 
     74   // Enable for both organic and distribution.
     75   return true;
     76 }
     77 
     78 // Returns true if a user has seen the sign in promo at startup previously.
     79 bool HasShownPromoAtStartup(Profile* profile) {
     80   return profile->GetPrefs()->HasPrefPath(prefs::kSignInPromoStartupCount);
     81 }
     82 
     83 // Returns true if the user has previously skipped the sign in promo.
     84 bool HasUserSkippedPromo(Profile* profile) {
     85   return profile->GetPrefs()->GetBoolean(prefs::kSignInPromoUserSkipped);
     86 }
     87 
     88 }  // namespace
     89 
     90 namespace signin {
     91 
     92 bool ShouldShowPromo(Profile* profile) {
     93 #if defined(OS_CHROMEOS)
     94   // There's no need to show the sign in promo on cros since cros users are
     95   // already logged in.
     96   return false;
     97 #else
     98 
     99   // Don't bother if we don't have any kind of network connection.
    100   if (net::NetworkChangeNotifier::IsOffline())
    101     return false;
    102 
    103   // Don't show for managed profiles.
    104   if (profile->IsManaged())
    105     return false;
    106 
    107   // Display the signin promo if the user is not signed in.
    108   SigninManager* signin = SigninManagerFactory::GetForProfile(
    109       profile->GetOriginalProfile());
    110   return !signin->AuthInProgress() && signin->IsSigninAllowed() &&
    111       signin->GetAuthenticatedUsername().empty();
    112 #endif
    113 }
    114 
    115 bool ShouldShowPromoAtStartup(Profile* profile, bool is_new_profile) {
    116   DCHECK(profile);
    117 
    118   // Don't show if the profile is an incognito.
    119   if (profile->IsOffTheRecord())
    120     return false;
    121 
    122   if (!ShouldShowPromo(profile))
    123     return false;
    124 
    125   if (!is_new_profile) {
    126     if (!HasShownPromoAtStartup(profile))
    127       return false;
    128   }
    129 
    130   if (HasUserSkippedPromo(profile))
    131     return false;
    132 
    133   // For Chinese users skip the sign in promo.
    134   if (g_browser_process->GetApplicationLocale() == "zh-CN")
    135     return false;
    136 
    137   PrefService* prefs = profile->GetPrefs();
    138   int show_count = prefs->GetInteger(prefs::kSignInPromoStartupCount);
    139   if (show_count >= kSignInPromoShowAtStartupMaximum)
    140     return false;
    141 
    142   // This pref can be set in the master preferences file to allow or disallow
    143   // showing the sign in promo at startup.
    144   if (prefs->HasPrefPath(prefs::kSignInPromoShowOnFirstRunAllowed))
    145     return prefs->GetBoolean(prefs::kSignInPromoShowOnFirstRunAllowed);
    146 
    147   // For now don't show the promo for some brands.
    148   if (!AllowPromoAtStartupForCurrentBrand())
    149     return false;
    150 
    151   // Default to show the promo for Google Chrome builds.
    152 #if defined(GOOGLE_CHROME_BUILD)
    153   return true;
    154 #else
    155   return false;
    156 #endif
    157 }
    158 
    159 void DidShowPromoAtStartup(Profile* profile) {
    160   int show_count = profile->GetPrefs()->GetInteger(
    161       prefs::kSignInPromoStartupCount);
    162   show_count++;
    163   profile->GetPrefs()->SetInteger(prefs::kSignInPromoStartupCount, show_count);
    164 }
    165 
    166 void SetUserSkippedPromo(Profile* profile) {
    167   profile->GetPrefs()->SetBoolean(prefs::kSignInPromoUserSkipped, true);
    168 }
    169 
    170 GURL GetLandingURL(const char* option, int value) {
    171   const std::string& locale = g_browser_process->GetApplicationLocale();
    172   std::string url = base::StringPrintf(kSignInLandingUrlPrefix, locale.c_str());
    173   base::StringAppendF(&url, "?%s=%d", option, value);
    174   return GURL(url);
    175 }
    176 
    177 GURL GetPromoURL(Source source, bool auto_close) {
    178   return GetPromoURL(source, auto_close, false /* is_constrained */);
    179 }
    180 
    181 GURL GetPromoURL(Source source, bool auto_close, bool is_constrained) {
    182   DCHECK_NE(SOURCE_UNKNOWN, source);
    183 
    184   bool enable_inline = CommandLine::ForCurrentProcess()->HasSwitch(
    185       switches::kEnableInlineSignin);
    186   if (enable_inline) {
    187     std::string url(chrome::kChromeUIChromeSigninURL);
    188     base::StringAppendF(&url, "?%s=%d", kSignInPromoQueryKeySource, source);
    189     if (auto_close)
    190       base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyAutoClose);
    191     if (is_constrained)
    192       base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyConstrained);
    193     return GURL(url);
    194   }
    195 
    196   // Build a Gaia-based URL that can be used to sign the user into chrome.
    197   // There are required request parameters:
    198   //
    199   //  - tell Gaia which service the user is signing into.  In this case,
    200   //    a chrome sign in uses the service "chromiumsync"
    201   //  - provide a continue URL.  This is the URL that Gaia will redirect to
    202   //    once the sign is complete.
    203   //
    204   // The continue URL includes a source parameter that can be extracted using
    205   // the function GetSourceForSignInPromoURL() below.  This is used to know
    206   // which of the chrome sign in access points was used to sign the user in.
    207   // It is also parsed for the |auto_close| flag, which indicates that the tab
    208   // must be closed after sync setup is successful.
    209   // See OneClickSigninHelper for details.
    210   std::string query_string = "?service=chromiumsync&sarp=1";
    211 
    212   std::string continue_url = GetLandingURL(kSignInPromoQueryKeySource,
    213                                            static_cast<int>(source)).spec();
    214   if (auto_close)
    215     base::StringAppendF(&continue_url, "&%s=1", kSignInPromoQueryKeyAutoClose);
    216 
    217   base::StringAppendF(&query_string, "&%s=%s", kSignInPromoQueryKeyContinue,
    218                       net::EscapeQueryParamValue(
    219                           continue_url, false).c_str());
    220 
    221   return GaiaUrls::GetInstance()->service_login_url().Resolve(query_string);
    222 }
    223 
    224 GURL GetNextPageURLForPromoURL(const GURL& url) {
    225   std::string value;
    226   if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinue, &value))
    227     return GURL(value);
    228 
    229   return GURL();
    230 }
    231 
    232 Source GetSourceForPromoURL(const GURL& url) {
    233   std::string value;
    234   if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeySource, &value)) {
    235     int source = 0;
    236     if (base::StringToInt(value, &source) && source >= SOURCE_START_PAGE &&
    237         source < SOURCE_UNKNOWN) {
    238       return static_cast<Source>(source);
    239     }
    240   }
    241   return SOURCE_UNKNOWN;
    242 }
    243 
    244 bool IsAutoCloseEnabledInURL(const GURL& url) {
    245   std::string value;
    246   if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyAutoClose, &value)) {
    247     int enabled = 0;
    248     if (base::StringToInt(value, &enabled) && enabled == 1)
    249       return true;
    250   }
    251   return false;
    252 }
    253 
    254 bool IsContinueUrlForWebBasedSigninFlow(const GURL& url) {
    255   GURL::Replacements replacements;
    256   replacements.ClearQuery();
    257   const std::string& locale = g_browser_process->GetApplicationLocale();
    258   return url.ReplaceComponents(replacements) ==
    259       GURL(base::StringPrintf(kSignInLandingUrlPrefix, locale.c_str()));
    260 }
    261 
    262 void ForceWebBasedSigninFlowForTesting(bool force) {
    263   g_force_web_based_signin_flow = force;
    264 }
    265 
    266 void RegisterProfilePrefs(
    267     user_prefs::PrefRegistrySyncable* registry) {
    268   registry->RegisterIntegerPref(
    269       prefs::kSignInPromoStartupCount,
    270       0,
    271       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    272   registry->RegisterBooleanPref(
    273       prefs::kSignInPromoUserSkipped,
    274       false,
    275       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    276   registry->RegisterBooleanPref(
    277       prefs::kSignInPromoShowOnFirstRunAllowed,
    278       true,
    279       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    280   registry->RegisterBooleanPref(
    281       prefs::kSignInPromoShowNTPBubble,
    282       false,
    283       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    284 }
    285 
    286 }  // namespace signin
    287