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/signin_manager_factory.h"
20 #include "chrome/browser/ui/webui/options/core_options_handler.h"
21 #include "chrome/browser/ui/webui/theme_source.h"
22 #include "chrome/common/net/url_util.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/common/url_constants.h"
25 #include "components/google/core/browser/google_util.h"
26 #include "components/pref_registry/pref_registry_syncable.h"
27 #include "components/signin/core/browser/signin_manager.h"
28 #include "components/signin/core/common/profile_management_switches.h"
29 #include "content/public/browser/url_data_source.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/browser/web_ui.h"
32 #include "content/public/browser/web_ui_data_source.h"
33 #include "google_apis/gaia/gaia_urls.h"
34 #include "net/base/escape.h"
35 #include "net/base/network_change_notifier.h"
36 #include "net/base/url_util.h"
39 using content::WebContents
;
43 // Gaia cannot support about:blank as a continue URL, so using a hosted blank
45 const char kSignInLandingUrlPrefix
[] =
46 "https://www.google.com/intl/%s/chrome/blank.html";
48 // The maximum number of times we want to show the sign in promo at startup.
49 const int kSignInPromoShowAtStartupMaximum
= 10;
51 // Forces the web based signin flow when set.
52 bool g_force_web_based_signin_flow
= false;
54 // Checks we want to show the sign in promo for the given brand.
55 bool AllowPromoAtStartupForCurrentBrand() {
57 google_brand::GetBrand(&brand
);
62 if (google_brand::IsInternetCafeBrandCode(brand
))
65 // Enable for both organic and distribution.
69 // Returns true if a user has seen the sign in promo at startup previously.
70 bool HasShownPromoAtStartup(Profile
* profile
) {
71 return profile
->GetPrefs()->HasPrefPath(prefs::kSignInPromoStartupCount
);
74 // Returns true if the user has previously skipped the sign in promo.
75 bool HasUserSkippedPromo(Profile
* profile
) {
76 return profile
->GetPrefs()->GetBoolean(prefs::kSignInPromoUserSkipped
);
83 bool ShouldShowPromo(Profile
* profile
) {
84 #if defined(OS_CHROMEOS)
85 // There's no need to show the sign in promo on cros since cros users are
90 // Don't bother if we don't have any kind of network connection.
91 if (net::NetworkChangeNotifier::IsOffline())
94 // Don't show for supervised profiles.
95 if (profile
->IsSupervised())
98 // Display the signin promo if the user is not signed in.
99 SigninManager
* signin
= SigninManagerFactory::GetForProfile(
100 profile
->GetOriginalProfile());
101 return !signin
->AuthInProgress() && signin
->IsSigninAllowed() &&
102 !signin
->IsAuthenticated();
106 bool ShouldShowPromoAtStartup(Profile
* profile
, bool is_new_profile
) {
109 // Don't show if the profile is an incognito.
110 if (profile
->IsOffTheRecord())
113 if (!ShouldShowPromo(profile
))
116 if (!is_new_profile
) {
117 if (!HasShownPromoAtStartup(profile
))
121 if (HasUserSkippedPromo(profile
))
124 // For Chinese users skip the sign in promo.
125 if (g_browser_process
->GetApplicationLocale() == "zh-CN")
128 PrefService
* prefs
= profile
->GetPrefs();
129 int show_count
= prefs
->GetInteger(prefs::kSignInPromoStartupCount
);
130 if (show_count
>= kSignInPromoShowAtStartupMaximum
)
133 // This pref can be set in the master preferences file to allow or disallow
134 // showing the sign in promo at startup.
135 if (prefs
->HasPrefPath(prefs::kSignInPromoShowOnFirstRunAllowed
))
136 return prefs
->GetBoolean(prefs::kSignInPromoShowOnFirstRunAllowed
);
138 // For now don't show the promo for some brands.
139 if (!AllowPromoAtStartupForCurrentBrand())
142 // Default to show the promo for Google Chrome builds.
143 #if defined(GOOGLE_CHROME_BUILD)
150 void DidShowPromoAtStartup(Profile
* profile
) {
151 int show_count
= profile
->GetPrefs()->GetInteger(
152 prefs::kSignInPromoStartupCount
);
154 profile
->GetPrefs()->SetInteger(prefs::kSignInPromoStartupCount
, show_count
);
157 void SetUserSkippedPromo(Profile
* profile
) {
158 profile
->GetPrefs()->SetBoolean(prefs::kSignInPromoUserSkipped
, true);
161 GURL
GetLandingURL(const char* option
, int value
) {
163 if (switches::IsEnableWebBasedSignin()) {
164 const std::string
& locale
= g_browser_process
->GetApplicationLocale();
165 url
= base::StringPrintf(kSignInLandingUrlPrefix
, locale
.c_str());
167 url
= base::StringPrintf(
168 "%s/success.html", extensions::kGaiaAuthExtensionOrigin
);
170 base::StringAppendF(&url
, "?%s=%d", option
, value
);
174 GURL
GetPromoURL(Source source
, bool auto_close
) {
175 return GetPromoURL(source
, auto_close
, false /* is_constrained */);
178 GURL
GetPromoURL(Source source
, bool auto_close
, bool is_constrained
) {
179 DCHECK_NE(SOURCE_UNKNOWN
, source
);
181 if (!switches::IsEnableWebBasedSignin()) {
182 std::string
url(chrome::kChromeUIChromeSigninURL
);
183 base::StringAppendF(&url
, "?%s=%d", kSignInPromoQueryKeySource
, source
);
185 base::StringAppendF(&url
, "&%s=1", kSignInPromoQueryKeyAutoClose
);
187 base::StringAppendF(&url
, "&%s=1", kSignInPromoQueryKeyConstrained
);
191 // Build a Gaia-based URL that can be used to sign the user into chrome.
192 // There are required request parameters:
194 // - tell Gaia which service the user is signing into. In this case,
195 // a chrome sign in uses the service "chromiumsync"
196 // - provide a continue URL. This is the URL that Gaia will redirect to
197 // once the sign is complete.
199 // The continue URL includes a source parameter that can be extracted using
200 // the function GetSourceForSignInPromoURL() below. This is used to know
201 // which of the chrome sign in access points was used to sign the user in.
202 // It is also parsed for the |auto_close| flag, which indicates that the tab
203 // must be closed after sync setup is successful.
204 // See OneClickSigninHelper for details.
205 std::string query_string
= "?service=chromiumsync&sarp=1";
207 std::string continue_url
= GetLandingURL(kSignInPromoQueryKeySource
,
208 static_cast<int>(source
)).spec();
210 base::StringAppendF(&continue_url
, "&%s=1", kSignInPromoQueryKeyAutoClose
);
215 kSignInPromoQueryKeyContinue
,
216 net::EscapeQueryParamValue(continue_url
, false).c_str());
218 return GaiaUrls::GetInstance()->service_login_url().Resolve(query_string
);
221 GURL
GetReauthURL(Profile
* profile
, const std::string
& account_id
) {
222 if (switches::IsEnableWebBasedSignin()) {
223 return net::AppendQueryParameter(
224 signin::GetPromoURL(signin::SOURCE_SETTINGS
, true),
229 signin::Source source
= switches::IsNewAvatarMenu() ?
230 signin::SOURCE_REAUTH
: signin::SOURCE_SETTINGS
;
232 GURL url
= signin::GetPromoURL(
233 source
, true /* auto_close */,
234 switches::IsNewAvatarMenu() /* is_constrained */);
235 url
= net::AppendQueryParameter(url
, "email", account_id
);
236 url
= net::AppendQueryParameter(url
, "validateEmail", "1");
237 return net::AppendQueryParameter(url
, "readOnlyEmail", "1");
240 GURL
GetNextPageURLForPromoURL(const GURL
& url
) {
242 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeyContinue
, &value
)) {
243 GURL continue_url
= GURL(value
);
244 if (continue_url
.is_valid())
251 Source
GetSourceForPromoURL(const GURL
& url
) {
253 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeySource
, &value
)) {
255 if (base::StringToInt(value
, &source
) && source
>= SOURCE_START_PAGE
&&
256 source
< SOURCE_UNKNOWN
) {
257 return static_cast<Source
>(source
);
260 return SOURCE_UNKNOWN
;
263 bool IsAutoCloseEnabledInURL(const GURL
& url
) {
265 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeyAutoClose
, &value
)) {
267 if (base::StringToInt(value
, &enabled
) && enabled
== 1)
273 bool ShouldShowAccountManagement(const GURL
& url
) {
275 if (net::GetValueForKeyInQuery(
276 url
, kSignInPromoQueryKeyShowAccountManagement
, &value
)) {
278 if (base::StringToInt(value
, &enabled
) && enabled
== 1)
284 bool IsContinueUrlForWebBasedSigninFlow(const GURL
& url
) {
285 GURL::Replacements replacements
;
286 replacements
.ClearQuery();
287 const std::string
& locale
= g_browser_process
->GetApplicationLocale();
289 GURL(base::StringPrintf(kSignInLandingUrlPrefix
, locale
.c_str()));
291 google_util::IsGoogleDomainUrl(
293 google_util::ALLOW_SUBDOMAIN
,
294 google_util::DISALLOW_NON_STANDARD_PORTS
) &&
295 url
.ReplaceComponents(replacements
).path() ==
296 continue_url
.ReplaceComponents(replacements
).path());
299 void ForceWebBasedSigninFlowForTesting(bool force
) {
300 g_force_web_based_signin_flow
= force
;
303 void RegisterProfilePrefs(
304 user_prefs::PrefRegistrySyncable
* registry
) {
305 registry
->RegisterIntegerPref(
306 prefs::kSignInPromoStartupCount
,
308 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
309 registry
->RegisterBooleanPref(
310 prefs::kSignInPromoUserSkipped
,
312 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
313 registry
->RegisterBooleanPref(
314 prefs::kSignInPromoShowOnFirstRunAllowed
,
316 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
317 registry
->RegisterBooleanPref(
318 prefs::kSignInPromoShowNTPBubble
,
320 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
323 } // namespace signin