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.
5 #include "chrome/browser/signin/signin_promo.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/signin/gaia_auth_extension_loader.h"
14 #include "chrome/browser/first_run/first_run.h"
15 #include "chrome/browser/google/google_brand.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/account_tracker_service_factory.h"
20 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/ui/webui/options/core_options_handler.h"
22 #include "chrome/browser/ui/webui/theme_source.h"
23 #include "chrome/common/net/url_util.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/common/url_constants.h"
26 #include "components/google/core/browser/google_util.h"
27 #include "components/pref_registry/pref_registry_syncable.h"
28 #include "components/signin/core/browser/account_tracker_service.h"
29 #include "components/signin/core/browser/signin_manager.h"
30 #include "components/signin/core/common/profile_management_switches.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 "net/base/escape.h"
37 #include "net/base/network_change_notifier.h"
38 #include "net/base/url_util.h"
41 using content::WebContents
;
45 // Gaia cannot support about:blank as a continue URL, so using a hosted blank
47 const char kSignInLandingUrlPrefix
[] =
48 "https://www.google.com/intl/%s/chrome/blank.html";
50 // The maximum number of times we want to show the sign in promo at startup.
51 const int kSignInPromoShowAtStartupMaximum
= 10;
53 // Forces the web based signin flow when set.
54 bool g_force_web_based_signin_flow
= false;
56 // Checks we want to show the sign in promo for the given brand.
57 bool AllowPromoAtStartupForCurrentBrand() {
59 google_brand::GetBrand(&brand
);
64 if (google_brand::IsInternetCafeBrandCode(brand
))
67 // Enable for both organic and distribution.
71 // Returns true if a user has seen the sign in promo at startup previously.
72 bool HasShownPromoAtStartup(Profile
* profile
) {
73 return profile
->GetPrefs()->HasPrefPath(prefs::kSignInPromoStartupCount
);
76 // Returns true if the user has previously skipped the sign in promo.
77 bool HasUserSkippedPromo(Profile
* profile
) {
78 return profile
->GetPrefs()->GetBoolean(prefs::kSignInPromoUserSkipped
);
85 bool ShouldShowPromo(Profile
* profile
) {
86 #if defined(OS_CHROMEOS)
87 // There's no need to show the sign in promo on cros since cros users are
92 // Don't bother if we don't have any kind of network connection.
93 if (net::NetworkChangeNotifier::IsOffline())
96 // Don't show for supervised profiles.
97 if (profile
->IsSupervised())
100 // Display the signin promo if the user is not signed in.
101 SigninManager
* signin
= SigninManagerFactory::GetForProfile(
102 profile
->GetOriginalProfile());
103 return !signin
->AuthInProgress() && signin
->IsSigninAllowed() &&
104 !signin
->IsAuthenticated();
108 bool ShouldShowPromoAtStartup(Profile
* profile
, bool is_new_profile
) {
111 // Don't show if the profile is an incognito.
112 if (profile
->IsOffTheRecord())
115 if (!ShouldShowPromo(profile
))
118 if (!is_new_profile
) {
119 if (!HasShownPromoAtStartup(profile
))
123 if (HasUserSkippedPromo(profile
))
126 // For Chinese users skip the sign in promo.
127 if (g_browser_process
->GetApplicationLocale() == "zh-CN")
130 PrefService
* prefs
= profile
->GetPrefs();
131 int show_count
= prefs
->GetInteger(prefs::kSignInPromoStartupCount
);
132 if (show_count
>= kSignInPromoShowAtStartupMaximum
)
135 // This pref can be set in the master preferences file to allow or disallow
136 // showing the sign in promo at startup.
137 if (prefs
->HasPrefPath(prefs::kSignInPromoShowOnFirstRunAllowed
))
138 return prefs
->GetBoolean(prefs::kSignInPromoShowOnFirstRunAllowed
);
140 // For now don't show the promo for some brands.
141 if (!AllowPromoAtStartupForCurrentBrand())
144 // Default to show the promo for Google Chrome builds.
145 #if defined(GOOGLE_CHROME_BUILD)
152 void DidShowPromoAtStartup(Profile
* profile
) {
153 int show_count
= profile
->GetPrefs()->GetInteger(
154 prefs::kSignInPromoStartupCount
);
156 profile
->GetPrefs()->SetInteger(prefs::kSignInPromoStartupCount
, show_count
);
159 void SetUserSkippedPromo(Profile
* profile
) {
160 profile
->GetPrefs()->SetBoolean(prefs::kSignInPromoUserSkipped
, true);
163 GURL
GetLandingURL(const char* option
, int value
) {
165 if (switches::IsEnableWebBasedSignin()) {
166 const std::string
& locale
= g_browser_process
->GetApplicationLocale();
167 url
= base::StringPrintf(kSignInLandingUrlPrefix
, locale
.c_str());
169 url
= base::StringPrintf(
170 "%s/success.html", extensions::kGaiaAuthExtensionOrigin
);
172 base::StringAppendF(&url
, "?%s=%d", option
, value
);
176 GURL
GetPromoURL(signin_metrics::Source source
, bool auto_close
) {
177 return GetPromoURL(source
, auto_close
, false /* is_constrained */);
180 GURL
GetPromoURL(signin_metrics::Source source
,
182 bool is_constrained
) {
183 DCHECK_NE(signin_metrics::SOURCE_UNKNOWN
, source
);
185 if (!switches::IsEnableWebBasedSignin()) {
186 std::string
url(chrome::kChromeUIChromeSigninURL
);
187 base::StringAppendF(&url
, "?%s=%d", kSignInPromoQueryKeySource
, source
);
189 base::StringAppendF(&url
, "&%s=1", kSignInPromoQueryKeyAutoClose
);
191 base::StringAppendF(&url
, "&%s=1", kSignInPromoQueryKeyConstrained
);
195 // Build a Gaia-based URL that can be used to sign the user into chrome.
196 // There are required request parameters:
198 // - tell Gaia which service the user is signing into. In this case,
199 // a chrome sign in uses the service "chromiumsync"
200 // - provide a continue URL. This is the URL that Gaia will redirect to
201 // once the sign is complete.
203 // The continue URL includes a source parameter that can be extracted using
204 // the function GetSourceForSignInPromoURL() below. This is used to know
205 // which of the chrome sign in access points was used to sign the user in.
206 // It is also parsed for the |auto_close| flag, which indicates that the tab
207 // must be closed after sync setup is successful.
208 // See OneClickSigninHelper for details.
209 std::string query_string
= "?service=chromiumsync&sarp=1";
211 std::string continue_url
= GetLandingURL(kSignInPromoQueryKeySource
,
212 static_cast<int>(source
)).spec();
214 base::StringAppendF(&continue_url
, "&%s=1", kSignInPromoQueryKeyAutoClose
);
219 kSignInPromoQueryKeyContinue
,
220 net::EscapeQueryParamValue(continue_url
, false).c_str());
222 return GaiaUrls::GetInstance()->service_login_url().Resolve(query_string
);
225 GURL
GetReauthURL(Profile
* profile
, const std::string
& account_id
) {
226 AccountTrackerService::AccountInfo info
=
227 AccountTrackerServiceFactory::GetForProfile(profile
)->
228 GetAccountInfo(account_id
);
230 if (switches::IsEnableWebBasedSignin()) {
231 return net::AppendQueryParameter(
232 signin::GetPromoURL(signin_metrics::SOURCE_SETTINGS
, true),
237 signin_metrics::Source source
= switches::IsNewAvatarMenu() ?
238 signin_metrics::SOURCE_REAUTH
: signin_metrics::SOURCE_SETTINGS
;
240 GURL url
= signin::GetPromoURL(
241 source
, true /* auto_close */,
242 switches::IsNewAvatarMenu() /* is_constrained */);
243 url
= net::AppendQueryParameter(url
, "email", info
.email
);
244 url
= net::AppendQueryParameter(url
, "validateEmail", "1");
245 return net::AppendQueryParameter(url
, "readOnlyEmail", "1");
248 GURL
GetNextPageURLForPromoURL(const GURL
& url
) {
250 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeyContinue
, &value
)) {
251 GURL continue_url
= GURL(value
);
252 if (continue_url
.is_valid())
259 GURL
GetSigninPartitionURL() {
260 return GURL(switches::IsEnableWebviewBasedSignin() ?
261 "chrome-guest://chrome-signin/?" :
262 chrome::kChromeUIChromeSigninURL
);
265 signin_metrics::Source
GetSourceForPromoURL(const GURL
& url
) {
267 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeySource
, &value
)) {
269 if (base::StringToInt(value
, &source
) &&
270 source
>= signin_metrics::SOURCE_START_PAGE
&&
271 source
< signin_metrics::SOURCE_UNKNOWN
) {
272 return static_cast<signin_metrics::Source
>(source
);
275 return signin_metrics::SOURCE_UNKNOWN
;
278 bool IsAutoCloseEnabledInURL(const GURL
& url
) {
280 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeyAutoClose
, &value
)) {
282 if (base::StringToInt(value
, &enabled
) && enabled
== 1)
288 bool ShouldShowAccountManagement(const GURL
& url
) {
290 if (net::GetValueForKeyInQuery(
291 url
, kSignInPromoQueryKeyShowAccountManagement
, &value
)) {
293 if (base::StringToInt(value
, &enabled
) && enabled
== 1)
299 bool IsContinueUrlForWebBasedSigninFlow(const GURL
& url
) {
300 GURL::Replacements replacements
;
301 replacements
.ClearQuery();
302 const std::string
& locale
= g_browser_process
->GetApplicationLocale();
304 GURL(base::StringPrintf(kSignInLandingUrlPrefix
, locale
.c_str()));
306 google_util::IsGoogleDomainUrl(
308 google_util::ALLOW_SUBDOMAIN
,
309 google_util::DISALLOW_NON_STANDARD_PORTS
) &&
310 url
.ReplaceComponents(replacements
).path() ==
311 continue_url
.ReplaceComponents(replacements
).path());
314 void ForceWebBasedSigninFlowForTesting(bool force
) {
315 g_force_web_based_signin_flow
= force
;
318 void RegisterProfilePrefs(
319 user_prefs::PrefRegistrySyncable
* registry
) {
320 registry
->RegisterIntegerPref(
321 prefs::kSignInPromoStartupCount
,
323 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
324 registry
->RegisterBooleanPref(
325 prefs::kSignInPromoUserSkipped
,
327 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
328 registry
->RegisterBooleanPref(
329 prefs::kSignInPromoShowOnFirstRunAllowed
,
331 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
332 registry
->RegisterBooleanPref(
333 prefs::kSignInPromoShowNTPBubble
,
335 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
338 } // namespace signin