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